Skip to content

Commit 2adc528

Browse files
perf(web): the branch toolbar selector compiles without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a702563 commit 2adc528

1 file changed

Lines changed: 104 additions & 125 deletions

File tree

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

Lines changed: 104 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ import {
1818
} from "@t3tools/contracts";
1919
import { ChevronDownIcon, GitBranchIcon } from "lucide-react";
2020
import {
21-
useCallback,
2221
useDeferredValue,
2322
useEffect,
2423
useImperativeHandle,
25-
useMemo,
2624
useOptimistic,
2725
useState,
2826
useTransition,
@@ -97,6 +95,39 @@ function toBranchActionErrorMessage(error: unknown): string {
9795
return error instanceof Error ? error.message : "An error occurred.";
9896
}
9997

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+
100131
export function BranchToolbarBranchSelector({
101132
forceNewWorktree = false,
102133
ref,
@@ -131,10 +162,7 @@ export function BranchToolbarBranchSelector({
131162
const createRefMutation = useAtomCommand(vcsEnvironment.createRef, {
132163
reportFailure: false,
133164
});
134-
const threadRef = useMemo(
135-
() => scopeThreadRef(environmentId, threadId),
136-
[environmentId, threadId],
137-
);
165+
const threadRef = scopeThreadRef(environmentId, threadId);
138166
const serverThread = useThreadShell(threadRef);
139167
const serverSession = serverThread?.runtime ?? null;
140168
const draftThread = useComposerDraftStore((store) =>
@@ -170,66 +198,51 @@ export function BranchToolbarBranchSelector({
170198
draftThreadEnvMode: draftThread?.envMode,
171199
});
172200

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 },
206216
});
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+
},
213227
});
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,
226236
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+
};
233246

234247
const [isBranchMenuOpen, setIsBranchMenuOpen] = useState(false);
235248
const [branchQuery, setBranchQuery] = useState("");
@@ -262,9 +275,8 @@ export function BranchToolbarBranchSelector({
262275
const isInitialBranchesLoadPending = branchRefState.isPending && branchRefState.data === null;
263276
const currentGitBranch =
264277
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,
268280
);
269281
const SourceControlIcon = sourceControlPresentation.Icon;
270282
const canonicalActiveBranch = resolveBranchToolbarValue({
@@ -273,11 +285,7 @@ export function BranchToolbarBranchSelector({
273285
activeThreadBranch,
274286
currentGitBranch,
275287
});
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));
281289
const normalizedDeferredBranchQuery = deferredTrimmedBranchQuery.toLowerCase();
282290
const prReference = parsePullRequestReference(trimmedBranchQuery);
283291
const isSelectingWorktreeBase =
@@ -293,35 +301,12 @@ export function BranchToolbarBranchSelector({
293301
const createBranchItemValue = canCreateBranch
294302
? `__create_new_branch__:${trimmedBranchQuery}`
295303
: 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+
});
325310
const [resolvedActiveBranch, setOptimisticBranch] = useOptimistic(
326311
canonicalActiveBranch,
327312
(_currentBranch: string | null, optimisticBranch: string | null) => optimisticBranch,
@@ -359,7 +344,7 @@ export function BranchToolbarBranchSelector({
359344
? `Showing ${refs.length} of ${totalBranchCount} refs`
360345
: null;
361346

362-
const copyBranchName = useCallback((branchName: string) => {
347+
const copyBranchName = (branchName: string) => {
363348
void writeTextToClipboard(branchName, "branch name").then(
364349
(didCopy) => {
365350
if (!didCopy) return;
@@ -379,24 +364,21 @@ export function BranchToolbarBranchSelector({
379364
);
380365
},
381366
);
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+
};
400382

401383
const runBranchAction = (action: () => Promise<void>) => {
402384
if (
@@ -517,10 +499,7 @@ export function BranchToolbarBranchSelector({
517499
});
518500
};
519501

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;
524503
const worktreeBaseBranchCandidate = isInitialBranchesLoadPending
525504
? null
526505
: (defaultBranchName ?? currentGitBranch);
@@ -549,12 +528,12 @@ export function BranchToolbarBranchSelector({
549528
worktreeBaseBranchCandidate,
550529
]);
551530

552-
const handleOpenChange = useCallback((open: boolean) => {
531+
const handleOpenChange = (open: boolean) => {
553532
setIsBranchMenuOpen(open);
554533
if (!open) {
555534
setBranchQuery("");
556535
}
557-
}, []);
536+
};
558537

559538
useImperativeHandle(
560539
ref,

0 commit comments

Comments
 (0)