Unverified Commit 3932a6bd authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Fix bundle analyzer source table delta sorting (#99369)

## Summary

Follow-up to #98539 addressing [Luke’s `toSorted()`
feedback](https://github.com/vercel/next.js/pull/98539#discussion_r4098899189),
[signed Δ ordering
feedback](https://github.com/vercel/next.js/pull/98539#discussion_r4098962512),
and [locale-aware name tie-breaker
feedback](https://github.com/vercel/next.js/pull/99369#discussion_r4125394575).

The source table sorts individual rows and package aggregates by signed
Δ in either direction; identical rows and groups remain last for every
column. The separate impact-ranked route picker is unchanged, and table
sorting uses `toSorted()` to preserve input arrays. Numeric ties now use
the same locale-aware name ordering as the Name column.

In compare mode the table defaults to **Changes**, showing added,
removed, and changed sources; **All** opts into unchanged sources. The
footer still reports full build totals. Single-build mode remains
unfiltered because its rows are all compared to themselves.

## Verification

- Focused filter checks: 6/6 passed (baseline 2/6).
- Focused sorting checks: 12/12 passed (two new comparator cases failed
before the fix).
- `pnpm exec prettier --check
apps/bundle-analyzer/components/diff-table.tsx`
- `pnpm exec eslint --config eslint.config.mjs
apps/bundle-analyzer/components/diff-table.tsx`
- `pnpm exec tsc -p apps/bundle-analyzer/tsconfig.json --noEmit`

<!-- NEXT_JS_LLM -->

<!-- fleet d2af60a7-2332-4bf1-a425-0329992b44dd -->

---------
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 520925bb
...@@ -17,7 +17,6 @@ import { ...@@ -17,7 +17,6 @@ import {
import { import {
delta, delta,
formatDelta, formatDelta,
sortByImpact,
type DiffRow, type DiffRow,
type DiffSummary, type DiffSummary,
type DiffStatus, type DiffStatus,
...@@ -30,6 +29,7 @@ import { Badge } from '@/components/ui/badge' ...@@ -30,6 +29,7 @@ import { Badge } from '@/components/ui/badge'
type SortColumn = 'name' | 'a' | 'b' | 'delta' type SortColumn = 'name' | 'a' | 'b' | 'delta'
/** Sort direction. */ /** Sort direction. */
type SortDirection = 'asc' | 'desc' type SortDirection = 'asc' | 'desc'
type StatusFilter = DiffStatus | 'changes' | 'all'
interface DiffTableProps<Row extends DiffRow> { interface DiffTableProps<Row extends DiffRow> {
summary: DiffSummary<Row> summary: DiffSummary<Row>
...@@ -99,7 +99,7 @@ const virtuosoComponents = { ...@@ -99,7 +99,7 @@ const virtuosoComponents = {
* *
* | Name | A (before) | B (after) | Δ | * | Name | A (before) | B (after) | Δ |
* *
* Rows are sorted by absolute delta (largest impact first). * In compare mode, rows are sorted by signed delta (largest increase first).
*/ */
export function DiffTable<Row extends DiffRow>({ export function DiffTable<Row extends DiffRow>({
summary, summary,
...@@ -114,9 +114,11 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -114,9 +114,11 @@ export function DiffTable<Row extends DiffRow>({
searchQuery, searchQuery,
}: DiffTableProps<Row>) { }: DiffTableProps<Row>) {
const isSingle = mode === 'single' const isSingle = mode === 'single'
const [statusFilter, setStatusFilter] = useState<DiffStatus | 'all'>('all') // Compare mode shows only changed sources by default. In single mode every
// row is `identical` against itself, so the status filter is ignored.
const [statusFilter, setStatusFilter] = useState<StatusFilter>('changes')
// Default sort: // Default sort:
// - compare mode: rank by largest absolute delta (most-impactful change first). // - compare mode: rank by signed delta (largest increase first).
// - single mode: rank by size descending (largest contributors first). // - single mode: rank by size descending (largest contributors first).
const [sortColumn, setSortColumn] = useState<SortColumn>( const [sortColumn, setSortColumn] = useState<SortColumn>(
isSingle ? 'b' : 'delta' isSingle ? 'b' : 'delta'
...@@ -137,16 +139,10 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -137,16 +139,10 @@ export function DiffTable<Row extends DiffRow>({
}) })
} }
const sorted = useMemo(() => { const sorted = useMemo(
// The default `delta`/`desc` mode preserves the existing impact-ranked () => sortRows(summary.rows, sortColumn, sortDirection, useCompressed),
// sort, which keeps `identical` rows pinned at the bottom regardless of [summary.rows, sortColumn, sortDirection, useCompressed]
// direction. Other columns sort lexicographically (name) or numerically )
// (A, B) using the user's chosen direction.
if (sortColumn === 'delta' && sortDirection === 'desc') {
return sortByImpact(summary.rows, useCompressed)
}
return sortRows(summary.rows, sortColumn, sortDirection, useCompressed)
}, [summary.rows, sortColumn, sortDirection, useCompressed])
const onHeaderClick = (column: SortColumn) => { const onHeaderClick = (column: SortColumn) => {
if (column === sortColumn) { if (column === sortColumn) {
...@@ -162,7 +158,13 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -162,7 +158,13 @@ export function DiffTable<Row extends DiffRow>({
const filtered = useMemo(() => { const filtered = useMemo(() => {
const q = searchQuery?.trim().toLowerCase() ?? '' const q = searchQuery?.trim().toLowerCase() ?? ''
return sorted.filter((row) => { return sorted.filter((row) => {
if (statusFilter !== 'all' && row.status !== statusFilter) return false if (!isSingle && statusFilter !== 'all') {
if (statusFilter === 'changes' && row.status === 'identical')
return false
if (statusFilter !== 'changes' && row.status !== statusFilter) {
return false
}
}
if (q !== '') { if (q !== '') {
const key = row.key.toLowerCase() const key = row.key.toLowerCase()
const name = row.name.toLowerCase() const name = row.name.toLowerCase()
...@@ -170,7 +172,7 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -170,7 +172,7 @@ export function DiffTable<Row extends DiffRow>({
} }
return true return true
}) })
}, [sorted, statusFilter, searchQuery]) }, [sorted, isSingle, statusFilter, searchQuery])
// Group filtered rows by package, preserving the user's chosen sort order. // Group filtered rows by package, preserving the user's chosen sort order.
// Groups themselves are sorted by their aggregate using the same column + // Groups themselves are sorted by their aggregate using the same column +
...@@ -309,7 +311,9 @@ export function DiffTable<Row extends DiffRow>({ ...@@ -309,7 +311,9 @@ export function DiffTable<Row extends DiffRow>({
/> />
{visibleItems.length === 0 ? ( {visibleItems.length === 0 ? (
<div className="absolute inset-x-0 top-20 px-4 py-8 text-center text-sm text-muted-foreground"> <div className="absolute inset-x-0 top-20 px-4 py-8 text-center text-sm text-muted-foreground">
No matching rows. {!isSingle && statusFilter === 'changes' && !searchQuery?.trim()
? 'No changes to show.'
: 'No matching rows.'}
</div> </div>
) : null} ) : null}
</div> </div>
...@@ -459,7 +463,7 @@ function sortRenderItems<Row extends DiffRow>( ...@@ -459,7 +463,7 @@ function sortRenderItems<Row extends DiffRow>(
} }
} }
return [...items].sort((left, right) => { return items.toSorted((left, right) => {
const a = values(left) const a = values(left)
const b = values(right) const b = values(right)
if (a.identical && !b.identical) return 1 if (a.identical && !b.identical) return 1
...@@ -471,14 +475,12 @@ function sortRenderItems<Row extends DiffRow>( ...@@ -471,14 +475,12 @@ function sortRenderItems<Row extends DiffRow>(
} else { } else {
const aValue = a[column] const aValue = a[column]
const bValue = b[column] const bValue = b[column]
const comparison = const comparison = (aValue - bValue) * sign
column === 'delta' && direction === 'desc'
? Math.abs(bValue) - Math.abs(aValue)
: (aValue - bValue) * sign
if (comparison !== 0) return comparison if (comparison !== 0) return comparison
} }
return a.name < b.name ? -1 : 1 // Break numeric ties using the same locale-aware order as the name column.
return a.name.localeCompare(b.name)
}) })
} }
...@@ -652,9 +654,9 @@ function PackageCountBreakdown({ ...@@ -652,9 +654,9 @@ function PackageCountBreakdown({
/** /**
* Sorts rows by the chosen column and direction. The `name` column compares * Sorts rows by the chosen column and direction. The `name` column compares
* using a locale-aware lexicographic ordering; numeric columns compare by * using a locale-aware lexicographic ordering; numeric columns compare by
* value. `identical` rows are always pinned to the bottom — they aren't part * value. `identical` rows are pinned to the bottom for every column when
* of the "what changed" story and would otherwise dominate the top of an * visible (in the All filter or single mode), since they aren't part of the
* ascending sort. * "what changed" story.
*/ */
function sortRows<Row extends DiffRow>( function sortRows<Row extends DiffRow>(
rows: Row[], rows: Row[],
...@@ -669,7 +671,7 @@ function sortRows<Row extends DiffRow>( ...@@ -669,7 +671,7 @@ function sortRows<Row extends DiffRow>(
if (column === 'b') return useCompressed ? row.compressedB : row.sizeB if (column === 'b') return useCompressed ? row.compressedB : row.sizeB
return delta(row, useCompressed) return delta(row, useCompressed)
} }
return [...rows].sort((a, b) => { return rows.toSorted((a, b) => {
if (a.status === 'identical' && b.status !== 'identical') return 1 if (a.status === 'identical' && b.status !== 'identical') return 1
if (b.status === 'identical' && a.status !== 'identical') return -1 if (b.status === 'identical' && a.status !== 'identical') return -1
const av = compareKey(a) const av = compareKey(a)
...@@ -1010,17 +1012,18 @@ function DiffStatusFilter({ ...@@ -1010,17 +1012,18 @@ function DiffStatusFilter({
onChange, onChange,
counts, counts,
}: { }: {
value: DiffStatus | 'all' value: StatusFilter
onChange: (value: DiffStatus | 'all') => void onChange: (value: StatusFilter) => void
counts: Record<DiffStatus, number> counts: Record<DiffStatus, number>
}) { }) {
const total = const total =
counts.added + counts.removed + counts.changed + counts.identical counts.added + counts.removed + counts.changed + counts.identical
const options: Array<{ const options: Array<{
value: DiffStatus | 'all' value: StatusFilter
label: string label: string
count: number count: number
}> = [ }> = [
{ value: 'changes', label: 'Changes', count: total - counts.identical },
{ value: 'all', label: 'All', count: total }, { value: 'all', label: 'All', count: total },
{ value: 'added', label: 'Added', count: counts.added }, { value: 'added', label: 'Added', count: counts.added },
{ value: 'removed', label: 'Removed', count: counts.removed }, { value: 'removed', label: 'Removed', count: counts.removed },
......
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