Skip to content
TypeScript

perf(web): branch toolbar label measuring reads its state through useEffectEvent (#20467)

Julius Marminge10d4fab2941f
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index aa87a56659e..85542f1a7a6 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -42,4 +42,5 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/hooks/useLiveRefresh.ts",
"apps/web/src/browser/useBrowserViewportResize.ts",
"apps/web/src/components/desktop/SnapShotCoordinator.tsx",
+ "apps/web/src/components/BranchToolbar.tsx",
] as const;
diff --git a/apps/web/src/components/BranchToolbar.tsx b/apps/web/src/components/BranchToolbar.tsx
index 9d1ffe2c5e9..3a9f2e1b116 100644
--- a/apps/web/src/components/BranchToolbar.tsx
+++ b/apps/web/src/components/BranchToolbar.tsx
@@ -16,10 +16,9 @@ import {
type MouseEvent as ReactMouseEvent,
memo,
useImperativeHandle,
- useCallback,
useEffect,
+ useEffectEvent,
useLayoutEffect,
- useMemo,
useRef,
useState,
} from "react";
@@ -179,10 +178,8 @@ const RunContextSelector = memo(function RunContextSelector({
onUsePreviousWorktree,
}: RunContextSelectorProps) {
const composerFloatingLayerProps = useComposerMenuProps();
- const activeEnvironment = useMemo(
- () => availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null,
- [availableEnvironments, environmentId],
- );
+ const activeEnvironment =
+ availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null;
const WorkspaceIcon =
effectiveEnvMode === "worktree"
? FolderGit2Icon
@@ -425,14 +422,9 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
const [overflows, setOverflows] = useState(false);
const pendingLabelRectsRef = useRef<Map<HTMLElement, DOMRect> | null>(null);
const labelAnimationsRef = useRef(new Map<HTMLElement, Animation>());
- // A render-synced mirror instead of useEffectEvent: the compiler memoizes
- // the event callback, which left observers reading the first render's null
- // element forever.
- const stateRef = useRef({ element, overflows });
- stateRef.current = { element, overflows };
-
- const measure = useCallback(() => {
- const { element: current, overflows: compact } = stateRef.current;
+ const measure = useEffectEvent(() => {
+ const current = element;
+ const compact = overflows;
if (!current) return;
const available = current.clientWidth;
if (available === 0) return;
@@ -495,7 +487,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
);
}
setOverflows(nextOverflows);
- }, []);
+ });
useLayoutEffect(() => {
const previousRects = pendingLabelRectsRef.current;
@@ -536,7 +528,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
{ once: true },
);
}
- }, [overflows]);
+ });
useEffect(
() => () => {
@@ -548,6 +540,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
);
useLayoutEffect(() => {
+ // oxlint-disable-next-line react/set-state-in-effect -- The label mode comes from measuring the committed DOM, which render cannot read.
measure();
});
@@ -559,7 +552,7 @@ function useLabelsOverflow(element: HTMLDivElement | null): boolean {
stopObserving();
document.fonts.removeEventListener("loadingdone", measure);
};
- }, [element, measure]);
+ }, [element]);
return overflows;
}
@@ -590,10 +583,7 @@ export const BranchToolbar = memo(function BranchToolbar({
contextStripVisible = true,
}: BranchToolbarProps) {
const branchSelectorRef = useRef<BranchToolbarBranchSelectorHandle>(null);
- const threadRef = useMemo(
- () => scopeThreadRef(environmentId, threadId),
- [environmentId, threadId],
- );
+ const threadRef = scopeThreadRef(environmentId, threadId);
const draftThread = useComposerDraftStore((store) =>
draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
);
@@ -614,25 +604,19 @@ export const BranchToolbar = memo(function BranchToolbar({
const canUsePreviousWorktree =
draftThread !== null && serverThread === null && !envModeLocked && !forceNewWorktree;
- const projectRefsForWorktreeLookup = useMemo(
- () => (canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : []),
- [canUsePreviousWorktree, activeProjectRef],
- );
+ const projectRefsForWorktreeLookup =
+ canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : [];
const projectThreads = useThreadShellsForProjectRefs(projectRefsForWorktreeLookup);
- const previousWorktreeSeed = useMemo(
- () =>
- canUsePreviousWorktree
- ? resolvePreviousWorktreeSeed({
- threads: projectThreads,
- currentWorktreePath: activeWorktreePath,
- })
- : null,
- [activeWorktreePath, canUsePreviousWorktree, projectThreads],
- );
+ const previousWorktreeSeed = canUsePreviousWorktree
+ ? resolvePreviousWorktreeSeed({
+ threads: projectThreads,
+ currentWorktreePath: activeWorktreePath,
+ })
+ : null;
const previousWorktreeLabel = previousWorktreeSeed
? resolvePreviousWorktreeLabel(previousWorktreeSeed)
: null;
- const onUsePreviousWorktree = useCallback(() => {
+ const onUsePreviousWorktree = () => {
if (!previousWorktreeSeed || !activeProjectRef) return;
setDraftThreadContext(draftId ?? threadRef, {
branch: previousWorktreeSeed.branch,
@@ -640,7 +624,7 @@ export const BranchToolbar = memo(function BranchToolbar({
envMode: "worktree",
projectRef: activeProjectRef,
});
- }, [activeProjectRef, draftId, previousWorktreeSeed, setDraftThreadContext, threadRef]);
+ };
useImperativeHandle(
ref,