Skip to content

Commit 6365fe7

Browse files
committed
fix(web): diff panel no longer re-renders every file header each time a patch arrives
Progressive loading delivers patches four at a time while the reader scrolls, and each arrival re-rendered the header content of every visible file through the viewer's portal slots: a tooltip, a button, a copy control and a status per header, 30 to 40 headers at a time. On a 139-file diff that was about 40 ms of main-thread work per arrival and the source of the scroll stutter. Header content now lives in its own components fed primitive props and stable callbacks, so the compiler keeps an untouched header's elements and React skips its subtree. No React.memo: measured, it adds nothing on top of the compiler here.
1 parent 7812230 commit 6365fe7

3 files changed

Lines changed: 136 additions & 75 deletions

File tree

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 106 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,79 @@ interface CollapsedDiffFilesState {
118118

119119
const EMPTY_COLLAPSED_DIFF_FILE_KEYS: ReadonlySet<string> = new Set();
120120

121+
// Header content re-renders only when its own file changes.
122+
function DiffFileCollapseToggle({
123+
filePath,
124+
fileKey,
125+
collapsed,
126+
unavailable,
127+
iconClassName,
128+
onToggle,
129+
}: {
130+
filePath: string;
131+
fileKey: string;
132+
collapsed: boolean;
133+
unavailable: boolean;
134+
iconClassName: string;
135+
onToggle: (fileKey: string) => void;
136+
}) {
137+
return (
138+
<Tooltip>
139+
<TooltipTrigger
140+
render={
141+
<Button
142+
size="icon-micro"
143+
variant="ghost"
144+
className="-ms-0.5"
145+
aria-label={collapsed ? `Expand ${filePath}` : `Collapse ${filePath}`}
146+
aria-expanded={!collapsed}
147+
disabled={unavailable}
148+
onClick={(event) => {
149+
event.stopPropagation();
150+
onToggle(fileKey);
151+
}}
152+
/>
153+
}
154+
>
155+
<MorphIcon
156+
className={cn("size-4", iconClassName)}
157+
icon={collapsed ? ChevronRight : ChevronDown}
158+
/>
159+
</TooltipTrigger>
160+
<TooltipPopup side="top">{collapsed ? "Expand diff" : "Collapse diff"}</TooltipPopup>
161+
</Tooltip>
162+
);
163+
}
164+
165+
// Header content re-renders only when its own file changes.
166+
function DiffFileHeaderSuffix({
167+
filePath,
168+
hasStat,
169+
error,
170+
truncated,
171+
onRetry,
172+
}: {
173+
filePath: string;
174+
hasStat: boolean;
175+
error: boolean;
176+
truncated: boolean;
177+
onRetry: (path: string) => void;
178+
}) {
179+
return (
180+
<>
181+
<DiffFilePathCopyButton filePath={filePath} />
182+
{hasStat ? (
183+
<DiffFileStatus error={error} truncated={truncated} retry={() => onRetry(filePath)} />
184+
) : null}
185+
</>
186+
);
187+
}
188+
189+
// Header content re-renders only when its own file changes.
190+
function DiffFileHeaderStat({ additions, deletions }: { additions: number; deletions: number }) {
191+
return <DiffStatLabel additions={additions} deletions={deletions} />;
192+
}
193+
121194
interface DiffPanelProps {
122195
mode?: DiffPanelMode;
123196
composerDraftTarget: ScopedThreadRef | DraftId;
@@ -598,22 +671,22 @@ export default function DiffPanel({
598671
},
599672
[activeCwd, activeRepositoryRoot, openInPreferredEditor, routeThreadRef],
600673
);
601-
const toggleDiffFileCollapsed = useCallback(
602-
(fileKey: string) => {
603-
setCollapsedDiffFiles((current) => {
604-
const next = new Set(
605-
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
606-
);
607-
if (next.has(fileKey)) {
608-
next.delete(fileKey);
609-
} else {
610-
next.add(fileKey);
611-
}
612-
return { scopeKey: collapseScopeKey, fileKeys: next };
613-
});
614-
},
615-
[collapseScopeKey, defaultCollapsedDiffFileKeys],
616-
);
674+
const collapseDefaultsRef = useRef({ collapseScopeKey, defaultCollapsedDiffFileKeys });
675+
collapseDefaultsRef.current = { collapseScopeKey, defaultCollapsedDiffFileKeys };
676+
const toggleDiffFileCollapsed = useCallback((fileKey: string) => {
677+
const { collapseScopeKey, defaultCollapsedDiffFileKeys } = collapseDefaultsRef.current;
678+
setCollapsedDiffFiles((current) => {
679+
const next = new Set(
680+
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
681+
);
682+
if (next.has(fileKey)) {
683+
next.delete(fileKey);
684+
} else {
685+
next.add(fileKey);
686+
}
687+
return { scopeKey: collapseScopeKey, fileKeys: next };
688+
});
689+
}, []);
617690

618691
const toggleDiffFileCollapse = useCallback(() => {
619692
setCodeViewRevision((current) => current + 1);
@@ -1082,14 +1155,15 @@ export default function DiffPanel({
10821155
composerDraftTarget={composerDraftTarget}
10831156
renderHeaderFilenameSuffix={(fileDiff) => {
10841157
const path = resolveFileDiffPath(fileDiff);
1085-
const stat = fileStats.get(path);
1158+
const state = fileStates.get(path);
10861159
return (
1087-
<>
1088-
<DiffFilePathCopyButton filePath={path} />
1089-
{stat ? (
1090-
<DiffFileStatus {...fileStates.get(path)} retry={() => retry(path)} />
1091-
) : null}
1092-
</>
1160+
<DiffFileHeaderSuffix
1161+
filePath={path}
1162+
hasStat={fileStats.has(path)}
1163+
error={state?.error ?? false}
1164+
truncated={state?.truncated ?? false}
1165+
onRetry={retry}
1166+
/>
10931167
);
10941168
}}
10951169
{...(lazySource
@@ -1099,7 +1173,7 @@ export default function DiffPanel({
10991173
renderHeaderMetadata: (fileDiff: FileDiffMetadata) => {
11001174
const stat = fileStats.get(resolveFileDiffPath(fileDiff));
11011175
return stat ? (
1102-
<DiffStatLabel
1176+
<DiffFileHeaderStat
11031177
additions={stat.additions}
11041178
deletions={stat.deletions}
11051179
/>
@@ -1109,37 +1183,15 @@ export default function DiffPanel({
11091183
: {})}
11101184
renderHeaderPrefix={(fileDiff, fileKey) => {
11111185
const unavailable = fileDiff.cacheKey?.endsWith(":pending") === true;
1112-
const collapsed = unavailable || collapsedDiffFileKeys.has(fileKey);
1113-
const filePath = resolveFileDiffPath(fileDiff);
11141186
return (
1115-
<Tooltip>
1116-
<TooltipTrigger
1117-
render={
1118-
<Button
1119-
size="icon-micro"
1120-
variant="ghost"
1121-
className="-ms-0.5"
1122-
aria-label={
1123-
collapsed ? `Expand ${filePath}` : `Collapse ${filePath}`
1124-
}
1125-
aria-expanded={!collapsed}
1126-
disabled={unavailable}
1127-
onClick={(event) => {
1128-
event.stopPropagation();
1129-
toggleDiffFileCollapsed(fileKey);
1130-
}}
1131-
/>
1132-
}
1133-
>
1134-
<MorphIcon
1135-
className={cn("size-4", getDiffCollapseIconClassName(fileDiff))}
1136-
icon={collapsed ? ChevronRight : ChevronDown}
1137-
/>
1138-
</TooltipTrigger>
1139-
<TooltipPopup side="top">
1140-
{collapsed ? "Expand diff" : "Collapse diff"}
1141-
</TooltipPopup>
1142-
</Tooltip>
1187+
<DiffFileCollapseToggle
1188+
filePath={resolveFileDiffPath(fileDiff)}
1189+
fileKey={fileKey}
1190+
collapsed={unavailable || collapsedDiffFileKeys.has(fileKey)}
1191+
unavailable={unavailable}
1192+
iconClassName={getDiffCollapseIconClassName(fileDiff)}
1193+
onToggle={toggleDiffFileCollapsed}
1194+
/>
11431195
);
11441196
}}
11451197
options={{

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
} from "@pierre/diffs";
99
import type { CodeViewHandle } from "@pierre/diffs/react";
1010
import type { ScopedThreadRef } from "@t3tools/contracts";
11-
import { useCallback, useMemo, useState, type ReactNode, type Ref } from "react";
11+
import { useCallback, useMemo, useRef, useState, type ReactNode, type Ref } from "react";
1212

1313
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
1414
import { fnv1a32 } from "~/lib/diffRendering";
@@ -132,6 +132,8 @@ export function AnnotatableCodeView({
132132
const [draftText, setDraftText] = useState("");
133133

134134
const filesByKey = useMemo(() => new Map(files.map((file) => [file.fileKey, file])), [files]);
135+
const filesByKeyRef = useRef(filesByKey);
136+
filesByKeyRef.current = filesByKey;
135137
const items = useMemo<CodeViewDiffItem<DiffCommentAnnotationGroup>[]>(
136138
() =>
137139
files.map(({ fileDiff, filePath, fileKey, fileVersion, collapsed }) => {
@@ -217,7 +219,7 @@ export function AnnotatableCodeView({
217219
if (!range) return;
218220
const item = context.item;
219221
if (item.type !== "diff") return;
220-
const file = filesByKey.get(item.id);
222+
const file = filesByKeyRef.current.get(item.id);
221223
if (!file) return;
222224
const id = nextFileCommentId();
223225
const comment = buildDiffReviewComment({
@@ -242,7 +244,7 @@ export function AnnotatableCodeView({
242244
},
243245
});
244246
},
245-
[filesByKey, sectionId, sectionTitle],
247+
[sectionId, sectionTitle],
246248
);
247249

248250
const hasOpenComment = draft !== null;

‎apps/web/src/components/diffs/useReviewFilePatches.ts‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,15 @@ export function useReviewFilePatches({
146146
[requestFiles, settledFileCount],
147147
);
148148
const requestFile = useCallback((index: number) => requestFiles([index]), [requestFiles]);
149+
const retryInputsRef = useRef({ queries, files });
150+
retryInputsRef.current = { queries, files };
149151
const retry = useCallback(
150152
(path: string) => {
153+
const { queries, files } = retryInputsRef.current;
151154
const query = queries.find(({ index }) => files[index]?.path === path)?.query;
152155
if (query) registry.refresh(query);
153156
},
154-
[queries, files, registry],
157+
[registry],
155158
);
156159
const renderableFiles = useMemo(
157160
() =>
@@ -185,23 +188,27 @@ export function useReviewFilePatches({
185188
),
186189
[source, files, patches, scope, preview],
187190
);
188-
const fileStates = new Map(
189-
files.map((file, index) => {
190-
const patch = patches.get(index);
191-
return [
192-
file.path,
193-
{
194-
error:
195-
patch?._tag === "Failure" ||
196-
(patch?._tag === "Success" &&
197-
(patch.value.patch?.kind !== "files" ||
198-
!patch.value.patch.files.some(
199-
(candidate) => resolveFileDiffPath(candidate) === file.path,
200-
))),
201-
truncated: patch?._tag === "Success" && patch.value.source.truncated,
202-
},
203-
] as const;
204-
}),
191+
const fileStates = useMemo(
192+
() =>
193+
new Map(
194+
files.map((file, index) => {
195+
const patch = patches.get(index);
196+
return [
197+
file.path,
198+
{
199+
error:
200+
patch?._tag === "Failure" ||
201+
(patch?._tag === "Success" &&
202+
(patch.value.patch?.kind !== "files" ||
203+
!patch.value.patch.files.some(
204+
(candidate) => resolveFileDiffPath(candidate) === file.path,
205+
))),
206+
truncated: patch?._tag === "Success" && patch.value.source.truncated,
207+
},
208+
] as const;
209+
}),
210+
),
211+
[files, patches],
205212
);
206213
const readyFilePaths = useMemo(
207214
() =>

0 commit comments

Comments
 (0)