Modern HAR Waterfall Viewer in React with Tailwind CSS
Here's a comprehensive implementation of a HAR-style waterfall viewer with all the requested features:
import { useState, useRef, useEffect } from 'react';
import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
import { ChevronDown, ChevronUp, Clock, File, Image, LucideIcon, Server, Shield, Table, Timeline } from 'lucide-react';
type RequestType = 'all' | 'xhr' | 'js' | 'css' | 'img' | 'media' | 'other' | 'errors';
interface RequestData {
id: string;
type: RequestType;
status: number;
time: string;
path: string;
phases: {
dns: number;
connect: number;
ssl: number;
waiting: number;
content: number;
};
startTime: number;
}
const WaterfallViewer = () => {
const [activeFilter, setActiveFilter] = useState<RequestType>('all');
const [viewMode, setViewMode] = useState<'waterfall' | 'table'>('waterfall');
const [expandedRow, setExpandedRow] = useState<string | null>(null);
const [sortConfig, setSortConfig] = useState<{ key: keyof RequestData; direction: 'asc' | 'desc' } | null>(null);
const timelineRef = useRef<HTMLDivElement>(null);
const [timelineWidth, setTimelineWidth] = useState(0);
const [maxDuration, setMaxDuration] = useState(0);
// Sample data - in a real app this would come from HAR file parsing
const sampleData: RequestData[] = [
{
id: '1',
type: 'xhr',
status: 200,
time: '12:37:25.123',
path: '/api/init',
phases: {
dns: 12,
connect: 45,
ssl: 23,
waiting: 120,
content: 345
},
startTime: 0
},
{
id: '2',
type: 'css',
status: 200,
time: '12:37:25.234',
path: '/styles/main.css',
phases: {
dns: 8,
connect: 32,
ssl: 0,
waiting: 45,
content: 120
},
startTime: 12
},
{
id: '3',
type: 'js',
status: 200,
time: '12:37:25.345',
path: '/scripts/app.js',
phases: {
dns: 5,
connect: 28,
ssl: 0,
waiting: 60,
content: 210
},
startTime: 25
},
{
id: '4',
type: 'img',
status: 200,
time: '12:37:25.456',
path: '/images/logo.png',
phases: {
dns: 15,
connect: 50,
ssl: 0,
waiting: 80,
content: 300
},
startTime: 50
},
{
id: '5',
type: 'media',
status: 200,
time: '12:37:25.567',
path: '/videos/intro.mp4',
phases: {
dns: 20,
connect: 60,
ssl: 0,
waiting: 150,
content: 1200
},
startTime: 100
},
{
id: '6',
type: 'other',
status: 404,
time: '12:37:25.678',
path: '/api/nonexistent',
phases: {
dns: 10,
connect: 30,
ssl: 0,
waiting: 200,
content: 0
},
startTime: 200
}
];
// Calculate max duration for timeline scaling
useEffect(() => {
const durations = sampleData.map(item => {
return item.phases.dns + item.phases.connect + item.phases.ssl +
item.phases.waiting + item.phases.content;
});
setMaxDuration(Math.max(...durations, 0));
}, [sampleData]);
// Handle window resize for timeline width
useEffect(() => {
const handleResize = () => {
if (timelineRef.current) {
setTimelineWidth(timelineRef.current.clientWidth);
}
};
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
const filteredData = sampleData.filter(item => {
if (activeFilter === 'all') return true;
if (activeFilter === 'errors' && item.status >= 400) return true;
return item.type === activeFilter;
});
const sortedData = [...filteredData].sort((a, b) => {
if (!sortConfig) return 0;
if (a[sortConfig.key] < b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? -1 : 1;
}
if (a[sortConfig.key] > b[sortConfig.key]) {
return sortConfig.direction === 'asc' ? 1 : -1;
}
return 0;
});
const requestTypeIcons: Record<RequestType, LucideIcon> = {
all: Server,
xhr: Server,
js: File,
css: File,
img: Image,
media: Image,
other: File,
errors: Server
};
const getIconForType = (type: RequestType) => {
const Icon = requestTypeIcons[type];
return <Icon className="h-4 w-4" />;
};
const requestTypeColors: Record<RequestType, string> = {
all: 'text-gray-500',
xhr: 'text-blue-500',
js: 'text-yellow-500',
css: 'text-purple-500',
img: 'text-green-500',
media: 'text-green-500',
other: 'text-gray-500',
errors: 'text-red-500'
};
const toggleSort = (key: keyof RequestData) => {
let direction: 'asc' | 'desc' = 'asc';
if (sortConfig && sortConfig.key === key && sortConfig.direction === 'asc') {
direction = 'desc';
}
setSortConfig({ key, direction });
};
const getSortIndicator = (key: keyof RequestData) => {
if (!sortConfig || sortConfig.key !== key) return null;
return sortConfig.direction === 'asc' ? <ChevronUp className="ml-1 h-3 w-3" /> : <ChevronDown className="ml-1 h-3 w-3" />;
};
const getPhaseTooltip = (phaseName: string, duration: number) => {
return `${phaseName}: ${duration}ms`;
};
const getTimelineWidth = (duration: number) => {
if (maxDuration === 0) return 0;
return (duration / maxDuration) * timelineWidth;
};
const getStartPosition = (startTime: number) => {
if (maxDuration === 0) return 0;
return (startTime / maxDuration) * timelineWidth;
};
return (
<div className="flex flex-col h-full">
{/* Header with Filter Buttons */}
<div className="flex items-center justify-between p-4 border-b">
<div className="flex gap-2">
{['all', 'xhr', 'js', 'css', 'img', 'media', 'other', 'errors'].map((type) => (
<Button
key={type}
variant={activeFilter === type ? 'default' : 'outline'}
onClick={() => setActiveFilter(type as RequestType)}
className="gap-1"
>
{getIconForType(type as RequestType)}
{type === 'all' ? 'All' : type.toUpperCase()}
</Button>
))}
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
onClick={() => setViewMode(viewMode === 'waterfall' ? 'table' : 'waterfall')}
className="gap-1"
>
<Table className="h-4 w-4" />
{viewMode === 'waterfall' ? 'Table View' : 'Waterfall View'}
</Button>
</div>
</div>
{/* Legend Bar */}
<div className="p-4 border-b">
<div className="flex items-center gap-4">
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-blue-500"></div>
<span className="text-sm text-gray-600">DNS Lookup</span>
</div>
<div className="w-20 h-2 bg-blue-500 rounded-full"></div>
</div>
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-purple-500"></div>
<span className="text-sm text-gray-600">Initial Connection</span>
</div>
<div className="w-20 h-2 bg-purple-500 rounded-full"></div>
</div>
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-pink-500"></div>
<span className="text-sm text-gray-600">SSL/TLS Negotiation</span>
</div>
<div className="w-20 h-2 bg-pink-500 rounded-full"></div>
</div>
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-orange-500"></div>
<span className="text-sm text-gray-600">Waiting (TTFB)</span>
</div>
<div className="w-20 h-2 bg-orange-500 rounded-full"></div>
</div>
<div className="flex items-center gap-2">
<div className="flex items-center gap-1">
<div className="w-2 h-2 rounded-full bg-green-500"></div>
<span className="text-sm text-gray-600">Content Download</span>
</div>
<div className="w-20 h-2 bg-green-500 rounded-full"></div>
</div>
</div>
</div>
{/* Main Table with Waterfall Timeline */}
<div className="flex flex-col h-full">
<div className="overflow-x-auto">
<div className="min-w-full">
{/* Table Header */}
<div className="grid grid-cols-[100px_120px_1fr_1fr] border-b sticky top-0 bg-background/95 backdrop-blur">
<div className="px-4 py-3 text-sm font-medium text-gray-500 cursor-pointer" onClick={() => toggleSort('status')}>
Status {getSortIndicator('status')}
</div>
<div className="px-4 py-3 text-sm font-medium text-gray-500 cursor-pointer" onClick={() => toggleSort('time')}>
Time {getSortIndicator('time')}
</div>
<div className="px-4 py-3 text-sm font-medium text-gray-500 cursor-pointer" onClick={() => toggleSort('path')}>
Path {getSortIndicator('path')}
</div>
<div className="px-4 py-3 text-sm font-medium text-gray-500">
Timeline
</div>
</div>
{/* Table Rows */}
<div className="divide-y">
{sortedData.map((item) => (
<div key={item.id} className="grid grid-cols-[100px_120px_1fr_1fr]">
{/* Status Cell */}
<div
className={`px-4 py-3 text-sm ${item.status >= 400 ? 'text-red-500' : 'text-gray-700'}`}
>
{item.status}
</div>
{/* Time Cell */}
<div className="px-4 py-3 text-sm text-gray-600">
{item.time}
</div>
{/* Path Cell */}
<div className="px-4 py-3 text-sm text-gray-700 truncate">
{item.path}
</div>
{/* Timeline Cell */}
<div className="px-4 py-3 relative group">
<div className="h-4 relative">
{/* Timeline bars */}
<div
className="absolute top-0 left-0 h-4"
style={{
width: `${getTimelineWidth(item.phases.dns)}px`,
transform: `translateX(${getStartPosition(item.startTime)}px)`
}}
>
<Tooltip>
<TooltipTrigger asChild>
<div className="h-4 bg-blue-500 rounded-l-full"></div>
</TooltipTrigger>
<TooltipContent>
<p>{getPhaseTooltip('DNS Lookup', item.phases.dns)}</p>
</TooltipContent>
</Tooltip>
</div>
<div
className="absolute top-0 left-0 h-4"
style={{
width: `${getTimelineWidth(item.phases.connect)}px`,
transform: `translateX(${getStartPosition(item.startTime + item.phases.dns)}px)`
}}
>
<Tooltip>
<TooltipTrigger asChild>
<div className="h-4 bg-purple-500"></div>
</TooltipTrigger>
<TooltipContent>
<p>{getPhaseTooltip('Initial Connection', item.phases.connect)}</p>
</TooltipContent>
</Tooltip>
</div>
<div
className="absolute top-0 left-0 h-4"
style={{
width: `${getTimelineWidth(item.phases.ssl)}px`,
transform: `translateX(${getStartPosition(item.startTime + item.phases.dns + item.phases.connect)}px)`
}}
>
<Tooltip>
<TooltipTrigger asChild>
<div className="h-4 bg-pink-500"></div>
</TooltipTrigger>
<TooltipContent>
<p>{getPhaseTooltip('SSL/TLS Negotiation', item.phases.ssl)}</p>
</TooltipContent>
</Tooltip>
</div>
<div
className="absolute top-0 left-0 h-4"
style={{
width: `${getTimelineWidth(item.phases.waiting)}px`,
transform: `translateX(${getStartPosition(item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl)}px)`
}}
>
<Tooltip>
<TooltipTrigger asChild>
<div className="h-4 bg-orange-500"></div>
</TooltipTrigger>
<TooltipContent>
<p>{getPhaseTooltip('Waiting (TTFB)', item.phases.waiting)}</p>
</TooltipContent>
</Tooltip>
</div>
<div
className="absolute top-0 left-0 h-4"
style={{
width: `${getTimelineWidth(item.phases.content)}px`,
transform: `translateX(${getStartPosition(item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl + item.phases.waiting)}px)`
}}
>
<Tooltip>
<TooltipTrigger asChild>
<div className="h-4 bg-green-500 rounded-r-full"></div>
</TooltipTrigger>
<TooltipContent>
<p>{getPhaseTooltip('Content Download', item.phases.content)}</p>
</TooltipContent>
</Tooltip>
</div>
{/* Time markers */}
<div className="absolute top-0 left-0 h-4 flex items-center">
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime}ms
</div>
</div>
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime + item.phases.dns)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime + item.phases.dns}ms
</div>
</div>
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime + item.phases.dns + item.phases.connect)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime + item.phases.dns + item.phases.connect}ms
</div>
</div>
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl}ms
</div>
</div>
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl + item.phases.waiting)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl + item.phases.waiting}ms
</div>
</div>
<div
className="absolute h-4 border-l border-dashed border-gray-300"
style={{
left: `${getStartPosition(item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl + item.phases.waiting + item.phases.content)}px`
}}
>
<div className="absolute -left-2 top-1/2 -translate-y-1/2 text-xs text-gray-500">
{item.startTime + item.phases.dns + item.phases.connect + item.phases.ssl + item.phases.waiting + item.phases.content}ms
</div>
</div>
</div>
</div>
{/* Expandable details */}
<div
className="absolute top-full left-0 w-full bg-gray-50 p-4 rounded-b-lg border border-t-0 border-gray-200"
style={{ display: expandedRow === item.id ? 'block' : 'none' }}
>
<div className="grid grid-cols-2 gap-4">
<div>
<h3 className="font-medium text-sm mb-1">Request Headers</h3>
<pre className="text-xs text-gray-600">{JSON.stringify({ "User-Agent": "Mozilla/5.0", "Accept": "*/*" }, null, 2)}</pre>
</div>
<div>
<h3 className="font-medium text-sm mb-1">Response Headers</h3>
<pre className="text-xs text-gray-600">{JSON.stringify({ "Content-Type": "application/json", "Content-Length": "1234" }, null, 2)}</pre>
</div>
</div>
</div>
{/* Toggle expand/collapse */}
<button
className="absolute top-1/2 right-2 -translate-y-1/2 p-1 rounded hover:bg-gray-100"
onClick={() => setExpandedRow(expandedRow === item.id ? null : item.id)}
>
{expandedRow === item.id ? (
<ChevronUp className="h-4 w-4" />
) : (
<ChevronDown className="h-4 w-4" />
)}
</button>
</div>
</div>
))}
</div>
</div>
</div>
</div>
</div>
);
};
export default WaterfallViewer;