Skip to content

Commit 0999854

Browse files
refactor(web): review file patches keep their caching without "use memo" hooks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c9b8f21 commit 0999854

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function useReviewFilePatches({
4242
baseRef,
4343
ignoreWhitespace,
4444
]);
45-
const [requested, setRequested] = useState({ scope, indices: DEFAULT_INDICES });
46-
const { files, queries } = useReviewFileQueries({
45+
const [requested, setRequested] = useState(() => ({ scope, indices: DEFAULT_INDICES }));
46+
const queries = reviewFileQueries({
4747
environmentId,
4848
cwd,
4949
source,
@@ -52,6 +52,7 @@ export function useReviewFilePatches({
5252
scope,
5353
requested,
5454
});
55+
const queriedPatchesAtom = patchesAtom(queries, theme);
5556
const previousPreview = useRef({ scope, revision, queries: [] as typeof queries });
5657
useEffect(() => {
5758
const previous = previousPreview.current;
@@ -65,7 +66,13 @@ export function useReviewFilePatches({
6566
if (changed || cached) registry.refresh(query);
6667
}
6768
}, [scope, revision, queries, registry]);
68-
const patches = useAtomValue(usePatchesAtom(queries, theme));
69+
const patches = useAtomValue(queriedPatchesAtom);
70+
const retry = useStableCallback((path: string) => {
71+
const files = sortedReviewFiles(source);
72+
const query = queries.find(({ index }) => files[index]?.path === path)?.query;
73+
if (query) registry.refresh(query);
74+
});
75+
const files = sortedReviewFiles(source);
6976
const pendingIndex = files.findIndex((_, index) => {
7077
const patch = patches.get(index);
7178
return !patch || patch._tag === "Initial";
@@ -86,10 +93,6 @@ export function useReviewFilePatches({
8693
const loadNextFiles = () =>
8794
requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index));
8895
const requestFile = (index: number) => requestFiles([index]);
89-
const retry = useStableCallback((path: string) => {
90-
const query = queries.find(({ index }) => files[index]?.path === path)?.query;
91-
if (query) registry.refresh(query);
92-
});
9396
const renderableFiles = source
9497
? files.map((file, index): FileDiffMetadata => {
9598
const result = patches.get(index);
@@ -154,9 +157,8 @@ export function useReviewFilePatches({
154157
};
155158
}
156159

157-
// The caller runs more hooks after these values, so the compiler would rebuild them
158-
// every render there; the refresh effect and the patches atom key on their identity.
159-
function useReviewFileQueries({
160+
// The refresh effect and the patches atom key on the identity of these queries.
161+
function reviewFileQueries({
160162
environmentId,
161163
cwd,
162164
source,
@@ -173,9 +175,8 @@ function useReviewFileQueries({
173175
scope: string;
174176
requested: { scope: string; indices: number[] };
175177
}) {
176-
"use memo";
177178
const indices = requested.scope === scope ? requested.indices : DEFAULT_INDICES;
178-
const files = source?.files?.toSorted((a, b) => filePathCollator.compare(a.path, b.path)) ?? [];
179+
const files = sortedReviewFiles(source);
179180
const queries =
180181
!environmentId || !cwd || !source
181182
? []
@@ -203,17 +204,20 @@ function useReviewFileQueries({
203204
}),
204205
};
205206
});
206-
return { files, queries };
207+
return queries;
208+
}
209+
210+
function sortedReviewFiles(source: ReviewDiffPreviewSource | null) {
211+
return source?.files?.toSorted((a, b) => filePathCollator.compare(a.path, b.path)) ?? [];
207212
}
208213

209-
function usePatchesAtom(
214+
function patchesAtom(
210215
queries: ReadonlyArray<{
211216
index: number;
212217
query: ReturnType<typeof reviewEnvironment.diffFilePatch>;
213218
}>,
214219
theme: "light" | "dark",
215220
) {
216-
"use memo";
217221
// Derived atoms parse each query result once, even when another file finishes loading.
218222
const parsedQuery = Atom.family((query: ReturnType<typeof reviewEnvironment.diffFilePatch>) =>
219223
Atom.map(query, (result) =>

0 commit comments

Comments
 (0)