Skip to content

Commit 815842c

Browse files
rodrigoB-CBclaude
andcommitted
fix(web): the pull request Code tab keeps its diff after a refresh
A refresh emptied the accumulated diff slices and refetched the first page, but slices were only refilled when the query's data changed identity. An equal refetch keeps the previous value's identity, so the tab showed "no file changes" until the diff's content changed. Read whether a page is already applied from the slices instead of a separate identity gate. Co-Authored-By: Claude Opus 5.5 <[email protected]> (cherry picked from commit 68b4e5a599e3561ddd5a23568bb5183a38fb342c)
1 parent 5307c23 commit 815842c

2 files changed

Lines changed: 182 additions & 40 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import {
2+
EnvironmentId,
3+
ProjectId,
4+
SourceControlProviderKind,
5+
type PullRequestDetailView,
6+
type PullRequestDiffResult,
7+
} from "@t3tools/contracts";
8+
import { act } from "react";
9+
import { createRoot, type Root } from "react-dom/client";
10+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
11+
import { page } from "vite-plus/test/browser";
12+
13+
import "~/index.css";
14+
15+
const { diffAtom } = await vi.hoisted(async () => {
16+
const { AsyncResult, Atom } = await import("effect/reactivity");
17+
const firstPage: PullRequestDiffResult = {
18+
patch: [
19+
"diff --git a/a.ts b/a.ts",
20+
"index 1111111..2222222 100644",
21+
"--- a/a.ts",
22+
"+++ b/a.ts",
23+
"@@ -1 +1 @@",
24+
"-before()",
25+
"+after()",
26+
"",
27+
].join("\n"),
28+
truncated: false,
29+
nextCursor: null,
30+
};
31+
// An environment query whose refetch returns equal content hands back the previous value
32+
// itself, so a refresh publishes a new result around the very same page.
33+
return { diffAtom: Atom.make(() => AsyncResult.success(firstPage)) };
34+
});
35+
36+
vi.mock(import("~/state/use-atom-command"), async (original) => ({
37+
...(await original()),
38+
useAtomCommand: (() => vi.fn()) as never,
39+
}));
40+
vi.mock(import("~/state/pullRequests"), async (original) => {
41+
const actual = await original();
42+
return {
43+
...actual,
44+
pullRequestEnvironment: {
45+
...actual.pullRequestEnvironment,
46+
diff: (() => diffAtom) as never,
47+
},
48+
};
49+
});
50+
51+
import PullRequestCodeTab from "./PullRequestCodeTab";
52+
53+
const detail: PullRequestDetailView = {
54+
provider: SourceControlProviderKind.make("github"),
55+
projectId: ProjectId.make("project"),
56+
projectTitle: "Project",
57+
workspaceRoot: "/workspace",
58+
repository: "owner/repo",
59+
number: 1,
60+
title: "Test pull request",
61+
body: "",
62+
url: "https://github.com/owner/repo/pull/1",
63+
author: { login: "author", name: null, avatarUrl: null },
64+
viewer: "author",
65+
state: "open",
66+
isDraft: false,
67+
mergeability: "mergeable",
68+
additions: 1,
69+
deletions: 1,
70+
changedFiles: 1,
71+
headBranch: "feature",
72+
baseBranch: "main",
73+
createdAt: "2026-09-01T00:00:00Z",
74+
updatedAt: "2026-09-01T00:00:00Z",
75+
mergedAt: null,
76+
closedAt: null,
77+
reviewers: [],
78+
labels: [],
79+
checks: [],
80+
comments: [],
81+
commentCount: 0,
82+
commentsTruncated: false,
83+
reviewThreads: [],
84+
commits: [],
85+
mergeCapabilities: { merge: false, squash: false, rebase: false },
86+
capabilities: {
87+
diff: true,
88+
comment: false,
89+
search: true,
90+
actions: [],
91+
mergeMethods: [],
92+
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
93+
reviewers: { request: false, listCandidates: false },
94+
edit: { changeRequest: false, comment: false },
95+
},
96+
viewerPermissions: {
97+
actions: [],
98+
comment: false,
99+
resolve: false,
100+
verdicts: [],
101+
requestReviewers: false,
102+
},
103+
};
104+
105+
let root: Root;
106+
let container: HTMLDivElement;
107+
beforeEach(() => {
108+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
109+
container = document.createElement("div");
110+
container.className = "flex h-[700px] w-[900px]";
111+
document.body.append(container);
112+
root = createRoot(container);
113+
});
114+
afterEach(async () => {
115+
await act(async () => root.unmount());
116+
container.remove();
117+
localStorage.clear();
118+
vi.unstubAllGlobals();
119+
});
120+
121+
const render = (refreshToken: number) =>
122+
act(async () =>
123+
root.render(
124+
<PullRequestCodeTab
125+
scrollerRef={() => {}}
126+
environmentId={EnvironmentId.make("environment")}
127+
reference={detail}
128+
detail={detail}
129+
selectedCommitOid={null}
130+
onSelectedCommitChange={() => {}}
131+
onRefresh={() => {}}
132+
refreshToken={refreshToken}
133+
/>,
134+
),
135+
);
136+
137+
it("keeps the diff after a refresh that brings back the same page", async () => {
138+
await render(0);
139+
await expect.element(page.getByText("1 file", { exact: true })).toBeVisible();
140+
141+
await render(1);
142+
143+
await expect
144+
.element(page.getByText("This pull request has no file changes."))
145+
.not.toBeInTheDocument();
146+
await expect.element(page.getByText("1 file", { exact: true })).toBeVisible();
147+
});

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ const REPLACE_FILE_COUNTS_CSS = `
145145

146146
const NO_SLICES: ReadonlyArray<DiffSlice> = [];
147147

148+
function sameSlice(slice: DiffSlice, page: DiffSlice) {
149+
return (
150+
slice.cursor === page.cursor &&
151+
slice.patch === page.patch &&
152+
slice.truncated === page.truncated &&
153+
slice.nextCursor === page.nextCursor &&
154+
slice.omittedFileStats.length === page.omittedFileStats.length &&
155+
slice.omittedFileStats.every((file, index) => {
156+
const refreshed = page.omittedFileStats[index];
157+
return (
158+
refreshed !== undefined &&
159+
refreshed.path === file.path &&
160+
refreshed.additions === file.additions &&
161+
refreshed.deletions === file.deletions
162+
);
163+
})
164+
);
165+
}
166+
148167
// Each slice is parsed once per cache key and the parse lives as long as the slice does, so a
149168
// slice arriving costs one parse rather than one per slice already on screen.
150169
const sliceParses = new WeakMap<DiffSlice, Map<string, RenderablePatch | null>>();
@@ -336,55 +355,31 @@ function PullRequestCodeTab({
336355
}),
337356
);
338357
// Pages accumulate into slices as each answer arrives, so the slice list is adjusted while
339-
// rendering the answer rather than one commit later.
340-
const [appliedPage, setAppliedPage] = useState<{
341-
readonly data: typeof diffQuery.data;
342-
readonly cursor: string | null;
343-
readonly scopeKey: string;
344-
} | null>(null);
358+
// rendering the answer rather than one commit later. Whether an answer is in is read from the
359+
// slices: an equal refetch keeps the answer's identity, so it cannot signal a reset's refill.
345360
const data = diffQuery.data;
346-
if (
347-
data !== null &&
348-
(appliedPage?.data !== data ||
349-
appliedPage.cursor !== cursor ||
350-
appliedPage.scopeKey !== scopeKey)
351-
) {
352-
setAppliedPage({ data, cursor, scopeKey });
361+
const page: DiffSlice | null =
362+
data === null
363+
? null
364+
: {
365+
cursor,
366+
patch: data.patch,
367+
truncated: data.truncated,
368+
nextCursor: data.nextCursor,
369+
omittedFileStats: data.omittedFileStats ?? [],
370+
};
371+
if (page !== null && !loadedSlices.some((slice) => sameSlice(slice, page))) {
353372
setSliceState((previous) => {
354373
const slices = previous.key === scopeKey ? previous.slices : NO_SLICES;
355-
const next = {
356-
cursor,
357-
patch: data.patch,
358-
truncated: data.truncated,
359-
nextCursor: data.nextCursor,
360-
omittedFileStats: data.omittedFileStats ?? [],
361-
};
362374
const index = slices.findIndex((slice) => slice.cursor === cursor);
363375
if (index === -1) {
364-
return { key: scopeKey, cursor, slices: [...slices, next] };
376+
return { key: scopeKey, cursor, slices: [...slices, page] };
365377
}
366378
const existing = slices[index];
367-
if (
368-
existing !== undefined &&
369-
existing.patch === next.patch &&
370-
existing.truncated === next.truncated &&
371-
existing.nextCursor === next.nextCursor &&
372-
existing.omittedFileStats.length === next.omittedFileStats.length &&
373-
existing.omittedFileStats.every((file, index) => {
374-
const refreshed = next.omittedFileStats[index];
375-
return (
376-
refreshed !== undefined &&
377-
refreshed.path === file.path &&
378-
refreshed.additions === file.additions &&
379-
refreshed.deletions === file.deletions
380-
);
381-
})
382-
) {
383-
return previous;
384-
}
379+
if (existing !== undefined && sameSlice(existing, page)) return previous;
385380
// A page that came back different means the diff moved under the review. The slices
386381
// after it go with the replacement: their cursors were positions in the old diff.
387-
return { key: scopeKey, cursor, slices: [...slices.slice(0, index), next] };
382+
return { key: scopeKey, cursor, slices: [...slices.slice(0, index), page] };
388383
});
389384
}
390385
const refreshFirstDiffPage = useAtomRefresh(

0 commit comments

Comments
 (0)