@devries-cameron20 / t3code Mirror · read-only / commits / a240408242
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 22, 15:45 UTC

refactor(web): file breadcrumb menus refetch on a new workspace change, not on every open (#25377)

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

Julius Marminge committed Oct 26, 2026 at 02:07 UTC a2404082424b3c9baedce6783a63d0caa35b7d26
5 files changed +205 -8
apps/web/src/components/files/FileBreadcrumbs.tsx
+6 -7
@@ -17,7 +17,6 @@ import {
17 } from "~/components/ui/menu";
18 import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
19 import { useTheme } from "~/hooks/useTheme";
20 -import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
20 import { cn } from "~/lib/utils";
21 import { isHostPath } from "@t3tools/shared/path";
22
@@ -85,12 +84,12 @@ function BreadcrumbMenuContent(props: {
84 readonly rootPath: string;
85 readonly workspaceMutationId: string | null;
86 }) {
88 - const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd, props.directoryPath);
89 - useWorkspaceMutationRefresh({
90 - mutationId: props.workspaceMutationId,
91 - refresh: entriesQuery.refresh,
92 - resourceKey: `files:${props.environmentId}:${props.cwd}`,
93 - });
87 + const entriesQuery = useProjectEntriesQuery(
88 + props.environmentId,
89 + props.cwd,
90 + props.directoryPath,
91 + props.workspaceMutationId,
92 + );
93 const { resolvedTheme } = useTheme();
94 const entries = entriesQuery.data?.entries ?? [];
95 const entriesTruncated = entriesQuery.data?.truncated ?? false;
apps/web/src/components/files/projectFilesQueryState.test.tsx
+25
@@ -161,6 +161,31 @@ describe("project query refresh", () => {
161 }
162 });
163
164 + it("refetches a listing when the workspace mutation changes, not when it is first read", () => {
165 + let fetches = 0;
166 + const listing = Atom.make(() => {
167 + fetches += 1;
168 + return AsyncResult.success({ entries: [], truncated: false });
169 + }).pipe(Atom.keepAlive);
170 + projectMocks.listEntries.mockReturnValue(listing);
171 + const registry = AtomRegistry.make();
172 + atomHooks.registry = registry;
173 + const read = (mutationId: string | null) =>
174 + useProjectEntriesQuery(environmentId, "/repo", "src", mutationId);
175 +
176 + try {
177 + read("mutation-1");
178 + read("mutation-1");
179 + expect(fetches).toBe(1);
180 +
181 + read("mutation-2");
182 + expect(fetches).toBe(2);
183 + } finally {
184 + registry.dispose();
185 + atomHooks.registry = null;
186 + }
187 + });
188 +
189 it("replaces an in-flight initial read when a workspace mutation arrives", async () => {
190 const requests: Array<ReturnType<typeof deferred<ProjectReadFileResult>>> = [];
191 const readAtom = Atom.make(
apps/web/src/components/files/projectFilesQueryState.ts
+4 -1
@@ -16,6 +16,7 @@ import { useFilesystemReadAccess } from "~/state/filesystem";
16 import { projectEnvironment } from "~/state/projects";
17 import { useProjectPathSearch } from "~/state/queries";
18 import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
19 +import { revisionedQuery } from "~/state/revisionedQuery";
20
21 const EMPTY_PROJECT_FILE_PATH = "";
22 const EMPTY_PROJECT_ENTRIES_QUERY_ATOM = Atom.make(
@@ -190,17 +191,19 @@ function errorMessage(cause: unknown): string | null {
191
192 const isProjectReadFileError = Schema.is(ProjectReadFileError);
193
194 +/** `revision` is the workspace mutation id; a new one refetches the listing. */
195 export function useProjectEntriesQuery(
196 environmentId: EnvironmentId,
197 cwd: string,
198 directoryPath?: string,
199 + revision?: string | null,
200 ): ProjectQueryState<ProjectListEntriesResult> {
201 const fileAccess = useFilesystemReadAccess(environmentId);
202 const { canReadFiles } = fileAccess;
203 const atom = canReadFiles
204 ? getProjectEntriesQueryAtom(environmentId, cwd, directoryPath)
205 : EMPTY_PROJECT_ENTRIES_QUERY_ATOM;
203 - const result = useAtomValue(atom);
206 + const result = useAtomValue(revisionedQuery(atom, canReadFiles ? revision : undefined));
207 const refreshAtom = useAtomRefresh(atom);
208 const refresh = () => refreshAtom();
209 return {
apps/web/src/state/revisionedQuery.test.ts new
+104
@@ -0,0 +1,104 @@
1 +import { Atom, AtomRegistry } from "effect/reactivity";
2 +import { describe, expect, it } from "vite-plus/test";
3 +
4 +import { revisionedQuery } from "./revisionedQuery";
5 +
6 +// A stand-in query that counts its fetches.
7 +function setup() {
8 + let fetches = 0;
9 + // Node removal runs on the registry's scheduler; releasing flushes it so the
10 + // next mount of the same revision builds a fresh node, as a React remount does.
11 + const scheduled: Array<() => void> = [];
12 + const registry = AtomRegistry.make({
13 + scheduleTask: (task) => {
14 + scheduled.push(task);
15 + return () => {};
16 + },
17 + });
18 + const query = Atom.make(() => {
19 + fetches += 1;
20 + return fetches;
21 + }).pipe(Atom.keepAlive);
22 + const mount = (revision: string | null | undefined) => {
23 + const release = registry.mount(revisionedQuery(query, revision));
24 + return () => {
25 + release();
26 + for (const task of scheduled.splice(0)) task();
27 + };
28 + };
29 + return { registry, query, mount, fetches: () => fetches };
30 +}
31 +
32 +describe("revisionedQuery", () => {
33 + it("refetches once per new revision, not on the first read or a remount", () => {
34 + const { mount, fetches } = setup();
35 +
36 + mount("mutation-1")();
37 + expect(fetches()).toBe(1);
38 +
39 + mount("mutation-1")();
40 + expect(fetches()).toBe(1);
41 +
42 + mount("mutation-2")();
43 + expect(fetches()).toBe(2);
44 + });
45 +
46 + it("does not ping-pong while readers of the old and new revision overlap", () => {
47 + const { mount, fetches } = setup();
48 +
49 + const releaseOld = mount("mutation-1");
50 + const releaseNew = mount("mutation-2");
51 + expect(fetches()).toBe(2);
52 + releaseOld();
53 + releaseNew();
54 + });
55 +
56 + it("refetches when an earlier revision is read again", () => {
57 + const { mount, fetches } = setup();
58 +
59 + mount("mutation-1")();
60 + mount("mutation-2")();
61 + mount("mutation-1")();
62 + expect(fetches()).toBe(3);
63 + });
64 +
65 + it("refetches a query another reader already cached on its first revisioned read", () => {
66 + const { mount, fetches } = setup();
67 +
68 + mount(undefined)();
69 + expect(fetches()).toBe(1);
70 +
71 + mount("mutation-1")();
72 + expect(fetches()).toBe(2);
73 +
74 + mount("mutation-1")();
75 + expect(fetches()).toBe(2);
76 + });
77 +
78 + it("does not refetch a cached query on a first read before any workspace change", () => {
79 + const { mount, fetches } = setup();
80 +
81 + mount(undefined)();
82 + mount(null)();
83 + expect(fetches()).toBe(1);
84 + });
85 +
86 + it("leaves the query alone when read without a revision", () => {
87 + const { mount, query, fetches } = setup();
88 +
89 + mount("mutation-1")();
90 + expect(revisionedQuery(query, undefined)).toBe(query);
91 + mount(undefined)();
92 + mount("mutation-1")();
93 + expect(fetches()).toBe(1);
94 + });
95 +
96 + it("refetches the query when the reader is refreshed", () => {
97 + const { registry, query, mount, fetches } = setup();
98 +
99 + const release = mount("mutation-1");
100 + registry.refresh(revisionedQuery(query, "mutation-1"));
101 + expect(fetches()).toBe(2);
102 + release();
103 + });
104 +});
apps/web/src/state/revisionedQuery.ts new
+66
@@ -0,0 +1,66 @@
1 +import * as Option from "effect/Option";
2 +import { Atom, type AtomRegistry } from "effect/reactivity";
3 +
4 +// Outside the nodes: a node is dropped once unmounted, and the next read must
5 +// still compare against the revision the query was last read under.
6 +const readRevisions = new WeakMap<AtomRegistry.AtomRegistry, WeakMap<object, string | null>>();
7 +
8 +// The registry keys serializable atoms' nodes by their key, not the atom.
9 +const queryKey = (query: Atom.Atom<unknown>) =>
10 + Atom.isSerializable(query) ? query[Atom.SerializableTypeId].key : query;
11 +
12 +const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
13 + Atom.family((revision: string | null) =>
14 + Atom.readable(
15 + (get) => {
16 + // Only a node's first build compares: a node still mounted under the old
17 + // revision rebuilds when the query refetches, and must not refetch it again.
18 + if (Option.isNone(get.self())) {
19 + let revisions = readRevisions.get(get.registry);
20 + if (revisions === undefined) {
21 + revisions = new WeakMap();
22 + readRevisions.set(get.registry, revisions);
23 + }
24 + const stale = revisions.has(query)
25 + ? revisions.get(query) !== revision
26 + : // A reader without a revision may have cached it before the latest
27 + // workspace change; a query nobody has read yet fetches fresh anyway.
28 + revision !== null && get.registry.getNodes().has(queryKey(query));
29 + if (stale) get.refresh(query);
30 + revisions.set(query, revision);
31 + }
32 + return get(query);
33 + },
34 + // Refreshing the reader refetches the query it reads.
35 + (refresh) => refresh(query),
36 + ),
37 + );
38 +
39 +// Held as long as the query lives, so one (query, revision) pair keeps one atom; an
40 +// `Atom.family` would hold the inner family only weakly and could swap the atom under a reader.
41 +const revisionedFamilies = new WeakMap<
42 + Atom.Atom<unknown>,
43 + ReturnType<typeof makeRevisionedFamily>
44 +>();
45 +const revisionedFamily = (query: Atom.Atom<unknown>) => {
46 + let family = revisionedFamilies.get(query);
47 + if (family === undefined) {
48 + family = makeRevisionedFamily(query);
49 + revisionedFamilies.set(query, family);
50 + }
51 + return family;
52 +};
53 +
54 +/**
55 + * Reads `query` under a workspace revision (latest mutation id); a different
56 + * revision than the last read refetches it. The first read records the revision,
57 + * and refetches only a query that a reader without a revision already cached.
58 + * `undefined` reads without taking part, for readers that must not refetch now.
59 + */
60 +export function revisionedQuery<A>(
61 + query: Atom.Atom<A>,
62 + revision: string | null | undefined,
63 +): Atom.Atom<A> {
64 + if (revision === undefined) return query;
65 + return revisionedFamily(query)(revision) as Atom.Atom<A>;
66 +}