Repository navigation
fix(web): diff tab keeps its scroll position across thread switches #35090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+185
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import type { CodeViewScrollTarget } from "@pierre/diffs"; | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { createCodeViewScrollRestore } from "./useCodeViewScrollRestore"; | ||
|
|
||
| function viewer(scrollHeight: number, height = 500) { | ||
| const instance = { scrollHeight, height, getHeight: () => 0, getScrollHeight: () => 0 }; | ||
| instance.getHeight = () => instance.height; | ||
| instance.getScrollHeight = () => instance.scrollHeight; | ||
| const scrolls: CodeViewScrollTarget[] = []; | ||
| return { | ||
| instance, | ||
| scrolls, | ||
| getInstance: () => instance, | ||
| scrollTo: (target: CodeViewScrollTarget) => scrolls.push(target), | ||
| }; | ||
| } | ||
|
|
||
| function mount( | ||
| scopeKey: string, | ||
| view: ReturnType<typeof viewer>, | ||
| settled = true, | ||
| loadMore: (() => void) | null = null, | ||
| ) { | ||
| const restore = createCodeViewScrollRestore(); | ||
| restore.update(scopeKey, settled, loadMore); | ||
| restore.attach(view); | ||
| return restore; | ||
| } | ||
|
|
||
| describe("createCodeViewScrollRestore", () => { | ||
| it("puts a remounted viewer back at its scope's position, not another scope's", () => { | ||
| const first = viewer(10_000); | ||
| mount("thread-a", first).onScroll(4000, first.instance); | ||
| const other = viewer(10_000); | ||
| mount("thread-b", other).onScroll(1200, other.instance); | ||
|
|
||
| const again = viewer(10_000); | ||
| mount("thread-a", again); | ||
| // Pierre subtracts the 32px sticky header from an unclamped position target. | ||
| expect(again.scrolls).toEqual([{ type: "position", position: 4032 }]); | ||
| }); | ||
|
|
||
| it("loads more files until the position is reachable, then gives up once none remain", () => { | ||
| const first = viewer(10_000); | ||
| mount("thread-c", first).onScroll(4000, first.instance); | ||
|
|
||
| const loading = viewer(700); | ||
| let batches = 0; | ||
| const restore = mount("thread-c", loading, false, () => { | ||
| batches += 1; | ||
| }); | ||
| restore.onScroll(0, loading.instance); | ||
| restore.flush(); | ||
| expect(loading.scrolls).toEqual([]); | ||
| expect(batches).toBe(2); | ||
| loading.instance.scrollHeight = 10_000; | ||
| restore.flush(); | ||
| restore.flush(); | ||
| expect(loading.scrolls).toEqual([{ type: "position", position: 4032 }]); | ||
|
|
||
| const shrunk = viewer(2000); | ||
| const settling = mount("thread-c", shrunk, false); | ||
| settling.flush(); | ||
| expect(shrunk.scrolls).toEqual([]); | ||
| settling.update("thread-c", true, null); | ||
| settling.flush(); | ||
| expect(shrunk.scrolls).toEqual([{ type: "position", position: 1500 }]); | ||
| }); | ||
|
|
||
| it("forgets a position that a reveal or collapsing every file replaced", () => { | ||
| const first = viewer(10_000); | ||
| const restore = mount("thread-d", first); | ||
| restore.onScroll(4000, first.instance); | ||
| restore.discard(); | ||
|
|
||
| const again = viewer(10_000); | ||
| mount("thread-d", again); | ||
| expect(again.scrolls).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import type { CodeViewScrollTarget } from "@pierre/diffs"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| interface Viewer { | ||
| getHeight(): number; | ||
| getScrollHeight(): number; | ||
| } | ||
|
|
||
| interface ScrollRestoreHandle { | ||
| getInstance(): Viewer | undefined; | ||
| scrollTo(target: CodeViewScrollTarget): void; | ||
| } | ||
|
|
||
| // Session-only, like the panel's folds. | ||
| const scrollTopByScopeKey = new Map<string, number>(); | ||
|
|
||
| /** | ||
| * Puts a remounted code view back where its scope was last scrolled. `attach` is the viewer's | ||
| * ref and `flush` its post-render handler; the position waits until the content can reach it, | ||
| * loading more files while any remain, and clamps once nothing is left to load. | ||
| */ | ||
| export function createCodeViewScrollRestore() { | ||
| let scopeKey: string | null = null; | ||
| let settled = true; | ||
| let loadMore: (() => void) | null = null; | ||
| let viewer: Viewer | undefined; | ||
| let handle: ScrollRestoreHandle | null = null; | ||
| let pending: number | null = null; | ||
| const flush = () => { | ||
| if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return; | ||
| const maxScrollTop = viewer.getScrollHeight() - viewer.getHeight(); | ||
| if (maxScrollTop < pending && !settled) { | ||
| loadMore?.(); | ||
| return; | ||
| } | ||
| // Pierre lands an unclamped position target one sticky header (32px) above it. | ||
| const position = pending + 32 <= maxScrollTop ? pending + 32 : maxScrollTop; | ||
| pending = null; | ||
| if (position > 0) handle.scrollTo({ type: "position", position }); | ||
| }; | ||
| return { | ||
| update(nextScopeKey: string | null, nextSettled: boolean, nextLoadMore: (() => void) | null) { | ||
| scopeKey = nextScopeKey; | ||
| settled = nextSettled; | ||
| loadMore = nextLoadMore; | ||
| }, | ||
| attach(next: ScrollRestoreHandle | null) { | ||
| handle = next; | ||
| const instance = next?.getInstance(); | ||
| if (instance && instance !== viewer) { | ||
| viewer = instance; | ||
| pending = scopeKey === null ? null : (scrollTopByScopeKey.get(scopeKey) ?? null); | ||
| } | ||
| flush(); | ||
| }, | ||
| onScroll(scrollTop: number, source: object) { | ||
| if (source !== viewer || pending !== null || scopeKey === null) return; | ||
| scrollTopByScopeKey.set(scopeKey, scrollTop); | ||
| }, | ||
| /** Something else positions the viewer, such as a file reveal or collapsing every file. */ | ||
| discard() { | ||
| pending = null; | ||
| if (scopeKey !== null) scrollTopByScopeKey.delete(scopeKey); | ||
| }, | ||
| flush, | ||
| }; | ||
| } | ||
|
|
||
| export function useCodeViewScrollRestore( | ||
| scopeKey: string | null, | ||
| settled: boolean, | ||
| loadMore: (() => void) | null, | ||
| ) { | ||
| const [restore] = useState(createCodeViewScrollRestore); | ||
| restore.update(scopeKey, settled, loadMore); | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a loaded batch can leave the viewer's rendered items alone, so no ref or post-render call re-checks the pending position | ||
| useEffect(() => restore.flush()); | ||
| return restore; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not discard restoration for an already consumed reveal request.
Open thread A through a chat diff link, then scroll away from the revealed file. Switch to thread B without a selected file, then return to A.
diffPanelStore.tsretains A'sfilePathandrevealRequestId. However, the effect at Line 732 clearsexternalRevealRefin B. On return, it treats A's unchanged request as new and callsrevealDiffFile. This line deletes A's saved position, and the file reveal replaces the restored position.Track consumed reveal requests per scope, or otherwise distinguish a new request from revisiting a stored selection. Only a new request should cancel restoration.