The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): the file browser keeps its caching without "use memo" hooks (#20784)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 23:19 UTC
3af41edb71e73075a475a445681e3e3ce778cc67
3 files changed
+29
-48
apps/web/src/components/files/FileBrowserPanel.tsx
+11
-25
@@ -10,7 +10,7 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
10
import * as AsyncResult from "effect/reactivity/AsyncResult";
11
import * as Cause from "effect/Cause";
12
import { ChevronsDownUp, ChevronsUpDown } from "lucide";
13
-import { useEffect, useRef, useState, type RefObject } from "react";
13
+import { useEffect, useRef, useState } from "react";
14
15
import { Button } from "~/components/ui/button";
16
import { InputGroup, InputGroupInput } from "~/components/ui/input-group";
@@ -105,15 +105,14 @@ function FileSearchField(props: {
105
);
106
}
107
108
-// The tree diffs `treePaths` by identity, so these derivations must stay cached
109
-// across the hooks that read them.
110
-function useTreeEntries(
108
+// The tree diffs `treePaths` by identity, so the panel must keep this result cached
109
+// across the hooks that read it.
110
+function buildTreeEntries(
111
directoryEntries: readonly ProjectEntry[],
112
searchEntries: readonly ProjectEntry[],
113
searchPending: boolean,
114
query: string,
115
) {
116
- "use memo";
116
const result = new Map(directoryEntries.map((entry) => [entry.path, entry]));
117
if (query.trim() && !searchPending) {
118
for (const entry of searchEntries) {
@@ -134,18 +133,6 @@ function useTreeEntries(
133
return { entries, entryKinds, treePaths, directoryPaths };
134
}
135
137
-function useDragMentionController(
138
- cwd: string,
139
- hostFolder: boolean,
140
- treeModelRef: RefObject<ReturnType<typeof useFileTree>["model"] | null>,
141
-) {
142
- "use memo";
143
- return createFileTreeDragMentionController({
144
- deselect: (path) => treeModelRef.current?.getItem(path)?.deselect(),
145
- ...(hostFolder ? { mentionPath: (path: string) => joinHostPath(cwd, path) } : {}),
146
- });
147
-}
148
-
136
export default function FileBrowserPanel({
137
environmentId,
138
cwd,
@@ -177,7 +164,7 @@ export default function FileBrowserPanel({
164
{ environmentId, cwd: hostFolder ? null : cwd, query: query.slice(0, 256) },
165
200,
166
);
180
- const { entries, entryKinds, treePaths, directoryPaths } = useTreeEntries(
167
+ const { entries, entryKinds, treePaths, directoryPaths } = buildTreeEntries(
168
directoryEntries,
169
pathSearch.entries,
170
pathSearch.isPending,
@@ -281,8 +268,9 @@ export default function FileBrowserPanel({
268
);
269
270
const loadingDirectoriesRef = useRef(loadingDirectories);
284
- const treeModelRef = useRef<ReturnType<typeof useFileTree>["model"] | null>(null);
285
- const dragMention = useDragMentionController(cwd, hostFolder, treeModelRef);
271
+ const dragMention = createFileTreeDragMentionController(
272
+ hostFolder ? { mentionPath: (path: string) => joinHostPath(cwd, path) } : {},
273
+ );
274
const { model } = useFileTree({
275
composition: {
276
contextMenu: {
@@ -498,23 +486,21 @@ export default function FileBrowserPanel({
486
// The capture phase runs before the tree's own dragstart handler selects
487
// the dragged row, so the drag flag is up before that selection emits.
488
const panelRef = useRef<HTMLDivElement | null>(null);
501
- useEffect(() => {
502
- treeModelRef.current = model;
503
- }, [model]);
489
useEffect(() => {
490
const panel = panelRef.current;
491
if (panel === null) {
492
return;
493
}
494
const handleDragStart = (event: DragEvent) => dragMention.handleDragStart(event);
510
- const handleDragEnd = () => dragMention.handleDragEnd();
495
+ const handleDragEnd = () =>
496
+ dragMention.handleDragEnd((path) => model.getItem(path)?.deselect());
497
panel.addEventListener("dragstart", handleDragStart, true);
498
panel.addEventListener("dragend", handleDragEnd);
499
return () => {
500
panel.removeEventListener("dragstart", handleDragStart, true);
501
panel.removeEventListener("dragend", handleDragEnd);
502
};
517
- }, [dragMention]);
503
+ }, [dragMention, model]);
504
505
return (
506
<div
apps/web/src/components/files/fileTreeDragMention.test.ts
+15
-19
@@ -18,7 +18,7 @@ const rowNode = (path: string) => ({
18
19
describe("createFileTreeDragMentionController", () => {
20
it("tags a row drag with the mention payload and flags the drag", () => {
21
- const controller = createFileTreeDragMentionController({ deselect: () => {} });
21
+ const controller = createFileTreeDragMentionController({});
22
const transfer = makeTransfer();
23
controller.handleDragStart({
24
dataTransfer: transfer,
@@ -29,7 +29,7 @@ describe("createFileTreeDragMentionController", () => {
29
});
30
31
it("strips the trailing slash from directory rows", () => {
32
- const controller = createFileTreeDragMentionController({ deselect: () => {} });
32
+ const controller = createFileTreeDragMentionController({});
33
const transfer = makeTransfer();
34
controller.handleDragStart({
35
dataTransfer: transfer,
@@ -40,7 +40,6 @@ describe("createFileTreeDragMentionController", () => {
40
41
it("mentions rows of a host folder by their host path", () => {
42
const controller = createFileTreeDragMentionController({
43
- deselect: () => {},
43
mentionPath: (path) => `/Users/me/scope/${path}`,
44
});
45
const transfer = makeTransfer();
@@ -52,7 +51,7 @@ describe("createFileTreeDragMentionController", () => {
51
});
52
53
it("does not tag drags of selected text from the panel chrome", () => {
55
- const controller = createFileTreeDragMentionController({ deselect: () => {} });
54
+ const controller = createFileTreeDragMentionController({});
55
const transfer = makeTransfer("selected text");
56
controller.handleDragStart({ dataTransfer: transfer, composedPath: () => [{}] });
57
expect(transfer.data.has(COMPOSER_MENTION_DRAG_TYPE)).toBe(false);
@@ -60,7 +59,7 @@ describe("createFileTreeDragMentionController", () => {
59
});
60
61
it("ignores drags that carry no row path", () => {
63
- const controller = createFileTreeDragMentionController({ deselect: () => {} });
62
+ const controller = createFileTreeDragMentionController({});
63
const transfer = makeTransfer();
64
controller.handleDragStart({ dataTransfer: transfer, composedPath: () => [{}] });
65
expect(transfer.data.has(COMPOSER_MENTION_DRAG_TYPE)).toBe(false);
@@ -69,24 +68,22 @@ describe("createFileTreeDragMentionController", () => {
68
69
it("deselects the dragged row exactly once when the drag ends", () => {
70
const deselected: Array<string> = [];
72
- const controller = createFileTreeDragMentionController({
73
- deselect: (path) => deselected.push(path),
74
- });
71
+ const controller = createFileTreeDragMentionController({});
72
+ const deselect = (path: string) => void deselected.push(path);
73
controller.handleDragStart({
74
dataTransfer: makeTransfer(),
75
composedPath: () => [rowNode("src/app.ts")],
76
});
79
- controller.handleDragEnd();
80
- controller.handleDragEnd();
77
+ controller.handleDragEnd(deselect);
78
+ controller.handleDragEnd(deselect);
79
expect(deselected).toEqual(["src/app.ts"]);
80
expect(controller.isDragInProgress()).toBe(false);
81
});
82
83
it("drags the whole selection when the dragged row is part of it", () => {
84
const deselected: Array<string> = [];
87
- const controller = createFileTreeDragMentionController({
88
- deselect: (path) => deselected.push(path),
89
- });
85
+ const controller = createFileTreeDragMentionController({});
86
+ const deselect = (path: string) => void deselected.push(path);
87
controller.handleSelectionChange(["docs/index.md", "docs/api.md", "src/app.ts"]);
88
const transfer = makeTransfer();
89
controller.handleDragStart({
@@ -96,12 +93,12 @@ describe("createFileTreeDragMentionController", () => {
93
expect(transfer.getData(COMPOSER_MENTION_DRAG_TYPE)).toBe(
94
"[index.md](docs/index.md) [api.md](docs/api.md) [app.ts](src/app.ts)",
95
);
99
- controller.handleDragEnd();
96
+ controller.handleDragEnd(deselect);
97
expect(deselected).toEqual(["docs/index.md", "docs/api.md", "src/app.ts"]);
98
});
99
100
it("drags only the row under the cursor when it is outside the selection", () => {
104
- const controller = createFileTreeDragMentionController({ deselect: () => {} });
101
+ const controller = createFileTreeDragMentionController({});
102
controller.handleSelectionChange(["docs/index.md"]);
103
const transfer = makeTransfer();
104
controller.handleDragStart({
@@ -113,10 +110,9 @@ describe("createFileTreeDragMentionController", () => {
110
111
it("does not deselect anything when no drag was started", () => {
112
const deselected: Array<string> = [];
116
- const controller = createFileTreeDragMentionController({
117
- deselect: (path) => deselected.push(path),
118
- });
119
- controller.handleDragEnd();
113
+ const controller = createFileTreeDragMentionController({});
114
+ const deselect = (path: string) => void deselected.push(path);
115
+ controller.handleDragEnd(deselect);
116
expect(deselected).toEqual([]);
117
});
118
});
apps/web/src/components/files/fileTreeDragMention.ts
+3
-4
@@ -13,7 +13,6 @@ export interface FileTreeDragStartEvent {
13
}
14
15
export interface FileTreeDragMentionHost {
16
- deselect(treePath: string): void;
16
/** The path a dragged row mentions; defaults to the tree path itself. */
17
mentionPath?(treePath: string): string;
18
}
@@ -22,7 +21,7 @@ export interface FileTreeDragMentionController {
21
isDragInProgress(): boolean;
22
handleSelectionChange(selectedPaths: ReadonlyArray<string>): void;
23
handleDragStart(event: FileTreeDragStartEvent): void;
25
- handleDragEnd(): void;
24
+ handleDragEnd(deselect: (treePath: string) => void): void;
25
}
26
27
const itemPathOf = (node: unknown): string | null => {
@@ -74,12 +73,12 @@ export function createFileTreeDragMentionController(
73
draggedPaths = dragged;
74
event.dataTransfer.setData(COMPOSER_MENTION_DRAG_TYPE, mentions.join(" "));
75
},
77
- handleDragEnd() {
76
+ handleDragEnd(deselect) {
77
if (draggedPaths.length === 0) {
78
return;
79
}
80
for (const path of draggedPaths) {
82
- host.deselect(path);
81
+ deselect(path);
82
}
83
draggedPaths = [];
84
},