Skip to content

Commit 767da3b

Browse files
refactor(web): the branch selector keeps its caching without "use memo" (#20822)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 8eddd16 commit 767da3b

1 file changed

Lines changed: 40 additions & 63 deletions

File tree

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -95,39 +95,6 @@ function toBranchActionErrorMessage(error: unknown): string {
9595
return error instanceof Error ? error.message : "An error occurred.";
9696
}
9797

98-
// The picker's list keys on these arrays, so they stay cached across the
99-
// queries and transitions the selector calls after computing them.
100-
function useBranchPickerItems({
101-
refs,
102-
createBranchItemValue,
103-
checkoutPullRequestItemValue,
104-
normalizedQuery,
105-
}: {
106-
refs: ReadonlyArray<VcsRef>;
107-
createBranchItemValue: string | null;
108-
checkoutPullRequestItemValue: string | null;
109-
normalizedQuery: string;
110-
}) {
111-
"use memo";
112-
const branchPickerItems = [
113-
...(checkoutPullRequestItemValue ? [checkoutPullRequestItemValue] : []),
114-
...refs.map((refName) => refName.name),
115-
...(createBranchItemValue ? [createBranchItemValue] : []),
116-
];
117-
const filteredBranchPickerItems =
118-
normalizedQuery.length === 0
119-
? branchPickerItems
120-
: branchPickerItems.filter((itemValue) =>
121-
shouldIncludeBranchPickerItem({
122-
itemValue,
123-
normalizedQuery,
124-
createBranchItemValue,
125-
checkoutPullRequestItemValue,
126-
}),
127-
);
128-
return { branchPickerItems, filteredBranchPickerItems };
129-
}
130-
13198
export function BranchToolbarBranchSelector({
13299
forceNewWorktree = false,
133100
ref,
@@ -256,35 +223,18 @@ export function BranchToolbarBranchSelector({
256223
input: { cwd: branchCwd },
257224
}),
258225
);
259-
const trimmedBranchQuery = branchQuery.trim();
260-
const deferredTrimmedBranchQuery = deferredBranchQuery.trim();
261-
// The server filters refs by substring, so it has to be given the sanitized
262-
// name as well: querying the raw "new branch" drops an existing new-branch
263-
// from the response entirely, which would defeat the collision check below.
264-
// Ref names cannot contain an ASCII space, so sanitizing loses no matches.
265-
const branchRefQuery = sanitizeNewRefName(deferredTrimmedBranchQuery);
266226
const branchRefState = usePaginatedBranches({
267227
environmentId,
268228
cwd: branchCwd,
269-
query: branchRefQuery,
229+
// The server filters refs by substring, so it has to be given the sanitized
230+
// name as well: querying the raw "new branch" drops an existing new-branch
231+
// from the response entirely, which would defeat the collision check below.
232+
// Ref names cannot contain an ASCII space, so sanitizing loses no matches.
233+
query: sanitizeNewRefName(deferredBranchQuery.trim()),
270234
});
235+
const trimmedBranchQuery = branchQuery.trim();
236+
const deferredTrimmedBranchQuery = deferredBranchQuery.trim();
271237
const refs = branchRefState.refs;
272-
const hasNextPage =
273-
branchRefState.data?.nextCursor !== null && branchRefState.data?.nextCursor !== undefined;
274-
const isFetchingNextPage = branchRefState.isFetchingNextPage;
275-
const isInitialBranchesLoadPending = branchRefState.isPending && branchRefState.data === null;
276-
const currentGitBranch =
277-
branchStatusQuery.data?.refName ?? refs.find((refName) => refName.current)?.name ?? null;
278-
const sourceControlPresentation = getSourceControlPresentation(
279-
branchStatusQuery.data?.sourceControlProvider,
280-
);
281-
const SourceControlIcon = sourceControlPresentation.Icon;
282-
const canonicalActiveBranch = resolveBranchToolbarValue({
283-
envMode: effectiveEnvMode,
284-
activeWorktreePath,
285-
activeThreadBranch,
286-
currentGitBranch,
287-
});
288238
const branchByName = new Map(refs.map((refName) => [refName.name, refName] as const));
289239
const normalizedDeferredBranchQuery = deferredTrimmedBranchQuery.toLowerCase();
290240
const prReference = parsePullRequestReference(trimmedBranchQuery);
@@ -301,11 +251,39 @@ export function BranchToolbarBranchSelector({
301251
const createBranchItemValue = canCreateBranch
302252
? `__create_new_branch__:${trimmedBranchQuery}`
303253
: null;
304-
const { branchPickerItems, filteredBranchPickerItems } = useBranchPickerItems({
305-
refs,
306-
createBranchItemValue: hasExactBranchMatch ? null : createBranchItemValue,
307-
checkoutPullRequestItemValue,
308-
normalizedQuery: normalizedDeferredBranchQuery,
254+
const defaultBranchName = refs.find((refName) => refName.isDefault)?.name ?? null;
255+
const pickerCreateBranchItemValue = hasExactBranchMatch ? null : createBranchItemValue;
256+
const branchPickerItems = [
257+
...(checkoutPullRequestItemValue ? [checkoutPullRequestItemValue] : []),
258+
...refs.map((refName) => refName.name),
259+
...(pickerCreateBranchItemValue ? [pickerCreateBranchItemValue] : []),
260+
];
261+
const filteredBranchPickerItems =
262+
normalizedDeferredBranchQuery.length === 0
263+
? branchPickerItems
264+
: branchPickerItems.filter((itemValue) =>
265+
shouldIncludeBranchPickerItem({
266+
itemValue,
267+
normalizedQuery: normalizedDeferredBranchQuery,
268+
createBranchItemValue: pickerCreateBranchItemValue,
269+
checkoutPullRequestItemValue,
270+
}),
271+
);
272+
const hasNextPage =
273+
branchRefState.data?.nextCursor !== null && branchRefState.data?.nextCursor !== undefined;
274+
const isFetchingNextPage = branchRefState.isFetchingNextPage;
275+
const isInitialBranchesLoadPending = branchRefState.isPending && branchRefState.data === null;
276+
const currentGitBranch =
277+
branchStatusQuery.data?.refName ?? refs.find((refName) => refName.current)?.name ?? null;
278+
const sourceControlPresentation = getSourceControlPresentation(
279+
branchStatusQuery.data?.sourceControlProvider,
280+
);
281+
const SourceControlIcon = sourceControlPresentation.Icon;
282+
const canonicalActiveBranch = resolveBranchToolbarValue({
283+
envMode: effectiveEnvMode,
284+
activeWorktreePath,
285+
activeThreadBranch,
286+
currentGitBranch,
309287
});
310288
const [resolvedActiveBranch, setOptimisticBranch] = useOptimistic(
311289
canonicalActiveBranch,
@@ -499,7 +477,6 @@ export function BranchToolbarBranchSelector({
499477
});
500478
};
501479

502-
const defaultBranchName = refs.find((refName) => refName.isDefault)?.name ?? null;
503480
const worktreeBaseBranchCandidate = isInitialBranchesLoadPending
504481
? null
505482
: (defaultBranchName ?? currentGitBranch);

0 commit comments

Comments
 (0)