From 5360167765ffdae2f7a08e4a6542a1392cecf829 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 26 May 2026 16:10:23 +0000 Subject: [PATCH] Reduce terminal drawer resize rerenders Co-authored-by: Julius Marminge --- .../src/components/ThreadTerminalDrawer.tsx | 103 +++++++++++------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index 6c71e5eb33..17cd06af3a 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -64,6 +64,47 @@ function clampDrawerHeight(height: number): number { return Math.min(Math.max(Math.round(safeHeight), MIN_DRAWER_HEIGHT), maxHeight); } +interface DrawerHeightDraft { + threadId: ThreadId; + sourceHeight: number; + height: number; +} + +function useThreadDrawerHeight(threadId: ThreadId, height: number) { + const clampedSourceHeight = clampDrawerHeight(height); + const [draft, setDraft] = useState(null); + const activeDraft = + draft?.threadId === threadId && draft.sourceHeight === clampedSourceHeight ? draft : null; + const drawerHeight = activeDraft?.height ?? clampedSourceHeight; + const drawerHeightRef = useRef(drawerHeight); + const lastSyncedHeightRef = useRef(clampedSourceHeight); + + drawerHeightRef.current = drawerHeight; + lastSyncedHeightRef.current = clampedSourceHeight; + + const setDrawerHeight = useCallback( + (nextHeight: number) => { + setDraft({ + threadId, + sourceHeight: clampedSourceHeight, + height: clampDrawerHeight(nextHeight), + }); + }, + [clampedSourceHeight, threadId], + ); + const clearDrawerHeight = useCallback(() => { + setDraft(null); + }, []); + + return { + drawerHeight, + drawerHeightRef, + lastSyncedHeightRef, + setDrawerHeight, + clearDrawerHeight, + }; +} + function writeSystemMessage(terminal: Terminal, message: string): void { terminal.write(`\r\n[terminal] ${message}\r\n`); } @@ -876,11 +917,14 @@ export default function ThreadTerminalDrawer({ onAddTerminalContext, keybindings, }: ThreadTerminalDrawerProps) { - const [drawerHeight, setDrawerHeight] = useState(() => clampDrawerHeight(height)); const [resizeEpoch, setResizeEpoch] = useState(0); - const drawerHeightRef = useRef(drawerHeight); - const lastSyncedHeightRef = useRef(clampDrawerHeight(height)); - const onHeightChangeRef = useRef(onHeightChange); + const { + drawerHeight, + drawerHeightRef, + lastSyncedHeightRef, + setDrawerHeight, + clearDrawerHeight, + } = useThreadDrawerHeight(threadId, height); const resizeStateRef = useRef<{ pointerId: number; startY: number; @@ -1007,27 +1051,15 @@ export default function ThreadTerminalDrawer({ onNewTerminal(); }, [onNewTerminal]); - useEffect(() => { - onHeightChangeRef.current = onHeightChange; - }, [onHeightChange]); - - useEffect(() => { - drawerHeightRef.current = drawerHeight; - }, [drawerHeight]); - - const syncHeight = useCallback((nextHeight: number) => { - const clampedHeight = clampDrawerHeight(nextHeight); - if (lastSyncedHeightRef.current === clampedHeight) return; - lastSyncedHeightRef.current = clampedHeight; - onHeightChangeRef.current(clampedHeight); - }, []); - - useEffect(() => { - const clampedHeight = clampDrawerHeight(height); - setDrawerHeight(clampedHeight); - drawerHeightRef.current = clampedHeight; - lastSyncedHeightRef.current = clampedHeight; - }, [height, threadId]); + const syncHeight = useCallback( + (nextHeight: number) => { + const clampedHeight = clampDrawerHeight(nextHeight); + if (lastSyncedHeightRef.current === clampedHeight) return; + lastSyncedHeightRef.current = clampedHeight; + onHeightChange(clampedHeight); + }, + [lastSyncedHeightRef, onHeightChange], + ); const handleResizePointerDown = useCallback((event: ReactPointerEvent) => { if (event.button !== 0) return; @@ -1068,9 +1100,10 @@ export default function ThreadTerminalDrawer({ return; } syncHeight(drawerHeightRef.current); + clearDrawerHeight(); setResizeEpoch((value) => value + 1); }, - [syncHeight], + [clearDrawerHeight, drawerHeightRef, syncHeight], ); useEffect(() => { @@ -1087,6 +1120,7 @@ export default function ThreadTerminalDrawer({ } if (!resizeStateRef.current) { syncHeight(clampedHeight); + clearDrawerHeight(); } setResizeEpoch((value) => value + 1); }; @@ -1094,20 +1128,15 @@ export default function ThreadTerminalDrawer({ return () => { window.removeEventListener("resize", onWindowResize); }; - }, [syncHeight, visible]); - - useEffect(() => { - if (!visible) { - return; - } - setResizeEpoch((value) => value + 1); - }, [visible]); + }, [clearDrawerHeight, drawerHeightRef, syncHeight, visible]); useEffect(() => { return () => { syncHeight(drawerHeightRef.current); }; - }, [syncHeight]); + }, [drawerHeightRef, syncHeight]); + + const effectiveResizeEpoch = resizeEpoch + (visible ? 1 : 0); return (