Skip to content

Commit 2ed4503

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 is now memoized on primitive props with stable callbacks, so only the header whose file changed renders.
1 parent 7812230 commit 2ed4503

3 files changed

Lines changed: 143 additions & 76 deletions

File tree

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

Lines changed: 113 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
import { ChevronDown, ChevronRight, ChevronsDownUp, ChevronsUpDown } from "lucide";
2222
import * as Schema from "effect/Schema";
2323
import * as DateTime from "effect/DateTime";
24-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
24+
import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
2525
import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal";
2626
import { useOpenInPreferredEditor } from "../editorPreferences";
2727
import { useFileContextMenuHandler } from "../fileContextMenu";
@@ -118,6 +118,85 @@ 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+
const DiffFileCollapseToggle = memo(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+
const DiffFileHeaderSuffix = memo(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+
const DiffFileHeaderStat = memo(function DiffFileHeaderStat({
191+
additions,
192+
deletions,
193+
}: {
194+
additions: number;
195+
deletions: number;
196+
}) {
197+
return <DiffStatLabel additions={additions} deletions={deletions} />;
198+
});
199+
121200
interface DiffPanelProps {
122201
mode?: DiffPanelMode;
123202
composerDraftTarget: ScopedThreadRef | DraftId;
@@ -598,22 +677,22 @@ export default function DiffPanel({
598677
},
599678
[activeCwd, activeRepositoryRoot, openInPreferredEditor, routeThreadRef],
600679
);
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-
);
680+
const collapseDefaultsRef = useRef({ collapseScopeKey, defaultCollapsedDiffFileKeys });
681+
collapseDefaultsRef.current = { collapseScopeKey, defaultCollapsedDiffFileKeys };
682+
const toggleDiffFileCollapsed = useCallback((fileKey: string) => {
683+
const { collapseScopeKey, defaultCollapsedDiffFileKeys } = collapseDefaultsRef.current;
684+
setCollapsedDiffFiles((current) => {
685+
const next = new Set(
686+
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
687+
);
688+
if (next.has(fileKey)) {
689+
next.delete(fileKey);
690+
} else {
691+
next.add(fileKey);
692+
}
693+
return { scopeKey: collapseScopeKey, fileKeys: next };
694+
});
695+
}, []);
617696

618697
const toggleDiffFileCollapse = useCallback(() => {
619698
setCodeViewRevision((current) => current + 1);
@@ -1082,14 +1161,15 @@ export default function DiffPanel({
10821161
composerDraftTarget={composerDraftTarget}
10831162
renderHeaderFilenameSuffix={(fileDiff) => {
10841163
const path = resolveFileDiffPath(fileDiff);
1085-
const stat = fileStats.get(path);
1164+
const state = fileStates.get(path);
10861165
return (
1087-
<>
1088-
<DiffFilePathCopyButton filePath={path} />
1089-
{stat ? (
1090-
<DiffFileStatus {...fileStates.get(path)} retry={() => retry(path)} />
1091-
) : null}
1092-
</>
1166+
<DiffFileHeaderSuffix
1167+
filePath={path}
1168+
hasStat={fileStats.has(path)}
1169+
error={state?.error ?? false}
1170+
truncated={state?.truncated ?? false}
1171+
onRetry={retry}
1172+
/>
10931173
);
10941174
}}
10951175
{...(lazySource
@@ -1099,7 +1179,7 @@ export default function DiffPanel({
10991179
renderHeaderMetadata: (fileDiff: FileDiffMetadata) => {
11001180
const stat = fileStats.get(resolveFileDiffPath(fileDiff));
11011181
return stat ? (
1102-
<DiffStatLabel
1182+
<DiffFileHeaderStat
11031183
additions={stat.additions}
11041184
deletions={stat.deletions}
11051185
/>
@@ -1109,37 +1189,15 @@ export default function DiffPanel({
11091189
: {})}
11101190
renderHeaderPrefix={(fileDiff, fileKey) => {
11111191
const unavailable = fileDiff.cacheKey?.endsWith(":pending") === true;
1112-
const collapsed = unavailable || collapsedDiffFileKeys.has(fileKey);
1113-
const filePath = resolveFileDiffPath(fileDiff);
11141192
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>
1193+
<DiffFileCollapseToggle
1194+
filePath={resolveFileDiffPath(fileDiff)}
1195+
fileKey={fileKey}
1196+
collapsed={unavailable || collapsedDiffFileKeys.has(fileKey)}
1197+
unavailable={unavailable}
1198+
iconClassName={getDiffCollapseIconClassName(fileDiff)}
1199+
onToggle={toggleDiffFileCollapsed}
1200+
/>
11431201
);
11441202
}}
11451203
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)