fix(web): restore diff scroll position when switching threads #11631
ClosedGigioxx(GitHub profile, opens in a new tab)wants to merge 11 commits into
main fromrefs/pull/11631/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/web/src/components/DiffPanel.tsx line 481OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -455,0 +461,21 @@461+ const rememberDiffScrollTop = useCallback(462+ (scrollTop: number) => {463+ if (collapseScopeKey) diffScrollTopByScopeKey.set(collapseScopeKey, scrollTop);464+ },465+ [collapseScopeKey],466+ );467+468+ useEffect(() => {469+ const scrollTop = collapseScopeKey ? diffScrollTopByScopeKey.get(collapseScopeKey) : undefined;470+ if (!scrollTop || !firstDiffFileKey || !codeView?.getInstance()) return;471+ // Position targets subtract the sticky header, so anchor to the first file instead.472+ codeView.scrollTo({473+ type: "item",474+ id: firstDiffFileKey,475+ align: "start",476+ offset: -scrollTop,477+ behavior: "instant",478+ });479+ }, [codeView, collapseScopeKey, firstDiffFileKey]);480+481+ const handledRevealRef = useRef<string | null>(null);
- apps/web/src/components/DiffPanel.tsx line 481OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -455,0 +461,21 @@461+ const rememberDiffScrollTop = useCallback(462+ (scrollTop: number) => {463+ if (collapseScopeKey) diffScrollTopByScopeKey.set(collapseScopeKey, scrollTop);464+ },465+ [collapseScopeKey],466+ );467+468+ useEffect(() => {469+ const scrollTop = collapseScopeKey ? diffScrollTopByScopeKey.get(collapseScopeKey) : undefined;470+ if (!scrollTop || !firstDiffFileKey || !codeView?.getInstance()) return;471+ // Position targets subtract the sticky header, so anchor to the first file instead.472+ codeView.scrollTo({473+ type: "item",474+ id: firstDiffFileKey,475+ align: "start",476+ offset: -scrollTop,477+ behavior: "instant",478+ });479+ }, [codeView, collapseScopeKey, firstDiffFileKey]);480+481+ const handledRevealRef = useRef<string | null>(null);
juliusmarminge added labelsmacroscope-review
juliusmarminge assignedjuliusmarminge
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/DiffPanel.tsx lines 552 to 553OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -520,3 +542,12 @@520542 ? (codeViewFiles.find((candidate) => candidate.filePath === selectedFilePath)?.fileKey ?? null)521543 : null;522544545+ const scrollScopeKey = JSON.stringify([reviewSectionId, selectedBaseRef]);546+ const viewerKey = JSON.stringify([547+ codeViewMountKey,548+ selectedBaseRef,549+ lazySource ? filePatchScope : "preview",550+ ]);551+ const codeView = codeViewState?.key === viewerKey ? codeViewState.viewer : null;552+ const setCodeView = (viewer: AnnotatableCodeViewHandle | null) =>553+ setCodeViewState(viewer ? { key: viewerKey, viewer } : null);apps/web/src/diffPanelStore.ts line 27OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -6,12 +7,21 @@67 import { resolveStorage } from "./lib/storage";7889 export type DiffPanelSelection =910 | { kind: "branch"; baseRef: string | null }1011 | { kind: "unstaged" }1112 | { kind: "turn"; turnId: RunId; filePath: string | null; revealRequestId: number };121314+export interface DiffPanelViewState {15+ scrollTop: number;16+ scrollAnchor: DiffScrollAnchor | null;17+ loadedFilePaths: ReadonlyArray<string>;18+ // Selection identity survives remounts; a new request is distinct even when its counter resets.19+ handledReveal: DiffPanelSelection | null;20+}21+1322 const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };14231524 interface DiffPanelStoreState {1625 byThreadKey: Record<string, DiffPanelSelection>;1726 branchBaseRefByThreadKey: Record<string, string | null>;27+ viewStateByThreadKey: Record<string, Record<string, DiffPanelViewState>>;
juliusmarminge added labelspr:gate-failed
juliusmarminge added labelsbugfix
- apps/web/src/components/DiffPanel.tsx line 741OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -710,12 +733,9 @@710733711734 // A file another view asked to reveal (a chat diff link): retried as the diff loads until it712735 // holds the file, then not again until a new request, so it does not undo what the user did since.713- const externalRevealRef = useRef<string | null>(null);714736 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reveals an externally requested file in the diff viewer, retrying as the diff DOM loads715737 useEffect(() => {716- if (!selectedFilePath) {717- externalRevealRef.current = null;718- return;719- }720- const key = JSON.stringify([collapseScopeKey, selectedFilePath, selectedFileRevealRequestId]);721- if (externalRevealRef.current === key) return;738+ if (!selectedFilePath) return;739+ const scope = collapseScopeKey ?? reviewSectionId;740+ const key = JSON.stringify([selectedFilePath, selectedFileRevealRequestId]);741+ if (handledDiffReveals.get(scope) === key) return;
- @@ -0,0 +104,21 @@104+ const stop = useStableCallback(() => {105+ const session = sessionRef.current;106+ if (!session) return;107+ session.restoring = false;108+ session.pending = null;109+ });110+111+ const flush = useStableCallback(() => {112+ const viewer = handleRef.current?.getInstance();113+ if (!viewer) return;114+ const session = sessionFor(viewer);115+ const anchor = session.pending;116+ if (!session.restoring || !anchor || !session.scopeKey) return;117+ const saved = diffScrollPositions.get(session.scopeKey);118+ const missing = saved?.loadedFilePaths.filter((path) => pendingFilePaths.has(path)) ?? [];119+ if (missing.length > 0 && !session.requestedFiles) {120+ session.requestedFiles = true;121+ for (const path of missing) requestFile(path);122+ return;123+ }124+ if (missing.length > 0 && patchesPending) return;
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Bugbot is paused — on-demand spend limit reached
Bugbot uses usage-based billing for this team and has hit its on-demand spend limit.
A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue.