import { Globe, Terminal as TerminalIcon, Wifi } from 'lucide-react'; import React,{ useMemo,useState } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import { cn } from '../lib/utils'; import { Host,HostProtocol } from '../types'; import { DistroAvatar } from './DistroAvatar'; import { Button } from './ui/button'; import { Input } from './ui/input'; interface ProtocolOption { protocol: HostProtocol; port: number; label: string; icon: React.ReactNode; description: string; enabled: boolean; } interface ProtocolSelectDialogProps { host: Host; onSelect: (protocol: HostProtocol, port: number) => void; onCancel: () => void; } const ProtocolSelectDialog: React.FC = ({ host, onSelect, onCancel, }) => { const { t } = useI18n(); // Build protocol options from host configuration const protocolOptions = useMemo(() => { const options: ProtocolOption[] = []; // SSH (always available if not explicitly disabled) const sshEnabled = host.protocol === 'ssh' || !host.protocol || host.protocols?.some(p => p.protocol === 'ssh' && p.enabled); if (sshEnabled !== false) { const sshConfig = host.protocols?.find(p => p.protocol === 'ssh'); options.push({ protocol: 'ssh', port: sshConfig?.port || host.port || 22, label: 'SSH', icon: , description: `ssh ${host.hostname}`, enabled: true, }); } // Mosh (if enabled) if (host.moshEnabled || host.protocols?.some(p => p.protocol === 'mosh' && p.enabled)) { const moshConfig = host.protocols?.find(p => p.protocol === 'mosh'); options.push({ protocol: 'mosh', port: moshConfig?.port || host.port || 22, label: 'Mosh', icon: , description: `mosh ${host.hostname}`, enabled: true, }); } // Telnet (if enabled) if (host.telnetEnabled || host.protocol === 'telnet' || host.protocols?.some(p => p.protocol === 'telnet' && p.enabled)) { const telnetConfig = host.protocols?.find(p => p.protocol === 'telnet'); options.push({ protocol: 'telnet', port: telnetConfig?.port || host.telnetPort || 23, label: 'Telnet', icon: , description: `telnet ${host.hostname}`, enabled: true, }); } return options; }, [host]); // State for custom ports (allow editing inline) const [ports, setPorts] = useState>(() => { const initial: Record = {}; protocolOptions.forEach(opt => { initial[opt.protocol] = opt.port; }); return initial as Record; }); const [selectedProtocol, setSelectedProtocol] = useState( protocolOptions[0]?.protocol || 'ssh' ); const handlePortChange = (protocol: HostProtocol, value: string) => { const port = parseInt(value, 10); if (!isNaN(port) && port > 0 && port <= 65535) { setPorts(prev => ({ ...prev, [protocol]: port })); } }; const handleContinue = () => { onSelect(selectedProtocol, ports[selectedProtocol] || 22); }; // If only one protocol, auto-select and proceed React.useEffect(() => { if (protocolOptions.length === 1) { // Don't auto-proceed, let user confirm } }, [protocolOptions]); return (
e.stopPropagation()}> {/* Header */}

{host.label}

{host.hostname}

{/* Progress indicator */}
{'>_'}
{/* Protocol selection */}

{t("protocolSelect.chooseProtocol")}

{protocolOptions.map((option) => ( ))}
{/* Footer */}
); }; export default ProtocolSelectDialog;