import { $, component$, useComputed$, useSignal, useTask$, useVisibleTask$ } from '@qwik.dev/core'; import type { QwikPerfStoreRemembered } from '@qwik.dev/devtools/kit'; import { computePerfViewModel } from './computePerfViewModel'; import { ComponentCard } from './components/ComponentCard'; import { HookDetailsPanel } from './components/HookDetailsPanel'; import { PerformanceOverview } from './components/PerformanceOverview'; import { getPageDataSource, type RenderEvent } from '../../devtools/page-data-source'; import type { DevtoolsState } from '../../devtools/state'; const MAX_RENDER_EVENTS = 200; export const Performance = component$<{ state: DevtoolsState }>(({ state }) => { const perf = useSignal(null); const selectedComponent = useSignal(null); const renderEvents = useSignal([]); useTask$(async () => { const source = getPageDataSource(); perf.value = (await source.readPerfData()) ?? { ssr: [], csr: [] }; }); // Seed render events from perf snapshot + subscribe to live updates useVisibleTask$(async ({ cleanup }) => { const source = getPageDataSource(); // Pre-populate from existing CSR data const data = await source.readPerfData(); const all = [...(data?.ssr || []), ...(data?.csr || [])]; if (all.length) { renderEvents.value = all .map((e: any) => ({ component: e.component || 'unknown', phase: e.phase || 'csr', duration: e.duration || 0, timestamp: e.end || e.start || Date.now(), })) .slice(-MAX_RENDER_EVENTS); } const unsub = source.subscribeRenderEvents((event) => { renderEvents.value = [...renderEvents.value, event].slice(-MAX_RENDER_EVENTS); }); if (unsub) { cleanup(unsub); } }); const vm = useComputed$(() => computePerfViewModel(perf.value)); const selectedVm = useComputed$(() => { const name = selectedComponent.value; if (!name) { return null; } return vm.value.components.find((c) => c.componentName === name) ?? null; }); const onSelect = $((name: string) => { selectedComponent.value = name; }); const clearSelect = $(() => { selectedComponent.value = null; }); const clearRenderEvents = $(() => { renderEvents.value = []; }); // The palette requests a clear; reset the flag once handled so it can fire again. useTask$(({ track }) => { if (!track(() => state.clearPerformanceRequested)) { return; } renderEvents.value = []; selectedComponent.value = null; state.clearPerformanceRequested = false; }); const hasData = (perf.value?.ssr?.length ?? 0) > 0 || (perf.value?.csr?.length ?? 0) > 0; const hasRenderEvents = renderEvents.value.length > 0; if (!hasData && !hasRenderEvents) { return (
No performance data found.
Ensure instrumentation is enabled and interact with the app, then reopen this tab.
); } // ── Main layout ── return (
{hasData && } {hasData && (
Components
CSR only • {vm.value.components.length} total
{vm.value.components.map((c) => ( onSelect(c.componentName)} /> ))}
{selectedVm.value ? ( ) : (
Select a component to view hook details.
)}
)} {/* Live Render Events */}
Live Renders
{renderEvents.value.length} events captured
{hasRenderEvents && ( )}
{!hasRenderEvents ? (
No renders captured yet. Interact with the app to trigger component renders.
) : (
{renderEvents.value .slice() .reverse() .map((evt, i) => ( ))}
Time Component Duration Phase
{new Date(evt.timestamp).toLocaleTimeString()} {evt.component} {evt.duration > 0 ? `${evt.duration.toFixed(1)}ms` : '-'} {evt.phase}
)}
); });