"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { apiFetch } from "@/lib/api/fetch"; import { useRouter } from "next/navigation"; interface V2Node { id: string; name: string; status: "Online" | "Busy" | "Offline" | "Draining"; last_heartbeat: string; active_job_id: string | null; slots_total: number; slots_used: number; version: string | null; tags: string[] | null; } const STATUS_COLORS: Record = { Online: "bg-emerald-500/20 text-emerald-300 border-emerald-500/30", Busy: "bg-blue-500/20 text-blue-300 border-blue-500/30", Offline: "bg-gray-500/20 text-gray-400 border-gray-500/30", Draining: "bg-yellow-500/20 text-yellow-300 border-yellow-500/30", }; function heartbeatAge(iso: string): string { const diff = Math.floor((Date.now() - new Date(iso).getTime()) / 1000); if (diff < 60) return `${diff}s ago`; if (diff < 3600) return `${Math.floor(diff / 60)}m ago`; return `${Math.floor(diff / 3600)}h ago`; } const AE_VERSIONS = ["2025", "2024", "2023", "2022", "2021", "2020"]; const NODE_KINDS = ["Shared", "Dedicated", "Spot"]; const emptyNode = { name: "", region: "", node_ip: "", worker_port: 8088, current_ae_version: "2024", node_kind: "Dedicated", ram_gb: "", cpu_cores: "", priority: 5 }; const fldCls = "w-full rounded-lg border border-[#262b40] bg-[#0c0e1a] px-3 py-2 text-sm text-gray-100 outline-none focus:border-indigo-500"; export function NodesTable({ nodes }: { nodes: V2Node[] }) { const t = useTranslations("auto.componentsAdminNodesTable"); const router = useRouter(); const [loading, setLoading] = useState>({}); const [showAdd, setShowAdd] = useState(false); const [nf, setNf] = useState({ ...emptyNode }); const [saving, setSaving] = useState(false); const [err, setErr] = useState(null); const action = async (nodeId: string, endpoint: string) => { setLoading((p) => ({ ...p, [nodeId]: true })); try { await apiFetch(`/api/admin/nodes/${nodeId}/${endpoint}`, { method: "POST" }); router.refresh(); } finally { setLoading((p) => ({ ...p, [nodeId]: false })); } }; const addNode = async () => { setSaving(true); setErr(null); const res = await fetch("/api/admin/nodes", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: nf.name, region: nf.region, node_ip: nf.node_ip, worker_port: Number(nf.worker_port), current_ae_version: nf.current_ae_version, node_kind: nf.node_kind, ram_gb: nf.ram_gb ? Number(nf.ram_gb) : null, cpu_cores: nf.cpu_cores ? Number(nf.cpu_cores) : null, priority: Number(nf.priority) || 5, }), }); const d = await res.json().catch(() => null); if (res.ok) { setShowAdd(false); setNf({ ...emptyNode }); router.refresh(); } else setErr(d?.error ?? "ساخت نود ناموفق بود"); setSaving(false); }; const addBtn = ( ); const addModal = showAdd && (
setShowAdd(false)}>
e.stopPropagation()}>

افزودن نود رندر

{err &&

{err}

}
setNf({ ...nf, name: e.target.value })} />
setNf({ ...nf, region: e.target.value })} placeholder="ir-tehran" dir="ltr" />
setNf({ ...nf, node_ip: e.target.value })} placeholder="192.168.1.10" dir="ltr" />
setNf({ ...nf, worker_port: Number(e.target.value) })} dir="ltr" />
setNf({ ...nf, ram_gb: e.target.value })} dir="ltr" />
setNf({ ...nf, cpu_cores: e.target.value })} dir="ltr" />
setNf({ ...nf, priority: Number(e.target.value) })} dir="ltr" />
); if (nodes.length === 0) { return (
{addBtn}
{t("emptyState")}
{addModal}
); } return (
{addBtn}
{addModal}
{nodes.map((node) => ( ))}
{t("colNode")} {t("colStatus")} {t("colSlots")} {t("colHeartbeat")} {t("colActiveJob")} {t("colTags")} {t("colActions")}
{node.name}
{node.id.slice(0, 8)}…
{node.status} {node.slots_used} / {node.slots_total} {heartbeatAge(node.last_heartbeat)} {node.active_job_id ? node.active_job_id.slice(0, 12) + "…" : "—"}
{(node.tags ?? []).map((t) => ( {t} ))}
); }