Skip to content

Commit 388bca4

Browse files
refactor(web): the diff panel keeps its caching without "use memo" hooks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b78296a commit 388bca4

1 file changed

Lines changed: 55 additions & 106 deletions

File tree

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

Lines changed: 55 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -113,62 +113,6 @@ function getCachedFileEntry(fileDiff: FileDiffMetadata) {
113113
return entry;
114114
}
115115

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-
172116
interface CollapsedDiffFilesState {
173117
readonly scopeKey: string | null;
174118
readonly fileKeys: ReadonlySet<string>;
@@ -412,19 +356,47 @@ export default function DiffPanel({
412356
const refreshDiffFromUserAction = refreshPreviewQuery;
413357

414358
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 =
417390
selectedRunId === null && activeThread && preview && selectedGitSource && canReadFiles
418-
? {
391+
? createGitDiffFileContentsLoader(getDiffFileContents, {
419392
environmentId: activeThread.environmentId,
420393
cwd: preview.cwd,
421394
sourceKind: selectedGitSource.kind,
422395
baseRef: selectedGitSource.baseRef,
423396
headRef: selectedGitSource.headRef,
424397
cacheKey: selectedGitSource.diffHash,
425-
}
426-
: null,
427-
);
398+
})
399+
: undefined;
428400
// The code view keeps one loader for its lifetime; a new identity would remount it.
429401
const loadDiffFiles: FileDiffContentsLoader = useStableCallback(async (fileDiff) => {
430402
if (!currentLoadDiffFiles) {
@@ -480,26 +452,6 @@ export default function DiffPanel({
480452
...(baseRefQuery.trim().length === 0 ? [AUTOMATIC_BASE_REF] : []),
481453
...matchingBaseRefChoices.map(valueForBaseRefChoice),
482454
];
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);
503455
const {
504456
scope: filePatchScope,
505457
isPending: areFilePatchesPending,
@@ -539,10 +491,11 @@ export default function DiffPanel({
539491
});
540492

541493
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;
546499
const collapsedDiffFileKeys =
547500
collapsedDiffFiles.scopeKey === collapseScopeKey
548501
? collapsedDiffFiles.fileKeys
@@ -554,22 +507,22 @@ export default function DiffPanel({
554507
count={renderableFiles.length - settledFileCount}
555508
/>
556509
) : 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);
562524
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);
573526
const fileTreeEntries = diffFileTreeEntries(renderableFiles);
574527
const selectedDiffFileKey = selectedFilePath
575528
? (codeViewFiles.find((candidate) => candidate.filePath === selectedFilePath)?.fileKey ?? null)
@@ -582,11 +535,7 @@ export default function DiffPanel({
582535
}, [codeView, codeViewMountKey, selectedDiffFileKey, selectedFileRevealRequestId]);
583536

584537
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);
590539
const revealDiffFile = (filePath: string) => {
591540
const index = renderableFileEntries.findIndex(
592541
(candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath,

0 commit comments

Comments
 (0)