The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): the diff panel keeps its caching without "use memo" hooks (#20745)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 23:24 UTC
c7838977915adfabf50bad1097d676f83072c410
1 file changed
+55
-106
apps/web/src/components/DiffPanel.tsx
+55
-106
@@ -113,62 +113,6 @@ function getCachedFileEntry(fileDiff: FileDiffMetadata) {
113
return entry;
114
}
115
116
-// These hooks call no hooks, so the compiler needs the directive to memoize them. Their results
117
-// feed the code view and the file patch loader, which key on identity.
118
-function useRenderablePatch(
119
- patch: string | undefined,
120
- theme: string,
121
- compactPartialHunkOffsets: boolean,
122
-) {
123
- "use memo";
124
- return getRenderablePatch(patch, `diff-panel:${theme}`, { compactPartialHunkOffsets });
125
-}
126
-
127
-function useGitDiffFileContentsLoader<E>(
128
- getDiffFileContents: Parameters<typeof createGitDiffFileContentsLoader<E>>[0],
129
- source: Parameters<typeof createGitDiffFileContentsLoader<E>>[1] | null,
130
-): FileDiffContentsLoader | undefined {
131
- "use memo";
132
- return source ? createGitDiffFileContentsLoader(getDiffFileContents, source) : undefined;
133
-}
134
-
135
-function useFileStats<File extends { readonly path: string }>(
136
- files: ReadonlyArray<File> | undefined,
137
-) {
138
- "use memo";
139
- return new Map(files?.map((file) => [file.path, file]));
140
-}
141
-
142
-function useDiffFileEntries(renderableFiles: ReadonlyArray<FileDiffMetadata>, collapsed: boolean) {
143
- "use memo";
144
- const renderableFileEntries = renderableFiles.map(getCachedFileEntry);
145
- const diffFileKeys = renderableFileEntries.map((file) => file.fileKey);
146
- return {
147
- renderableFileEntries,
148
- diffFileKeys,
149
- defaultCollapsedDiffFileKeys: collapsed
150
- ? new Set(diffFileKeys)
151
- : EMPTY_COLLAPSED_DIFF_FILE_KEYS,
152
- };
153
-}
154
-
155
-function useCodeViewFiles(
156
- entries: ReadonlyArray<ReturnType<typeof getCachedFileEntry>>,
157
- readyFilePaths: ReadonlySet<string> | null,
158
- collapsedFileKeys: ReadonlySet<string>,
159
-) {
160
- "use memo";
161
- return entries
162
- .filter(({ fileDiff }) => !readyFilePaths || readyFilePaths.has(resolveFileDiffPath(fileDiff)))
163
- .map(({ fileDiff, fileKey, fileVersion }) => ({
164
- fileDiff,
165
- filePath: resolveFileDiffPath(fileDiff),
166
- fileKey,
167
- fileVersion,
168
- collapsed: collapsedFileKeys.has(fileKey) || fileDiff.cacheKey?.endsWith(":pending") === true,
169
- }));
170
-}
171
-
116
interface CollapsedDiffFilesState {
117
readonly scopeKey: string | null;
118
readonly fileKeys: ReadonlySet<string>;
@@ -412,19 +356,47 @@ export default function DiffPanel({
356
const refreshDiffFromUserAction = refreshPreviewQuery;
357
358
const preview = branchDiffPreview.data;
415
- const currentLoadDiffFiles = useGitDiffFileContentsLoader(
416
- getDiffFileContents,
359
+ const gitDiff = selectedGitSource?.diff;
360
+
361
+ const selectedPatch = selectedTurn ? activeCheckpointDiff.data?.diff : gitDiff;
362
+ const isSelectedPatchTruncated = !selectedTurn && selectedGitSource?.truncated === true;
363
+ const isLoadingSelectedPatch = selectedTurn
364
+ ? activeCheckpointDiff.isPending
365
+ : branchDiffPreview.isPending;
366
+ const selectedPatchError = selectedTurn ? activeCheckpointDiff.error : branchDiffPreview.error;
367
+ const hasResolvedPatch = typeof selectedPatch === "string";
368
+ const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
369
+ const lazySource =
370
+ !selectedTurn && selectedGitSource?.truncated && selectedGitSource.files
371
+ ? selectedGitSource
372
+ : null;
373
+ const renderablePatch = getRenderablePatch(
374
+ lazySource ? undefined : selectedPatch,
375
+ `diff-panel:${resolvedTheme}`,
376
+ { compactPartialHunkOffsets: selectedRunId === null },
377
+ );
378
+ const fileStats = new Map(lazySource?.files?.map((file) => [file.path, file]));
379
+ const sourceLineStat =
380
+ !selectedTurn && selectedGitSource?.files
381
+ ? selectedGitSource.files.reduce(
382
+ (total, file) => ({
383
+ additions: total.additions + file.additions,
384
+ deletions: total.deletions + file.deletions,
385
+ }),
386
+ { additions: 0, deletions: 0 },
387
+ )
388
+ : null;
389
+ const currentLoadDiffFiles =
390
selectedRunId === null && activeThread && preview && selectedGitSource && canReadFiles
418
- ? {
391
+ ? createGitDiffFileContentsLoader(getDiffFileContents, {
392
environmentId: activeThread.environmentId,
393
cwd: preview.cwd,
394
sourceKind: selectedGitSource.kind,
395
baseRef: selectedGitSource.baseRef,
396
headRef: selectedGitSource.headRef,
397
cacheKey: selectedGitSource.diffHash,
425
- }
426
- : null,
427
- );
398
+ })
399
+ : undefined;
400
// The code view keeps one loader for its lifetime; a new identity would remount it.
401
const loadDiffFiles: FileDiffContentsLoader = useStableCallback(async (fileDiff) => {
402
if (!currentLoadDiffFiles) {
@@ -480,26 +452,6 @@ export default function DiffPanel({
452
...(baseRefQuery.trim().length === 0 ? [AUTOMATIC_BASE_REF] : []),
453
...matchingBaseRefChoices.map(valueForBaseRefChoice),
454
];
483
- const gitDiff = selectedGitSource?.diff;
484
-
485
- const selectedPatch = selectedTurn ? activeCheckpointDiff.data?.diff : gitDiff;
486
- const isSelectedPatchTruncated = !selectedTurn && selectedGitSource?.truncated === true;
487
- const isLoadingSelectedPatch = selectedTurn
488
- ? activeCheckpointDiff.isPending
489
- : branchDiffPreview.isPending;
490
- const selectedPatchError = selectedTurn ? activeCheckpointDiff.error : branchDiffPreview.error;
491
- const hasResolvedPatch = typeof selectedPatch === "string";
492
- const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
493
- const lazySource =
494
- !selectedTurn && selectedGitSource?.truncated && selectedGitSource.files
495
- ? selectedGitSource
496
- : null;
497
- const renderablePatch = useRenderablePatch(
498
- lazySource ? undefined : selectedPatch,
499
- resolvedTheme,
500
- selectedRunId === null,
501
- );
502
- const fileStats = useFileStats(lazySource?.files);
455
const {
456
scope: filePatchScope,
457
isPending: areFilePatchesPending,
@@ -539,10 +491,11 @@ export default function DiffPanel({
491
});
492
493
const isRefreshingDiff = branchDiffPreview.isPending || areFilePatchesPending;
542
- const { renderableFileEntries, diffFileKeys, defaultCollapsedDiffFileKeys } = useDiffFileEntries(
543
- renderableFiles,
544
- settings.diffFilesCollapsed,
545
- );
494
+ const renderableFileEntries = renderableFiles.map(getCachedFileEntry);
495
+ const diffFileKeys = renderableFileEntries.map((file) => file.fileKey);
496
+ const defaultCollapsedDiffFileKeys = settings.diffFilesCollapsed
497
+ ? new Set(diffFileKeys)
498
+ : EMPTY_COLLAPSED_DIFF_FILE_KEYS;
499
const collapsedDiffFileKeys =
500
collapsedDiffFiles.scopeKey === collapseScopeKey
501
? collapsedDiffFiles.fileKeys
@@ -554,22 +507,22 @@ export default function DiffPanel({
507
count={renderableFiles.length - settledFileCount}
508
/>
509
) : null;
557
- const codeViewFiles = useCodeViewFiles(
558
- renderableFileEntries,
559
- lazySource ? readyFilePaths : null,
560
- collapsedDiffFileKeys,
561
- );
510
+ const codeViewFiles = renderableFileEntries
511
+ .filter(
512
+ ({ fileDiff }) =>
513
+ !lazySource || !readyFilePaths || readyFilePaths.has(resolveFileDiffPath(fileDiff)),
514
+ )
515
+ .map(({ fileDiff, fileKey, fileVersion }) => ({
516
+ fileDiff,
517
+ filePath: resolveFileDiffPath(fileDiff),
518
+ fileKey,
519
+ fileVersion,
520
+ collapsed:
521
+ collapsedDiffFileKeys.has(fileKey) || fileDiff.cacheKey?.endsWith(":pending") === true,
522
+ }));
523
+ const codeViewFileKeys = codeViewFiles.map((file) => file.fileKey);
524
const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys);
563
- const diffLineStat =
564
- !selectedTurn && selectedGitSource?.files
565
- ? selectedGitSource.files.reduce(
566
- (total, file) => ({
567
- additions: total.additions + file.additions,
568
- deletions: total.deletions + file.deletions,
569
- }),
570
- { additions: 0, deletions: 0 },
571
- )
572
- : getDiffLineStat(renderableFiles);
525
+ const diffLineStat = sourceLineStat ?? getDiffLineStat(renderableFiles);
526
const fileTreeEntries = diffFileTreeEntries(renderableFiles);
527
const selectedDiffFileKey = selectedFilePath
528
? (codeViewFiles.find((candidate) => candidate.filePath === selectedFilePath)?.fileKey ?? null)
@@ -582,11 +535,7 @@ export default function DiffPanel({
535
}, [codeView, codeViewMountKey, selectedDiffFileKey, selectedFileRevealRequestId]);
536
537
const treeRevealScope = { collapseScopeKey, diffSelection };
585
- const requestTreeReveal = useCodeViewFileReveal(
586
- codeView,
587
- treeRevealScope,
588
- codeViewFiles.map((file) => file.fileKey),
589
- );
538
+ const requestTreeReveal = useCodeViewFileReveal(codeView, treeRevealScope, codeViewFileKeys);
539
const revealDiffFile = (filePath: string) => {
540
const index = renderableFileEntries.findIndex(
541
(candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath,