import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Eye, EyeOff, FolderSearch, Monitor, MousePointerClick, Terminal, } from "lucide-react"; import { Input } from "@/components/input"; import type { Host } from "@/types/ui-types"; import { getCredentials } from "@/api/credentials-api"; import { mapCredentials } from "./HostManagerData"; import { createQuickConnectHost, type QuickConnectProtocol, } from "./quick-connect-host"; const DEFAULT_PORTS: Record = { ssh: "22", rdp: "3389", vnc: "5900", }; import { Select2 } from "@/components/select2"; interface QuickConnectPanelProps { onConnect: (host: Host, type: "terminal" | "files" | "rdp" | "vnc") => void; } export function QuickConnectPanel({ onConnect }: QuickConnectPanelProps) { const { t } = useTranslation(); const [host, setHost] = useState(""); const [protocol, setProtocol] = useState("ssh"); const [port, setPort] = useState("22"); const [domain, setDomain] = useState(""); const [username, setUsername] = useState("root"); const [authType, setAuthType] = useState<"password" | "key" | "credential">( "password", ); const [password, setPassword] = useState(""); const [privateKey, setPrivateKey] = useState(""); const [showPassword, setShowPassword] = useState(false); const [credentialId, setCredentialId] = useState(""); const [credentials, setCredentials] = useState< { id: string; name: string; username: string }[] >([]); useEffect(() => { getCredentials() .then((res) => setCredentials(mapCredentials(res))) .catch(() => {}); }, []); const isDesktop = protocol !== "ssh"; const switchProtocol = (next: QuickConnectProtocol) => { // Keep a port the user typed; only swap the protocol default. if (port === DEFAULT_PORTS[protocol]) setPort(DEFAULT_PORTS[next]); setProtocol(next); }; const connect = (type: "terminal" | "files" | "rdp" | "vnc") => { if (!host) return; if (!isDesktop && !username) return; const hostConfig = createQuickConnectHost({ ip: host, port: parseInt(port) || parseInt(DEFAULT_PORTS[protocol]), username, authType: isDesktop ? "password" : authType, password, key: privateKey, credentialId, protocol, domain: domain || undefined, }); onConnect(hostConfig, type); }; const connectDefault = () => connect(isDesktop ? protocol : "terminal"); return (
{(["ssh", "rdp", "vnc"] as const).map((type) => ( ))}
setHost(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") connectDefault(); }} className="h-7 text-xs" />
setPort(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") connectDefault(); }} className="h-7 text-xs" />
{ if (username === "root") setUsername(""); }} onBlur={() => { if (username === "") setUsername("root"); }} onChange={(e) => setUsername(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") connectDefault(); }} className="h-7 text-xs" />
{!isDesktop && (
{(["password", "key", "credential"] as const).map((type) => ( ))}
)} {(isDesktop || authType === "password") && (
setPassword(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") connectDefault(); }} className="h-7 text-xs pr-8" />
)} {isDesktop && protocol === "rdp" && (
setDomain(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") connectDefault(); }} className="h-7 text-xs" />
)} {!isDesktop && authType === "key" && (