Skip to content

Commit 9754e11

Browse files
perf(web): branch toolbar label measuring reads its state through useEffectEvent
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2522ecf commit 9754e11

2 files changed

Lines changed: 22 additions & 37 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ export const REACT_COMPILED_FILES = [
4242
"apps/web/src/hooks/useLiveRefresh.ts",
4343
"apps/web/src/browser/useBrowserViewportResize.ts",
4444
"apps/web/src/components/desktop/SnapShotCoordinator.tsx",
45+
"apps/web/src/components/BranchToolbar.tsx",
4546
] as const;

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

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import {
1616
type MouseEvent as ReactMouseEvent,
1717
memo,
1818
useImperativeHandle,
19-
useCallback,
2019
useEffect,
20+
useEffectEvent,
2121
useLayoutEffect,
22-
useMemo,
2322
useRef,
2423
useState,
2524
} from "react";
@@ -179,10 +178,8 @@ const RunContextSelector = memo(function RunContextSelector({
179178
onUsePreviousWorktree,
180179
}: RunContextSelectorProps) {
181180
const composerFloatingLayerProps = useComposerMenuProps();
182-
const activeEnvironment = useMemo(
183-
() => availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null,
184-
[availableEnvironments, environmentId],
185-
);
181+
const activeEnvironment =
182+
availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null;
186183
const WorkspaceIcon =
187184
effectiveEnvMode === "worktree"
188185
? FolderGit2Icon
@@ -425,14 +422,9 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
425422
const [overflows, setOverflows] = useState(false);
426423
const pendingLabelRectsRef = useRef<Map<HTMLElement, DOMRect> | null>(null);
427424
const labelAnimationsRef = useRef(new Map<HTMLElement, Animation>());
428-
// A render-synced mirror instead of useEffectEvent: the compiler memoizes
429-
// the event callback, which left observers reading the first render's null
430-
// element forever.
431-
const stateRef = useRef({ element, overflows });
432-
stateRef.current = { element, overflows };
433-
434-
const measure = useCallback(() => {
435-
const { element: current, overflows: compact } = stateRef.current;
425+
const measure = useEffectEvent(() => {
426+
const current = element;
427+
const compact = overflows;
436428
if (!current) return;
437429
const available = current.clientWidth;
438430
if (available === 0) return;
@@ -495,7 +487,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
495487
);
496488
}
497489
setOverflows(nextOverflows);
498-
}, []);
490+
});
499491

500492
useLayoutEffect(() => {
501493
const previousRects = pendingLabelRectsRef.current;
@@ -536,7 +528,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
536528
{ once: true },
537529
);
538530
}
539-
}, [overflows]);
531+
});
540532

541533
useEffect(
542534
() => () => {
@@ -548,6 +540,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
548540
);
549541

550542
useLayoutEffect(() => {
543+
// oxlint-disable-next-line react/set-state-in-effect -- The label mode comes from measuring the committed DOM, which render cannot read.
551544
measure();
552545
});
553546

@@ -559,7 +552,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
559552
stopObserving();
560553
document.fonts.removeEventListener("loadingdone", measure);
561554
};
562-
}, [element, measure]);
555+
}, [element]);
563556

564557
return overflows;
565558
}
@@ -590,10 +583,7 @@ export const BranchToolbar = memo(function BranchToolbar({
590583
contextStripVisible = true,
591584
}: BranchToolbarProps) {
592585
const branchSelectorRef = useRef<BranchToolbarBranchSelectorHandle>(null);
593-
const threadRef = useMemo(
594-
() => scopeThreadRef(environmentId, threadId),
595-
[environmentId, threadId],
596-
);
586+
const threadRef = scopeThreadRef(environmentId, threadId);
597587
const draftThread = useComposerDraftStore((store) =>
598588
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
599589
);
@@ -614,33 +604,27 @@ export const BranchToolbar = memo(function BranchToolbar({
614604

615605
const canUsePreviousWorktree =
616606
draftThread !== null && serverThread === null && !envModeLocked && !forceNewWorktree;
617-
const projectRefsForWorktreeLookup = useMemo(
618-
() => (canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : []),
619-
[canUsePreviousWorktree, activeProjectRef],
620-
);
607+
const projectRefsForWorktreeLookup =
608+
canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : [];
621609
const projectThreads = useThreadShellsForProjectRefs(projectRefsForWorktreeLookup);
622-
const previousWorktreeSeed = useMemo(
623-
() =>
624-
canUsePreviousWorktree
625-
? resolvePreviousWorktreeSeed({
626-
threads: projectThreads,
627-
currentWorktreePath: activeWorktreePath,
628-
})
629-
: null,
630-
[activeWorktreePath, canUsePreviousWorktree, projectThreads],
631-
);
610+
const previousWorktreeSeed = canUsePreviousWorktree
611+
? resolvePreviousWorktreeSeed({
612+
threads: projectThreads,
613+
currentWorktreePath: activeWorktreePath,
614+
})
615+
: null;
632616
const previousWorktreeLabel = previousWorktreeSeed
633617
? resolvePreviousWorktreeLabel(previousWorktreeSeed)
634618
: null;
635-
const onUsePreviousWorktree = useCallback(() => {
619+
const onUsePreviousWorktree = () => {
636620
if (!previousWorktreeSeed || !activeProjectRef) return;
637621
setDraftThreadContext(draftId ?? threadRef, {
638622
branch: previousWorktreeSeed.branch,
639623
worktreePath: previousWorktreeSeed.worktreePath,
640624
envMode: "worktree",
641625
projectRef: activeProjectRef,
642626
});
643-
}, [activeProjectRef, draftId, previousWorktreeSeed, setDraftThreadContext, threadRef]);
627+
};
644628

645629
useImperativeHandle(
646630
ref,

0 commit comments

Comments
 (0)