fix(site/src/pages/AgentsPage): pre-compute selectedLines to avoid busting LazyFileDiff memo (#23353)

This commit is contained in:
Danielle Maywood
2026-03-20 09:43:11 +00:00
committed by GitHub
parent 25445714b3
commit 484f637c6c
+27 -9
View File
@@ -623,16 +623,32 @@ export const DiffViewer: FC<DiffViewerProps> = ({
// Pre-compute per-file line annotations for the same reason.
const perFileAnnotations = useMemo(() => {
if (!getLineAnnotations) return null;
const map = new Map<string, DiffLineAnnotation<string>[]>();
for (const file of sortedFiles) {
const annotations = getLineAnnotations(file.name);
if (annotations.length > 0) {
map.set(file.name, annotations);
}
}
return map;
return new Map(
sortedFiles
.map((f) => [f.name, getLineAnnotations(f.name)] as const)
.filter(
(entry): entry is [string, DiffLineAnnotation<string>[]] =>
entry[1].length > 0,
),
);
}, [sortedFiles, getLineAnnotations]);
// Pre-compute per-file selected lines so each LazyFileDiff
// receives a stable reference. Without this, calling
// getSelectedLines during render returns a new object every
// time, which busts the memo comparator and forces an
// expensive Shadow DOM + shiki re-highlight.
const perFileSelectedLines = useMemo(() => {
if (!getSelectedLines) return null;
return new Map(
sortedFiles
.map((f) => [f.name, getSelectedLines(f.name)] as const)
.filter(
(entry): entry is [string, SelectedLineRange] => entry[1] != null,
),
);
}, [sortedFiles, getSelectedLines]);
// ---------------------------------------------------------------
// Container width measurement via ResizeObserver so we can decide
// whether to show the file tree sidebar without a prop from the
@@ -889,7 +905,9 @@ export const DiffViewer: FC<DiffViewerProps> = ({
}
lineAnnotations={perFileAnnotations?.get(fileDiff.name)}
renderAnnotation={renderAnnotation}
selectedLines={getSelectedLines?.(fileDiff.name)}
selectedLines={
perFileSelectedLines?.get(fileDiff.name) ?? null
}
/>
{isLast && (
<div className="flex items-center justify-center py-4 text-xs text-content-secondary">