/** * Terminal Connection Dialog * Full connection overlay with host info, progress indicator, and auth/progress content */ import { Fingerprint, Loader2, Plug, TerminalSquare, X } from 'lucide-react'; import React from 'react'; import { useI18n } from '../../application/i18n/I18nProvider'; import { cn } from '../../lib/utils'; import { Host, SSHKey } from '../../types'; import { formatHostPort, resolveTelnetPort } from '../../domain/host'; import { DistroAvatar } from '../DistroAvatar'; import { Button } from '../ui/button'; import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip'; import { TerminalAuthDialog, TerminalAuthDialogProps } from './TerminalAuthDialog'; import { TerminalConnectionProgress, TerminalConnectionProgressProps } from './TerminalConnectionProgress'; import { HostKeyInfo, TerminalHostKeyVerification } from './TerminalHostKeyVerification'; export interface ChainProgress { currentHop: number; totalHops: number; currentHostLabel: string; } export interface TerminalConnectionDialogProps { host: Host; status: 'connecting' | 'connected' | 'disconnected'; error: string | null; progressValue: number; chainProgress: ChainProgress | null; needsAuth: boolean; showLogs: boolean; _setShowLogs: (show: boolean) => void; // Auth dialog props authProps: Omit; keys: SSHKey[]; onDismissDisconnected?: () => void; hostKeyVerification?: { hostKeyInfo: HostKeyInfo; onClose: () => void; onContinue: () => void; onAddAndContinue: () => void; }; // Progress props progressProps: Omit; } // Helper to get protocol display info const getProtocolInfo = (host: Host): { i18nKey: string; showPort: boolean; port: number } => { // Check moshEnabled first since mosh uses protocol: "ssh" with moshEnabled: true if (host.moshEnabled) { return { i18nKey: 'terminal.connection.protocol.mosh', showPort: true, port: host.port || 22 }; } const protocol = host.protocol || 'ssh'; switch (protocol) { case 'local': return { i18nKey: 'terminal.connection.protocol.local', showPort: false, port: 0 }; case 'telnet': // Telnet uses telnetPort, not port (which is SSH port) return { i18nKey: 'terminal.connection.protocol.telnet', showPort: true, port: resolveTelnetPort(host) }; case 'mosh': return { i18nKey: 'terminal.connection.protocol.mosh', showPort: true, port: host.port || 22 }; case 'serial': return { i18nKey: 'terminal.connection.protocol.serial', showPort: false, port: 0 }; case 'ssh': default: return { i18nKey: 'terminal.connection.protocol.ssh', showPort: true, port: host.port || 22 }; } }; export const TerminalConnectionDialog: React.FC = ({ host, status, error, progressValue, chainProgress, needsAuth, showLogs, _setShowLogs: setShowLogs, // Rename back to setShowLogs for internal use authProps, keys, onDismissDisconnected, hostKeyVerification, progressProps, }) => { const { t } = useI18n(); const hasError = Boolean(error); const isConnecting = status === 'connecting'; const canDismissDisconnected = status === 'disconnected' && !needsAuth && !!onDismissDisconnected; const protocolInfo = getProtocolInfo(host); const isVerifyingHostKey = Boolean(hostKeyVerification); const isHostKeyChanged = hostKeyVerification?.hostKeyInfo.status === 'changed'; const shouldCompleteProgress = hasError || (!isConnecting && !needsAuth); const targetFirstSegmentWidth = isVerifyingHostKey || shouldCompleteProgress ? 100 : Math.min(100, progressValue * 2); const targetSecondSegmentWidth = isVerifyingHostKey ? 0 : shouldCompleteProgress ? 100 : Math.max(0, Math.min(100, (progressValue - 50) * 2)); const [secondSegmentUnlocked, setSecondSegmentUnlocked] = React.useState( () => shouldCompleteProgress || targetSecondSegmentWidth <= 0 ); const secondSegmentUnlockTimerRef = React.useRef | null>(null); React.useEffect(() => { return () => { if (secondSegmentUnlockTimerRef.current) { clearTimeout(secondSegmentUnlockTimerRef.current); } }; }, []); React.useEffect(() => { if (needsAuth || isVerifyingHostKey || targetSecondSegmentWidth <= 0 || shouldCompleteProgress) { if (secondSegmentUnlockTimerRef.current) { clearTimeout(secondSegmentUnlockTimerRef.current); secondSegmentUnlockTimerRef.current = null; } setSecondSegmentUnlocked(shouldCompleteProgress); return; } if (secondSegmentUnlocked || secondSegmentUnlockTimerRef.current) return; secondSegmentUnlockTimerRef.current = setTimeout(() => { secondSegmentUnlockTimerRef.current = null; setSecondSegmentUnlocked(true); }, 320); }, [isVerifyingHostKey, needsAuth, secondSegmentUnlocked, shouldCompleteProgress, targetSecondSegmentWidth]); const firstSegmentWidth = targetFirstSegmentWidth; const secondSegmentWidth = shouldCompleteProgress || secondSegmentUnlocked ? targetSecondSegmentWidth : 0; return (
{chainProgress ? ( <>
{t('terminal.connection.chainOf', { current: chainProgress.currentHop, total: chainProgress.totalHops, })} {': '} {chainProgress.currentHostLabel}
{t(protocolInfo.i18nKey)} {protocolInfo.showPort ? formatHostPort(host.hostname, protocolInfo.port) : host.hostname}
) : ( <>
{host.label}
{t(protocolInfo.i18nKey)} {protocolInfo.showPort ? formatHostPort(host.hostname, protocolInfo.port) : host.hostname}
)}
{!needsAuth && ( )} {status === 'connecting' && !needsAuth && !isVerifyingHostKey && ( )} {canDismissDisconnected && ( {t('terminal.connection.dismissDisconnectedDialog')} )}
50 && !hasError ? "bg-primary/15 text-primary" : hasError ? "bg-destructive/20 text-destructive" : "bg-muted text-muted-foreground" )}>
{isConnecting ? ( ) : ( )}
{needsAuth ? ( ) : hostKeyVerification ? ( ) : ( )}
); }; export default TerminalConnectionDialog;