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'
import { useEffect, useMemo, useState, type ReactNode } from 'react'
import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react'
import useSWR from 'swr'
import { CompareLayout } from '@/components/compare-layout'
import { ErrorState } from '@/components/error-state'
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 { Badge } from '@/components/ui/badge'
......@@ -58,6 +58,22 @@ export default function Home() {
const comparisonBaseDir = comparisonSnapshot
? `history/${comparisonSnapshot.id}`
: '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 {
data: modulesData,
......@@ -101,10 +117,8 @@ export default function Home() {
{ revalidateOnFocus: false, revalidateOnReconnect: false }
)
// Whether the selected route exists on comparison side B. We only fetch
// its analyze.data when this is true because an output directory can contain
// stale per-route subdirectories from previous builds. routes.json is the
// source of truth for either a live or historical comparison build.
// Whether the selected route exists on comparison side B. routes.json is
// the source of truth because output directories can retain stale routes.
const currentAnalyzeRouteExists =
currentRoutes != null &&
selectedRoute != null &&
......@@ -336,6 +350,8 @@ export default function Home() {
<CompareLayout
baselineSnapshot={baselineSnapshot}
comparisonSnapshot={comparisonSnapshot}
compareView={compareView}
searchQuery={searchQuery}
selectedRoute={selectedRoute}
comparisonRouteCount={currentRoutes?.length ?? null}
routeDiff={routeDiff}
......@@ -424,6 +440,9 @@ export default function Home() {
<main className="h-screen flex flex-col bg-background">
<TopBar
hasSourceData={analyzeData != null}
showViewToggle={isCompareMode}
compareView={compareView}
onCompareViewChange={setCompareView}
selectedRoute={selectedRoute}
setSelectedRoute={setSelectedRoute}
environmentFilter={environmentFilter}
......
......@@ -2,10 +2,11 @@
import { useSidebarResize } from '@/lib/use-sidebar-resize'
import { CompareSidebar } from '@/components/sidebar'
import { DiffTable } from '@/components/diff-table'
import { DiffTreemap } from '@/components/diff-treemap'
import { TreemapSkeleton } from '@/components/ui/skeleton'
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 {
diffRoutesWithSizes,
......@@ -19,6 +20,7 @@ import { cn, formatBytes } from '@/lib/utils'
export interface CompareLayoutProps {
baselineSnapshot: SnapshotMetadata
comparisonSnapshot: SnapshotMetadata | null
compareView: CompareView
selectedRoute: string | null
comparisonRouteCount: number | null
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
......@@ -29,6 +31,7 @@ export interface CompareLayoutProps {
isBaselineAnalyzeLoading: boolean
baselineAnalyzeError: unknown | null
compressed: boolean
searchQuery: string
compareSelectedKey: string | null
setCompareSelectedKey: (key: string | null) => void
modulesData: ModulesData | null
......@@ -39,10 +42,9 @@ export interface CompareLayoutProps {
}
/**
* Renders the comparison view (treemap) with a context strip at the top
* showing the two builds being compared. The "before" build (A) is rendered
* on the left and the selected "after" build (B) on the right,
* matching the user's mental model.
* Renders the comparison view (treemap + table) with a context strip at the
* top showing the two builds being compared. The "before" build (A) is
* rendered on the left and the selected "after" build (B) on the right, matching the user's mental model.
*
* Route selection lives in the top-bar route picker (`RouteTypeahead`),
* which also renders per-route deltas in compare mode.
......@@ -50,6 +52,7 @@ export interface CompareLayoutProps {
export function CompareLayout({
baselineSnapshot,
comparisonSnapshot,
compareView,
selectedRoute,
comparisonRouteCount,
routeDiff,
......@@ -60,6 +63,7 @@ export function CompareLayout({
isBaselineAnalyzeLoading,
baselineAnalyzeError,
compressed,
searchQuery,
compareSelectedKey,
setCompareSelectedKey,
modulesData,
......@@ -100,12 +104,13 @@ export function CompareLayout({
</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.
*/}
<div className="flex flex-1 min-h-0">
<div className="flex flex-1 min-w-0 flex-col">
<ComparePerRoutePanel
compareView={compareView}
selectedRoute={selectedRoute}
sourceDiff={sourceDiff}
analyzeData={analyzeData}
......@@ -113,6 +118,9 @@ export function CompareLayout({
isAnalyzeLoading={isAnalyzeLoading}
isBaselineAnalyzeLoading={isBaselineAnalyzeLoading}
compressed={compressed}
searchQuery={searchQuery}
baselineSnapshot={baselineSnapshot}
comparisonSnapshot={comparisonSnapshot}
compareSelectedKey={compareSelectedKey}
onCompareSelectedKeyChange={setCompareSelectedKey}
/>
......@@ -370,11 +378,12 @@ function RouteStatsBody({
}
/**
* Per-route comparison panel. Shows the diff treemap. Handles missing-route
* states (the route is new in the comparison build, or was removed) with
* friendly messaging.
* Per-route comparison panel. Shows either the diff treemap or the diff table,
* controlled by `compareView`. Handles missing-route states (the route is new
* in comparison build B, or was removed) with friendly messaging.
*/
function ComparePerRoutePanel({
compareView,
selectedRoute,
sourceDiff,
analyzeData,
......@@ -382,9 +391,13 @@ function ComparePerRoutePanel({
isAnalyzeLoading,
isBaselineAnalyzeLoading,
compressed,
searchQuery,
baselineSnapshot,
comparisonSnapshot,
compareSelectedKey,
onCompareSelectedKeyChange,
}: {
compareView: CompareView
selectedRoute: string | null
sourceDiff: DiffSummary<SourceDiffRow> | null
analyzeData: AnalyzeData | null
......@@ -392,6 +405,9 @@ function ComparePerRoutePanel({
isAnalyzeLoading: boolean
isBaselineAnalyzeLoading: boolean
compressed: boolean
searchQuery: string
baselineSnapshot: SnapshotMetadata
comparisonSnapshot: SnapshotMetadata | null
compareSelectedKey: string | null
onCompareSelectedKeyChange: (key: string | null) => void
}) {
......@@ -419,14 +435,31 @@ function ComparePerRoutePanel({
return (
<div className="flex flex-1 min-h-0 flex-col">
<DiffTreemap
summary={sourceDiff}
useCompressed={compressed}
analyzeData={analyzeData}
baselineAnalyzeData={baselineAnalyzeData}
selectedKey={compareSelectedKey}
onSelectKey={onCompareSelectedKeyChange}
/>
{compareView === CompareView.Treemap ? (
<DiffTreemap
summary={sourceDiff}
useCompressed={compressed}
analyzeData={analyzeData}
baselineAnalyzeData={baselineAnalyzeData}
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>
)
}
This diff is collapsed.
......@@ -11,6 +11,7 @@ import {
SelectValue,
} from '@/components/ui/select'
import { MultiSelect } from '@/components/ui/multi-select'
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
import { diffRoutesWithSizes } from '@/lib/diff'
import { type SnapshotMetadata } from '@/lib/snapshot'
import {
......@@ -20,6 +21,8 @@ import {
FileJson,
Palette,
Package,
Table as TableIcon,
LayoutGrid,
} from 'lucide-react'
export enum Environment {
......@@ -27,6 +30,11 @@ export enum Environment {
Server = 'server',
}
export enum CompareView {
Treemap = 'treemap',
Table = 'table',
}
const typeFilterOptions = [
{
value: 'js',
......@@ -65,10 +73,14 @@ export function TopBar({
onBaselineChange,
comparisonSnapshot,
onComparisonChange,
compareView,
onCompareViewChange,
routeDiff,
hasSourceData,
showViewToggle,
}: {
hasSourceData: boolean
showViewToggle: boolean
selectedRoute: string | null
setSelectedRoute: (route: string | null) => void
environmentFilter: Environment
......@@ -83,6 +95,8 @@ export function TopBar({
onBaselineChange: (snapshot: SnapshotMetadata | null) => void
comparisonSnapshot: SnapshotMetadata | null
onComparisonChange: (snapshot: SnapshotMetadata | null) => void
compareView: CompareView
onCompareViewChange: (view: CompareView) => void
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
}) {
const isCompareMode = baselineSnapshot != null
......@@ -120,6 +134,37 @@ export function TopBar({
/>
) : 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 && (
<>
<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