Skip to content

Commit 5f48aa8

Browse files
fix(web): PR refresh keeps selected lines and leaves the scroll alone
The viewer reports no selection while the selected file is not rendered, for example while a refreshed page drops the later pages; that no longer clears the stored selection. Refresh keeps the loaded pages mounted, so it no longer re-applies the saved offset, which drifted below the reader's position at narrow panel widths. AI-assisted; verified with the tests and tools listed above.
1 parent 55f056b commit 5f48aa8

4 files changed

Lines changed: 49 additions & 43 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,44 @@ it("keeps an unfinished inline comment when the panel unmounts and returns", asy
255255
.toHaveValue("Half a thought");
256256
});
257257

258+
it("keeps selected lines on a file whose page has not loaded again yet", async () => {
259+
setup({ paged: true });
260+
const parsed = getRenderablePatch(
261+
[
262+
"diff --git a/late.test.ts b/late.test.ts",
263+
"index 1111111..2222222 100644",
264+
"--- a/late.test.ts",
265+
"+++ b/late.test.ts",
266+
"@@ -1,6 +1,6 @@",
267+
"-before()",
268+
"+after()",
269+
" one()",
270+
" two()",
271+
" three()",
272+
" four()",
273+
" five()",
274+
"",
275+
].join("\n"),
276+
"selection",
277+
{ compactPartialHunkOffsets: true },
278+
);
279+
if (parsed?.kind !== "files") throw new Error("fixture must parse as files");
280+
const file = parsed.files[0]!;
281+
const selectedLines = {
282+
id: buildFileDiffIdentityKey(file),
283+
contentVersion: buildFileDiffContentVersion(file),
284+
range: { start: 2, end: 3, side: "additions" as const },
285+
};
286+
usePullRequestReviewStore
287+
.getState()
288+
.updateCodeView("code-view", (current) => ({ ...current, selectedLines }));
289+
await using screen = await render(tab(0), { container: pagedContainer() });
290+
await expect.element(screen.getByText("early-0.ts", { exact: true }).first()).toBeVisible();
291+
expect(usePullRequestReviewStore.getState().codeViews["code-view"]?.selectedLines).toEqual(
292+
selectedLines,
293+
);
294+
});
295+
258296
it("pages forward to a file asked for that is not loaded yet, then scrolls to it", async () => {
259297
setup({ paged: true });
260298
__setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true });

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

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,10 @@ function PullRequestCodeTab({
582582
},
583583
);
584584
const appliedRefreshToken = useRef(refreshToken);
585-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new refresh token clears loaded slices and refetches the first diff page and viewed files
585+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a new refresh token revalidates every loaded diff page and the viewed files
586586
useEffect(() => {
587587
if (appliedRefreshToken.current === refreshToken) return;
588588
appliedRefreshToken.current = refreshToken;
589-
const saved = readPullRequestCodeView(codeViewKey);
590-
setScrollRestoration(
591-
scrollRestoration.restart(
592-
viewer?.getInstance()?.getScrollTop() ?? saved.scrollTop,
593-
Math.max(loadedSlices.length, saved.loadedPageCount),
594-
),
595-
);
596589
// The loaded pages stay mounted while each is read again from the first.
597590
refreshPullRequestDiffPages(loadedSlices, (pageCursor) => {
598591
registry.refresh(
@@ -610,18 +603,7 @@ function PullRequestCodeTab({
610603
setAppliedPage(null);
611604
setSliceState((previous) => ({ ...previous, cursor: null }));
612605
refreshFilesViewed();
613-
}, [
614-
refreshToken,
615-
scrollRestoration,
616-
refreshFilesViewed,
617-
codeViewKey,
618-
viewer,
619-
loadedSlices,
620-
registry,
621-
environmentId,
622-
reference,
623-
commit,
624-
]);
606+
}, [refreshToken, refreshFilesViewed, loadedSlices, registry, environmentId, reference, commit]);
625607
const appliedBackgroundRefreshToken = useRef(backgroundRefreshToken);
626608
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a background refresh token refetches the first diff page and viewed files while keeping slices
627609
useEffect(() => {
@@ -701,6 +683,15 @@ function PullRequestCodeTab({
701683
? savedDraft
702684
: null;
703685
const changeSelectedLines = (selection: { id: string; range: SelectedLineRange } | null) => {
686+
// The viewer reports no selection while the selected file is not rendered, such as when a
687+
// refreshed page drops the later pages; that is not the reader clearing it.
688+
if (
689+
selection === null &&
690+
savedSelectedLines !== null &&
691+
!files.some((candidate) => buildFileDiffIdentityKey(candidate) === savedSelectedLines.id)
692+
) {
693+
return;
694+
}
704695
const file =
705696
selection === null
706697
? undefined

‎apps/web/src/components/pullRequest/pullRequestCodeScroll.logic.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ export class PullRequestCodeScrollRestoration {
1717
this.pending = false;
1818
}
1919

20-
restart(scrollTop: number, loadedPageCount: number) {
21-
return new PullRequestCodeScrollRestoration({
22-
scrollTop: this.pending ? this.saved.scrollTop : scrollTop,
23-
loadedPageCount: Math.max(this.pending ? this.saved.loadedPageCount : 0, loadedPageCount),
24-
});
25-
}
26-
2720
restore(
2821
surface: ScrollSurface | undefined,
2922
loadedPageCount: number,

‎apps/web/src/components/pullRequest/pullRequestCodeViewState.test.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,6 @@ describe("pull request scroll restoration", () => {
135135
expect(surface.scrollTop).toBe(1900);
136136
});
137137

138-
it("keeps a pending target when refresh arrives before its saved pages", () => {
139-
const initial = new PullRequestCodeScrollRestoration({ scrollTop: 1800, loadedPageCount: 3 });
140-
const surface = { scrollTop: 0, scrollHeight: 600, clientHeight: 400 };
141-
expect(initial.restore(surface, 1, true)).toBe("load-more");
142-
const refreshed = initial.restart(surface.scrollTop, 1);
143-
expect(refreshed.restore(surface, 1, true)).toBe("load-more");
144-
surface.scrollHeight = 3000;
145-
expect(refreshed.restore(surface, 3, false)).toBe("restored");
146-
expect(surface.scrollTop).toBe(1800);
147-
surface.scrollTop = 2000;
148-
const later = refreshed.restart(surface.scrollTop, 3);
149-
surface.scrollTop = 0;
150-
expect(later.restore(surface, 3, false)).toBe("restored");
151-
expect(surface.scrollTop).toBe(2000);
152-
});
153-
154138
it.each(["pointerdown", "mousedown", "keydown", "wheel"])(
155139
"keeps the user's position when %s interrupts restoration",
156140
(eventType) => {

0 commit comments

Comments
 (0)