From 67c8bb046551c31b098dc7f161b6f0f6c58d009b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 19 Jul 2026 16:42:28 +0000 Subject: [PATCH] refactor(web): name terminal fit synchronization hook Co-authored-by: Julius Marminge --- .../src/components/ThreadTerminalDrawer.tsx | 81 ++++++++++++++----- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index 3aea95d876..bfd8e24928 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -22,6 +22,7 @@ import { Terminal, type ITheme } from "@xterm/xterm"; import { type PointerEvent as ReactPointerEvent, type ReactNode, + type RefObject, type SetStateAction, useCallback, useEffect, @@ -293,6 +294,46 @@ interface TerminalLaunchLocation { readonly runtimeEnv?: Record; } +function useFitTerminalOnViewportChange({ + visible, + drawerHeight, + environmentId, + resizeEpoch, + terminalId, + threadId, + terminalRef, + fitAddonRef, + resizeTerminal, +}: { + visible: boolean; + drawerHeight: number; + environmentId: string; + resizeEpoch: number; + terminalId: string; + threadId: ThreadId; + terminalRef: RefObject; + fitAddonRef: RefObject; + resizeTerminal: (cols: number, rows: number) => unknown; +}) { + useEffect(() => { + if (!visible) return; + const terminal = terminalRef.current; + const fitAddon = fitAddonRef.current; + if (!terminal || !fitAddon) return; + const wasAtBottom = terminal.buffer.active.viewportY >= terminal.buffer.active.baseY; + const frame = window.requestAnimationFrame(() => { + fitTerminalSafely(fitAddon); + if (wasAtBottom) { + terminal.scrollToBottom(); + } + void resizeTerminal(terminal.cols, terminal.rows); + }); + return () => { + window.cancelAnimationFrame(frame); + }; + }, [drawerHeight, environmentId, resizeEpoch, resizeTerminal, terminalId, threadId, visible]); +} + export function TerminalViewport({ threadRef, threadId, @@ -360,11 +401,13 @@ export function TerminalViewport({ input: { threadId, terminalId, data }, }), ); - const resizeTerminal = useEffectEvent((cols: number, rows: number) => - runTerminalResize({ - environmentId, - input: { threadId, terminalId, cols, rows }, - }), + const resizeTerminal = useCallback( + (cols: number, rows: number) => + runTerminalResize({ + environmentId, + input: { threadId, terminalId, cols, rows }, + }), + [environmentId, runTerminalResize, terminalId, threadId], ); const terminalBuffer = terminalSession.buffer; const terminalError = terminalSession.error; @@ -809,23 +852,17 @@ export function TerminalViewport({ }; }, [autoFocus, focusRequestId]); - useEffect(() => { - if (!visible) return; - const terminal = terminalRef.current; - const fitAddon = fitAddonRef.current; - if (!terminal || !fitAddon) return; - const wasAtBottom = terminal.buffer.active.viewportY >= terminal.buffer.active.baseY; - const frame = window.requestAnimationFrame(() => { - fitTerminalSafely(fitAddon); - if (wasAtBottom) { - terminal.scrollToBottom(); - } - void resizeTerminal(terminal.cols, terminal.rows); - }); - return () => { - window.cancelAnimationFrame(frame); - }; - }, [drawerHeight, environmentId, resizeEpoch, terminalId, threadId, visible]); + useFitTerminalOnViewportChange({ + visible, + drawerHeight, + environmentId, + resizeEpoch, + terminalId, + threadId, + terminalRef, + fitAddonRef, + resizeTerminal, + }); return (