"use client"; import { useCallback, useEffect, useRef, useState } from "react"; import { Badge } from "@kandev/ui/badge"; import { Button } from "@kandev/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@kandev/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@kandev/ui/table"; import { IconLoader2 } from "@tabler/icons-react"; import { listSSHSessions } from "@/lib/api/domains/ssh-api"; import type { SSHSession } from "@/lib/types/http-ssh"; export interface SSHSessionsCardProps { executorId: string; } const REFRESH_INTERVAL_MS = 90_000; export function SSHSessionsCard({ executorId }: SSHSessionsCardProps) { const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // Monotonic sequence used to ignore stale poll responses: a slow request // that resolves after a newer one (or after executorId changes) would // otherwise overwrite the fresh row set. const seqRef = useRef(0); const refresh = useCallback(async () => { const seq = ++seqRef.current; setLoading(true); setError(null); try { const rows = await listSSHSessions(executorId); if (seq !== seqRef.current) return; // a newer call (or executor switch) won the race setSessions(rows); } catch (e) { if (seq !== seqRef.current) return; setError(e instanceof Error ? e.message : "Failed to load sessions"); } finally { if (seq === seqRef.current) setLoading(false); } }, [executorId]); useEffect(() => { // Reset sequence so a previous executor's pending response can't land // here and pollute the new executor's data. seqRef.current = 0; refresh(); const id = setInterval(refresh, REFRESH_INTERVAL_MS); return () => { clearInterval(id); // Bump the sequence to invalidate any in-flight response from the // unmounted instance. seqRef.current = -1; }; }, [refresh]); return (
Active sessions Sessions currently running on this SSH host. Refreshes every 90 seconds.
); } function SSHSessionsBody({ loading, error, sessions, }: { loading: boolean; error: string | null; sessions: SSHSession[]; }) { if (error) { return (

{error}

); } if (sessions.length === 0 && !loading) { return (

No active sessions.

); } if (sessions.length === 0) return null; return ; } function SSHSessionsTable({ sessions }: { sessions: SSHSession[] }) { return ( Task Session Host Remote port Local fwd Uptime Status {sessions.map((s) => ( ))}
); } function SSHSessionsRow({ session: s }: { session: SSHSession }) { return ( {s.task_id.slice(0, 8)} {s.session_id.slice(0, 8)} {s.user ? `${s.user}@${s.host}` : s.host} {s.remote_agentctl_port ?? "—"} {s.local_forward_port ?? "—"} {formatUptime(s.uptime_seconds)} {s.status} ); } function formatUptime(s: number): string { if (s < 60) return `${s}s`; if (s < 3600) return `${Math.floor(s / 60)}m`; if (s < 86400) return `${Math.floor(s / 3600)}h`; return `${Math.floor(s / 86400)}d`; }