Skip to content

Commit 0a3806c

Browse files
refactor(web): paginated branches keep their caching without "use memo" (pingdotgg#20743)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent fc73c24 commit 0a3806c

1 file changed

Lines changed: 15 additions & 36 deletions

File tree

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

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ export function useThreadSearch(
9797
};
9898
}
9999

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";
100+
export function usePaginatedBranches(target: VcsRefTarget) {
101+
// Starts unkeyed: a key mismatch already resolves to the initial cursors, and seeding
102+
// the key here would make every derivation below straddle this hook call.
103+
const [pagination, setPagination] = useState<{
104+
readonly targetKey: string | null;
105+
readonly cursors: ReadonlyArray<number | undefined>;
106+
}>({
107+
targetKey: null,
108+
cursors: INITIAL_BRANCH_CURSORS,
109+
});
110+
const { environmentId, cwd } = target;
111+
const query = target.query?.trim() ?? "";
112+
const targetKey =
113+
environmentId !== null && cwd !== null ? JSON.stringify([environmentId, cwd, query]) : null;
114+
const cursors = pagination.targetKey === targetKey ? pagination.cursors : INITIAL_BRANCH_CURSORS;
112115
const pageAtoms =
113116
environmentId !== null && cwd !== null
114117
? cursors.map((cursor) =>
@@ -126,30 +129,6 @@ function useBranchPageAtoms(
126129
const pagesAtom = Atom.make((get) => pageAtoms.map((atom) => get(atom))).pipe(
127130
Atom.withLabel(`web:vcs-ref-pages:${targetKey ?? "empty"}`),
128131
);
129-
return { pageAtoms, pagesAtom };
130-
}
131-
132-
export function usePaginatedBranches(target: VcsRefTarget) {
133-
const query = target.query?.trim() ?? "";
134-
const targetKey =
135-
target.environmentId !== null && target.cwd !== null
136-
? JSON.stringify([target.environmentId, target.cwd, query])
137-
: null;
138-
const [pagination, setPagination] = useState<{
139-
readonly targetKey: string | null;
140-
readonly cursors: ReadonlyArray<number | undefined>;
141-
}>({
142-
targetKey,
143-
cursors: INITIAL_BRANCH_CURSORS,
144-
});
145-
const cursors = pagination.targetKey === targetKey ? pagination.cursors : INITIAL_BRANCH_CURSORS;
146-
const { pageAtoms, pagesAtom } = useBranchPageAtoms(
147-
target.environmentId,
148-
target.cwd,
149-
query,
150-
cursors,
151-
targetKey,
152-
);
153132
const results = useAtomValue(pagesAtom);
154133
const values = results.flatMap((result) => {
155134
const value = Option.getOrNull(AsyncResult.value(result));

0 commit comments

Comments
 (0)