Skip to content

Commit ee157c1

Browse files
t3dotggclaude
andcommitted
fix(web): reset the applied page on refresh instead of rewriting the slice check
Keep the existing appliedPage gate and forget it when a refresh empties the slices, so an equal refetch refills them. Move the browser test to the disposable render helper. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 49dfd01 commit ee157c1

2 files changed

Lines changed: 64 additions & 75 deletions

File tree

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

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {
55
type PullRequestDetailView,
66
type PullRequestDiffResult,
77
} 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";
8+
import { expect, it, vi } from "vite-plus/test";
129

1310
import "~/index.css";
1411

@@ -48,6 +45,7 @@ vi.mock(import("~/state/pullRequests"), async (original) => {
4845
};
4946
});
5047

48+
import { render } from "../../../test/render";
5149
import PullRequestCodeTab from "./PullRequestCodeTab";
5250

5351
const detail: PullRequestDetailView = {
@@ -102,46 +100,29 @@ const detail: PullRequestDetailView = {
102100
},
103101
};
104102

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-
);
103+
const tab = (refreshToken: number) => (
104+
<PullRequestCodeTab
105+
scrollerRef={() => {}}
106+
environmentId={EnvironmentId.make("environment")}
107+
reference={detail}
108+
detail={detail}
109+
selectedCommitOid={null}
110+
onSelectedCommitChange={() => {}}
111+
onRefresh={() => {}}
112+
refreshToken={refreshToken}
113+
/>
114+
);
136115

137116
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();
117+
const container = document.createElement("div");
118+
container.className = "flex h-[700px] w-[900px]";
119+
await using screen = await render(tab(0), { container });
120+
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
140121

141-
await render(1);
122+
await screen.rerender(tab(1));
142123

143124
await expect
144-
.element(page.getByText("This pull request has no file changes."))
125+
.element(screen.getByText("This pull request has no file changes."))
145126
.not.toBeInTheDocument();
146-
await expect.element(page.getByText("1 file", { exact: true })).toBeVisible();
127+
await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible();
147128
});

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

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,6 @@ 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-
167148
// Each slice is parsed once per cache key and the parse lives as long as the slice does, so a
168149
// slice arriving costs one parse rather than one per slice already on screen.
169150
const sliceParses = new WeakMap<DiffSlice, Map<string, RenderablePatch | null>>();
@@ -357,31 +338,55 @@ function PullRequestCodeTab({
357338
}),
358339
);
359340
// Pages accumulate into slices as each answer arrives, so the slice list is adjusted while
360-
// rendering the answer rather than one commit later. Whether an answer is in is read from the
361-
// slices: an equal refetch keeps the answer's identity, so it cannot signal a reset's refill.
341+
// rendering the answer rather than one commit later.
342+
const [appliedPage, setAppliedPage] = useState<{
343+
readonly data: typeof diffQuery.data;
344+
readonly cursor: string | null;
345+
readonly scopeKey: string;
346+
} | null>(null);
362347
const data = diffQuery.data;
363-
const page: DiffSlice | null =
364-
data === null
365-
? null
366-
: {
367-
cursor,
368-
patch: data.patch,
369-
truncated: data.truncated,
370-
nextCursor: data.nextCursor,
371-
omittedFileStats: data.omittedFileStats ?? [],
372-
};
373-
if (page !== null && !loadedSlices.some((slice) => sameSlice(slice, page))) {
348+
if (
349+
data !== null &&
350+
(appliedPage?.data !== data ||
351+
appliedPage.cursor !== cursor ||
352+
appliedPage.scopeKey !== scopeKey)
353+
) {
354+
setAppliedPage({ data, cursor, scopeKey });
374355
setSliceState((previous) => {
375356
const slices = previous.key === scopeKey ? previous.slices : NO_SLICES;
357+
const next = {
358+
cursor,
359+
patch: data.patch,
360+
truncated: data.truncated,
361+
nextCursor: data.nextCursor,
362+
omittedFileStats: data.omittedFileStats ?? [],
363+
};
376364
const index = slices.findIndex((slice) => slice.cursor === cursor);
377365
if (index === -1) {
378-
return { key: scopeKey, cursor, slices: [...slices, page] };
366+
return { key: scopeKey, cursor, slices: [...slices, next] };
379367
}
380368
const existing = slices[index];
381-
if (existing !== undefined && sameSlice(existing, page)) return previous;
369+
if (
370+
existing !== undefined &&
371+
existing.patch === next.patch &&
372+
existing.truncated === next.truncated &&
373+
existing.nextCursor === next.nextCursor &&
374+
existing.omittedFileStats.length === next.omittedFileStats.length &&
375+
existing.omittedFileStats.every((file, index) => {
376+
const refreshed = next.omittedFileStats[index];
377+
return (
378+
refreshed !== undefined &&
379+
refreshed.path === file.path &&
380+
refreshed.additions === file.additions &&
381+
refreshed.deletions === file.deletions
382+
);
383+
})
384+
) {
385+
return previous;
386+
}
382387
// A page that came back different means the diff moved under the review. The slices
383388
// after it go with the replacement: their cursors were positions in the old diff.
384-
return { key: scopeKey, cursor, slices: [...slices.slice(0, index), page] };
389+
return { key: scopeKey, cursor, slices: [...slices.slice(0, index), next] };
385390
});
386391
}
387392
const refreshFirstDiffPage = useAtomRefresh(
@@ -426,6 +431,9 @@ function PullRequestCodeTab({
426431
if (appliedRefreshToken.current === refreshToken) return;
427432
appliedRefreshToken.current = refreshToken;
428433
setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES });
434+
// An equal refetch keeps the answer's identity, so the emptied slices refill only if the
435+
// applied page is forgotten too.
436+
setAppliedPage(null);
429437
refreshFirstDiffPage();
430438
refreshFilesViewed();
431439
}, [refreshToken, scopeKey, refreshFirstDiffPage, refreshFilesViewed]);

0 commit comments

Comments
 (0)