Unverified Commit 7b4be632 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

bundle-analyzer: add table view to compare mode

Co-authored-by: 's avatarWill Binns-Smith <wbinnssmith@gmail.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent a742cc64
'use client' 'use client'
import { useEffect, useMemo, useState, type ReactNode } from 'react' import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
import useSWR from 'swr' import useSWR from 'swr'
import { CompareLayout } from '@/components/compare-layout' import { CompareLayout } from '@/components/compare-layout'
import { ErrorState } from '@/components/error-state' import { ErrorState } from '@/components/error-state'
import { Sidebar } from '@/components/sidebar' import { Sidebar } from '@/components/sidebar'
import { TopBar, Environment } from '@/components/top-bar' import { TopBar, Environment, CompareView } from '@/components/top-bar'
import { TreemapVisualizer } from '@/components/treemap-visualizer' import { TreemapVisualizer } from '@/components/treemap-visualizer'
import { Badge } from '@/components/ui/badge' import { Badge } from '@/components/ui/badge'
...@@ -58,6 +58,22 @@ export default function Home() { ...@@ -58,6 +58,22 @@ export default function Home() {
const comparisonBaseDir = comparisonSnapshot const comparisonBaseDir = comparisonSnapshot
? `history/${comparisonSnapshot.id}` ? `history/${comparisonSnapshot.id}`
: 'data' : 'data'
// Default view depends on mode: compare mode opens to the diff table
// (the change list is the headline), single-build mode opens to the
// treemap (size-by-area is the headline). Whenever the user toggles
// between modes we reset to that mode's default; explicit user choices
// within a mode are preserved until the mode changes again.
const [compareView, setCompareView] = useState<CompareView>(
CompareView.Treemap
)
const wasCompareModeRef = useRef(baselineSnapshot != null)
useEffect(() => {
const isNowCompare = baselineSnapshot != null
if (wasCompareModeRef.current !== isNowCompare) {
wasCompareModeRef.current = isNowCompare
setCompareView(isNowCompare ? CompareView.Table : CompareView.Treemap)
}
}, [baselineSnapshot])
const { const {
data: modulesData, data: modulesData,
...@@ -101,10 +117,8 @@ export default function Home() { ...@@ -101,10 +117,8 @@ export default function Home() {
{ revalidateOnFocus: false, revalidateOnReconnect: false } { revalidateOnFocus: false, revalidateOnReconnect: false }
) )
// Whether the selected route exists on comparison side B. We only fetch // Whether the selected route exists on comparison side B. routes.json is
// its analyze.data when this is true because an output directory can contain // the source of truth because output directories can retain stale routes.
// stale per-route subdirectories from previous builds. routes.json is the
// source of truth for either a live or historical comparison build.
const currentAnalyzeRouteExists = const currentAnalyzeRouteExists =
currentRoutes != null && currentRoutes != null &&
selectedRoute != null && selectedRoute != null &&
...@@ -336,6 +350,8 @@ export default function Home() { ...@@ -336,6 +350,8 @@ export default function Home() {
<CompareLayout <CompareLayout
baselineSnapshot={baselineSnapshot} baselineSnapshot={baselineSnapshot}
comparisonSnapshot={comparisonSnapshot} comparisonSnapshot={comparisonSnapshot}
compareView={compareView}
searchQuery={searchQuery}
selectedRoute={selectedRoute} selectedRoute={selectedRoute}
comparisonRouteCount={currentRoutes?.length ?? null} comparisonRouteCount={currentRoutes?.length ?? null}
routeDiff={routeDiff} routeDiff={routeDiff}
...@@ -424,6 +440,9 @@ export default function Home() { ...@@ -424,6 +440,9 @@ export default function Home() {
<main className="h-screen flex flex-col bg-background"> <main className="h-screen flex flex-col bg-background">
<TopBar <TopBar
hasSourceData={analyzeData != null} hasSourceData={analyzeData != null}
showViewToggle={isCompareMode}
compareView={compareView}
onCompareViewChange={setCompareView}
selectedRoute={selectedRoute} selectedRoute={selectedRoute}
setSelectedRoute={setSelectedRoute} setSelectedRoute={setSelectedRoute}
environmentFilter={environmentFilter} environmentFilter={environmentFilter}
......
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
import { useSidebarResize } from '@/lib/use-sidebar-resize' import { useSidebarResize } from '@/lib/use-sidebar-resize'
import { CompareSidebar } from '@/components/sidebar' import { CompareSidebar } from '@/components/sidebar'
import { DiffTable } from '@/components/diff-table'
import { DiffTreemap } from '@/components/diff-treemap' import { DiffTreemap } from '@/components/diff-treemap'
import { TreemapSkeleton } from '@/components/ui/skeleton' import { TreemapSkeleton } from '@/components/ui/skeleton'
import { StatCard, CountCard } from '@/components/stat-cards' import { StatCard, CountCard } from '@/components/stat-cards'
import { Environment } from '@/components/top-bar' import { CompareView, Environment } from '@/components/top-bar'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data' import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
import { import {
diffRoutesWithSizes, diffRoutesWithSizes,
...@@ -19,6 +20,7 @@ import { cn, formatBytes } from '@/lib/utils' ...@@ -19,6 +20,7 @@ import { cn, formatBytes } from '@/lib/utils'
export interface CompareLayoutProps { export interface CompareLayoutProps {
baselineSnapshot: SnapshotMetadata baselineSnapshot: SnapshotMetadata
comparisonSnapshot: SnapshotMetadata | null comparisonSnapshot: SnapshotMetadata | null
compareView: CompareView
selectedRoute: string | null selectedRoute: string | null
comparisonRouteCount: number | null comparisonRouteCount: number | null
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
...@@ -29,6 +31,7 @@ export interface CompareLayoutProps { ...@@ -29,6 +31,7 @@ export interface CompareLayoutProps {
isBaselineAnalyzeLoading: boolean isBaselineAnalyzeLoading: boolean
baselineAnalyzeError: unknown | null baselineAnalyzeError: unknown | null
compressed: boolean compressed: boolean
searchQuery: string
compareSelectedKey: string | null compareSelectedKey: string | null
setCompareSelectedKey: (key: string | null) => void setCompareSelectedKey: (key: string | null) => void
modulesData: ModulesData | null modulesData: ModulesData | null
...@@ -39,10 +42,9 @@ export interface CompareLayoutProps { ...@@ -39,10 +42,9 @@ export interface CompareLayoutProps {
} }
/** /**
* Renders the comparison view (treemap) with a context strip at the top * Renders the comparison view (treemap + table) with a context strip at the
* showing the two builds being compared. The "before" build (A) is rendered * top showing the two builds being compared. The "before" build (A) is
* on the left and the selected "after" build (B) on the right, * rendered on the left and the selected "after" build (B) on the right, matching the user's mental model.
* matching the user's mental model.
* *
* Route selection lives in the top-bar route picker (`RouteTypeahead`), * Route selection lives in the top-bar route picker (`RouteTypeahead`),
* which also renders per-route deltas in compare mode. * which also renders per-route deltas in compare mode.
...@@ -50,6 +52,7 @@ export interface CompareLayoutProps { ...@@ -50,6 +52,7 @@ export interface CompareLayoutProps {
export function CompareLayout({ export function CompareLayout({
baselineSnapshot, baselineSnapshot,
comparisonSnapshot, comparisonSnapshot,
compareView,
selectedRoute, selectedRoute,
comparisonRouteCount, comparisonRouteCount,
routeDiff, routeDiff,
...@@ -60,6 +63,7 @@ export function CompareLayout({ ...@@ -60,6 +63,7 @@ export function CompareLayout({
isBaselineAnalyzeLoading, isBaselineAnalyzeLoading,
baselineAnalyzeError, baselineAnalyzeError,
compressed, compressed,
searchQuery,
compareSelectedKey, compareSelectedKey,
setCompareSelectedKey, setCompareSelectedKey,
modulesData, modulesData,
...@@ -100,12 +104,13 @@ export function CompareLayout({ ...@@ -100,12 +104,13 @@ export function CompareLayout({
</div> </div>
{/* {/*
Per-route source diff + sidebar. The sidebar slides in when a Per-route source diff + sidebar. The sidebar slides in when a row or
treemap tile is selected and shows the import chain for that source. treemap tile is selected and shows the import chain for that source.
*/} */}
<div className="flex flex-1 min-h-0"> <div className="flex flex-1 min-h-0">
<div className="flex flex-1 min-w-0 flex-col"> <div className="flex flex-1 min-w-0 flex-col">
<ComparePerRoutePanel <ComparePerRoutePanel
compareView={compareView}
selectedRoute={selectedRoute} selectedRoute={selectedRoute}
sourceDiff={sourceDiff} sourceDiff={sourceDiff}
analyzeData={analyzeData} analyzeData={analyzeData}
...@@ -113,6 +118,9 @@ export function CompareLayout({ ...@@ -113,6 +118,9 @@ export function CompareLayout({
isAnalyzeLoading={isAnalyzeLoading} isAnalyzeLoading={isAnalyzeLoading}
isBaselineAnalyzeLoading={isBaselineAnalyzeLoading} isBaselineAnalyzeLoading={isBaselineAnalyzeLoading}
compressed={compressed} compressed={compressed}
searchQuery={searchQuery}
baselineSnapshot={baselineSnapshot}
comparisonSnapshot={comparisonSnapshot}
compareSelectedKey={compareSelectedKey} compareSelectedKey={compareSelectedKey}
onCompareSelectedKeyChange={setCompareSelectedKey} onCompareSelectedKeyChange={setCompareSelectedKey}
/> />
...@@ -370,11 +378,12 @@ function RouteStatsBody({ ...@@ -370,11 +378,12 @@ function RouteStatsBody({
} }
/** /**
* Per-route comparison panel. Shows the diff treemap. Handles missing-route * Per-route comparison panel. Shows either the diff treemap or the diff table,
* states (the route is new in the comparison build, or was removed) with * controlled by `compareView`. Handles missing-route states (the route is new
* friendly messaging. * in comparison build B, or was removed) with friendly messaging.
*/ */
function ComparePerRoutePanel({ function ComparePerRoutePanel({
compareView,
selectedRoute, selectedRoute,
sourceDiff, sourceDiff,
analyzeData, analyzeData,
...@@ -382,9 +391,13 @@ function ComparePerRoutePanel({ ...@@ -382,9 +391,13 @@ function ComparePerRoutePanel({
isAnalyzeLoading, isAnalyzeLoading,
isBaselineAnalyzeLoading, isBaselineAnalyzeLoading,
compressed, compressed,
searchQuery,
baselineSnapshot,
comparisonSnapshot,
compareSelectedKey, compareSelectedKey,
onCompareSelectedKeyChange, onCompareSelectedKeyChange,
}: { }: {
compareView: CompareView
selectedRoute: string | null selectedRoute: string | null
sourceDiff: DiffSummary<SourceDiffRow> | null sourceDiff: DiffSummary<SourceDiffRow> | null
analyzeData: AnalyzeData | null analyzeData: AnalyzeData | null
...@@ -392,6 +405,9 @@ function ComparePerRoutePanel({ ...@@ -392,6 +405,9 @@ function ComparePerRoutePanel({
isAnalyzeLoading: boolean isAnalyzeLoading: boolean
isBaselineAnalyzeLoading: boolean isBaselineAnalyzeLoading: boolean
compressed: boolean compressed: boolean
searchQuery: string
baselineSnapshot: SnapshotMetadata
comparisonSnapshot: SnapshotMetadata | null
compareSelectedKey: string | null compareSelectedKey: string | null
onCompareSelectedKeyChange: (key: string | null) => void onCompareSelectedKeyChange: (key: string | null) => void
}) { }) {
...@@ -419,14 +435,31 @@ function ComparePerRoutePanel({ ...@@ -419,14 +435,31 @@ function ComparePerRoutePanel({
return ( return (
<div className="flex flex-1 min-h-0 flex-col"> <div className="flex flex-1 min-h-0 flex-col">
<DiffTreemap {compareView === CompareView.Treemap ? (
summary={sourceDiff} <DiffTreemap
useCompressed={compressed} summary={sourceDiff}
analyzeData={analyzeData} useCompressed={compressed}
baselineAnalyzeData={baselineAnalyzeData} analyzeData={analyzeData}
selectedKey={compareSelectedKey} baselineAnalyzeData={baselineAnalyzeData}
onSelectKey={onCompareSelectedKeyChange} selectedKey={compareSelectedKey}
/> onSelectKey={onCompareSelectedKeyChange}
/>
) : (
<DiffTable
summary={sourceDiff}
useCompressed={compressed}
nameHeading="Source"
aHeading={formatSnapshotLabel(baselineSnapshot)}
bHeading={
comparisonSnapshot
? formatSnapshotLabel(comparisonSnapshot)
: 'Latest'
}
searchQuery={searchQuery}
selectedKey={compareSelectedKey}
onRowSelect={(row) => onCompareSelectedKeyChange(row.key)}
/>
)}
</div> </div>
) )
} }
This diff is collapsed.
...@@ -11,6 +11,7 @@ import { ...@@ -11,6 +11,7 @@ import {
SelectValue, SelectValue,
} from '@/components/ui/select' } from '@/components/ui/select'
import { MultiSelect } from '@/components/ui/multi-select' import { MultiSelect } from '@/components/ui/multi-select'
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
import { diffRoutesWithSizes } from '@/lib/diff' import { diffRoutesWithSizes } from '@/lib/diff'
import { type SnapshotMetadata } from '@/lib/snapshot' import { type SnapshotMetadata } from '@/lib/snapshot'
import { import {
...@@ -20,6 +21,8 @@ import { ...@@ -20,6 +21,8 @@ import {
FileJson, FileJson,
Palette, Palette,
Package, Package,
Table as TableIcon,
LayoutGrid,
} from 'lucide-react' } from 'lucide-react'
export enum Environment { export enum Environment {
...@@ -27,6 +30,11 @@ export enum Environment { ...@@ -27,6 +30,11 @@ export enum Environment {
Server = 'server', Server = 'server',
} }
export enum CompareView {
Treemap = 'treemap',
Table = 'table',
}
const typeFilterOptions = [ const typeFilterOptions = [
{ {
value: 'js', value: 'js',
...@@ -65,10 +73,14 @@ export function TopBar({ ...@@ -65,10 +73,14 @@ export function TopBar({
onBaselineChange, onBaselineChange,
comparisonSnapshot, comparisonSnapshot,
onComparisonChange, onComparisonChange,
compareView,
onCompareViewChange,
routeDiff, routeDiff,
hasSourceData, hasSourceData,
showViewToggle,
}: { }: {
hasSourceData: boolean hasSourceData: boolean
showViewToggle: boolean
selectedRoute: string | null selectedRoute: string | null
setSelectedRoute: (route: string | null) => void setSelectedRoute: (route: string | null) => void
environmentFilter: Environment environmentFilter: Environment
...@@ -83,6 +95,8 @@ export function TopBar({ ...@@ -83,6 +95,8 @@ export function TopBar({
onBaselineChange: (snapshot: SnapshotMetadata | null) => void onBaselineChange: (snapshot: SnapshotMetadata | null) => void
comparisonSnapshot: SnapshotMetadata | null comparisonSnapshot: SnapshotMetadata | null
onComparisonChange: (snapshot: SnapshotMetadata | null) => void onComparisonChange: (snapshot: SnapshotMetadata | null) => void
compareView: CompareView
onCompareViewChange: (view: CompareView) => void
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
}) { }) {
const isCompareMode = baselineSnapshot != null const isCompareMode = baselineSnapshot != null
...@@ -120,6 +134,37 @@ export function TopBar({ ...@@ -120,6 +134,37 @@ export function TopBar({
/> />
) : null} ) : null}
{showViewToggle && (
<ToggleGroup
type="single"
size="sm"
value={compareView}
onValueChange={(value) => {
if (value) onCompareViewChange(value as CompareView)
}}
aria-label="View"
>
<ToggleGroupItem
value={CompareView.Table}
aria-label="Table view"
title="Table view"
className="gap-1.5"
>
<TableIcon className="h-3.5 w-3.5" />
<span className="text-xs">Table</span>
</ToggleGroupItem>
<ToggleGroupItem
value={CompareView.Treemap}
aria-label="Treemap view"
title="Treemap view"
className="gap-1.5"
>
<LayoutGrid className="h-3.5 w-3.5" />
<span className="text-xs">Treemap</span>
</ToggleGroupItem>
</ToggleGroup>
)}
{hasSourceData && ( {hasSourceData && (
<> <>
<ControlDivider /> <ControlDivider />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment