// Adapted from jalcoui (MIT) — github.com/jal-co/ui 'use client'; import * as React from 'react'; import { useStylePresets } from '@djangocfg/ui-core/styles/palette'; import { cn } from '@djangocfg/ui-core/lib'; import type { NetworkRequest } from '../types'; import { EmptyState } from './EmptyState'; import { formatDuration } from './utils'; /** * Categorical palette for timing phases (DNS, TCP, TLS, …). Stages are not * statuses, so we route through `useStylePresets()` — 8 distinguishable hex * colors that respect the active theme. Indices wrap modulo length. */ function useStagePalette(): string[] { const presets = useStylePresets(); return React.useMemo( () => [ presets.primary.fill, presets.success.fill, presets.warning.fill, presets.danger.fill, presets.info.fill, presets.chart3.fill, presets.chart4.fill, presets.chart5.fill, ], [presets], ); } export function TimingTab({ request }: { request: NetworkRequest }) { const stagePalette = useStagePalette(); if (!request.timing || request.timing.length === 0) { return ; } const timing = request.timing; const maxDuration = Math.max(...timing.map((t) => t.duration)); const totalDuration = request.duration ?? timing.reduce((sum, t) => sum + t.duration, 0); return (
{/* Total */}
Total Duration {formatDuration(totalDuration)}
{/* Waterfall */}
{timing.map((entry, i) => { const percentage = maxDuration > 0 ? (entry.duration / maxDuration) * 100 : 0; const color = stagePalette[i % stagePalette.length]; return (
{entry.label}
{formatDuration(entry.duration)}
); })}
{/* Legend */}
{timing.map((entry, i) => { const color = stagePalette[i % stagePalette.length]; return (
{entry.label}
); })}
); }