The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the branch toolbar selector compiles without manual memoization (#20567)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:06 UTC
7383cce7fc07a2dce9fa8d907f82f2acd69e5c37
1 file changed
+104
-125
apps/web/src/components/BranchToolbarBranchSelector.tsx
+104
-125
@@ -18,11 +18,9 @@ import {
18
} from "@t3tools/contracts";
19
import { ChevronDownIcon, GitBranchIcon } from "lucide-react";
20
import {
21
- useCallback,
21
useDeferredValue,
22
useEffect,
23
useImperativeHandle,
25
- useMemo,
24
useOptimistic,
25
useState,
26
useTransition,
@@ -97,6 +95,39 @@ function toBranchActionErrorMessage(error: unknown): string {
95
return error instanceof Error ? error.message : "An error occurred.";
96
}
97
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
+
131
export function BranchToolbarBranchSelector({
132
forceNewWorktree = false,
133
ref,
@@ -131,10 +162,7 @@ export function BranchToolbarBranchSelector({
162
const createRefMutation = useAtomCommand(vcsEnvironment.createRef, {
163
reportFailure: false,
164
});
134
- const threadRef = useMemo(
135
- () => scopeThreadRef(environmentId, threadId),
136
- [environmentId, threadId],
137
- );
165
+ const threadRef = scopeThreadRef(environmentId, threadId);
166
const serverThread = useThreadShell(threadRef);
167
const serverSession = serverThread?.runtime ?? null;
168
const draftThread = useComposerDraftStore((store) =>
@@ -170,66 +198,51 @@ export function BranchToolbarBranchSelector({
198
draftThreadEnvMode: draftThread?.envMode,
199
});
200
173
- const setThreadBranch = useCallback(
174
- (branch: string | null, worktreePath: string | null, automatic = false) => {
175
- if (
176
- !activeThreadId ||
177
- !activeProject ||
178
- (hasServerThread && !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope))
179
- )
180
- return;
181
- if (serverSession && worktreePath !== activeWorktreePath) {
182
- void stopThreadSession({
183
- environmentId,
184
- input: { threadId: activeThreadId },
185
- });
186
- }
187
- if (hasServerThread) {
188
- void (automatic ? updateEmptyThreadMetadata : updateThreadMetadata)({
189
- environmentId,
190
- input: {
191
- threadId: activeThreadId,
192
- branch,
193
- worktreePath,
194
- ...(automatic ? { expectedEmpty: true } : {}),
195
- },
196
- });
197
- }
198
- if (hasServerThread) {
199
- onActiveThreadBranchOverrideChange?.(branch);
200
- return;
201
- }
202
- const nextDraftEnvMode = resolveDraftEnvModeAfterBranchChange({
203
- nextWorktreePath: worktreePath,
204
- currentWorktreePath: activeWorktreePath,
205
- effectiveEnvMode,
201
+ const setThreadBranch = (
202
+ branch: string | null,
203
+ worktreePath: string | null,
204
+ automatic = false,
205
+ ) => {
206
+ if (
207
+ !activeThreadId ||
208
+ !activeProject ||
209
+ (hasServerThread && !readEnvironmentScope(environmentId, AuthOrchestrationOperateScope))
210
+ )
211
+ return;
212
+ if (serverSession && worktreePath !== activeWorktreePath) {
213
+ void stopThreadSession({
214
+ environmentId,
215
+ input: { threadId: activeThreadId },
216
});
207
- setDraftThreadContext(draftId ?? threadRef, {
208
- branch,
209
- worktreePath,
210
- envMode: nextDraftEnvMode,
211
- environmentSelection: automatic ? (draftThread?.environmentSelection ?? "auto") : "manual",
212
- projectRef: scopeProjectRef(environmentId, activeProject.id),
217
+ }
218
+ if (hasServerThread) {
219
+ void (automatic ? updateEmptyThreadMetadata : updateThreadMetadata)({
220
+ environmentId,
221
+ input: {
222
+ threadId: activeThreadId,
223
+ branch,
224
+ worktreePath,
225
+ ...(automatic ? { expectedEmpty: true } : {}),
226
+ },
227
});
214
- },
215
- [
216
- activeThreadId,
217
- activeProject,
218
- serverSession,
219
- activeWorktreePath,
220
- hasServerThread,
221
- onActiveThreadBranchOverrideChange,
222
- setDraftThreadContext,
223
- draftId,
224
- threadRef,
225
- environmentId,
228
+ }
229
+ if (hasServerThread) {
230
+ onActiveThreadBranchOverrideChange?.(branch);
231
+ return;
232
+ }
233
+ const nextDraftEnvMode = resolveDraftEnvModeAfterBranchChange({
234
+ nextWorktreePath: worktreePath,
235
+ currentWorktreePath: activeWorktreePath,
236
effectiveEnvMode,
227
- draftThread?.environmentSelection,
228
- stopThreadSession,
229
- updateEmptyThreadMetadata,
230
- updateThreadMetadata,
231
- ],
232
- );
237
+ });
238
+ setDraftThreadContext(draftId ?? threadRef, {
239
+ branch,
240
+ worktreePath,
241
+ envMode: nextDraftEnvMode,
242
+ environmentSelection: automatic ? (draftThread?.environmentSelection ?? "auto") : "manual",
243
+ projectRef: scopeProjectRef(environmentId, activeProject.id),
244
+ });
245
+ };
246
247
const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
248
const [branchQuery, setBranchQuery] = useState("");
@@ -262,9 +275,8 @@ export function BranchToolbarBranchSelector({
275
const isInitialBranchesLoadPending = branchRefState.isPending && branchRefState.data === null;
276
const currentGitBranch =
277
branchStatusQuery.data?.refName ?? refs.find((refName) => refName.current)?.name ?? null;
265
- const sourceControlPresentation = useMemo(
266
- () => getSourceControlPresentation(branchStatusQuery.data?.sourceControlProvider),
267
- [branchStatusQuery.data?.sourceControlProvider],
278
+ const sourceControlPresentation = getSourceControlPresentation(
279
+ branchStatusQuery.data?.sourceControlProvider,
280
);
281
const SourceControlIcon = sourceControlPresentation.Icon;
282
const canonicalActiveBranch = resolveBranchToolbarValue({
@@ -273,11 +285,7 @@ export function BranchToolbarBranchSelector({
285
activeThreadBranch,
286
currentGitBranch,
287
});
276
- const branchNames = useMemo(() => refs.map((refName) => refName.name), [refs]);
277
- const branchByName = useMemo(
278
- () => new Map(refs.map((refName) => [refName.name, refName] as const)),
279
- [refs],
280
- );
288
+ const branchByName = new Map(refs.map((refName) => [refName.name, refName] as const));
289
const normalizedDeferredBranchQuery = deferredTrimmedBranchQuery.toLowerCase();
290
const prReference = parsePullRequestReference(trimmedBranchQuery);
291
const isSelectingWorktreeBase =
@@ -293,35 +301,12 @@ export function BranchToolbarBranchSelector({
301
const createBranchItemValue = canCreateBranch
302
? `__create_new_branch__:${trimmedBranchQuery}`
303
: null;
296
- const branchPickerItems = useMemo(() => {
297
- const items = [...branchNames];
298
- if (createBranchItemValue && !hasExactBranchMatch) {
299
- items.push(createBranchItemValue);
300
- }
301
- if (checkoutPullRequestItemValue) {
302
- items.unshift(checkoutPullRequestItemValue);
303
- }
304
- return items;
305
- }, [branchNames, checkoutPullRequestItemValue, createBranchItemValue, hasExactBranchMatch]);
306
- const filteredBranchPickerItems = useMemo(
307
- () =>
308
- normalizedDeferredBranchQuery.length === 0
309
- ? branchPickerItems
310
- : branchPickerItems.filter((itemValue) =>
311
- shouldIncludeBranchPickerItem({
312
- itemValue,
313
- normalizedQuery: normalizedDeferredBranchQuery,
314
- createBranchItemValue,
315
- checkoutPullRequestItemValue,
316
- }),
317
- ),
318
- [
319
- branchPickerItems,
320
- checkoutPullRequestItemValue,
321
- createBranchItemValue,
322
- normalizedDeferredBranchQuery,
323
- ],
324
- );
304
+ const { branchPickerItems, filteredBranchPickerItems } = useBranchPickerItems({
305
+ refs,
306
+ createBranchItemValue: hasExactBranchMatch ? null : createBranchItemValue,
307
+ checkoutPullRequestItemValue,
308
+ normalizedQuery: normalizedDeferredBranchQuery,
309
+ });
310
const [resolvedActiveBranch, setOptimisticBranch] = useOptimistic(
311
canonicalActiveBranch,
312
(_currentBranch: string | null, optimisticBranch: string | null) => optimisticBranch,
@@ -359,7 +344,7 @@ export function BranchToolbarBranchSelector({
344
? `Showing ${refs.length} of ${totalBranchCount} refs`
345
: null;
346
362
- const copyBranchName = useCallback((branchName: string) => {
347
+ const copyBranchName = (branchName: string) => {
348
void writeTextToClipboard(branchName, "branch name").then(
349
(didCopy) => {
350
if (!didCopy) return;
@@ -379,24 +364,21 @@ export function BranchToolbarBranchSelector({
364
);
365
},
366
);
382
- }, []);
383
-
384
- const handleBranchContextMenu = useCallback(
385
- (event: ReactMouseEvent, branchName: string | null) => {
386
- if (!branchName) return;
387
- const api = readLocalApi();
388
- if (!api) return;
389
- event.preventDefault();
390
- event.stopPropagation();
391
- const items: ContextMenuItem<"copy-branch-name">[] = [
392
- { id: "copy-branch-name", label: "Copy branch name", icon: "copy" },
393
- ];
394
- void api.contextMenu.show(items, { x: event.clientX, y: event.clientY }).then((action) => {
395
- if (action === "copy-branch-name") copyBranchName(branchName);
396
- });
397
- },
398
- [copyBranchName],
399
- );
367
+ };
368
+
369
+ const handleBranchContextMenu = (event: ReactMouseEvent, branchName: string | null) => {
370
+ if (!branchName) return;
371
+ const api = readLocalApi();
372
+ if (!api) return;
373
+ event.preventDefault();
374
+ event.stopPropagation();
375
+ const items: ContextMenuItem<"copy-branch-name">[] = [
376
+ { id: "copy-branch-name", label: "Copy branch name", icon: "copy" },
377
+ ];
378
+ void api.contextMenu.show(items, { x: event.clientX, y: event.clientY }).then((action) => {
379
+ if (action === "copy-branch-name") copyBranchName(branchName);
380
+ });
381
+ };
382
383
const runBranchAction = (action: () => Promise<void>) => {
384
if (
@@ -517,10 +499,7 @@ export function BranchToolbarBranchSelector({
499
});
500
};
501
520
- const defaultBranchName = useMemo(
521
- () => refs.find((refName) => refName.isDefault)?.name ?? null,
522
- [refs],
523
- );
502
+ const defaultBranchName = refs.find((refName) => refName.isDefault)?.name ?? null;
503
const worktreeBaseBranchCandidate = isInitialBranchesLoadPending
504
? null
505
: (defaultBranchName ?? currentGitBranch);
@@ -549,12 +528,12 @@ export function BranchToolbarBranchSelector({
528
worktreeBaseBranchCandidate,
529
]);
530
552
- const handleOpenChange = useCallback((open: boolean) => {
531
+ const handleOpenChange = (open: boolean) => {
532
setIsBranchMenuOpen(open);
533
if (!open) {
534
setBranchQuery("");
535
}
557
- }, []);
536
+ };
537
538
useImperativeHandle(
539
ref,