Skip to content

Commit bf50da4

Browse files
perf(web): query hooks compile with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ebf5ab7 commit bf50da4

2 files changed

Lines changed: 57 additions & 45 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export const REACT_COMPILED_FILES = [
3030
"apps/web/src/assets/assetUrls.ts",
3131
"apps/web/src/components/ui/toggle-group.tsx",
3232
"apps/web/src/components/ThreadNotificationCoordinator.tsx",
33+
"apps/web/src/state/queries.ts",
3334
] as const;

‎apps/web/src/state/queries.ts‎

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type {
2222
import * as Cause from "effect/Cause";
2323
import * as Option from "effect/Option";
2424
import { AsyncResult, Atom } from "effect/reactivity";
25-
import { useCallback, useEffect, useMemo, useState } from "react";
25+
import { useEffect, useState } from "react";
2626

2727
import { appAtomRegistry } from "../rpc/atomRegistry";
2828
import { useFilesystemReadAccess } from "./filesystem";
@@ -84,10 +84,8 @@ export function useThreadSearch(
8484
const debouncedQuery = useDebouncedValue(normalizedQuery, THREAD_SEARCH_DEBOUNCE_MS);
8585
const canSearch = environmentIds.length > 0 && normalizedQuery.length >= 2;
8686
const settledQuery = canSearch && normalizedQuery === debouncedQuery ? debouncedQuery : null;
87-
const searchKey = useMemo(
88-
() => (settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery)),
89-
[environmentIds, settledQuery],
90-
);
87+
const searchKey =
88+
settledQuery === null ? null : makeThreadSearchKey(environmentIds, settledQuery);
9189
const result = useAtomValue(
9290
searchKey === null ? EMPTY_THREAD_SEARCH_ATOM : threadSearchResultsAtom(searchKey),
9391
);
@@ -99,6 +97,38 @@ export function useThreadSearch(
9997
};
10098
}
10199

100+
// A hook so its result is frozen: `refresh` hands a page atom to the registry,
101+
// which the compiler would treat as a mutation and leave pagesAtom uncached,
102+
// resubscribing every render.
103+
function useBranchPageAtoms(
104+
environmentId: EnvironmentId | null,
105+
cwd: string | null,
106+
query: string,
107+
cursors: ReadonlyArray<number | undefined>,
108+
targetKey: string | null,
109+
) {
110+
// Calls no hooks, so the compiler needs the directive to memoize it.
111+
"use memo";
112+
const pageAtoms =
113+
environmentId !== null && cwd !== null
114+
? cursors.map((cursor) =>
115+
vcsEnvironment.listRefs({
116+
environmentId,
117+
input: {
118+
cwd,
119+
...(query.length > 0 ? { query } : {}),
120+
...(cursor === undefined ? {} : { cursor }),
121+
limit: VCS_REF_LIST_LIMIT,
122+
},
123+
}),
124+
)
125+
: [];
126+
const pagesAtom = Atom.make((get) => pageAtoms.map((atom) => get(atom))).pipe(
127+
Atom.withLabel(`web:vcs-ref-pages:${targetKey ?? "empty"}`),
128+
);
129+
return { pageAtoms, pagesAtom };
130+
}
131+
102132
export function usePaginatedBranches(target: VcsRefTarget) {
103133
const query = target.query?.trim() ?? "";
104134
const targetKey =
@@ -113,29 +143,12 @@ export function usePaginatedBranches(target: VcsRefTarget) {
113143
cursors: INITIAL_BRANCH_CURSORS,
114144
});
115145
const cursors = pagination.targetKey === targetKey ? pagination.cursors : INITIAL_BRANCH_CURSORS;
116-
const pageAtoms = useMemo(
117-
() =>
118-
target.environmentId !== null && target.cwd !== null
119-
? cursors.map((cursor) =>
120-
vcsEnvironment.listRefs({
121-
environmentId: target.environmentId!,
122-
input: {
123-
cwd: target.cwd!,
124-
...(query.length > 0 ? { query } : {}),
125-
...(cursor === undefined ? {} : { cursor }),
126-
limit: VCS_REF_LIST_LIMIT,
127-
},
128-
}),
129-
)
130-
: [],
131-
[cursors, query, target.cwd, target.environmentId],
132-
);
133-
const pagesAtom = useMemo(
134-
() =>
135-
Atom.make((get) => pageAtoms.map((atom) => get(atom))).pipe(
136-
Atom.withLabel(`web:vcs-ref-pages:${targetKey ?? "empty"}`),
137-
),
138-
[pageAtoms, targetKey],
146+
const { pageAtoms, pagesAtom } = useBranchPageAtoms(
147+
target.environmentId,
148+
target.cwd,
149+
query,
150+
cursors,
151+
targetKey,
139152
);
140153
const results = useAtomValue(pagesAtom);
141154
const values = results.flatMap((result) => {
@@ -171,25 +184,26 @@ export function usePaginatedBranches(target: VcsRefTarget) {
171184
: "Failed to load refs.";
172185
})()
173186
: null;
174-
const refresh = useCallback(() => {
187+
const refresh = () => {
175188
const firstPage = pageAtoms[0];
176189
setPagination({ targetKey, cursors: INITIAL_BRANCH_CURSORS });
177190
if (firstPage !== undefined) {
178191
appAtomRegistry.refresh(firstPage);
179192
}
180-
}, [pageAtoms, targetKey]);
181-
const loadNext = useCallback(() => {
182-
if (targetKey === null || data?.nextCursor === null || data?.nextCursor === undefined) {
193+
};
194+
const nextCursor = data?.nextCursor;
195+
const loadNext = () => {
196+
if (targetKey === null || nextCursor === null || nextCursor === undefined) {
183197
return;
184198
}
185199
setPagination((current) => {
186200
const currentCursors =
187201
current.targetKey === targetKey ? current.cursors : INITIAL_BRANCH_CURSORS;
188-
return currentCursors.includes(data.nextCursor!)
202+
return currentCursors.includes(nextCursor)
189203
? { targetKey, cursors: currentCursors }
190-
: { targetKey, cursors: [...currentCursors, data.nextCursor!] };
204+
: { targetKey, cursors: [...currentCursors, nextCursor] };
191205
});
192-
}, [data?.nextCursor, targetKey]);
206+
};
193207

194208
return {
195209
data,
@@ -226,16 +240,13 @@ export function useProjectPathSearch(
226240
options?: { readonly allowEmptyQuery?: boolean },
227241
) {
228242
const allowEmptyQuery = options?.allowEmptyQuery === true;
229-
const normalizedTarget = useMemo(
230-
() => ({
231-
environmentId: target.environmentId,
232-
cwd: target.cwd,
233-
query: target.query == null ? null : target.query.trim(),
234-
kind: target.kind,
235-
imageOnly: target.imageOnly,
236-
}),
237-
[target.cwd, target.environmentId, target.imageOnly, target.kind, target.query],
238-
);
243+
const normalizedTarget = {
244+
environmentId: target.environmentId,
245+
cwd: target.cwd,
246+
query: target.query == null ? null : target.query.trim(),
247+
kind: target.kind,
248+
imageOnly: target.imageOnly,
249+
};
239250
const debouncedTarget = useDebouncedValue(normalizedTarget, PROJECT_PATH_SEARCH_DEBOUNCE_MS);
240251
const fileAccess = useFilesystemReadAccess(debouncedTarget.environmentId);
241252
const { canReadFiles } = fileAccess;

0 commit comments

Comments
 (0)