Skip to content

Commit 1e0dcae

Browse files
refactor(web): the file browser keeps its caching without "use memo" hooks
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 47683f6 commit 1e0dcae

3 files changed

Lines changed: 29 additions & 48 deletions

File tree

‎apps/web/src/components/files/FileBrowserPanel.tsx‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { serializeComposerFileLink } from "@t3tools/shared/composerTrigger";
1010
import * as AsyncResult from "effect/reactivity/AsyncResult";
1111
import * as Cause from "effect/Cause";
1212
import { ChevronsDownUp, ChevronsUpDown } from "lucide";
13-
import { useEffect, useRef, useState, type RefObject } from "react";
13+
import { useEffect, useRef, useState } from "react";
1414

1515
import { Button } from "~/components/ui/button";
1616
import { InputGroup, InputGroupInput } from "~/components/ui/input-group";
@@ -105,15 +105,14 @@ function FileSearchField(props: {
105105
);
106106
}
107107

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(
111111
directoryEntries: readonly ProjectEntry[],
112112
searchEntries: readonly ProjectEntry[],
113113
searchPending: boolean,
114114
query: string,
115115
) {
116-
"use memo";
117116
const result = new Map(directoryEntries.map((entry) => [entry.path, entry]));
118117
if (query.trim() && !searchPending) {
119118
for (const entry of searchEntries) {
@@ -134,18 +133,6 @@ function useTreeEntries(
134133
return { entries, entryKinds, treePaths, directoryPaths };
135134
}
136135

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-
149136
export default function FileBrowserPanel({
150137
environmentId,
151138
cwd,
@@ -177,7 +164,7 @@ export default function FileBrowserPanel({
177164
{ environmentId, cwd: hostFolder ? null : cwd, query: query.slice(0, 256) },
178165
200,
179166
);
180-
const { entries, entryKinds, treePaths, directoryPaths } = useTreeEntries(
167+
const { entries, entryKinds, treePaths, directoryPaths } = buildTreeEntries(
181168
directoryEntries,
182169
pathSearch.entries,
183170
pathSearch.isPending,
@@ -281,8 +268,9 @@ export default function FileBrowserPanel({
281268
);
282269

283270
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+
);
286274
const { model } = useFileTree({
287275
composition: {
288276
contextMenu: {
@@ -498,23 +486,21 @@ export default function FileBrowserPanel({
498486
// The capture phase runs before the tree's own dragstart handler selects
499487
// the dragged row, so the drag flag is up before that selection emits.
500488
const panelRef = useRef<HTMLDivElement | null>(null);
501-
useEffect(() => {
502-
treeModelRef.current = model;
503-
}, [model]);
504489
useEffect(() => {
505490
const panel = panelRef.current;
506491
if (panel === null) {
507492
return;
508493
}
509494
const handleDragStart = (event: DragEvent) => dragMention.handleDragStart(event);
510-
const handleDragEnd = () => dragMention.handleDragEnd();
495+
const handleDragEnd = () =>
496+
dragMention.handleDragEnd((path) => model.getItem(path)?.deselect());
511497
panel.addEventListener("dragstart", handleDragStart, true);
512498
panel.addEventListener("dragend", handleDragEnd);
513499
return () => {
514500
panel.removeEventListener("dragstart", handleDragStart, true);
515501
panel.removeEventListener("dragend", handleDragEnd);
516502
};
517-
}, [dragMention]);
503+
}, [dragMention, model]);
518504

519505
return (
520506
<div

‎apps/web/src/components/files/fileTreeDragMention.test.ts‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const rowNode = (path: string) => ({
1818

1919
describe("createFileTreeDragMentionController", () => {
2020
it("tags a row drag with the mention payload and flags the drag", () => {
21-
const controller = createFileTreeDragMentionController({ deselect: () => {} });
21+
const controller = createFileTreeDragMentionController({});
2222
const transfer = makeTransfer();
2323
controller.handleDragStart({
2424
dataTransfer: transfer,
@@ -29,7 +29,7 @@ describe("createFileTreeDragMentionController", () => {
2929
});
3030

3131
it("strips the trailing slash from directory rows", () => {
32-
const controller = createFileTreeDragMentionController({ deselect: () => {} });
32+
const controller = createFileTreeDragMentionController({});
3333
const transfer = makeTransfer();
3434
controller.handleDragStart({
3535
dataTransfer: transfer,
@@ -40,7 +40,6 @@ describe("createFileTreeDragMentionController", () => {
4040

4141
it("mentions rows of a host folder by their host path", () => {
4242
const controller = createFileTreeDragMentionController({
43-
deselect: () => {},
4443
mentionPath: (path) => `/Users/me/scope/${path}`,
4544
});
4645
const transfer = makeTransfer();
@@ -52,15 +51,15 @@ describe("createFileTreeDragMentionController", () => {
5251
});
5352

5453
it("does not tag drags of selected text from the panel chrome", () => {
55-
const controller = createFileTreeDragMentionController({ deselect: () => {} });
54+
const controller = createFileTreeDragMentionController({});
5655
const transfer = makeTransfer("selected text");
5756
controller.handleDragStart({ dataTransfer: transfer, composedPath: () => [{}] });
5857
expect(transfer.data.has(COMPOSER_MENTION_DRAG_TYPE)).toBe(false);
5958
expect(controller.isDragInProgress()).toBe(false);
6059
});
6160

6261
it("ignores drags that carry no row path", () => {
63-
const controller = createFileTreeDragMentionController({ deselect: () => {} });
62+
const controller = createFileTreeDragMentionController({});
6463
const transfer = makeTransfer();
6564
controller.handleDragStart({ dataTransfer: transfer, composedPath: () => [{}] });
6665
expect(transfer.data.has(COMPOSER_MENTION_DRAG_TYPE)).toBe(false);
@@ -69,24 +68,22 @@ describe("createFileTreeDragMentionController", () => {
6968

7069
it("deselects the dragged row exactly once when the drag ends", () => {
7170
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);
7573
controller.handleDragStart({
7674
dataTransfer: makeTransfer(),
7775
composedPath: () => [rowNode("src/app.ts")],
7876
});
79-
controller.handleDragEnd();
80-
controller.handleDragEnd();
77+
controller.handleDragEnd(deselect);
78+
controller.handleDragEnd(deselect);
8179
expect(deselected).toEqual(["src/app.ts"]);
8280
expect(controller.isDragInProgress()).toBe(false);
8381
});
8482

8583
it("drags the whole selection when the dragged row is part of it", () => {
8684
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);
9087
controller.handleSelectionChange(["docs/index.md", "docs/api.md", "src/app.ts"]);
9188
const transfer = makeTransfer();
9289
controller.handleDragStart({
@@ -96,12 +93,12 @@ describe("createFileTreeDragMentionController", () => {
9693
expect(transfer.getData(COMPOSER_MENTION_DRAG_TYPE)).toBe(
9794
"[index.md](docs/index.md) [api.md](docs/api.md) [app.ts](src/app.ts)",
9895
);
99-
controller.handleDragEnd();
96+
controller.handleDragEnd(deselect);
10097
expect(deselected).toEqual(["docs/index.md", "docs/api.md", "src/app.ts"]);
10198
});
10299

103100
it("drags only the row under the cursor when it is outside the selection", () => {
104-
const controller = createFileTreeDragMentionController({ deselect: () => {} });
101+
const controller = createFileTreeDragMentionController({});
105102
controller.handleSelectionChange(["docs/index.md"]);
106103
const transfer = makeTransfer();
107104
controller.handleDragStart({
@@ -113,10 +110,9 @@ describe("createFileTreeDragMentionController", () => {
113110

114111
it("does not deselect anything when no drag was started", () => {
115112
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);
120116
expect(deselected).toEqual([]);
121117
});
122118
});

‎apps/web/src/components/files/fileTreeDragMention.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export interface FileTreeDragStartEvent {
1313
}
1414

1515
export interface FileTreeDragMentionHost {
16-
deselect(treePath: string): void;
1716
/** The path a dragged row mentions; defaults to the tree path itself. */
1817
mentionPath?(treePath: string): string;
1918
}
@@ -22,7 +21,7 @@ export interface FileTreeDragMentionController {
2221
isDragInProgress(): boolean;
2322
handleSelectionChange(selectedPaths: ReadonlyArray<string>): void;
2423
handleDragStart(event: FileTreeDragStartEvent): void;
25-
handleDragEnd(): void;
24+
handleDragEnd(deselect: (treePath: string) => void): void;
2625
}
2726

2827
const itemPathOf = (node: unknown): string | null => {
@@ -74,12 +73,12 @@ export function createFileTreeDragMentionController(
7473
draggedPaths = dragged;
7574
event.dataTransfer.setData(COMPOSER_MENTION_DRAG_TYPE, mentions.join(" "));
7675
},
77-
handleDragEnd() {
76+
handleDragEnd(deselect) {
7877
if (draggedPaths.length === 0) {
7978
return;
8079
}
8180
for (const path of draggedPaths) {
82-
host.deselect(path);
81+
deselect(path);
8382
}
8483
draggedPaths = [];
8584
},

0 commit comments

Comments
 (0)