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, 12:15 UTC
perf(web): Expand all folders in the diff file tree no longer rescans every folder for each expansion (#31465)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Pyseph committed
Nov 15, 2026 at 04:06 UTC
1c86881dd5aa845c125957bf2a7c87013819d639
3 files changed
+67
-5
apps/web/src/components/diffs/DiffFileTree.browser.test.tsx
new
+33
@@ -0,0 +1,33 @@
1
+import { FileTree } from "@pierre/trees";
2
+import { expect, it, vi } from "vite-plus/test";
3
+import { page, userEvent } from "vite-plus/test/browser";
4
+
5
+import { render } from "../../../test/render";
6
+import { DiffFileTree, type DiffFileTreeEntry } from "./DiffFileTree";
7
+
8
+const FOLDERS = 300;
9
+
10
+it("expands every folder without asking the tree about all folders for each one", async () => {
11
+ // Two levels, so flattening empty folders cannot merge them into one.
12
+ const entries: DiffFileTreeEntry[] = Array.from({ length: FOLDERS }, (_, index) => [
13
+ { path: `package-${index}/a.ts`, status: "modified" as const },
14
+ { path: `package-${index}/lib/b.ts`, status: "modified" as const },
15
+ ]).flat();
16
+ using getItem = vi.spyOn(FileTree.prototype, "getItem");
17
+ await using _screen = await render(
18
+ <div className="flex h-[600px] flex-col">
19
+ <DiffFileTree entries={entries} onSelectFile={() => {}} ariaLabel="Changed files" />
20
+ </div>,
21
+ );
22
+ const collapseAll = page.getByRole("button", { name: "Collapse all folders" });
23
+ await expect.element(collapseAll).toBeVisible();
24
+ await userEvent.click(collapseAll);
25
+ const expandAll = page.getByRole("button", { name: "Expand all folders" });
26
+ await expect.element(expandAll).toBeVisible();
27
+ getItem.mockClear();
28
+ await userEvent.click(expandAll);
29
+ await expect.element(page.getByRole("button", { name: "Collapse all folders" })).toBeVisible();
30
+ // Each of the expansions used to look up the folders again from the first one, so the lookups
31
+ // grew with the square of the folder count (hundreds of thousands at 600 folders).
32
+ expect(getItem.mock.calls.length).toBeLessThan(FOLDERS * 2 * 20);
33
+});
apps/web/src/components/diffs/DiffFileTree.tsx
+10
-5
@@ -1,14 +1,18 @@
1
import type { GitStatusEntry } from "@pierre/trees";
2
-import { FileTree, useFileTree, useFileTreeSelector } from "@pierre/trees/react";
2
+import { FileTree, useFileTree } from "@pierre/trees/react";
3
import { ChevronsDownUpIcon, ChevronsUpDownIcon } from "lucide-react";
4
-import { useEffect, useRef, useState, type ReactNode } from "react";
4
+import { useEffect, useRef, useState, useSyncExternalStore, type ReactNode } from "react";
5
6
import { useStableCallback } from "~/hooks/useStableCallback";
7
import { useTheme } from "~/hooks/useTheme";
8
import { cn } from "~/lib/utils";
9
import { PIERRE_TREE_UNSAFE_CSS, pierreTreeStyle } from "~/pierre-tree-theme";
10
11
-import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "../files/fileTreeExpansion";
11
+import {
12
+ areAllDirectoriesExpanded,
13
+ setAllDirectoriesExpanded,
14
+ subscribeOncePerTask,
15
+} from "../files/fileTreeExpansion";
16
import { Button } from "../ui/button";
17
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
18
import {
@@ -90,8 +94,9 @@ export function DiffFileTree({
94
sort: ordering.sort,
95
unsafeCSS: PIERRE_TREE_UNSAFE_CSS,
96
});
93
- const allDirectoriesExpanded = useFileTreeSelector(model, (currentModel) =>
94
- areAllDirectoriesExpanded(currentModel, directoryPaths),
97
+ const allDirectoriesExpanded = useSyncExternalStore(
98
+ (listener) => subscribeOncePerTask(model, listener),
99
+ () => areAllDirectoriesExpanded(model, directoryPaths),
100
);
101
102
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes the icon set into the imperative Pierre file tree model
apps/web/src/components/files/fileTreeExpansion.ts
+24
@@ -32,6 +32,30 @@ function asDirectoryHandle(item: unknown): DirectoryHandle | null {
32
return item as DirectoryHandle;
33
}
34
35
+/**
36
+ * Calls `listener` once per task, however many events the model emitted in it. Expanding every
37
+ * folder emits one event per folder, and a listener that rescans the folders is quadratic otherwise.
38
+ */
39
+export function subscribeOncePerTask(
40
+ model: { subscribe(listener: () => void): () => void },
41
+ listener: () => void,
42
+): () => void {
43
+ let queued = false;
44
+ let subscribed = true;
45
+ const unsubscribe = model.subscribe(() => {
46
+ if (queued) return;
47
+ queued = true;
48
+ queueMicrotask(() => {
49
+ queued = false;
50
+ if (subscribed) listener();
51
+ });
52
+ });
53
+ return () => {
54
+ subscribed = false;
55
+ unsubscribe();
56
+ };
57
+}
58
+
59
export function areAllDirectoriesExpanded(
60
model: FileTreeExpansionModel,
61
directoryPaths: readonly string[],