refactor(web): minimize terminal drawer render fix
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
3073d9d492
commit
7c87dec4d1
1 file changed
+22
-59
@@ -22,7 +22,6 @@ import { Terminal, type ITheme } from "@xterm/xterm";
|
||||
import {
|
||||
type PointerEvent as ReactPointerEvent,
|
||||
type ReactNode,
|
||||
type RefObject,
|
||||
type SetStateAction,
|
||||
useCallback,
|
||||
useEffect,
|
||||
@@ -294,46 +293,6 @@ interface TerminalLaunchLocation {
|
||||
readonly runtimeEnv?: Record<string, string>;
|
||||
}
|
||||
|
||||
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<Terminal | null>;
|
||||
fitAddonRef: RefObject<FitAddon | null>;
|
||||
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,
|
||||
@@ -401,13 +360,11 @@ export function TerminalViewport({
|
||||
input: { threadId, terminalId, data },
|
||||
}),
|
||||
);
|
||||
const resizeTerminal = useCallback(
|
||||
(cols: number, rows: number) =>
|
||||
runTerminalResize({
|
||||
environmentId,
|
||||
input: { threadId, terminalId, cols, rows },
|
||||
}),
|
||||
[environmentId, runTerminalResize, terminalId, threadId],
|
||||
const resizeTerminal = useEffectEvent((cols: number, rows: number) =>
|
||||
runTerminalResize({
|
||||
environmentId,
|
||||
input: { threadId, terminalId, cols, rows },
|
||||
}),
|
||||
);
|
||||
const terminalBuffer = terminalSession.buffer;
|
||||
const terminalError = terminalSession.error;
|
||||
@@ -852,17 +809,23 @@ export function TerminalViewport({
|
||||
};
|
||||
}, [autoFocus, focusRequestId]);
|
||||
|
||||
useFitTerminalOnViewportChange({
|
||||
visible,
|
||||
drawerHeight,
|
||||
environmentId,
|
||||
resizeEpoch,
|
||||
terminalId,
|
||||
threadId,
|
||||
terminalRef,
|
||||
fitAddonRef,
|
||||
resizeTerminal,
|
||||
});
|
||||
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]);
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
|
||||
Reference in new issue
Block a user