@devries-cameron20 / t3code Mirror · read-only / commits / 28f1786829
Fork 0

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, 16:00 UTC

fix(web): recreated folders no longer show deleted files (#36590)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 10:05 UTC 28f178682949e0eaf732a6a8314cb7c2ba94dcc9
2 files changed +125 -30
apps/web/src/components/files/useDirectoryEntries.browser.test.tsx
+76 -15
@@ -12,6 +12,7 @@ const { listEntries, mocks } = vi.hoisted(() => ({
12 listEntries: vi.fn(),
13 mocks: {
14 reconnect: false,
15 + generation: 0,
16 reachable: null as unknown as Atom.Writable<boolean>,
17 connectedSession: null as unknown as Atom.Writable<number>,
18 },
@@ -21,26 +22,39 @@ vi.mock("~/connection/catalog", () => ({
22 sessionGenerationAtom: () => Atom.make((get) => get(mocks.connectedSession)),
23 },
24 }));
24 -vi.mock("~/state/projects", () => ({
25 - projectEnvironment: {
26 - listEntries: (request: unknown) =>
27 - mocks.reconnect
28 - ? Atom.make((get) =>
29 - get(mocks.reachable)
30 - ? AsyncResult.success({
31 - entries: [{ path: "README.md", kind: "file" }],
32 - truncated: false,
33 - })
34 - : AsyncResult.failure(Cause.fail(new Error("Environment is reconnecting."))),
35 - )
36 - : Atom.make(Effect.promise(() => listEntries(request))),
37 - },
38 -}));
25 +vi.mock("~/state/projects", () => {
26 + const queries = new Map<string, ReturnType<typeof makeQuery>>();
27 + const makeQuery = (request: unknown) => Atom.make(Effect.promise(() => listEntries(request)));
28 + return {
29 + projectEnvironment: {
30 + listEntries: (request: unknown) =>
31 + mocks.reconnect
32 + ? Atom.make((get) =>
33 + get(mocks.reachable)
34 + ? AsyncResult.success({
35 + entries: [{ path: "README.md", kind: "file" }],
36 + truncated: false,
37 + })
38 + : AsyncResult.failure(Cause.fail(new Error("Environment is reconnecting."))),
39 + )
40 + : (() => {
41 + const key = JSON.stringify([mocks.generation, request]);
42 + let query = queries.get(key);
43 + if (!query) {
44 + query = makeQuery(request);
45 + queries.set(key, query);
46 + }
47 + return query;
48 + })(),
49 + },
50 + };
51 +});
52
53 import { useDirectoryEntries } from "./useDirectoryEntries";
54
55 function setup() {
56 mocks.reconnect = false;
57 + mocks.generation++;
58 mocks.reachable = Atom.make(false);
59 mocks.connectedSession = Atom.make(1);
60 let view!: ReturnType<typeof useDirectoryEntries>;
@@ -125,6 +139,18 @@ it("rescans mutations during both the initial listing and the follow-up", async
139 await state.settle(2, [entry]);
140 expect(state.view.entries).toEqual([entry]);
141 expect(state.view.isPending).toBe(false);
142 +
143 + const folder = { path: "src", kind: "directory" } as const;
144 + const child = { path: "src/new.ts", kind: "file" } as const;
145 + await act(async () => {
146 + void state.view.load("src");
147 + });
148 + await state.settle(3, [child]);
149 + await act(async () => {
150 + void state.view.load("", true);
151 + });
152 + await state.settle(4, [folder]);
153 + expect(state.view.entries).toEqual([folder, child]);
154 });
155
156 it("shares the follow-up promise and does not resolve callers before the final listing", async () => {
@@ -214,3 +240,38 @@ it("reads a folder that failed while unreachable again once a new session is up"
240 });
241 await expect.element(screen.getByText("README.md")).toBeInTheDocument();
242 });
243 +
244 +it("reloads a removed folder when it reappears under the same name", async () => {
245 + const state = setup();
246 + // The hook probe does not navigate.
247 + await using _screen = await render(<state.Probe />, { router: false });
248 + const folder = { path: "src", kind: "directory" } as const;
249 + await state.settle(0, [folder]);
250 + await act(async () => {
251 + void state.view.load("src");
252 + });
253 + await state.settle(1, [{ path: "src/old.ts", kind: "file" }]);
254 +
255 + await act(async () => {
256 + state.view.refresh();
257 + void state.view.load("src", true);
258 + });
259 + await state.settle(2, []);
260 + expect(state.view.entries).toEqual([]);
261 +
262 + await act(async () => state.view.refresh());
263 + await state.settle(4, [folder]);
264 + await act(async () => {
265 + void state.view.load("src");
266 + });
267 + const replacementRefresh = state.view.load("src", true);
268 + await state.settle(3, [{ path: "src/old.ts", kind: "file" }]);
269 + expect(state.view.load("src", true)).toBe(replacementRefresh);
270 + expect(state.view.entries).toEqual([folder]);
271 + expect(state.view.loadingDirectories.has("src")).toBe(true);
272 + await state.settle(5, [{ path: "src/new.ts", kind: "file" }]);
273 + await state.settle(6, [{ path: "src/new.ts", kind: "file" }]);
274 + await replacementRefresh;
275 + expect(state.view.entries).toEqual([folder, { path: "src/new.ts", kind: "file" }]);
276 + expect(state.view.isPending).toBe(false);
277 +});
apps/web/src/components/files/useDirectoryEntries.ts
+49 -15
@@ -15,7 +15,10 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
15 const [loadingDirectories, setLoadingDirectories] = useState<ReadonlySet<string>>(
16 () => new Set([""]),
17 );
18 + // Source of truth for `directories`, so a listing can purge folders it no longer holds.
19 + const listings = useRef(new Map<string, readonly ProjectEntry[]>());
20 const requests = useRef(new Map<string, Promise<void>>());
21 + const detachedRequests = useRef(new Map<string, Promise<void>>());
22 const followUpRefreshes = useRef(new Map<string, Promise<void>>());
23 const loaded = useRef(new Set<string>());
24 const requested = useRef(new Set<string>());
@@ -27,7 +30,10 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
30 loaded.current.add(directoryPath);
31 requested.current.add(directoryPath);
32 const atom = projectEnvironment.listEntries({ environmentId, input: { cwd, directoryPath } });
33 + const detached = detachedRequests.current.get(directoryPath);
34 const request = (async () => {
35 + // A shared query still waiting would join the removed folder's read.
36 + if (detached) await detached;
37 if (running.current >= 4) await new Promise<void>((resolve) => waiting.current.push(resolve));
38 else running.current++;
39 const result = active.current
@@ -43,21 +49,44 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
49 return result;
50 })()
51 .then((result) => {
46 - if (!active.current || !result) return;
52 + // A request is dropped from `requests` when its folder disappears, so its read is stale.
53 + if (!active.current || !result || requests.current.get(directoryPath) !== request) return;
54 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 - ),
55 + const children = result.value.entries.filter(
56 + (entry) =>
57 + entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
58 + );
59 + const next = new Map(listings.current).set(directoryPath, children);
60 + const childDirectories = new Set(
61 + children.filter((entry) => entry.kind === "directory").map((entry) => entry.path),
62 );
63 + const prefix = directoryPath === "" ? "" : `${directoryPath}/`;
64 + const removed = [...requested.current].filter((path) => {
65 + if (path === directoryPath || !path.startsWith(prefix)) return false;
66 + const slash = path.indexOf("/", prefix.length);
67 + return !childDirectories.has(slash === -1 ? path : path.slice(0, slash));
68 + });
69 + for (const path of removed) {
70 + next.delete(path);
71 + loaded.current.delete(path);
72 + requested.current.delete(path);
73 + const detachedRequest = requests.current.get(path);
74 + if (detachedRequest) detachedRequests.current.set(path, detachedRequest);
75 + requests.current.delete(path);
76 + followUpRefreshes.current.delete(path);
77 + }
78 + listings.current = next;
79 + setDirectories(next);
80 + if (removed.length > 0)
81 + setLoadingDirectories((previous) => {
82 + const remaining = new Set(previous);
83 + for (const path of removed) remaining.delete(path);
84 + return remaining;
85 + });
86 setErrors((previous) => {
58 - const next = new Map(previous);
59 - next.delete(directoryPath);
60 - return next;
87 + const remaining = new Map(previous);
88 + for (const path of [directoryPath, ...removed]) remaining.delete(path);
89 + return remaining;
90 });
91 } else {
92 loaded.current.delete(directoryPath);
@@ -71,10 +100,13 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
100 }
101 })
102 .finally(() => {
74 - requests.current.delete(directoryPath);
103 + if (detachedRequests.current.get(directoryPath) === request)
104 + detachedRequests.current.delete(directoryPath);
105 + const current = requests.current.get(directoryPath) === request;
106 + if (current) requests.current.delete(directoryPath);
107 if (!active.current) return;
108 setPending((count) => count - 1);
77 - if (!refresh)
109 + if (!refresh && current)
110 setLoadingDirectories((previous) => {
111 const next = new Set(previous);
112 next.delete(directoryPath);
@@ -92,8 +124,10 @@ export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
124 let followUp = followUpRefreshes.current.get(directoryPath);
125 if (!followUp) {
126 followUp = existing.then(() => {
127 + if (followUpRefreshes.current.get(directoryPath) !== followUp) return;
128 followUpRefreshes.current.delete(directoryPath);
96 - if (active.current) return loadDirectory(directoryPath, true);
129 + if (active.current && requested.current.has(directoryPath))
130 + return loadDirectory(directoryPath, true);
131 });
132 followUpRefreshes.current.set(directoryPath, followUp);
133 }