Unverified Commit 1bf5bf76 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Fix bundle analyzer route selector (#98702)

Fix route selector overflow for long route names and constrain the
popover to the viewport.

Deduplicate route names when generating analyzer data and before
rendering the picker so parallel routes and existing analyzer corpora
cannot produce duplicate command items.

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent d71d13d3
......@@ -88,11 +88,13 @@ export function RouteTypeahead({
// the most-changed route bubbles to the top — matching the rest of the
// compare UI. Without a diff, fall back to the natural routes.json order.
const orderedItems: RouteItem[] = routeDiff
? sortByImpact(routeDiff.rows, useCompressed).map((row) => ({
name: row.key,
row,
}))
: routes.map((name) => ({ name, row: null }))
? uniqueRouteItems(
sortByImpact(routeDiff.rows, useCompressed).map((row) => ({
name: row.key,
row,
}))
)
: uniqueRouteItems(routes.map((name) => ({ name, row: null })))
// Find the currently selected route's diff row, used to render a delta
// badge in the trigger button.
......@@ -104,35 +106,45 @@ export function RouteTypeahead({
const ctaText = selectedRoute ?? 'Select route...'
return (
<div className="flex items-center gap-2 min-w-64 max-w-full">
<div className="flex min-w-0 items-center gap-2 sm:min-w-64">
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="flex-grow-1 w-full justify-between font-mono text-sm"
aria-label={
selectedRoute
? `Select route. Current route: ${selectedRoute}`
: 'Select route'
}
className="w-full min-w-0 justify-between font-mono text-sm"
>
<div className="flex items-center min-w-0">
<div className="flex min-w-0 flex-1 items-center">
<Route className="inline mr-2 shrink-0" />
<span className="truncate">{ctaText}</span>
<span className="min-w-0 flex-1 truncate" title={ctaText}>
{truncateMiddle(ctaText, 32)}
</span>
{selectedRow ? (
<DeltaBadge row={selectedRow} useCompressed={useCompressed} />
) : null}
</div>
<div className="flex items-center gap-2">
<div className="ml-2 flex shrink-0 items-center gap-2">
{shortcutLabel && <Kbd>{shortcutLabel}</Kbd>}
<ChevronsUpDown className="h-4 w-4 shrink-0 opacity-50" />
</div>
</Button>
</PopoverTrigger>
<PopoverContent className="w-96 p-0">
<Command>
<PopoverContent
align="start"
className="w-[40rem] max-w-[calc(100vw-1rem)] overflow-hidden p-0"
>
<Command className="min-w-0">
<CommandInput placeholder="Search routes..." className="h-9" />
<CommandList>
<CommandList className="min-w-0">
<CommandEmpty>No route found.</CommandEmpty>
<CommandGroup>
<CommandGroup className="min-w-0 [&_[cmdk-group-items]]:min-w-0">
{orderedItems.map(({ name, row }) => (
<CommandItem
key={name}
......@@ -141,7 +153,7 @@ export function RouteTypeahead({
onRouteSelected(name)
setOpen(false)
}}
className="font-mono"
className="w-full min-w-0 overflow-hidden font-mono"
>
<Check
className={cn(
......@@ -149,7 +161,14 @@ export function RouteTypeahead({
selectedRoute === name ? 'opacity-100' : 'opacity-0'
)}
/>
<span className="truncate">{name}</span>
<span className="sr-only">{name}</span>
<span
aria-hidden="true"
className="min-w-0 flex-1 truncate"
title={name}
>
{truncateMiddle(name, 64)}
</span>
{row ? (
<DeltaBadge
row={row}
......@@ -173,6 +192,23 @@ interface RouteItem {
row: DiffRow | null
}
function truncateMiddle(value: string, maxLength: number): string {
if (value.length <= maxLength) return value
const startLength = Math.ceil((maxLength - 1) / 2)
const endLength = Math.floor((maxLength - 1) / 2)
return `${value.slice(0, startLength)}…${value.slice(-endLength)}`
}
function uniqueRouteItems(items: RouteItem[]): RouteItem[] {
const names = new Set<string>()
return items.filter(({ name }) => {
if (names.has(name)) return false
names.add(name)
return true
})
}
/**
* Compact, color-coded badge showing a route's size delta. Hidden when the
* row has no meaningful change.
......
......@@ -102,7 +102,7 @@ export function TopBar({
const isCompareMode = baselineSnapshot != null
return (
<div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3">
<div className="flex-1 flex">
<div className="flex min-w-0 flex-1">
<RouteTypeahead
selectedRoute={selectedRoute}
onRouteSelected={(route) => {
......
......@@ -199,9 +199,13 @@ async function collectRoutesForAnalyze(
isAppPPREnabled,
})
return routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
return Array.from(
new Set(
routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
)
)
}
function startServer(dir: string, port: number): Promise<void> {
......
......@@ -4763,9 +4763,13 @@ export default async function build(
})
// Write an index of routes for the route picker
const routes = routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
const routes = Array.from(
new Set(
routesManifest.dynamicRoutes
.map((r) => r.page)
.concat(routesManifest.staticRoutes.map((r) => r.page))
)
)
await writeFile(
path.join(analyzeDir, 'data/routes.json'),
JSON.stringify(routes, null, 2)
......
export default function SlotPage() {
return <div>Slot</div>
}
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