import { Copy, Minus, Square, Unplug, X } from 'lucide-react'; import React, { lazy, Suspense, useCallback, useEffect, useMemo, useState } from 'react'; import { I18nProvider, useI18n } from '../application/i18n/I18nProvider'; import { canReuseTerminalConnection } from '../application/state/terminalConnectionReuse'; import { useSettingsState } from '../application/state/useSettingsState'; import { useTerminalPopupWindow } from '../application/state/useTerminalPopupWindow'; import { useVaultState } from '../application/state/useVaultState'; import { useWindowControls } from '../application/state/useWindowControls'; import { shouldCloseTerminalPopupOnExit } from '../application/state/resolveTerminalSessionExitIntent'; import type { TerminalPopupPayload } from '../domain/systemManager/types'; import type { TerminalTheme } from '../domain/models'; import type { Host } from '../types'; import { cn } from '../lib/utils'; const Terminal = lazy(() => import('./Terminal')); const isMac = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform); const POPUP_STARTUP_REVEAL_EXTRA_DELAY_MS = 900; const POPUP_STARTUP_REVEAL_MIN_DELAY_MS = 1500; const POPUP_STARTUP_REVEAL_MAX_DELAY_MS = 12000; type PopupThemeVars = React.CSSProperties & Record; const buildPopupThemeVars = (theme: TerminalTheme): PopupThemeVars => { const { colors } = theme; return { '--terminal-popup-bg': colors.background, '--terminal-popup-fg': colors.foreground, '--terminal-popup-muted': colors.foreground, '--terminal-popup-accent': colors.cursor, '--terminal-popup-control-hover': `color-mix(in srgb, ${colors.foreground} 10%, transparent)`, }; }; function TerminalPopupWindowControls({ mac, onClose }: { mac: boolean; onClose: () => void }) { const { minimize, maximize, isMaximized: fetchIsMaximized } = useWindowControls(); const [isWindowMaximized, setIsWindowMaximized] = useState(false); useEffect(() => { let cancelled = false; void fetchIsMaximized().then((value) => { if (!cancelled) setIsWindowMaximized(!!value); }); const handleResize = () => { void fetchIsMaximized().then((value) => setIsWindowMaximized(!!value)); }; window.addEventListener('resize', handleResize); return () => { cancelled = true; window.removeEventListener('resize', handleResize); }; }, [fetchIsMaximized]); const handleMaximize = async () => { const value = await maximize(); setIsWindowMaximized(!!value); }; if (mac) return null; const buttonClass = 'app-no-drag flex h-10 w-11 items-center justify-center text-[color:var(--terminal-popup-muted)] transition-colors hover:bg-[color:var(--terminal-popup-control-hover)] hover:text-[color:var(--terminal-popup-fg)]'; return (
); } function TerminalPopupSpinner() { return (
); } function TerminalPopupBlank() { return (
); } function TerminalPopupStartupError({ message, closeLabel, onClose, }: { message: string; closeLabel: string; onClose: () => void; }) { return (
{message}
); } function TerminalPopupTitleIcon({ icon }: { icon: TerminalPopupPayload['icon'] }) { if (!icon) return null; if (icon.kind !== 'image' || !icon.src) return null; return ( {icon.alt ); } /** Fallback when the parent session's host is no longer in the vault (e.g. quick connect). */ function buildHostFromSession(source: TerminalPopupPayload['sourceSession']): Host { return { id: source.hostId, label: source.hostLabel, hostname: source.hostname, username: source.username, port: source.port ?? (source.protocol === 'local' ? undefined : 22), protocol: source.protocol === 'local' ? 'local' : 'ssh', tags: [], os: 'linux', moshEnabled: source.moshEnabled, etEnabled: source.etEnabled, charset: source.charset, }; } function TerminalPopupPageInner() { const { t } = useI18n(); const { close, setWindowTitle, onPopupConfig } = useTerminalPopupWindow(); const { notifyRendererReady, onWindowCommandCloseRequested } = useWindowControls(); const settings = useSettingsState(); const { isInitialized: vaultInitialized, hosts, keys, identities, knownHosts, snippets, snippetPackages } = useVaultState(); const [config, setConfig] = useState(null); const [terminalReady, setTerminalReady] = useState(false); const [startupError, setStartupError] = useState(null); const sessionId = useMemo(() => crypto.randomUUID(), []); const popupThemeVars = useMemo( () => buildPopupThemeVars(settings.currentTerminalTheme), [settings.currentTerminalTheme], ); useEffect(() => { const unsubscribe = onPopupConfig((payload) => { setConfig(payload); if (payload.title) { void setWindowTitle(payload.title); } }); // Main delivers the popup payload as soon as the renderer reports ready // (and destroys the window if it never does) — so report ready only after // the config listener above is registered. notifyRendererReady(); return unsubscribe; }, [notifyRendererReady, onPopupConfig, setWindowTitle]); useEffect(() => { return onWindowCommandCloseRequested(() => { void close(); }); }, [close, onWindowCommandCloseRequested]); const host = useMemo(() => { if (!config) return null; const vaultHost = hosts.find((h) => h.id === config.sourceSession.hostId); return vaultHost ?? buildHostFromSession(config.sourceSession); }, [config, hosts]); const reuseId = useMemo(() => { if (!config) return undefined; return canReuseTerminalConnection(config.sourceSession) ? config.parentSessionId : undefined; }, [config]); const ready = Boolean(config && host && vaultInitialized); const startupRevealDelayMs = useMemo(() => { if (!config?.startupCommand) return 0; const configuredDelay = settings.terminalSettings?.startupCommandDelayMs; const startupDelay = typeof configuredDelay === 'number' && Number.isFinite(configuredDelay) ? Math.max(0, configuredDelay) : 600; return Math.min( POPUP_STARTUP_REVEAL_MAX_DELAY_MS, Math.max(POPUP_STARTUP_REVEAL_MIN_DELAY_MS, startupDelay + POPUP_STARTUP_REVEAL_EXTRA_DELAY_MS), ); }, [config?.startupCommand, settings.terminalSettings?.startupCommandDelayMs]); const revealTerminal = useCallback(() => { setTerminalReady(true); }, []); useEffect(() => { setTerminalReady(false); setStartupError(null); }, [config?.popupId, sessionId]); useEffect(() => { if (!ready) return undefined; const timeout = window.setTimeout(() => setTerminalReady(true), startupRevealDelayMs); return () => window.clearTimeout(timeout); }, [config?.popupId, ready, startupRevealDelayMs]); return (
{isMac &&
}
{config?.title ?? ''}
{!isMac && void close()} />}
{!ready || !config || !host ? ( ) : startupError ? ( void close()} /> ) : (
}> { void close(); }} onSessionExit={(_closedSessionId, evt) => { if (shouldCloseTerminalPopupOnExit(evt)) { void close(); return; } if (!terminalReady && config.startupCommand) { setStartupError(t('systemManager.popup.startupFailed')); } }} onStatusChange={(_changedSessionId, status) => { if (!config.startupCommand && status === 'connected') revealTerminal(); }} onTerminalDataCapture={revealTerminal} /> {!terminalReady && (
)}
)}
); } export default function TerminalPopupPage() { const settings = useSettingsState(); return ( ); }