Files
t3code/apps/web/src/components/ThreadTerminalDrawer.tsx
T

1672 lines
62 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import {
INITIAL_TERMINAL_OUTPUT_CURSOR,
readTerminalOutputUpdate,
type TerminalOutputCursor,
type TerminalOutputUpdate,
type TerminalSessionState,
} from "@t3tools/client-runtime/state/terminal";
import {
Plus,
Square,
SquareSplitHorizontal,
SquareSplitVertical,
TerminalSquare,
Trash2,
} from "lucide-react";
import {
type ContextMenuItem,
type ResolvedKeybindingsConfig,
type ScopedThreadRef,
type ThreadId,
} from "@t3tools/contracts";
import { getTerminalLabel } from "@t3tools/shared/terminalLabels";
import * as Schema from "effect/Schema";
import {
type PointerEvent as ReactPointerEvent,
type ReactNode,
type SetStateAction,
useCallback,
useEffect,
useEffectEvent,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
import { Button } from "~/components/ui/button";
import { PanelTabCloseButton } from "~/components/ui/panel-tab-close-button";
import { readTextFromClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";
import { cn } from "~/lib/utils";
import { type TerminalContextSelection } from "~/lib/terminalContext";
import {
observeSelectionActions,
resolveSelectionActionPosition,
type SelectionActionPoint,
} from "~/lib/selectionActions";
import {
GhosttyTerminalSurface,
type GhosttyTerminalSurfaceOptions,
} from "~/terminal/ghostty/surface";
import { type GhosttyColor, type GhosttyTheme } from "~/terminal/ghostty/core";
import { useOpenInPreferredEditor } from "../editorPreferences";
import { isTerminalLinkActivation, isTerminalUrl, resolvePathLinkTarget } from "../terminal-links";
import {
isDiffToggleShortcut,
isTerminalClearShortcut,
isTerminalNewShortcut,
isTerminalSplitShortcut,
isTerminalSplitVerticalShortcut,
isTerminalToggleShortcut,
terminalDeleteShortcutData,
terminalNavigationShortcutData,
} from "../keybindings";
import {
DEFAULT_THREAD_TERMINAL_HEIGHT,
MAX_TERMINALS_PER_GROUP,
type ThreadTerminalGroup,
} from "../types";
import { readLocalApi } from "~/localApi";
import { confirmTerminalClose } from "~/lib/terminalCloseConfirm";
import { useClientSettings } from "../hooks/useSettings";
import { useLocalStorage } from "../hooks/useLocalStorage";
import { useAttachedTerminalSession } from "../state/terminalSessions";
import { serverEnvironment } from "../state/server";
import { previewEnvironment } from "../state/preview";
import { terminalEnvironment } from "../state/terminal";
import { openTerminalLinkInPreview } from "./preview/openTerminalLinkInPreview";
import { useAtomCommand } from "../state/use-atom-command";
import { preventTerminalCloseShortcut } from "../lib/terminalCloseShortcut";
import {
resolveTerminalFontPreference,
resolveTerminalFontSizePreference,
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
} from "../appearanceFonts";
const MIN_DRAWER_HEIGHT = 180;
const MAX_DRAWER_HEIGHT_RATIO = 0.75;
function maxDrawerHeight(): number {
if (typeof window === "undefined") return DEFAULT_THREAD_TERMINAL_HEIGHT;
return Math.max(MIN_DRAWER_HEIGHT, Math.floor(window.innerHeight * MAX_DRAWER_HEIGHT_RATIO));
}
function clampDrawerHeight(height: number): number {
const safeHeight = Number.isFinite(height) ? height : DEFAULT_THREAD_TERMINAL_HEIGHT;
const maxHeight = maxDrawerHeight();
return Math.min(Math.max(Math.round(safeHeight), MIN_DRAWER_HEIGHT), maxHeight);
}
function writeSystemMessage(terminal: GhosttyTerminalSurface, message: string): void {
terminal.write(`\r\n[terminal] ${message}\r\n`);
}
export function writeTerminalOutputUpdate(
terminal: Pick<GhosttyTerminalSurface, "resetAndWrite" | "write">,
update: TerminalOutputUpdate,
): void {
if (update.type === "reset") {
terminal.resetAndWrite(update.data);
} else if (update.type === "append") {
terminal.write(update.data);
}
}
function parseTerminalColor(value: string, fallback: GhosttyColor): GhosttyColor {
if (typeof document === "undefined") return fallback;
const canvas = document.createElement("canvas");
canvas.width = 1;
canvas.height = 1;
const context = canvas.getContext("2d", { willReadFrequently: true });
if (!context) return fallback;
context.clearRect(0, 0, 1, 1);
context.fillStyle = value;
context.fillRect(0, 0, 1, 1);
const [red, green, blue, alpha] = context.getImageData(0, 0, 1, 1).data;
if (alpha === 0) return fallback;
return {
r: red ?? fallback.r,
g: green ?? fallback.g,
b: blue ?? fallback.b,
};
}
function runtimeEnvSignature(runtimeEnv: Record<string, string> | undefined): string {
if (!runtimeEnv) return "";
return JSON.stringify(
Object.entries(runtimeEnv)
.filter(([key, value]) => key.length > 0 && typeof value === "string")
.sort(([leftKey], [rightKey]) => leftKey.localeCompare(rightKey)),
);
}
function normalizeComputedColor(value: string | null | undefined, fallback: string): string {
const normalizedValue = value?.trim().toLowerCase();
if (
!normalizedValue ||
normalizedValue === "transparent" ||
normalizedValue === "rgba(0, 0, 0, 0)" ||
normalizedValue === "rgba(0 0 0 / 0)"
) {
return fallback;
}
return value ?? fallback;
}
function readThemeColor(styles: CSSStyleDeclaration, variable: string, fallback: string): string {
return normalizeComputedColor(styles.getPropertyValue(variable), fallback);
}
/** The surface treats an omitted family or size as "use the built-in default". */
function terminalFontOptions(family: string, size: number): { family?: string; size: number } {
const trimmed = family.trim();
return trimmed.length > 0 ? { family: trimmed, size } : { size };
}
export function terminalThemeFromApp(mountElement?: HTMLElement | null): GhosttyTheme {
const isDark = document.documentElement.classList.contains("dark");
const fallbackBackground = isDark ? "rgb(14, 18, 24)" : "rgb(255, 255, 255)";
const fallbackForeground = isDark ? "rgb(237, 241, 247)" : "rgb(28, 33, 41)";
const drawerSurface =
mountElement?.closest(".thread-terminal-drawer") ??
document.querySelector(".thread-terminal-drawer") ??
document.body;
const drawerStyles = getComputedStyle(drawerSurface);
const bodyStyles = getComputedStyle(document.body);
const themeStyles = getComputedStyle(document.documentElement);
const background = normalizeComputedColor(
drawerStyles.backgroundColor,
normalizeComputedColor(bodyStyles.backgroundColor, fallbackBackground),
);
const foreground = normalizeComputedColor(
drawerStyles.color,
normalizeComputedColor(bodyStyles.color, fallbackForeground),
);
const terminalBackground = readThemeColor(themeStyles, "--terminal-background", background);
const terminalForeground = readThemeColor(themeStyles, "--terminal-foreground", foreground);
const terminalCursor = readThemeColor(
themeStyles,
"--terminal-cursor",
isDark ? "rgb(180, 203, 255)" : "rgb(38, 56, 78)",
);
const terminalSelection = readThemeColor(
themeStyles,
"--terminal-selection-background",
isDark ? "rgba(180, 203, 255, 0.25)" : "rgba(37, 63, 99, 0.2)",
);
return {
background: parseTerminalColor(
terminalBackground,
isDark ? { r: 14, g: 18, b: 24 } : { r: 255, g: 255, b: 255 },
),
foreground: parseTerminalColor(
terminalForeground,
isDark ? { r: 237, g: 241, b: 247 } : { r: 28, g: 33, b: 41 },
),
cursor: parseTerminalColor(
terminalCursor,
isDark ? { r: 180, g: 203, b: 255 } : { r: 38, g: 56, b: 78 },
),
selectionBackground: terminalSelection,
};
}
export function terminalSelectionLineRange(position: {
start: { y: number };
end: { y: number };
}): { lineStart: number; lineEnd: number } {
const lineStart = position.start.y + 1;
return {
lineStart,
lineEnd: Math.max(lineStart, position.end.y + 1),
};
}
export type TerminalContextMenuAction = "add-to-chat" | "copy" | "paste";
/** Post-selection popup: just the two selection actions, always enabled. */
export function terminalSelectionMenuItems(): ContextMenuItem<"add-to-chat" | "copy">[] {
return [
{ id: "add-to-chat", label: "Add to chat" },
{ id: "copy", label: "Copy" },
];
}
/**
* Right-click menu for the terminal canvas: the selection actions (disabled
* until a selection exists) plus Paste. Paste is always offered: the browser
* (and Electron's default editing menu) can only paste into an editable
* element, so a canvas terminal never gets a usable entry from them.
*/
export function terminalContextMenuItems(options: {
hasSelection: boolean;
}): ContextMenuItem<TerminalContextMenuAction>[] {
return [
...terminalSelectionMenuItems().map((item) => ({
...item,
disabled: !options.hasSelection,
})),
{ id: "paste", label: "Paste" },
];
}
/**
* An empty selection change may only cancel a selection-action flow that is
* still current: a pending popup timer, or an open popup whose request id has
* not been superseded. A popup already superseded by a right-click keeps its
* menu promise unsettled for a moment; treating it as active would cancel the
* newer context-menu flow instead.
*/
export function shouldClearTerminalSelectionAction(options: {
actionPending: boolean;
openMenuRequestId: number | null;
currentRequestId: number;
}): boolean {
return options.actionPending || options.openMenuRequestId === options.currentRequestId;
}
export function shouldHandleTerminalExit(
current: TerminalSessionState["status"],
synchronized: TerminalSessionState["status"],
alreadyHandled: boolean,
): boolean {
return (
(current === "closed" || current === "exited") && current !== synchronized && !alreadyHandled
);
}
interface TerminalViewportProps {
advancedTypography: boolean;
threadRef: ScopedThreadRef;
threadId: ThreadId;
terminalId: string;
terminalLabel: string;
cwd: string;
worktreePath?: string | null;
runtimeEnv?: Record<string, string>;
onSessionExited: () => void;
onAddTerminalContext: (selection: TerminalContextSelection) => void;
focusRequestId: number;
autoFocus: boolean;
visible: boolean;
resizeEpoch: number;
drawerHeight: number;
keybindings: ResolvedKeybindingsConfig;
}
interface TerminalLaunchLocation {
readonly cwd: string;
readonly worktreePath?: string | null;
readonly runtimeEnv?: Record<string, string>;
}
export function TerminalViewport({
advancedTypography,
threadRef,
threadId,
terminalId,
terminalLabel,
cwd,
worktreePath,
runtimeEnv,
onSessionExited,
onAddTerminalContext,
focusRequestId,
autoFocus,
visible,
resizeEpoch,
drawerHeight,
keybindings,
}: TerminalViewportProps) {
const containerRef = useRef<HTMLDivElement>(null);
const terminalRef = useRef<GhosttyTerminalSurface | null>(null);
const visibleRef = useRef(visible);
const environmentId = threadRef.environmentId;
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
const openInPreferredEditor = useOpenInPreferredEditor(
environmentId,
serverConfig?.availableEditors ?? [],
);
const openTerminalPath = useEffectEvent((target: string) => openInPreferredEditor(target));
const openPreview = useAtomCommand(previewEnvironment.open, {
reportFailure: false,
});
const runTerminalWrite = useAtomCommand(terminalEnvironment.write, {
reportFailure: false,
});
const runTerminalResize = useAtomCommand(terminalEnvironment.resize, {
reportFailure: false,
});
const hasHandledExitRef = useRef(false);
const selectionActionRequestIdRef = useRef(0);
// Holds the request id of the selection popup currently on screen, so a
// popup that was superseded (but whose menu promise has not settled yet)
// cannot be mistaken for the active flow.
const openSelectionMenuRequestIdRef = useRef<number | null>(null);
const keybindingsRef = useRef(keybindings);
const runtimeEnvKey = useMemo(() => runtimeEnvSignature(runtimeEnv), [runtimeEnv]);
const handleSessionExited = useEffectEvent(() => {
onSessionExited();
});
const handleAddTerminalContext = useEffectEvent((selection: TerminalContextSelection) => {
onAddTerminalContext(selection);
});
const readTerminalLabel = useEffectEvent(() => terminalLabel);
const terminalFontFamily = useClientSettings((settings) =>
resolveTerminalFontPreference({
advanced: advancedTypography,
code: settings.fontFamilyCode,
terminal: settings.fontFamilyTerminal,
}),
);
const terminalFontSize = useClientSettings((settings) =>
resolveTerminalFontSizePreference({
advanced: advancedTypography,
code: settings.fontSizeCode,
terminal: settings.fontSizeTerminal,
}),
);
const terminalFontRef = useRef({ family: terminalFontFamily, size: terminalFontSize });
const terminalSession = useAttachedTerminalSession({
environmentId,
terminal: {
threadId,
terminalId,
cwd,
...(worktreePath !== undefined ? { worktreePath } : {}),
...(runtimeEnv ? { env: runtimeEnv } : {}),
},
});
const writeTerminal = useEffectEvent((data: string) =>
runTerminalWrite({
environmentId,
input: { threadId, terminalId, data },
}),
);
const resizeTerminal = useEffectEvent((cols: number, rows: number) =>
runTerminalResize({
environmentId,
input: { threadId, terminalId, cols, rows },
}),
);
const terminalOutput = terminalSession.output;
const terminalError = terminalSession.error;
const terminalStatus = terminalSession.status;
const outputCursorRef = useRef<TerminalOutputCursor>(INITIAL_TERMINAL_OUTPUT_CURSOR);
const synchronizedStatusRef = useRef<TerminalSessionState["status"]>("closed");
const synchronizeTerminalStatus = useEffectEvent(
(terminal: GhosttyTerminalSurface, status: TerminalSessionState["status"]) => {
const synchronized = synchronizedStatusRef.current;
if (status === "running") {
hasHandledExitRef.current = false;
} else if (shouldHandleTerminalExit(status, synchronized, hasHandledExitRef.current)) {
hasHandledExitRef.current = true;
writeSystemMessage(terminal, status === "closed" ? "Terminal closed" : "Process exited");
window.setTimeout(() => {
if (hasHandledExitRef.current) {
handleSessionExited();
}
}, 0);
}
synchronizedStatusRef.current = status;
},
);
const terminalVersion = terminalSession.version;
const previousSessionRef = useRef({
output: terminalOutput,
error: terminalError,
status: terminalStatus,
version: terminalVersion,
});
const latestSessionRef = useRef(previousSessionRef.current);
latestSessionRef.current = {
output: terminalOutput,
error: terminalError,
status: terminalStatus,
version: terminalVersion,
};
useEffect(() => {
keybindingsRef.current = keybindings;
}, [keybindings]);
useLayoutEffect(() => {
visibleRef.current = visible;
terminalRef.current?.setVisible(visible);
}, [visible]);
useEffect(() => {
const current = terminalFontRef.current;
if (current.family === terminalFontFamily && current.size === terminalFontSize) return;
terminalFontRef.current = { family: terminalFontFamily, size: terminalFontSize };
void terminalRef.current?.setFont(terminalFontOptions(terminalFontFamily, terminalFontSize));
}, [terminalFontFamily, terminalFontSize]);
useEffect(() => {
const mount = containerRef.current;
if (!mount) return;
const localApi = readLocalApi();
let cancelled = false;
let teardown: (() => void) | null = null;
let setupTerminal: GhosttyTerminalSurface | null = null;
let setupCleanups: Array<() => void> = [];
let selectionActions: ReturnType<typeof observeSelectionActions> | null = null;
const setup = async (): Promise<(() => void) | null> => {
const setupFont = terminalFontRef.current;
const terminalOptions: GhosttyTerminalSurfaceOptions = {
theme: terminalThemeFromApp(mount),
font: terminalFontOptions(setupFont.family, setupFont.size),
get visible() {
return visibleRef.current;
},
onData: (data) => handleData(data),
onResize: (cols, rows) => void resizeTerminal(cols, rows),
onSelectionChange: () => handleSelectionChange(),
beforeKey: (event) => handleBeforeKey(event),
onLinkActivate: (text, event) => handleLinkActivate(text, event),
// The surface listens from construction, so a right-click can land
// while `create` is still awaiting WASM — before the handler below it
// exists. The ref is only assigned once that setup has run.
onContextMenu: (event) => {
if (terminalRef.current) void showTerminalContextMenu(event);
},
};
const terminal = await GhosttyTerminalSurface.create(mount, terminalOptions);
if (cancelled) {
terminal.dispose();
return null;
}
terminal.setVisible(visibleRef.current);
// The theme observer is not installed yet, so re-read the theme in case
// the app toggled light/dark while the WASM surface was loading.
terminal.setTheme(terminalThemeFromApp(mount));
setupTerminal = terminal;
terminalRef.current = terminal;
// Client settings hydrate asynchronously; a font preference that landed
// while the surface was loading found terminalRef null, so its setFont
// was dropped. Re-apply whatever is current once the terminal exists.
const currentFont = terminalFontRef.current;
if (currentFont.family !== setupFont.family || currentFont.size !== setupFont.size) {
void terminal.setFont(terminalFontOptions(currentFont.family, currentFont.size));
}
const latestSession = latestSessionRef.current;
previousSessionRef.current = latestSession;
const initialOutput = readTerminalOutputUpdate(
latestSession.output,
INITIAL_TERMINAL_OUTPUT_CURSOR,
);
if (initialOutput.type === "reset" && initialOutput.data.length > 0) {
writeTerminalOutputUpdate(terminal, initialOutput);
}
outputCursorRef.current = initialOutput.cursor;
if (latestSession.error !== null) writeSystemMessage(terminal, latestSession.error);
// Attaching to a session that already exited must still run exit handling
// once, so mount synchronization starts from the empty "closed" state.
// (A session that is "closed" at mount is indistinguishable from one that
// never started, so only "exited" triggers the message — as with xterm.)
synchronizedStatusRef.current = "closed";
synchronizeTerminalStatus(terminal, latestSession.status);
// Startup may finish after the user has returned to the composer.
if (visibleRef.current && mount.contains(document.activeElement)) {
terminal.focus();
}
const dismissSelectionAction = (supersede = false) => {
const ownsMenu =
openSelectionMenuRequestIdRef.current === selectionActionRequestIdRef.current;
// Passive cancellation must not invalidate a newer right-click flow.
if (supersede || ownsMenu) selectionActionRequestIdRef.current += 1;
if (ownsMenu) void localApi?.contextMenu.close();
};
const clearSelectionAction = () => {
selectionActions?.cancel();
dismissSelectionAction(true);
};
setupCleanups.push(clearSelectionAction);
const readSelectionAction = (
pointer: SelectionActionPoint | null = null,
): {
position: { x: number; y: number };
clipboardText: string;
selection: TerminalContextSelection;
} | null => {
const activeTerminal = terminalRef.current;
const mountElement = containerRef.current;
if (!activeTerminal || !mountElement || !activeTerminal.hasSelection()) {
return null;
}
const selectionText = activeTerminal.getSelection();
const selectionPosition = activeTerminal.getSelectionPosition();
const normalizedText = selectionText.replace(/\r\n/g, "\n").replace(/^\n+|\n+$/g, "");
if (!selectionPosition || normalizedText.length === 0) {
return null;
}
const { lineStart, lineEnd } = terminalSelectionLineRange(selectionPosition);
const bounds = mountElement.getBoundingClientRect();
const position = resolveSelectionActionPosition({
bounds,
selectionRect: activeTerminal.getSelectionEndClientRect(),
pointer,
viewport: { width: window.innerWidth, height: window.innerHeight },
});
return {
position,
clipboardText: selectionText,
selection: {
terminalId,
terminalLabel: readTerminalLabel(),
lineStart,
lineEnd,
text: normalizedText,
},
};
};
const addSelectionToChat = (selection: TerminalContextSelection) => {
handleAddTerminalContext(selection);
terminalRef.current?.clearSelection();
terminalRef.current?.focus();
};
// A selection-action flow that was superseded while its async work ran
// must go silent: no error message, no focus steal.
const reportIfCurrent = (requestId: number, error: unknown, fallback: string) => {
if (requestId !== selectionActionRequestIdRef.current) return;
const activeTerminal = terminalRef.current;
if (activeTerminal) {
writeSystemMessage(activeTerminal, error instanceof Error ? error.message : fallback);
}
};
const focusIfCurrent = (requestId: number) => {
if (requestId === selectionActionRequestIdRef.current) {
terminalRef.current?.focus();
}
};
const copySelection = async (text: string, requestId: number) => {
try {
await writeTextToClipboard(text, "terminal selection");
} catch (error) {
reportIfCurrent(requestId, error, "Unable to copy terminal selection");
}
focusIfCurrent(requestId);
};
const pasteFromClipboard = async (requestId: number) => {
const activeTerminal = terminalRef.current;
if (!activeTerminal) return;
try {
// The surface owns the read so it can claim the paste race before it
// starts: a paste shortcut fired while the menu read is in flight
// supersedes this paste instead of landing alongside it.
await activeTerminal.pasteFromClipboard(
() => readTextFromClipboard("terminal input"),
() => requestId === selectionActionRequestIdRef.current,
);
} catch (error) {
reportIfCurrent(requestId, error, "Unable to read the clipboard");
return;
}
focusIfCurrent(requestId);
};
const showTerminalContextMenu = async (event: MouseEvent) => {
if (!localApi || !terminalRef.current) return;
// Own the gesture before anything async: leaving the default alive lets
// the browser (or Electron's editing menu) answer with a Paste entry
// that is permanently disabled over the terminal canvas.
event.preventDefault();
// A right-click supersedes a selection popup that is pending or open.
clearSelectionAction();
const selectionAction = readSelectionAction();
const requestId = selectionActionRequestIdRef.current;
let clicked: TerminalContextMenuAction | null;
try {
clicked = await localApi.contextMenu.show(
terminalContextMenuItems({ hasSelection: selectionAction !== null }),
{ x: event.clientX, y: event.clientY },
);
} catch (error) {
reportIfCurrent(requestId, error, "Unable to open the terminal context menu");
focusIfCurrent(requestId);
return;
}
if (requestId !== selectionActionRequestIdRef.current || clicked === null) {
return;
}
switch (clicked) {
case "add-to-chat":
if (selectionAction) addSelectionToChat(selectionAction.selection);
return;
case "copy":
if (selectionAction) await copySelection(selectionAction.clipboardText, requestId);
return;
case "paste":
await pasteFromClipboard(requestId);
return;
}
};
const showSelectionAction = async (pointer: SelectionActionPoint | null) => {
if (!localApi) {
clearSelectionAction();
return;
}
if (openSelectionMenuRequestIdRef.current === selectionActionRequestIdRef.current) {
return;
}
const nextAction = readSelectionAction(pointer);
if (!nextAction) {
clearSelectionAction();
return;
}
const requestId = ++selectionActionRequestIdRef.current;
openSelectionMenuRequestIdRef.current = requestId;
const clicked = await localApi.contextMenu
.show(terminalSelectionMenuItems(), nextAction.position)
.finally(() => {
if (openSelectionMenuRequestIdRef.current === requestId) {
openSelectionMenuRequestIdRef.current = null;
}
});
if (requestId !== selectionActionRequestIdRef.current || clicked === null) {
return;
}
switch (clicked) {
case "add-to-chat":
addSelectionToChat(nextAction.selection);
return;
case "copy":
await copySelection(nextAction.clipboardText, requestId);
return;
}
};
const sendTerminalInput = async (data: string, fallbackError: string) => {
const activeTerminal = terminalRef.current;
if (!activeTerminal) return;
const result = await writeTerminal(data);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
const error = squashAtomCommandFailure(result);
writeSystemMessage(
activeTerminal,
error instanceof Error ? error.message : fallbackError,
);
}
};
function handleBeforeKey(event: KeyboardEvent): boolean {
const currentKeybindings = keybindingsRef.current;
const options = { context: { terminalFocus: true, terminalOpen: true } };
if (preventTerminalCloseShortcut(event, currentKeybindings)) {
return false;
}
if (
isTerminalToggleShortcut(event, currentKeybindings, options) ||
isTerminalSplitShortcut(event, currentKeybindings, options) ||
isTerminalSplitVerticalShortcut(event, currentKeybindings, options) ||
isTerminalNewShortcut(event, currentKeybindings, options) ||
isDiffToggleShortcut(event, currentKeybindings, options)
) {
return false;
}
const navigationData = terminalNavigationShortcutData(event);
if (navigationData !== null) {
event.preventDefault();
event.stopPropagation();
void sendTerminalInput(navigationData, "Failed to move cursor");
return false;
}
const deleteData = terminalDeleteShortcutData(event);
if (deleteData !== null) {
event.preventDefault();
event.stopPropagation();
void sendTerminalInput(deleteData, "Failed to delete terminal input");
return false;
}
if (!isTerminalClearShortcut(event)) return true;
event.preventDefault();
event.stopPropagation();
void sendTerminalInput("\u000c", "Failed to clear terminal");
return false;
}
function handleLinkActivate(text: string, event: MouseEvent): void {
if (!isTerminalLinkActivation(event)) return;
const latestTerminal = terminalRef.current;
if (!latestTerminal) return;
if (isTerminalUrl(text)) {
if (!localApi) {
writeSystemMessage(latestTerminal, "Opening links is unavailable in this browser.");
return;
}
const fallbackToBrowser = () => {
void localApi.shell.openExternal(text).catch((error: unknown) => {
writeSystemMessage(
latestTerminal,
error instanceof Error ? error.message : "Unable to open link",
);
});
};
void openTerminalLinkInPreview({
url: text,
threadRef,
openPreview,
fallbackToBrowser,
});
return;
}
const target = resolvePathLinkTarget(text, cwd);
void (async () => {
const result = await openTerminalPath(target);
if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
return;
}
const error = squashAtomCommandFailure(result);
writeSystemMessage(
latestTerminal,
error instanceof Error ? error.message : "Unable to open path",
);
})();
}
function handleData(data: string): void {
void (async () => {
const result = await writeTerminal(data);
if (result._tag === "Success" || isAtomCommandInterrupted(result)) return;
const error = squashAtomCommandFailure(result);
writeSystemMessage(
terminal,
error instanceof Error ? error.message : "Terminal write failed",
);
})();
}
function handleSelectionChange(): void {
if (terminalRef.current?.hasSelection()) {
return;
}
const shouldClear = shouldClearTerminalSelectionAction({
actionPending: selectionActions?.pending ?? false,
openMenuRequestId: openSelectionMenuRequestIdRef.current,
currentRequestId: selectionActionRequestIdRef.current,
});
if (!shouldClear) return;
clearSelectionAction();
}
selectionActions = observeSelectionActions({
element: mount,
onSelection: (pointer) => {
void showSelectionAction(pointer);
},
onDismiss: (reason) => dismissSelectionAction(reason === "interaction"),
});
setupCleanups.push(() => selectionActions?.dispose());
const themeObserver = new MutationObserver(() => {
const activeTerminal = terminalRef.current;
if (!activeTerminal) return;
activeTerminal.setTheme(terminalThemeFromApp(containerRef.current));
});
themeObserver.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class", "style"],
});
setupCleanups.push(() => themeObserver.disconnect());
const fitTimer = window.setTimeout(() => {
const activeTerminal = terminalRef.current;
if (!activeTerminal) return;
const wasAtBottom = activeTerminal.isAtBottom();
activeTerminal.fit();
if (wasAtBottom) {
activeTerminal.scrollToBottom();
}
}, 30);
setupCleanups.push(() => window.clearTimeout(fitTimer));
const cleanups = setupCleanups;
setupCleanups = [];
setupTerminal = null;
return () => {
for (const cleanup of cleanups.toReversed()) cleanup();
if (terminalRef.current === terminal) terminalRef.current = null;
terminal.dispose();
};
};
void setup()
.then((nextTeardown) => {
if (cancelled) {
nextTeardown?.();
return;
}
teardown = nextTeardown;
})
.catch((error: unknown) => {
for (const cleanup of setupCleanups.toReversed()) cleanup();
setupCleanups = [];
if (terminalRef.current === setupTerminal) terminalRef.current = null;
setupTerminal?.dispose();
setupTerminal = null;
if (cancelled) return;
const message =
error instanceof Error ? error.message : "Unable to initialize libghostty-vt";
mount.textContent = `${message} — close and reopen the terminal to retry.`;
});
return () => {
cancelled = true;
const hadFocus = mount.contains(document.activeElement);
teardown?.();
if (hadFocus && mount.isConnected) mount.focus({ preventScroll: true });
};
}, [cwd, environmentId, runtimeEnvKey, terminalId, threadId, worktreePath]);
useEffect(() => {
const terminal = terminalRef.current;
const current = {
output: terminalOutput,
error: terminalError,
status: terminalStatus,
version: terminalVersion,
};
if (!terminal) {
previousSessionRef.current = current;
return;
}
const previous = previousSessionRef.current;
synchronizeTerminalStatus(terminal, current.status);
if (current.version === previous.version && current.output === previous.output) {
return;
}
const outputUpdate = readTerminalOutputUpdate(current.output, outputCursorRef.current);
writeTerminalOutputUpdate(terminal, outputUpdate);
outputCursorRef.current = outputUpdate.cursor;
terminal.clearSelection();
if (current.error !== null && current.error !== previous.error) {
writeSystemMessage(terminal, current.error);
}
previousSessionRef.current = current;
}, [terminalOutput, terminalError, terminalStatus, terminalVersion]);
useEffect(() => {
if (!autoFocus || !visible) return;
// Claim focus when requested, then hand it to the terminal once ready only
// if the user has not focused something else in the meantime.
(terminalRef.current ?? containerRef.current)?.focus();
}, [autoFocus, focusRequestId, visible]);
useEffect(() => {
const terminal = terminalRef.current;
if (!terminal || !visibleRef.current) return;
const wasAtBottom = terminal.isAtBottom();
// The surface reports grid changes through onResize, which is the single
// channel for PTY resize RPCs; fitting here only refreshes the layout.
const frame = window.requestAnimationFrame(() => {
if (!visibleRef.current) return;
terminal.fit();
if (wasAtBottom) {
terminal.scrollToBottom();
}
});
return () => {
window.cancelAnimationFrame(frame);
};
}, [drawerHeight, environmentId, resizeEpoch, terminalId, threadId]);
return (
<div
ref={containerRef}
tabIndex={-1}
className="relative h-full w-full overflow-hidden bg-[var(--terminal-background)]"
/>
);
}
interface ThreadTerminalDrawerProps {
mode?: "drawer" | "panel";
threadRef: ScopedThreadRef;
threadId: ThreadId;
cwd: string;
worktreePath?: string | null;
runtimeEnv?: Record<string, string>;
visible?: boolean;
height: number;
terminalIds: string[];
activeTerminalId: string;
terminalGroups: ThreadTerminalGroup[];
activeTerminalGroupId: string;
focusRequestId: number;
onSplitTerminal: () => void;
onSplitTerminalVertical: () => void;
onNewTerminal: () => void;
splitShortcutLabel?: string | undefined;
splitVerticalShortcutLabel?: string | undefined;
newShortcutLabel?: string | undefined;
closeShortcutLabel?: string | undefined;
onActiveTerminalChange: (terminalId: string) => void;
onCloseTerminal: (terminalId: string) => void;
onHeightChange: (height: number) => void;
onAddTerminalContext: (selection: TerminalContextSelection) => void;
keybindings: ResolvedKeybindingsConfig;
/** Prefer server-provided tab titles when present (e.g. active subprocess name). */
terminalLabelsById?: ReadonlyMap<string, string>;
/** Prefer per-session launch locations when the server already knows a terminal. */
terminalLaunchLocationsById?: ReadonlyMap<string, TerminalLaunchLocation>;
}
interface TerminalActionButtonProps {
label: string;
className: string;
onClick: () => void;
children: ReactNode;
}
function TerminalActionButton({ label, className, onClick, children }: TerminalActionButtonProps) {
return (
<Popover>
<PopoverTrigger
openOnHover
render={<button type="button" className={className} onClick={onClick} aria-label={label} />}
>
{children}
</PopoverTrigger>
<PopoverPopup
tooltipStyle
side="bottom"
sideOffset={6}
align="center"
className="pointer-events-none select-none"
>
{label}
</PopoverPopup>
</Popover>
);
}
export default function ThreadTerminalDrawer({
mode = "drawer",
threadRef,
threadId,
cwd,
worktreePath,
runtimeEnv,
visible = true,
height,
terminalIds,
activeTerminalId,
terminalGroups,
activeTerminalGroupId,
focusRequestId,
onSplitTerminal,
onSplitTerminalVertical,
onNewTerminal,
splitShortcutLabel,
splitVerticalShortcutLabel,
newShortcutLabel,
closeShortcutLabel,
onActiveTerminalChange,
onCloseTerminal,
onHeightChange,
onAddTerminalContext,
keybindings,
terminalLabelsById,
terminalLaunchLocationsById,
}: ThreadTerminalDrawerProps) {
const isPanel = mode === "panel";
const [advancedTypography] = useLocalStorage(
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
false,
Schema.Boolean,
);
const controlledDrawerHeight = clampDrawerHeight(height);
const [drawerHeightState, setDrawerHeightState] = useState(() => ({
threadId,
height: controlledDrawerHeight,
}));
const drawerHeight =
drawerHeightState.threadId === threadId ? drawerHeightState.height : controlledDrawerHeight;
const setDrawerHeight = useCallback(
(update: SetStateAction<number>) => {
setDrawerHeightState((current) => {
const currentHeight =
current.threadId === threadId ? current.height : controlledDrawerHeight;
const nextHeight = typeof update === "function" ? update(currentHeight) : update;
return nextHeight === currentHeight && current.threadId === threadId
? current
: { threadId, height: nextHeight };
});
},
[controlledDrawerHeight, threadId],
);
const setDrawerHeightFromWindowResize = useEffectEvent((nextHeight: number) => {
setDrawerHeight(nextHeight);
});
const [resizeEpoch, setResizeEpoch] = useState(0);
const drawerHeightRef = useRef(drawerHeight);
const lastSyncedHeightRef = useRef(controlledDrawerHeight);
const onHeightChangeRef = useRef(onHeightChange);
const resizeStateRef = useRef<{
pointerId: number;
startY: number;
startHeight: number;
} | null>(null);
const didResizeDuringDragRef = useRef(false);
const normalizedTerminalIds = useMemo(() => {
const normalizedIds: string[] = [];
const seen = new Set<string>();
for (const id of terminalIds) {
const trimmedId = id.trim();
if (trimmedId.length === 0 || seen.has(trimmedId)) continue;
seen.add(trimmedId);
normalizedIds.push(trimmedId);
}
return normalizedIds;
}, [terminalIds]);
const resolvedActiveTerminalId =
normalizedTerminalIds.length === 0
? ""
: normalizedTerminalIds.includes(activeTerminalId)
? activeTerminalId
: (normalizedTerminalIds[0] ?? "");
const resolvedTerminalGroups = useMemo(() => {
if (normalizedTerminalIds.length === 0) {
return [];
}
const validTerminalIdSet = new Set(normalizedTerminalIds);
const assignedTerminalIds = new Set<string>();
const usedGroupIds = new Set<string>();
const nextGroups: ThreadTerminalGroup[] = [];
const assignUniqueGroupId = (groupId: string): string => {
if (!usedGroupIds.has(groupId)) {
usedGroupIds.add(groupId);
return groupId;
}
let suffix = 2;
while (usedGroupIds.has(`${groupId}-${suffix}`)) {
suffix += 1;
}
const uniqueGroupId = `${groupId}-${suffix}`;
usedGroupIds.add(uniqueGroupId);
return uniqueGroupId;
};
for (const terminalGroup of terminalGroups) {
const nextTerminalIds: string[] = [];
const seenGroupTerminalIds = new Set<string>();
for (const id of terminalGroup.terminalIds) {
const terminalId = id.trim();
if (terminalId.length === 0) continue;
if (seenGroupTerminalIds.has(terminalId)) continue;
seenGroupTerminalIds.add(terminalId);
if (!validTerminalIdSet.has(terminalId)) continue;
if (assignedTerminalIds.has(terminalId)) continue;
nextTerminalIds.push(terminalId);
}
if (nextTerminalIds.length === 0) continue;
for (const terminalId of nextTerminalIds) {
assignedTerminalIds.add(terminalId);
}
const baseGroupId =
terminalGroup.id.trim().length > 0
? terminalGroup.id.trim()
: `group-${nextTerminalIds[0] ?? normalizedTerminalIds[0] ?? ""}`;
nextGroups.push({
id: assignUniqueGroupId(baseGroupId),
terminalIds: nextTerminalIds,
...(terminalGroup.splitDirection === "vertical"
? { splitDirection: "vertical" as const }
: {}),
});
}
for (const terminalId of normalizedTerminalIds) {
if (assignedTerminalIds.has(terminalId)) continue;
nextGroups.push({
id: assignUniqueGroupId(`group-${terminalId}`),
terminalIds: [terminalId],
});
}
const terminalOrderIndex = new Map(
normalizedTerminalIds.map((id, index) => [id, index] as const),
);
nextGroups.sort((left, right) => {
const rank = (ids: readonly string[]) =>
Math.min(...ids.map((id) => terminalOrderIndex.get(id) ?? Number.POSITIVE_INFINITY));
return rank(left.terminalIds) - rank(right.terminalIds);
});
return nextGroups;
}, [normalizedTerminalIds, terminalGroups]);
const resolvedActiveGroupIndex = useMemo(() => {
const indexById = resolvedTerminalGroups.findIndex(
(terminalGroup) => terminalGroup.id === activeTerminalGroupId,
);
if (indexById >= 0) return indexById;
const indexByTerminal = resolvedTerminalGroups.findIndex((terminalGroup) =>
terminalGroup.terminalIds.includes(resolvedActiveTerminalId),
);
return indexByTerminal >= 0 ? indexByTerminal : 0;
}, [activeTerminalGroupId, resolvedActiveTerminalId, resolvedTerminalGroups]);
const visibleTerminalIds =
resolvedTerminalGroups[resolvedActiveGroupIndex]?.terminalIds ??
(normalizedTerminalIds.length > 0 ? [resolvedActiveTerminalId] : []);
const splitDirection =
resolvedTerminalGroups[resolvedActiveGroupIndex]?.splitDirection ?? "horizontal";
const hasTerminalSidebar = normalizedTerminalIds.length > 1;
const isSplitView = visibleTerminalIds.length > 1;
const showGroupHeaders =
resolvedTerminalGroups.length > 1 ||
resolvedTerminalGroups.some((terminalGroup) => terminalGroup.terminalIds.length > 1);
const hasReachedSplitLimit = visibleTerminalIds.length >= MAX_TERMINALS_PER_GROUP;
const terminalLabelById = useMemo(() => {
const next = new Map<string, string>();
for (const terminalId of normalizedTerminalIds) {
next.set(terminalId, terminalLabelsById?.get(terminalId) ?? getTerminalLabel(terminalId));
}
return next;
}, [normalizedTerminalIds, terminalLabelsById]);
const resolveTerminalLaunchLocation = useCallback(
(terminalId: string): TerminalLaunchLocation => {
return (
terminalLaunchLocationsById?.get(terminalId) ?? {
cwd,
...(worktreePath !== undefined ? { worktreePath } : {}),
...(runtimeEnv ? { runtimeEnv } : {}),
}
);
},
[cwd, runtimeEnv, terminalLaunchLocationsById, worktreePath],
);
const splitTerminalActionLabel = hasReachedSplitLimit
? `Split Terminal Horizontally (max ${MAX_TERMINALS_PER_GROUP} per group)`
: splitShortcutLabel
? `Split Terminal Horizontally (${splitShortcutLabel})`
: "Split Terminal Horizontally";
const splitTerminalVerticalActionLabel = hasReachedSplitLimit
? `Split Terminal Vertically (max ${MAX_TERMINALS_PER_GROUP} per group)`
: splitVerticalShortcutLabel
? `Split Terminal Vertically (${splitVerticalShortcutLabel})`
: "Split Terminal Vertically";
const newTerminalActionLabel = newShortcutLabel
? `New Terminal (${newShortcutLabel})`
: "New Terminal";
const closeTerminalActionLabel = closeShortcutLabel
? `Close Terminal (${closeShortcutLabel})`
: "Close Terminal";
const onSplitTerminalAction = useCallback(() => {
if (hasReachedSplitLimit) return;
onSplitTerminal();
}, [hasReachedSplitLimit, onSplitTerminal]);
const onSplitTerminalVerticalAction = useCallback(() => {
if (hasReachedSplitLimit) return;
onSplitTerminalVertical();
}, [hasReachedSplitLimit, onSplitTerminalVertical]);
const onNewTerminalAction = useCallback(() => {
onNewTerminal();
}, [onNewTerminal]);
const confirmCloseTerminal = useCallback(
(terminalId: string) => {
const label = terminalLabelById.get(terminalId) ?? getTerminalLabel(terminalId);
void confirmTerminalClose([label]).then((confirmed) => {
if (confirmed) onCloseTerminal(terminalId);
});
},
[onCloseTerminal, terminalLabelById],
);
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(() => {
lastSyncedHeightRef.current = controlledDrawerHeight;
}, [controlledDrawerHeight, threadId]);
const handleResizePointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>) => {
if (event.button !== 0) return;
event.preventDefault();
event.currentTarget.setPointerCapture(event.pointerId);
didResizeDuringDragRef.current = false;
resizeStateRef.current = {
pointerId: event.pointerId,
startY: event.clientY,
startHeight: drawerHeightRef.current,
};
}, []);
const handleResizePointerMove = useCallback(
(event: ReactPointerEvent<HTMLDivElement>) => {
const resizeState = resizeStateRef.current;
if (!resizeState || resizeState.pointerId !== event.pointerId) return;
event.preventDefault();
const clampedHeight = clampDrawerHeight(
resizeState.startHeight + (resizeState.startY - event.clientY),
);
if (clampedHeight === drawerHeightRef.current) {
return;
}
didResizeDuringDragRef.current = true;
drawerHeightRef.current = clampedHeight;
setDrawerHeight(clampedHeight);
},
[setDrawerHeight],
);
const handleResizePointerEnd = useCallback(
(event: ReactPointerEvent<HTMLDivElement>) => {
const resizeState = resizeStateRef.current;
if (!resizeState || resizeState.pointerId !== event.pointerId) return;
resizeStateRef.current = null;
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
event.currentTarget.releasePointerCapture(event.pointerId);
}
if (!didResizeDuringDragRef.current) {
return;
}
syncHeight(drawerHeightRef.current);
setResizeEpoch((value) => value + 1);
},
[syncHeight],
);
useEffect(() => {
if (!visible) {
return;
}
const onWindowResize = () => {
const clampedHeight = clampDrawerHeight(drawerHeightRef.current);
const changed = clampedHeight !== drawerHeightRef.current;
if (changed) {
setDrawerHeightFromWindowResize(clampedHeight);
drawerHeightRef.current = clampedHeight;
}
if (!resizeStateRef.current) {
syncHeight(clampedHeight);
}
setResizeEpoch((value) => value + 1);
};
window.addEventListener("resize", onWindowResize);
return () => {
window.removeEventListener("resize", onWindowResize);
};
}, [syncHeight, visible]);
useEffect(() => {
if (!visible) {
return;
}
setResizeEpoch((value) => value + 1);
}, [visible]);
useEffect(() => {
return () => {
syncHeight(drawerHeightRef.current);
};
}, [syncHeight]);
if (normalizedTerminalIds.length === 0) {
return (
<aside
data-terminal-owner={isPanel ? "right-panel" : "drawer"}
className={cn(
"thread-terminal-drawer relative flex min-w-0 flex-col overflow-hidden bg-background",
isPanel ? "h-full flex-1" : "shrink-0 border-t border-border/80",
)}
style={isPanel ? undefined : { height: `${drawerHeight}px` }}
>
{!isPanel ? (
<div
className="absolute inset-x-0 top-0 z-20 h-1.5 cursor-row-resize"
onPointerDown={handleResizePointerDown}
onPointerMove={handleResizePointerMove}
onPointerUp={handleResizePointerEnd}
onPointerCancel={handleResizePointerEnd}
/>
) : null}
<div className="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 px-4 py-6 text-center text-sm text-muted-foreground">
<p>No terminal sessions for this thread yet.</p>
<Button size="xs" variant="outline" onClick={onNewTerminalAction}>
{newTerminalActionLabel}
</Button>
</div>
</aside>
);
}
const activeTerminalLaunchLocation = resolveTerminalLaunchLocation(resolvedActiveTerminalId);
return (
<aside
data-terminal-owner={isPanel ? "right-panel" : "drawer"}
className={cn(
"thread-terminal-drawer relative flex min-w-0 flex-col overflow-hidden bg-background",
isPanel ? "h-full flex-1" : "shrink-0 border-t border-border/80",
)}
style={isPanel ? undefined : { height: `${drawerHeight}px` }}
>
{!isPanel ? (
<div
className="absolute inset-x-0 top-0 z-20 h-1.5 cursor-row-resize"
onPointerDown={handleResizePointerDown}
onPointerMove={handleResizePointerMove}
onPointerUp={handleResizePointerEnd}
onPointerCancel={handleResizePointerEnd}
/>
) : null}
{!hasTerminalSidebar && (
<div className="pointer-events-none absolute right-2 top-2 z-20">
<div className="pointer-events-auto inline-flex items-center overflow-hidden rounded-md border border-border/80 bg-background shadow-xs">
<TerminalActionButton
className={`p-1 text-foreground/90 transition-colors ${
hasReachedSplitLimit
? "cursor-not-allowed opacity-45 hover:bg-transparent"
: "hover:bg-accent"
}`}
onClick={onSplitTerminalAction}
label={splitTerminalActionLabel}
>
<SquareSplitHorizontal className="size-3.25" />
</TerminalActionButton>
<div className="h-4 w-px bg-border/80" />
<TerminalActionButton
className={`p-1 text-foreground/90 transition-colors ${
hasReachedSplitLimit
? "cursor-not-allowed opacity-45 hover:bg-transparent"
: "hover:bg-accent"
}`}
onClick={onSplitTerminalVerticalAction}
label={splitTerminalVerticalActionLabel}
>
<SquareSplitVertical className="size-3.25" />
</TerminalActionButton>
<div className="h-4 w-px bg-border/80" />
<TerminalActionButton
className="p-1 text-foreground/90 transition-colors hover:bg-accent"
onClick={onNewTerminalAction}
label={newTerminalActionLabel}
>
<Plus className="size-3.25" />
</TerminalActionButton>
<div className="h-4 w-px bg-border/80" />
<TerminalActionButton
className="p-1 text-foreground/90 transition-colors hover:bg-accent"
onClick={() => confirmCloseTerminal(resolvedActiveTerminalId)}
label={closeTerminalActionLabel}
>
<Trash2 className="size-3.25" />
</TerminalActionButton>
</div>
</div>
)}
<div className="min-h-0 w-full flex-1">
<div
className={cn(
"flex h-full min-h-0 bg-[var(--terminal-background)]",
hasTerminalSidebar && "gap-1.5",
)}
>
<div className="min-w-0 flex-1">
{isSplitView ? (
<div
className="grid h-full w-full min-w-0 gap-0 overflow-hidden"
style={
splitDirection === "vertical"
? {
gridTemplateRows: `repeat(${visibleTerminalIds.length}, minmax(0, 1fr))`,
}
: {
gridTemplateColumns: `repeat(${visibleTerminalIds.length}, minmax(0, 1fr))`,
}
}
>
{visibleTerminalIds.map((terminalId) => {
const terminalLaunchLocation = resolveTerminalLaunchLocation(terminalId);
return (
<div
key={terminalId}
className={`min-h-0 min-w-0 ${
splitDirection === "vertical"
? "border-t first:border-t-0"
: "border-l first:border-l-0"
} ${
terminalId === resolvedActiveTerminalId
? "border-border"
: "border-border/70"
}`}
onMouseDown={() => {
if (terminalId !== resolvedActiveTerminalId) {
onActiveTerminalChange(terminalId);
}
}}
>
<div className="h-full">
<TerminalViewport
advancedTypography={advancedTypography}
threadRef={threadRef}
threadId={threadId}
terminalId={terminalId}
terminalLabel={terminalLabelById.get(terminalId) ?? "Terminal"}
cwd={terminalLaunchLocation.cwd}
{...(terminalLaunchLocation.worktreePath !== undefined
? { worktreePath: terminalLaunchLocation.worktreePath }
: {})}
{...(terminalLaunchLocation.runtimeEnv
? { runtimeEnv: terminalLaunchLocation.runtimeEnv }
: {})}
onSessionExited={() => onCloseTerminal(terminalId)}
onAddTerminalContext={onAddTerminalContext}
focusRequestId={focusRequestId}
autoFocus={terminalId === resolvedActiveTerminalId}
visible={visible}
resizeEpoch={resizeEpoch}
drawerHeight={drawerHeight}
keybindings={keybindings}
/>
</div>
</div>
);
})}
</div>
) : (
<div className="h-full">
<TerminalViewport
advancedTypography={advancedTypography}
key={resolvedActiveTerminalId}
threadRef={threadRef}
threadId={threadId}
terminalId={resolvedActiveTerminalId}
terminalLabel={terminalLabelById.get(resolvedActiveTerminalId) ?? "Terminal"}
cwd={activeTerminalLaunchLocation.cwd}
{...(activeTerminalLaunchLocation.worktreePath !== undefined
? { worktreePath: activeTerminalLaunchLocation.worktreePath }
: {})}
{...(activeTerminalLaunchLocation.runtimeEnv
? { runtimeEnv: activeTerminalLaunchLocation.runtimeEnv }
: {})}
onSessionExited={() => onCloseTerminal(resolvedActiveTerminalId)}
onAddTerminalContext={onAddTerminalContext}
focusRequestId={focusRequestId}
autoFocus
visible={visible}
resizeEpoch={resizeEpoch}
drawerHeight={drawerHeight}
keybindings={keybindings}
/>
</div>
)}
</div>
{hasTerminalSidebar && (
<aside className="flex w-36 min-w-36 flex-col border border-border/70 bg-muted/10">
<div className="flex h-[22px] items-stretch justify-end border-b border-border/70">
<div className="inline-flex h-full items-stretch">
<TerminalActionButton
className={`inline-flex h-full items-center px-1 text-foreground/90 transition-colors ${
hasReachedSplitLimit
? "cursor-not-allowed opacity-45 hover:bg-transparent"
: "hover:bg-accent/70"
}`}
onClick={onSplitTerminalAction}
label={splitTerminalActionLabel}
>
<SquareSplitHorizontal className="size-3.25" />
</TerminalActionButton>
<TerminalActionButton
className={`inline-flex h-full items-center border-l border-border/70 px-1 text-foreground/90 transition-colors ${
hasReachedSplitLimit
? "cursor-not-allowed opacity-45 hover:bg-transparent"
: "hover:bg-accent/70"
}`}
onClick={onSplitTerminalVerticalAction}
label={splitTerminalVerticalActionLabel}
>
<SquareSplitVertical className="size-3.25" />
</TerminalActionButton>
<TerminalActionButton
className="inline-flex h-full items-center border-l border-border/70 px-1 text-foreground/90 transition-colors hover:bg-accent/70"
onClick={onNewTerminalAction}
label={newTerminalActionLabel}
>
<Plus className="size-3.25" />
</TerminalActionButton>
<TerminalActionButton
className="inline-flex h-full items-center border-l border-border/70 px-1 text-foreground/90 transition-colors hover:bg-accent/70"
onClick={() => confirmCloseTerminal(resolvedActiveTerminalId)}
label={closeTerminalActionLabel}
>
<Trash2 className="size-3.25" />
</TerminalActionButton>
</div>
</div>
<div className="min-h-0 flex-1 overflow-y-auto px-1 py-1">
{resolvedTerminalGroups.map((terminalGroup) => {
const isGroupActive =
terminalGroup.terminalIds.includes(resolvedActiveTerminalId);
const groupActiveTerminalId = isGroupActive
? resolvedActiveTerminalId
: (terminalGroup.terminalIds[0] ?? resolvedActiveTerminalId);
const terminalCount = terminalGroup.terminalIds.length;
const isSplitGroup = terminalCount > 1;
const groupLabel = !isSplitGroup
? "Single"
: terminalGroup.splitDirection === "vertical"
? "Stacked"
: "Side by side";
const GroupIcon = !isSplitGroup
? Square
: terminalGroup.splitDirection === "vertical"
? SquareSplitVertical
: SquareSplitHorizontal;
return (
<div key={terminalGroup.id} className="pb-0.5">
{showGroupHeaders && (
<button
type="button"
className={`flex h-[22px] w-full cursor-pointer items-center gap-1 rounded px-1.5 text-[11px] ${
isGroupActive
? "bg-accent/50 text-foreground"
: "text-muted-foreground hover:bg-accent/40 hover:text-foreground"
}`}
onClick={() => onActiveTerminalChange(groupActiveTerminalId)}
>
<GroupIcon className="size-3 shrink-0" />
<span className="min-w-0 flex-1 truncate text-left">{groupLabel}</span>
<span className="text-muted-foreground/70 text-[10px] tabular-nums">
{terminalCount}
</span>
</button>
)}
<div className="flex flex-col gap-0.5">
{terminalGroup.terminalIds.map((terminalId) => {
const isActive = terminalId === resolvedActiveTerminalId;
const terminalLabel = terminalLabelById.get(terminalId) ?? "Terminal";
const closeTerminalLabel = `Close ${terminalLabel}${
isActive && closeShortcutLabel ? ` (${closeShortcutLabel})` : ""
}`;
return (
<div
key={terminalId}
className={cn(
"group/tab flex h-6 w-full items-center gap-0.5 rounded-md pr-2 pl-1.5 text-xs",
isActive
? "bg-accent text-foreground"
: "text-muted-foreground hover:bg-accent/60 hover:text-foreground",
)}
>
<PanelTabCloseButton
label={closeTerminalLabel}
onClick={() => confirmCloseTerminal(terminalId)}
tooltip={closeTerminalLabel}
>
<TerminalSquare className="size-3 shrink-0" />
</PanelTabCloseButton>
<button
type="button"
className="flex min-w-0 flex-1 cursor-pointer items-center gap-1 text-left"
onClick={() => onActiveTerminalChange(terminalId)}
>
<span className="truncate">{terminalLabel}</span>
</button>
</div>
);
})}
</div>
</div>
);
})}
</div>
</aside>
)}
</div>
</div>
</aside>
);
}