@devries-cameron20 / t3code-1 / commits / 1144a7ecc4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): useDirectoryEntries compiles with the React Compiler (#20482)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 18:46 UTC 1144a7ecc4b7f437de9c5caaa88524c057c9a8b4
2 files changed +97 -98
apps/web/reactCompiledFiles.ts
+1
@@ -57,4 +57,5 @@ export const REACT_COMPILED_FILES = [
57 "apps/web/src/components/files/AttachmentFilePreview.tsx",
58 "apps/web/src/components/usage/UsagePage.tsx",
59 "apps/web/src/components/ProviderUpdateEnvironmentRows.tsx",
60 + "apps/web/src/components/files/useDirectoryEntries.ts",
61 ] as const;
apps/web/src/components/files/useDirectoryEntries.ts
+96 -98
@@ -1,7 +1,7 @@
1 import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts";
2 import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
3 import * as Cause from "effect/Cause";
4 -import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4 +import { useEffect, useRef, useState } from "react";
5
6 import { appAtomRegistry } from "~/rpc/atomRegistry";
7 import { projectEnvironment } from "~/state/projects";
@@ -9,8 +9,11 @@ import { projectEnvironment } from "~/state/projects";
9 export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
10 const [directories, setDirectories] = useState(new Map<string, readonly ProjectEntry[]>());
11 const [errors, setErrors] = useState(new Map<string, string>());
12 - const [pending, setPending] = useState(0);
13 - const [loadingDirectories, setLoadingDirectories] = useState<ReadonlySet<string>>(new Set());
12 + // The root starts counted as loading because the mount effect requests it.
13 + const [pending, setPending] = useState(1);
14 + const [loadingDirectories, setLoadingDirectories] = useState<ReadonlySet<string>>(
15 + () => new Set([""]),
16 + );
17 const requests = useRef(new Map<string, Promise<void>>());
18 const loaded = useRef(new Set<string>());
19 const requested = useRef(new Set<string>());
@@ -18,119 +21,114 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
21 const running = useRef(0);
22 const waiting = useRef<Array<() => void>>([]);
23
21 - const load = useCallback(
22 - function loadDirectory(directoryPath: string, refresh = false): Promise<void> {
23 - const existing = requests.current.get(directoryPath);
24 - if (existing)
25 - return refresh ? existing.then(() => loadDirectory(directoryPath, true)) : existing;
26 - if (!refresh && loaded.current.has(directoryPath)) return Promise.resolve();
27 - loaded.current.add(directoryPath);
28 - requested.current.add(directoryPath);
29 - const atom = projectEnvironment.listEntries({ environmentId, input: { cwd, directoryPath } });
30 - setPending((count) => count + 1);
31 - if (!refresh) setLoadingDirectories((previous) => new Set(previous).add(directoryPath));
32 - const request = (async () => {
33 - if (running.current >= 4)
34 - await new Promise<void>((resolve) => waiting.current.push(resolve));
35 - else running.current++;
36 - try {
37 - if (!active.current) return undefined;
38 - return await executeAtomQuery(appAtomRegistry, atom, {
24 + // Callers count the request as pending first; the mount load is counted by the initial state.
25 + const startRequest = (directoryPath: string, refresh: boolean): Promise<void> => {
26 + loaded.current.add(directoryPath);
27 + requested.current.add(directoryPath);
28 + const atom = projectEnvironment.listEntries({ environmentId, input: { cwd, directoryPath } });
29 + const request = (async () => {
30 + if (running.current >= 4) await new Promise<void>((resolve) => waiting.current.push(resolve));
31 + else running.current++;
32 + // executeAtomQuery settles instead of throwing, so the slot is always released.
33 + const result = active.current
34 + ? await executeAtomQuery(appAtomRegistry, atom, {
35 refresh: true,
36 reportFailure: false,
37 reportDefect: false,
38 + })
39 + : undefined;
40 + const next = waiting.current.shift();
41 + if (next) next();
42 + else running.current--;
43 + return result;
44 + })()
45 + .then((result) => {
46 + if (!active.current || !result) return;
47 + if (result._tag === "Success") {
48 + setDirectories((previous) =>
49 + new Map(previous).set(
50 + directoryPath,
51 + result.value.entries.filter(
52 + (entry) =>
53 + entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
54 + ),
55 + ),
56 + );
57 + setErrors((previous) => {
58 + const next = new Map(previous);
59 + next.delete(directoryPath);
60 + return next;
61 });
43 - } finally {
44 - const next = waiting.current.shift();
45 - if (next) next();
46 - else running.current--;
62 + } else {
63 + loaded.current.delete(directoryPath);
64 + const cause = Cause.squash(result.cause);
65 + setErrors((previous) =>
66 + new Map(previous).set(
67 + directoryPath,
68 + cause instanceof Error ? cause.message : "Unable to load folder.",
69 + ),
70 + );
71 }
48 - })()
49 - .then((result) => {
50 - if (!active.current || !result) return;
51 - if (result._tag === "Success") {
52 - setDirectories((previous) =>
53 - new Map(previous).set(
54 - directoryPath,
55 - result.value.entries.filter(
56 - (entry) =>
57 - entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
58 - ),
59 - ),
60 - );
61 - setErrors((previous) => {
62 - const next = new Map(previous);
63 - next.delete(directoryPath);
64 - return next;
65 - });
66 - } else {
67 - loaded.current.delete(directoryPath);
68 - const cause = Cause.squash(result.cause);
69 - setErrors((previous) =>
70 - new Map(previous).set(
71 - directoryPath,
72 - cause instanceof Error ? cause.message : "Unable to load folder.",
73 - ),
74 - );
75 - }
76 - })
77 - .finally(() => {
78 - requests.current.delete(directoryPath);
79 - if (!active.current) return;
80 - setPending((count) => count - 1);
81 - if (!refresh)
82 - setLoadingDirectories((previous) => {
83 - const next = new Set(previous);
84 - next.delete(directoryPath);
85 - return next;
86 - });
87 - });
88 - requests.current.set(directoryPath, request);
89 - return request;
90 - },
91 - [cwd, environmentId],
92 - );
72 + })
73 + .finally(() => {
74 + requests.current.delete(directoryPath);
75 + if (!active.current) return;
76 + setPending((count) => count - 1);
77 + if (!refresh)
78 + setLoadingDirectories((previous) => {
79 + const next = new Set(previous);
80 + next.delete(directoryPath);
81 + return next;
82 + });
83 + });
84 + requests.current.set(directoryPath, request);
85 + return request;
86 + };
87 +
88 + const load = function loadDirectory(directoryPath: string, refresh = false): Promise<void> {
89 + const existing = requests.current.get(directoryPath);
90 + if (existing)
91 + return refresh ? existing.then(() => loadDirectory(directoryPath, true)) : existing;
92 + if (!refresh && loaded.current.has(directoryPath)) return Promise.resolve();
93 + setPending((count) => count + 1);
94 + if (!refresh) setLoadingDirectories((previous) => new Set(previous).add(directoryPath));
95 + return startRequest(directoryPath, refresh);
96 + };
97
98 useEffect(() => {
99 active.current = true;
96 - void load("");
100 + if (!requests.current.has("") && !loaded.current.has("")) void startRequest("", false);
101 return () => {
102 active.current = false;
103 };
100 - }, [load]);
104 + }, [startRequest]);
105
102 - const entries = useMemo(() => {
103 - const result: ProjectEntry[] = [];
104 - const visit = (path: string) => {
105 - for (const entry of directories.get(path) ?? []) {
106 - result.push(entry);
107 - if (entry.kind === "directory") visit(entry.path);
108 - }
109 - };
110 - visit("");
111 - return result;
112 - }, [directories]);
106 + const entries: ProjectEntry[] = [];
107 + const visit = (path: string) => {
108 + for (const entry of directories.get(path) ?? []) {
109 + entries.push(entry);
110 + if (entry.kind === "directory") visit(entry.path);
111 + }
112 + };
113 + visit("");
114
114 - const reachableDirectories = useMemo(
115 - () =>
116 - new Set([
117 - "",
118 - ...entries.filter((entry) => entry.kind === "directory").map((entry) => entry.path),
119 - ]),
120 - [entries],
121 - );
115 + const reachableDirectories = new Set([
116 + "",
117 + ...entries.filter((entry) => entry.kind === "directory").map((entry) => entry.path),
118 + ]);
119
123 - const refresh = useCallback(() => {
120 + const refresh = () => {
121 const paths = [...requested.current].filter((path) => reachableDirectories.has(path));
125 - let next = 0;
122 + const workerCount = Math.min(4, paths.length);
123 const worker = async () => {
127 - while (next < paths.length && active.current) {
128 - const path = paths[next++];
129 - if (path !== undefined) await load(path, true);
124 + while (active.current) {
125 + const path = paths.shift();
126 + if (path === undefined) return;
127 + await load(path, true);
128 }
129 };
132 - for (let index = 0; index < Math.min(4, paths.length); index++) void worker();
133 - }, [load, reachableDirectories]);
130 + for (let index = 0; index < workerCount; index++) void worker();
131 + };
132
133 return {
134 entries,