Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(issues): stabilize scroll restoration (#5398)

Jiayuan Zhang9e4c73f8f4bb
diff --git a/packages/views/issues/hooks/use-issue-detail-scroll-restore.test.tsx b/packages/views/issues/hooks/use-issue-detail-scroll-restore.test.tsx
index 954a1795fb..7b7708316d 100644
--- a/packages/views/issues/hooks/use-issue-detail-scroll-restore.test.tsx
+++ b/packages/views/issues/hooks/use-issue-detail-scroll-restore.test.tsx
@@ -97,6 +97,22 @@ describe("useIssueDetailScrollRestore", () => {
expect(scroller.scrollTop).toBe(500);
});
+ it("keeps positions when navigating through a page that unmounts issue detail", () => {
+ const issueA = nextKey("issue-a");
+ const issueB = nextKey("issue-b");
+
+ const firstPage = render(<Harness restoreKey={issueA} />);
+ setScroll(firstPage.getByTestId("scroller"), 610);
+ firstPage.unmount();
+
+ const secondPage = render(<Harness restoreKey={issueB} />);
+ expect(secondPage.getByTestId("scroller").scrollTop).toBe(0);
+ secondPage.unmount();
+
+ const returnedPage = render(<Harness restoreKey={issueA} />);
+ expect(returnedPage.getByTestId("scroller").scrollTop).toBe(610);
+ });
+
it("waits until the page is ready before restoring a saved scrollTop", () => {
const issueA = nextKey("issue-a");
const issueB = nextKey("issue-b");
@@ -191,6 +207,29 @@ describe("useIssueDetailScrollRestore", () => {
expect(scroller.scrollTop).toBe(480);
});
+ it("restores again when a virtualized timeline resets scroll after the initial write", () => {
+ const issueA = nextKey("issue-a");
+ const issueB = nextKey("issue-b");
+
+ const { getByTestId, rerender } = render(<Harness restoreKey={issueA} />);
+ const scroller = getByTestId("scroller") as HTMLElement;
+
+ setScroll(scroller, 520);
+
+ rerender(<Harness restoreKey={issueB} />);
+ expect(scroller.scrollTop).toBe(0);
+
+ rerender(<Harness restoreKey={issueA} />);
+ expect(scroller.scrollTop).toBe(520);
+
+ // Virtuoso initializes after the parent's layout effect and can reset a
+ // successful synchronous restore before the next animation frame.
+ scroller.scrollTop = 0;
+ flushNextAnimationFrame();
+
+ expect(scroller.scrollTop).toBe(520);
+ });
+
it("cancels a pending restore retry when the issue key changes", () => {
const issueA = nextKey("issue-a");
const issueB = nextKey("issue-b");
diff --git a/packages/views/issues/hooks/use-issue-detail-scroll-restore.ts b/packages/views/issues/hooks/use-issue-detail-scroll-restore.ts
index c4bb209887..e47a214912 100644
--- a/packages/views/issues/hooks/use-issue-detail-scroll-restore.ts
+++ b/packages/views/issues/hooks/use-issue-detail-scroll-restore.ts
@@ -48,6 +48,11 @@ export function useIssueDetailScrollRestore({
restoredKeyRef.current = restoreKey;
const target = scrollPositions.get(restoreKey) ?? 0;
+ if (target <= 1) {
+ scrollContainerEl.scrollTop = target;
+ return;
+ }
+
return restoreScrollTopWithRetry(scrollContainerEl, target);
}, [scrollContainerEl, restoreKey, ready, disabled]);
}
@@ -66,20 +71,30 @@ function saveScrollPosition(restoreKey: string, scrollTop: number) {
function restoreScrollTopWithRetry(el: HTMLElement, target: number) {
let cancelled = false;
let attempts = 0;
+ let stableFrames = 0;
const maxAttempts = 30;
+ const requiredStableFrames = 2;
el.scrollTop = target;
- if (Math.abs(el.scrollTop - target) <= 1) return () => {};
let frameId: number;
const tick = () => {
if (cancelled || !el.isConnected) return;
- el.scrollTop = target;
attempts += 1;
- if (Math.abs(el.scrollTop - target) <= 1 || attempts >= maxAttempts) {
+ if (Math.abs(el.scrollTop - target) <= 1) {
+ stableFrames += 1;
+ } else {
+ stableFrames = 0;
+ el.scrollTop = target;
+ }
+
+ // A virtualized timeline initializes after the parent's layout effect and
+ // may reset a synchronous scroll write. Requiring stability across frames
+ // keeps the restore alive long enough to outlast that initialization.
+ if (stableFrames >= requiredStableFrames || attempts >= maxAttempts) {
return;
}