The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 19:45 UTC
fix(web): diff tab keeps its scroll position across thread switches (#35090)
Julius Marminge committed
Nov 12, 2026 at 02:39 UTC
f39c4405fabad4fa15b17b150d0e260db71577aa
4 files changed
+185
-4
apps/web/src/components/DiffPanel.tsx
+21
-3
@@ -24,6 +24,7 @@ import {
24
import * as Schema from "effect/Schema";
25
import { type MouseEvent, useEffect, useRef, useState } from "react";
26
import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal";
27
+import { useCodeViewScrollRestore } from "./diffs/useCodeViewScrollRestore";
28
import { useFilesystemReadAccess } from "~/state/filesystem";
29
import { useOpenInPreferredEditor } from "../editorPreferences";
30
import { useFileContextMenuHandler } from "../fileContextMenu";
@@ -67,7 +68,7 @@ import { formatRelativeTimeLabel, formatShortTimestamp } from "../timestampForma
68
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
69
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
70
import { DiffStatLabel } from "./chat/DiffStatLabel";
70
-import { AnnotatableCodeView } from "./diffs/AnnotatableCodeView";
71
+import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
72
import { DiffFileTree } from "./diffs/DiffFileTree";
73
import { FileExplorerPane } from "./files/FileExplorerPane";
74
import { diffFileTreeEntries } from "./diffs/diffFileTree.logic";
@@ -693,12 +694,27 @@ export default function DiffPanel({
694
const diffLineStat = sourceLineStat ?? getDiffLineStat(renderableFiles);
695
const fileTreeEntries = diffFileTreeEntries(renderableFiles.filter(isFileShown));
696
const codeViewReveal = useCodeViewFileReveal();
697
+ // Lazy files that sat above the saved position load again only when asked for.
698
+ const codeViewScroll = useCodeViewScrollRestore(
699
+ collapseScopeKey,
700
+ !isRefreshingDiff && remainingFileCount === 0,
701
+ remainingFileCount > 0 ? loadNextFiles : null,
702
+ );
703
+ const attachCodeView = useStableCallback((handle: AnnotatableCodeViewHandle | null) => {
704
+ codeViewScroll.attach(handle);
705
+ codeViewReveal.attach(handle);
706
+ });
707
+ const flushCodeView = useStableCallback(() => {
708
+ codeViewScroll.flush();
709
+ codeViewReveal.flush();
710
+ });
711
const revealDiffFile = (filePath: string) => {
712
const index = renderableFileEntries.findIndex(
713
(candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath,
714
);
715
const file = renderableFileEntries[index];
716
if (!file) return false;
717
+ codeViewScroll.discard();
718
updateCollapsedDiffFiles((current) => {
719
const next = new Set(current);
720
next.delete(file.fileKey);
@@ -775,6 +791,7 @@ export default function DiffPanel({
791
});
792
793
const toggleDiffFileCollapse = () => {
794
+ codeViewScroll.discard();
795
setCodeViewRevision((current) => current + 1);
796
updateCollapsedDiffFiles((current) => toggleAllDiffFiles(shownFileKeys, current));
797
};
@@ -1431,7 +1448,8 @@ export default function DiffPanel({
1448
key={collapseScopeKey ?? reviewSectionId}
1449
environmentId={activeThread?.environmentId}
1450
projectId={activeThread?.projectId}
1434
- viewerRef={codeViewReveal.attach}
1451
+ viewerRef={attachCodeView}
1452
+ onScroll={codeViewScroll.onScroll}
1453
codeViewKey={codeViewMountKey}
1454
className="h-full min-h-0 overflow-auto"
1455
files={codeViewFiles}
@@ -1493,7 +1511,7 @@ export default function DiffPanel({
1511
preferredHighlighter: PREFERRED_HIGHLIGHTER,
1512
themeType: resolvedTheme as DiffThemeType,
1513
stickyHeaders: true,
1496
- onPostRender: codeViewReveal.flush,
1514
+ onPostRender: flushCodeView,
1515
...(currentLoadDiffFiles ? { loadDiffFiles } : {}),
1516
}}
1517
/>
apps/web/src/components/diffs/AnnotatableCodeView.tsx
+4
-1
@@ -6,7 +6,7 @@ import type {
6
FileDiffMetadata,
7
SelectedLineRange,
8
} from "@pierre/diffs";
9
-import type { CodeViewHandle } from "@pierre/diffs/react";
9
+import type { CodeViewHandle, CodeViewProps } from "@pierre/diffs/react";
10
import type { EnvironmentId, ProjectId, ScopedThreadRef } from "@t3tools/contracts";
11
import { useState, type ReactNode, type Ref } from "react";
12
@@ -88,6 +88,7 @@ interface AnnotatableCodeViewProps {
88
composerDraftTarget: ScopedThreadRef | DraftId;
89
options: StyledDiffCodeViewOptions<DiffCommentAnnotationGroup>;
90
viewerRef?: Ref<AnnotatableCodeViewHandle>;
91
+ onScroll?: CodeViewProps<DiffCommentAnnotationGroup, undefined>["onScroll"];
92
className?: string;
93
renderCodeViewFooter?: () => ReactNode;
94
unsafeCSSExtra?: string;
@@ -116,6 +117,7 @@ export function AnnotatableCodeView({
117
composerDraftTarget,
118
options,
119
viewerRef,
120
+ onScroll,
121
className,
122
renderCodeViewFooter,
123
unsafeCSSExtra,
@@ -260,6 +262,7 @@ export function AnnotatableCodeView({
262
<StyledDiffCodeView<DiffCommentAnnotationGroup>
263
key={codeViewKey}
264
{...(viewerRef ? { viewerRef } : {})}
265
+ {...(onScroll ? { onScroll } : {})}
266
{...(className ? { className } : {})}
267
{...(unsafeCSSExtra ? { unsafeCSSExtra } : {})}
268
{...(renderHeaderMetadata
apps/web/src/components/diffs/useCodeViewScrollRestore.test.ts
new
+81
@@ -0,0 +1,81 @@
1
+import type { CodeViewScrollTarget } from "@pierre/diffs";
2
+import { describe, expect, it } from "vite-plus/test";
3
+
4
+import { createCodeViewScrollRestore } from "./useCodeViewScrollRestore";
5
+
6
+function viewer(scrollHeight: number, height = 500) {
7
+ const instance = { scrollHeight, height, getHeight: () => 0, getScrollHeight: () => 0 };
8
+ instance.getHeight = () => instance.height;
9
+ instance.getScrollHeight = () => instance.scrollHeight;
10
+ const scrolls: CodeViewScrollTarget[] = [];
11
+ return {
12
+ instance,
13
+ scrolls,
14
+ getInstance: () => instance,
15
+ scrollTo: (target: CodeViewScrollTarget) => scrolls.push(target),
16
+ };
17
+}
18
+
19
+function mount(
20
+ scopeKey: string,
21
+ view: ReturnType<typeof viewer>,
22
+ settled = true,
23
+ loadMore: (() => void) | null = null,
24
+) {
25
+ const restore = createCodeViewScrollRestore();
26
+ restore.update(scopeKey, settled, loadMore);
27
+ restore.attach(view);
28
+ return restore;
29
+}
30
+
31
+describe("createCodeViewScrollRestore", () => {
32
+ it("puts a remounted viewer back at its scope's position, not another scope's", () => {
33
+ const first = viewer(10_000);
34
+ mount("thread-a", first).onScroll(4000, first.instance);
35
+ const other = viewer(10_000);
36
+ mount("thread-b", other).onScroll(1200, other.instance);
37
+
38
+ const again = viewer(10_000);
39
+ mount("thread-a", again);
40
+ // Pierre subtracts the 32px sticky header from an unclamped position target.
41
+ expect(again.scrolls).toEqual([{ type: "position", position: 4032 }]);
42
+ });
43
+
44
+ it("loads more files until the position is reachable, then gives up once none remain", () => {
45
+ const first = viewer(10_000);
46
+ mount("thread-c", first).onScroll(4000, first.instance);
47
+
48
+ const loading = viewer(700);
49
+ let batches = 0;
50
+ const restore = mount("thread-c", loading, false, () => {
51
+ batches += 1;
52
+ });
53
+ restore.onScroll(0, loading.instance);
54
+ restore.flush();
55
+ expect(loading.scrolls).toEqual([]);
56
+ expect(batches).toBe(2);
57
+ loading.instance.scrollHeight = 10_000;
58
+ restore.flush();
59
+ restore.flush();
60
+ expect(loading.scrolls).toEqual([{ type: "position", position: 4032 }]);
61
+
62
+ const shrunk = viewer(2000);
63
+ const settling = mount("thread-c", shrunk, false);
64
+ settling.flush();
65
+ expect(shrunk.scrolls).toEqual([]);
66
+ settling.update("thread-c", true, null);
67
+ settling.flush();
68
+ expect(shrunk.scrolls).toEqual([{ type: "position", position: 1500 }]);
69
+ });
70
+
71
+ it("forgets a position that a reveal or collapsing every file replaced", () => {
72
+ const first = viewer(10_000);
73
+ const restore = mount("thread-d", first);
74
+ restore.onScroll(4000, first.instance);
75
+ restore.discard();
76
+
77
+ const again = viewer(10_000);
78
+ mount("thread-d", again);
79
+ expect(again.scrolls).toEqual([]);
80
+ });
81
+});
apps/web/src/components/diffs/useCodeViewScrollRestore.ts
new
+79
@@ -0,0 +1,79 @@
1
+import type { CodeViewScrollTarget } from "@pierre/diffs";
2
+import { useEffect, useState } from "react";
3
+
4
+interface Viewer {
5
+ getHeight(): number;
6
+ getScrollHeight(): number;
7
+}
8
+
9
+interface ScrollRestoreHandle {
10
+ getInstance(): Viewer | undefined;
11
+ scrollTo(target: CodeViewScrollTarget): void;
12
+}
13
+
14
+// Session-only, like the panel's folds.
15
+const scrollTopByScopeKey = new Map<string, number>();
16
+
17
+/**
18
+ * Puts a remounted code view back where its scope was last scrolled. `attach` is the viewer's
19
+ * ref and `flush` its post-render handler; the position waits until the content can reach it,
20
+ * loading more files while any remain, and clamps once nothing is left to load.
21
+ */
22
+export function createCodeViewScrollRestore() {
23
+ let scopeKey: string | null = null;
24
+ let settled = true;
25
+ let loadMore: (() => void) | null = null;
26
+ let viewer: Viewer | undefined;
27
+ let handle: ScrollRestoreHandle | null = null;
28
+ let pending: number | null = null;
29
+ const flush = () => {
30
+ if (pending === null || !viewer || !handle || viewer.getHeight() === 0) return;
31
+ const maxScrollTop = viewer.getScrollHeight() - viewer.getHeight();
32
+ if (maxScrollTop < pending && !settled) {
33
+ loadMore?.();
34
+ return;
35
+ }
36
+ // Pierre lands an unclamped position target one sticky header (32px) above it.
37
+ const position = pending + 32 <= maxScrollTop ? pending + 32 : maxScrollTop;
38
+ pending = null;
39
+ if (position > 0) handle.scrollTo({ type: "position", position });
40
+ };
41
+ return {
42
+ update(nextScopeKey: string | null, nextSettled: boolean, nextLoadMore: (() => void) | null) {
43
+ scopeKey = nextScopeKey;
44
+ settled = nextSettled;
45
+ loadMore = nextLoadMore;
46
+ },
47
+ attach(next: ScrollRestoreHandle | null) {
48
+ handle = next;
49
+ const instance = next?.getInstance();
50
+ if (instance && instance !== viewer) {
51
+ viewer = instance;
52
+ pending = scopeKey === null ? null : (scrollTopByScopeKey.get(scopeKey) ?? null);
53
+ }
54
+ flush();
55
+ },
56
+ onScroll(scrollTop: number, source: object) {
57
+ if (source !== viewer || pending !== null || scopeKey === null) return;
58
+ scrollTopByScopeKey.set(scopeKey, scrollTop);
59
+ },
60
+ /** Something else positions the viewer, such as a file reveal or collapsing every file. */
61
+ discard() {
62
+ pending = null;
63
+ if (scopeKey !== null) scrollTopByScopeKey.delete(scopeKey);
64
+ },
65
+ flush,
66
+ };
67
+}
68
+
69
+export function useCodeViewScrollRestore(
70
+ scopeKey: string | null,
71
+ settled: boolean,
72
+ loadMore: (() => void) | null,
73
+) {
74
+ const [restore] = useState(createCodeViewScrollRestore);
75
+ restore.update(scopeKey, settled, loadMore);
76
+ // 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
77
+ useEffect(() => restore.flush());
78
+ return restore;
79
+}