@devries-cameron20 / t3code Mirror · read-only / commits / 73af712275
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 05:30 UTC

refactor(web): review file patches keep their caching without "use memo" hooks (#20788)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 23:32 UTC 73af712275f7969774012f8404cea722c8988839
1 file changed +19 -15
apps/web/src/components/diffs/useReviewFilePatches.ts
+19 -15
@@ -42,8 +42,8 @@ export function useReviewFilePatches({
42 baseRef,
43 ignoreWhitespace,
44 ]);
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({
47 environmentId,
48 cwd,
49 source,
@@ -52,6 +52,7 @@ export function useReviewFilePatches({
52 scope,
53 requested,
54 });
55 + const queriedPatchesAtom = patchesAtom(queries, theme);
56 const previousPreview = useRef({ scope, revision, queries: [] as typeof queries });
57 useEffect(() => {
58 const previous = previousPreview.current;
@@ -65,7 +66,13 @@ export function useReviewFilePatches({
66 if (changed || cached) registry.refresh(query);
67 }
68 }, [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);
76 const pendingIndex = files.findIndex((_, index) => {
77 const patch = patches.get(index);
78 return !patch || patch._tag === "Initial";
@@ -86,10 +93,6 @@ export function useReviewFilePatches({
93 const loadNextFiles = () =>
94 requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index));
95 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 - });
96 const renderableFiles = source
97 ? files.map((file, index): FileDiffMetadata => {
98 const result = patches.get(index);
@@ -154,9 +157,8 @@ export function useReviewFilePatches({
157 };
158 }
159
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({
162 environmentId,
163 cwd,
164 source,
@@ -173,9 +175,8 @@ function useReviewFileQueries({
175 scope: string;
176 requested: { scope: string; indices: number[] };
177 }) {
176 - "use memo";
178 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);
180 const queries =
181 !environmentId || !cwd || !source
182 ? []
@@ -203,17 +204,20 @@ function useReviewFileQueries({
204 }),
205 };
206 });
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)) ?? [];
212 }
213
209 -function usePatchesAtom(
214 +function patchesAtom(
215 queries: ReadonlyArray<{
216 index: number;
217 query: ReturnType<typeof reviewEnvironment.diffFilePatch>;
218 }>,
219 theme: "light" | "dark",
220 ) {
216 - "use memo";
221 // Derived atoms parse each query result once, even when another file finishes loading.
222 const parsedQuery = Atom.family((query: ReturnType<typeof reviewEnvironment.diffFilePatch>) =>
223 Atom.map(query, (result) =>