The best way to control Claude Code, Codex and more from any computer or phone
fix(web): keep revealed folder in view while restored folders load (#39470)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Jex committed
Nov 20, 2026 at 10:35 UTC
3ea709e8bd95ac784dc711ff68fac62bccedb0c4
2 files changed
+111
-1
apps/web/src/components/files/FileBrowserPanel.browser.test.tsx
+92
@@ -8,6 +8,7 @@ import { isMacPlatform } from "~/lib/utils";
8
import { render } from "../../../test/render";
9
import FileBrowserPanel from "./FileBrowserPanel";
10
import { dispatchPreviewAction } from "../preview/previewActionBus";
11
+import { writeExpandedDirectories } from "./fileTreeExpansion";
12
13
const entries: readonly ProjectEntry[] = [
14
{ path: "src", kind: "directory" },
@@ -54,11 +55,39 @@ function findTreeRow(path: string): HTMLElement | null {
55
return null;
56
}
57
58
+function findTreeScroller(): HTMLElement | null {
59
+ for (const host of document.querySelectorAll("*")) {
60
+ const scroller = host.shadowRoot?.querySelector<HTMLElement>(
61
+ "[data-file-tree-virtualized-scroll]",
62
+ );
63
+ if (scroller) return scroller;
64
+ }
65
+ return null;
66
+}
67
+
68
+function isTreeRowInView(path: string): boolean {
69
+ const rowRect = findTreeRow(path)?.getBoundingClientRect();
70
+ const scrollerRect = findTreeScroller()?.getBoundingClientRect();
71
+ return (
72
+ rowRect !== undefined &&
73
+ scrollerRect !== undefined &&
74
+ rowRect.top >= scrollerRect.top &&
75
+ rowRect.bottom <= scrollerRect.bottom
76
+ );
77
+}
78
+
79
+function sizedContainer(): HTMLElement {
80
+ const container = document.createElement("div");
81
+ container.style.cssText = "display:flex;flex-direction:column;height:300px;width:300px";
82
+ return container;
83
+}
84
+
85
function withDirectoryListing(listing: readonly ProjectEntry[]): Disposable {
86
directory.entries = listing;
87
return {
88
[Symbol.dispose]: () => {
89
directory.entries = entries;
90
+ directory.ready = true;
91
},
92
};
93
}
@@ -289,4 +318,67 @@ describe("FileBrowserPanel", () => {
318
await vi.waitFor(() => expect(findTreeRow("src/")).not.toBeNull());
319
await vi.waitFor(() => expect(findTreeRow("src/")!.getAttribute("aria-expanded")).toBe("true"));
320
});
321
+ describe("revealing a folder while folders above it are still loading", () => {
322
+ const roots = Array.from({ length: 12 }, (_, index): ProjectEntry => {
323
+ return { path: `d${String(index).padStart(2, "0")}`, kind: "directory" };
324
+ });
325
+ const listing = (parent: string, count: number) =>
326
+ Array.from({ length: count }, (_, index): ProjectEntry => {
327
+ return { path: `${parent}/f${String(index).padStart(3, "0")}.ts`, kind: "file" };
328
+ });
329
+ const renderPanel = (cwd: string) => (
330
+ <FileBrowserPanel
331
+ environmentId={EnvironmentId.make("env-files")}
332
+ cwd={cwd}
333
+ projectName="repo"
334
+ selectedPath="d09"
335
+ selectedPathRevealId={1}
336
+ onOpenFile={() => {}}
337
+ workspaceMutationId={null}
338
+ />
339
+ );
340
+ // The tree mounts empty and measures itself before the first listing arrives, as it does for real.
341
+ async function renderRevealedPanel(cwd: string) {
342
+ writeExpandedDirectories(`env-files:${cwd}`, ["d01/", "d01/sub/"]);
343
+ directory.ready = false;
344
+ await using stack = new AsyncDisposableStack();
345
+ const screen = stack.use(await render(renderPanel(cwd), { container: sizedContainer() }));
346
+ directory.entries = roots;
347
+ directory.ready = true;
348
+ await screen.rerender(renderPanel(cwd));
349
+ await vi.waitFor(() => expect(isTreeRowInView("d09/")).toBe(true));
350
+ stack.move();
351
+ return screen;
352
+ }
353
+
354
+ it("keeps the revealed folder in view as restored folders above it load", async () => {
355
+ const cwd = "/repo-reveal-follows-loading";
356
+ using _listing = withDirectoryListing([]);
357
+ await using screen = await renderRevealedPanel(cwd);
358
+
359
+ // d01 lists after the reveal and holds another restored folder, which lists in turn.
360
+ directory.entries = [...roots, ...listing("d01", 60), { path: "d01/sub", kind: "directory" }];
361
+ await screen.rerender(renderPanel(cwd));
362
+ await vi.waitFor(() => expect(findTreeScroller()!.scrollHeight).toBeGreaterThan(60 * 24));
363
+ directory.entries = [...directory.entries, ...listing("d01/sub", 60)];
364
+ await screen.rerender(renderPanel(cwd));
365
+
366
+ await vi.waitFor(() => expect(isTreeRowInView("d09/")).toBe(true));
367
+ });
368
+
369
+ it("leaves the scroll position alone once the user scrolls the tree", async () => {
370
+ const cwd = "/repo-reveal-user-scroll";
371
+ using _listing = withDirectoryListing([]);
372
+ await using screen = await renderRevealedPanel(cwd);
373
+
374
+ const scroller = findTreeScroller()!;
375
+ scroller.dispatchEvent(new WheelEvent("wheel", { bubbles: true, composed: true }));
376
+ scroller.scrollTop = 0;
377
+ directory.entries = [...roots, ...listing("d01", 60)];
378
+ await screen.rerender(renderPanel(cwd));
379
+ await vi.waitFor(() => expect(scroller.scrollHeight).toBeGreaterThan(60 * 24));
380
+
381
+ expect(scroller.scrollTop).toBe(0);
382
+ });
383
+ });
384
});
apps/web/src/components/files/FileBrowserPanel.tsx
+19
-1
@@ -194,6 +194,9 @@ export default function FileBrowserPanel({
194
const syncingSelectionRef = useRef(false);
195
const treeSelectionPathRef = useRef<string | null>(null);
196
const handledRevealRef = useRef<{ path: string; revealId: number } | null>(null);
197
+ // Tree path of the revealed row. Folders restored or loaded later insert rows above it, so each
198
+ // path update scrolls to it again until the user takes over the scroll position.
199
+ const followedRevealRef = useRef<string | null>(null);
200
201
// Shadow-DOM row rects are unreliable; capture the composed contextmenu event's viewport position.
202
const contextMenuPointerRef = useRef<{ x: number; y: number; at: number } | null>(null);
@@ -413,6 +416,9 @@ export default function FileBrowserPanel({
416
controller.abort();
417
};
418
}, [load, selectedPath]);
419
+ const stopFollowingReveal = () => {
420
+ followedRevealRef.current = null;
421
+ };
422
const handleSearchValueChange = (value: string) => {
423
setQuery(value);
424
if (value.trim().length === 0) {
@@ -453,9 +459,14 @@ export default function FileBrowserPanel({
459
if (activeSearch !== null) model.closeSearch();
460
model.resetPaths(treePaths, { initialExpandedPaths: [...expandedPathsRef.current] });
461
if (activeSearch !== null) model.setSearch(activeSearch);
462
+ } else if (updates.length > 0) {
463
+ model.batch(updates);
464
+ } else {
465
return;
466
}
458
- if (updates.length > 0) model.batch(updates);
467
+ if (followedRevealRef.current !== null) {
468
+ model.scrollToPath(followedRevealRef.current, { focus: false, offset: "center" });
469
+ }
470
}, [ready, entryKinds, model, treePaths]);
471
472
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- expands every directory in the Pierre tree model while expand-all is on and no search is active
@@ -468,6 +479,7 @@ export default function FileBrowserPanel({
479
useEffect(() => {
480
if (!selectedPath) {
481
handledRevealRef.current = null;
482
+ followedRevealRef.current = null;
483
return;
484
}
485
const selectedKind = entryKinds.get(selectedPath);
@@ -495,6 +507,7 @@ export default function FileBrowserPanel({
507
if (selectedInTree && treeSelectionPathRef.current === selectedPath) {
508
treeSelectionPathRef.current = null;
509
handledRevealRef.current = revealRequest;
510
+ followedRevealRef.current = null;
511
return;
512
}
513
treeSelectionPathRef.current = null;
@@ -522,6 +535,7 @@ export default function FileBrowserPanel({
535
focus: true,
536
offset: "center",
537
});
538
+ followedRevealRef.current = selectedTreePath;
539
queueMicrotask(() => {
540
syncingSelectionRef.current = false;
541
});
@@ -538,7 +552,11 @@ export default function FileBrowserPanel({
552
// Capture dragstart before the tree selects the row so selection sees the drag flag.
553
onDragStartCapture={(event) => dragMention.handleDragStart(event.nativeEvent)}
554
onDragEnd={() => dragMention.handleDragEnd((path) => model.getItem(path)?.deselect())}
555
+ onWheel={stopFollowingReveal}
556
+ onPointerDown={stopFollowingReveal}
557
+ onTouchStart={stopFollowingReveal}
558
onKeyDown={(event) => {
559
+ stopFollowingReveal();
560
const isModPressed = isMacPlatform(navigator.platform) ? event.metaKey : event.ctrlKey;
561
if (!isModPressed || event.altKey || event.shiftKey || event.key.toLowerCase() !== "f")
562
return;