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, 11:00 UTC
fix(web): file tabs keep their scroll position when you switch back (#37361)
Julius Marminge committed
Nov 15, 2026 at 14:54 UTC
109e9dbdfd1329828e1db35da0a910db9bf06a7e
2 files changed
+79
-2
apps/web/src/components/files/FilePreviewPanel.browser.test.tsx
+36
-1
@@ -1,7 +1,7 @@
1
import { DEFAULT_THEMES, preloadHighlighter } from "@pierre/diffs";
2
import { EnvironmentId, ThreadId, type ProjectReadFileResult } from "@t3tools/contracts";
3
import { AsyncResult } from "effect/reactivity";
4
-import type { ReactNode } from "react";
4
+import { StrictMode, type ReactNode } from "react";
5
import { describe, expect, it, vi } from "vite-plus/test";
6
import { userEvent } from "vite-plus/test/browser";
7
import { act, fakeTimers, render } from "../../../test/render";
@@ -268,6 +268,41 @@ describe("source file focus", () => {
268
});
269
});
270
271
+describe("source file scroll", () => {
272
+ it("returns a file tab to where it was scrolled after another file was shown", async () => {
273
+ using _settings = setup();
274
+ const style = document.createElement("style");
275
+ style.textContent = appCss;
276
+ document.head.append(style);
277
+ using _style = { [Symbol.dispose]: () => style.remove() };
278
+ await preloadHighlighter({ themes: Object.values(DEFAULT_THEMES), langs: ["typescript"] });
279
+ const sourceFile = (relativePath: string, lines: number) => {
280
+ const contents = Array.from({ length: lines }, (_, i) => `const line${i} = ${i};`).join("\n");
281
+ state.file = { relativePath, contents, byteLength: contents.length, truncated: false };
282
+ // The app mounts under StrictMode, whose remount must not wipe the saved offset.
283
+ return (
284
+ <StrictMode>
285
+ <div style={{ height: 400, display: "flex", flexDirection: "column" }}>
286
+ {filePreview(relativePath)}
287
+ </div>
288
+ </StrictMode>
289
+ );
290
+ };
291
+ const scroller = () => document.querySelector<HTMLElement>(".file-preview-virtualizer");
292
+
293
+ await using screen = await render(sourceFile("a.ts", 2000));
294
+ await vi.waitFor(() => expect(scroller()?.scrollHeight).toBeGreaterThan(5000));
295
+ scroller()!.scrollTop = 4200;
296
+ await vi.waitFor(() => expect(scroller()?.scrollTop).toBe(4200));
297
+
298
+ await screen.rerender(sourceFile("b.ts", 1500));
299
+ await vi.waitFor(() => expect(scroller()?.scrollTop).toBe(0));
300
+ await screen.rerender(sourceFile("a.ts", 2000));
301
+
302
+ await vi.waitFor(() => expect(scroller()?.scrollTop).toBe(4200));
303
+ });
304
+});
305
+
306
describe("rendered Markdown task saves", () => {
307
it("keeps a truncated read read-only without queuing a replacement", async () => {
308
using _settings = setup();
apps/web/src/components/files/FilePreviewPanel.tsx
+43
-1
@@ -503,6 +503,10 @@ interface FileRevealState {
503
cancelGuard: (() => void) | null;
504
handledRequestId: number | null;
505
latestRequestId: number | null;
506
+ /** The path's code view offset when it last unmounted, restored when it is shown again. */
507
+ scrollTop: number;
508
+ scrollContainer: HTMLElement | null;
509
+ restoreFrameId: number | null;
510
}
511
512
function useFileLineReveal(
@@ -521,6 +525,9 @@ function useFileLineReveal(
525
cancelGuard: null,
526
handledRequestId: null,
527
latestRequestId: null,
528
+ scrollTop: 0,
529
+ scrollContainer: null,
530
+ restoreFrameId: null,
531
};
532
if (!existingState) revealStatesByPath.set(relativePath, state);
533
@@ -530,10 +537,20 @@ function useFileLineReveal(
537
state.frameId = null;
538
}
539
state.cancelGuard?.();
540
+ if (state.restoreFrameId !== null) {
541
+ cancelAnimationFrame(state.restoreFrameId);
542
+ state.restoreFrameId = null;
543
+ }
544
};
545
546
+ const scrollContainer = fileContainer.closest<HTMLElement>(".file-preview-virtualizer");
547
+
548
if (phase === "unmount") {
549
+ // A view torn down before its restore ran never showed the saved offset, so keep it.
550
+ const restorePending = state.restoreFrameId !== null;
551
cancelPendingReveal();
552
+ if (scrollContainer && !restorePending) state.scrollTop = scrollContainer.scrollTop;
553
+ state.scrollContainer = null;
554
return;
555
}
556
@@ -550,12 +567,37 @@ function useFileLineReveal(
567
state.handledRequestId = null;
568
}
569
570
+ if (scrollContainer && state.scrollContainer !== scrollContainer) {
571
+ state.scrollContainer = scrollContainer;
572
+ const savedScrollTop = state.scrollTop;
573
+ // A pending line reveal owns the scroll position.
574
+ if (
575
+ savedScrollTop > 0 &&
576
+ (revealLine === null || state.handledRequestId === revealRequestId)
577
+ ) {
578
+ const restoreScroll = (attempt: number) => {
579
+ state.restoreFrameId = requestAnimationFrame(() => {
580
+ state.restoreFrameId = null;
581
+ if (!scrollContainer.isConnected || scrollContainer.scrollTop !== 0) return;
582
+ // Wait for the virtualizer to size the file so the offset is not clamped.
583
+ const fits =
584
+ scrollContainer.scrollHeight - scrollContainer.clientHeight >= savedScrollTop;
585
+ if (fits || attempt >= REVEAL_MAX_ATTEMPTS) {
586
+ scrollContainer.scrollTop = savedScrollTop;
587
+ return;
588
+ }
589
+ restoreScroll(attempt + 1);
590
+ });
591
+ };
592
+ restoreScroll(0);
593
+ }
594
+ }
595
+
596
if (revealLine === null) {
597
fileContainer.style.minHeight = "";
598
return;
599
}
600
558
- const scrollContainer = fileContainer.closest<HTMLElement>(".file-preview-virtualizer");
601
if (!scrollContainer) return;
602
fileContainer.style.minHeight = `${Math.ceil(
603
Math.max(instance.height, scrollContainer.clientHeight),