From a2919d6d4fe5ef43aaa44af4ae74ff2e655c4f57 Mon Sep 17 00:00:00 2001 From: Yuruzuu Date: Tue, 27 Oct 2026 17:57:34 +0800 Subject: [PATCH] perf(web,mobile): Changes tab no longer redraws every file when one changes The lazy file-patch scope included the whole diff's hash, so editing any file reset the requested files, re-created every patch query, and remounted the code view. Patch freshness already comes from the preview revision refresh, so drop the hash from the scope. Co-Authored-By: Claude Opus 5.5 --- .../src/features/review/useReviewDiffData.ts | 8 +-- .../diffs/useReviewFilePatches.test.ts | 51 +++++++++++++++++++ .../components/diffs/useReviewFilePatches.ts | 24 +++++---- 3 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 apps/web/src/components/diffs/useReviewFilePatches.test.ts diff --git a/apps/mobile/src/features/review/useReviewDiffData.ts b/apps/mobile/src/features/review/useReviewDiffData.ts index 21cec36eca523..b75f34321c7e0 100644 --- a/apps/mobile/src/features/review/useReviewDiffData.ts +++ b/apps/mobile/src/features/review/useReviewDiffData.ts @@ -147,13 +147,7 @@ export function useReviewDiffData(input: { ); const registry = useContext(RegistryContext); const { environmentId, cwd } = input; - const scope = JSON.stringify([ - environmentId, - cwd, - source?.kind, - source?.baseRef, - source?.diffHash, - ]); + const scope = JSON.stringify([environmentId, cwd, lazySource?.kind, source?.baseRef]); const [requested, setRequested] = useState({ scope, indices: [0, 1, 2] }); const indices = useMemo( () => new Set(requested.scope === scope ? requested.indices : [0, 1, 2]), diff --git a/apps/web/src/components/diffs/useReviewFilePatches.test.ts b/apps/web/src/components/diffs/useReviewFilePatches.test.ts new file mode 100644 index 0000000000000..93e25d07fa716 --- /dev/null +++ b/apps/web/src/components/diffs/useReviewFilePatches.test.ts @@ -0,0 +1,51 @@ +import { EnvironmentId, type ReviewDiffPreviewSource } from "@t3tools/contracts"; +import { expect, it } from "vite-plus/test"; + +import { reviewFilePatchScope, reviewFileQueries } from "./useReviewFilePatches"; + +it("retains requested files and their loaded query atoms when another file changes", () => { + const source: ReviewDiffPreviewSource = { + id: "working-tree", + kind: "working-tree", + title: "Working tree", + baseRef: null, + headRef: null, + diff: "", + diffHash: "before", + truncated: true, + files: Array.from({ length: 6 }, (_, index) => ({ + path: `src/file-${index}.ts`, + previousPath: null, + additions: 1, + deletions: 1, + })), + }; + const input = { + environmentId: EnvironmentId.make("local"), + cwd: "/workspace", + source, + baseRef: null, + ignoreWhitespace: false, + previewInput: { cwd: "/workspace", ignoreWhitespace: false }, + }; + const scope = reviewFilePatchScope(input); + const requested = { scope, indices: [0, 1, 2, 3, 4, 5] }; + const before = reviewFileQueries({ ...input, scope, requested }); + const updated = { ...input, source: { ...source, diffHash: "after" } }; + const after = reviewFileQueries({ + ...updated, + scope: reviewFilePatchScope(updated), + requested, + }); + + expect + .soft(after.map(({ target }) => target.input.request.file.path)) + .toEqual(before.map(({ target }) => target.input.request.file.path)); + expect + .soft( + before.map( + ({ index, query }) => after.find((entry) => entry.index === index)?.query === query, + ), + ) + .toEqual(before.map(() => true)); +}); diff --git a/apps/web/src/components/diffs/useReviewFilePatches.ts b/apps/web/src/components/diffs/useReviewFilePatches.ts index b683e13a8f6ee..1331bcdc099f1 100644 --- a/apps/web/src/components/diffs/useReviewFilePatches.ts +++ b/apps/web/src/components/diffs/useReviewFilePatches.ts @@ -39,14 +39,7 @@ export function useReviewFilePatches({ preview: RenderablePatch | null; }) { const registry = useContext(RegistryContext); - const scope = JSON.stringify([ - environmentId, - cwd, - source?.kind, - source?.diffHash, - baseRef, - ignoreWhitespace, - ]); + const scope = reviewFilePatchScope({ environmentId, cwd, source, baseRef, ignoreWhitespace }); const [requested, setRequested] = useState(() => ({ scope, indices: DEFAULT_INDICES })); const queries = reviewFileQueries({ environmentId, @@ -157,7 +150,20 @@ export function useReviewFilePatches({ }; } -function reviewFileQueries({ +export function reviewFilePatchScope({ + environmentId, + cwd, + source, + baseRef, + ignoreWhitespace, +}: Pick< + Parameters[0], + "environmentId" | "cwd" | "source" | "baseRef" | "ignoreWhitespace" +>) { + return JSON.stringify([environmentId, cwd, source?.kind, baseRef, ignoreWhitespace]); +} + +export function reviewFileQueries({ environmentId, cwd, source,