import React, { useState, useEffect } from "react"; import { Play, Square, Trash2, Plus, RefreshCw } from "lucide-react"; import { useKubernetesStore } from "@/stores/kubernetesStore"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, Badge, Button, } from "@/components/ui"; import type { PortForwardResponse } from "@/lib/tauriCommands"; import { listPortForwardsCmd, startPortForwardCmd, stopPortForwardCmd, deletePortForwardCmd, listPodsCmd, listNamespacesCmd, } from "@/lib/tauriCommands"; import { PortForwardForm } from "@/components/Kubernetes"; export function PortForwardPage() { const { selectedClusterId } = useKubernetesStore(); const [portForwards, setPortForwards] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isFormOpen, setIsFormOpen] = useState(false); const [error, setError] = useState(null); const loadPortForwards = async () => { if (!selectedClusterId) return; setIsLoading(true); setError(null); try { const data = await listPortForwardsCmd(); setPortForwards(data); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setIsLoading(false); } }; useEffect(() => { loadPortForwards(); const interval = setInterval(loadPortForwards, 5000); return () => clearInterval(interval); }, [selectedClusterId]); const handleStop = async (id: string) => { try { await stopPortForwardCmd(id); setPortForwards((prev) => prev.filter((pf) => pf.id !== id)); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } }; const handleDelete = async (id: string) => { try { await deletePortForwardCmd(id); setPortForwards((prev) => prev.filter((pf) => pf.id !== id)); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } }; const handleStart = async (pf: PortForwardResponse) => { try { if (!selectedClusterId) return; const result = await startPortForwardCmd({ cluster_id: selectedClusterId, namespace: pf.namespace, pod: pf.pod, container_port: pf.container_ports[0] ?? 80, local_port: pf.local_ports[0] ?? 0, }); setPortForwards((prev) => [...prev, result]); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } }; const getStatusColor = (status: string) => { switch (status.toLowerCase()) { case "active": return "bg-green-500"; case "stopped": return "bg-gray-500"; default: return "bg-red-500"; } }; if (!selectedClusterId) { return (

No cluster selected

Select a cluster from the dropdown to manage port forwards.

); } return (

Port Forwarding

Manage port forwards to access pods locally

{error && (
{error}
)}
Name Namespace Kind Pod Port Local Port Protocol Address Status Actions {portForwards.length === 0 ? ( {isLoading ? "Loading port forwards..." : "No active port forwards"} ) : ( portForwards.map((pf) => ( {pf.pod} {pf.namespace} Pod {pf.container_ports.join(", ")} {pf.local_ports.join(", ")} TCP localhost:{pf.local_ports[0]} {pf.status}
{pf.status.toLowerCase() === "active" ? ( ) : ( )}
)) )}
setIsFormOpen(false)} onStart={(pf) => { setPortForwards((prev) => [...prev, pf]); setIsFormOpen(false); }} />
); }