import React, { useState } from "react"; import { X, Loader2 } from "lucide-react"; import { Button } from "@/components/ui"; import type { PortForwardResponse } from "@/lib/tauriCommands"; import { startPortForwardCmd } from "@/lib/tauriCommands"; import { listClustersCmd } from "@/lib/tauriCommands"; interface PortForwardFormProps { isOpen: boolean; onClose: () => void; onStart: (portForward: PortForwardResponse) => void; } export function PortForwardForm({ isOpen, onClose, onStart }: PortForwardFormProps) { const [clusterId, setClusterId] = useState(""); const [namespace, setNamespace] = useState("default"); const [pod, setPod] = useState(""); const [containerPort, setContainerPort] = useState("80"); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(""); const [clusters, setClusters] = useState<{ id: string; name: string }[]>([]); if (!isOpen) return null; React.useEffect(() => { if (isOpen) { loadClusters(); } }, [isOpen]); const loadClusters = async () => { try { const clusters = await listClustersCmd(); setClusters(clusters.map((c) => ({ id: c.id, name: c.name }))); } catch (err) { console.error("Failed to load clusters:", err); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(""); if (!clusterId || !pod || !containerPort) { setError("All fields are required"); return; } const port = parseInt(containerPort, 10); if (isNaN(port) || port < 1 || port > 65535) { setError("Container port must be a valid port number (1-65535)"); return; } setIsLoading(true); try { const portForward = await startPortForwardCmd({ cluster_id: clusterId, namespace, pod, container_port: port, }); onStart(portForward); onClose(); setClusterId(""); setNamespace("default"); setPod(""); setContainerPort("80"); } catch (err) { setError(err instanceof Error ? err.message : "Failed to start port forward"); } finally { setIsLoading(false); } }; return (

Start Port Forward

{error && (
{error}
)}
setNamespace(e.target.value)} placeholder="default" className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" disabled={isLoading} />
setPod(e.target.value)} placeholder="e.g., nginx-abc123" className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" disabled={isLoading} />
setContainerPort(e.target.value)} placeholder="80" min="1" max="65535" className="flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" disabled={isLoading} />
); }