"use client"; import { motion } from "framer-motion"; import { Activity, ArrowUpRight, ChevronDown, CirclePlay, Cpu, ExternalLink, HardDrive, MonitorPlay, Radio, Server, Smartphone, Sparkles, Wifi, } from "lucide-react"; import { useMemo, useState, type ReactNode } from "react"; import OverviewCharts from "@/components/OverviewCharts"; import { LiveStudioScene3D } from "@/components/LiveStudioScene3D"; import { formatBytes, formatPercent, formatRate, formatUptime, statusLabel, statusPill } from "@/lib/dashboardFormat"; import { normalizeBrowserReachableHttpUrl } from "@/lib/panelUrls"; import type { NekoBrowserLink } from "@/lib/nekoLinks"; type PortalTheme = "dark" | "light"; type ViewKey = | "dashboard" | "services" | "live_youtube" | "live_tiktok" | "android" | "studio" | "network" | "settings"; type TFn = (zh: string, en: string) => string; type HubDashboard = { snapshot?: { cpu_load?: { "1m"?: number; "5m"?: number; "15m"?: number }; memory?: { total?: number; available?: number; free?: number }; disk?: { total?: number; free?: number; used?: number }; netdata?: { available?: boolean; version?: string }; uptime_seconds?: number; }; hardware?: Record; stack?: { hostname?: string; mdns_host?: string; machine?: string; kernel?: string; python?: string; ips?: string[]; quick_links?: Record; neko_login?: { user_login?: string; user_password?: string; admin_login?: string; admin_password?: string; note_zh?: string; }; }; services?: Array<{ service_id: string; label: string; description: string; available: boolean; running: boolean; status: string; url?: string; }>; live?: { backend_mode?: string; processes?: Array<{ pm2: string; label?: string; process_status?: string; script?: string }>; }; network?: { interfaces?: Array<{ name?: string; rx_bytes?: number; tx_bytes?: number; rx_bps?: number; tx_bps?: number; operstate?: string; speed_mbps?: number | null; }>; total_rx_bytes?: number; total_tx_bytes?: number; total_rx_bps?: number; total_tx_bps?: number; }; live_events?: { pm2_tail?: string[]; process_digest?: Array<{ pm2?: string; label?: string; status?: string }>; }; probes?: { srs?: { port?: number; api_port?: number; api_ok?: boolean; tcp?: { reachable?: boolean; latency_ms?: number }; http?: { http_code?: number; latency_ms?: number; error?: string; reachable?: boolean }; }; neko?: { port?: number; tcp?: { reachable?: boolean }; http?: { http_code?: number; reachable?: boolean; error?: string }; instances?: Array<{ id?: number; port?: number; tcp?: { reachable?: boolean }; http?: { http_code?: number } }>; running_count?: number; required_count?: number; }; }; }; type DeploymentReport = { ready?: boolean; pass_count?: number; total_count?: number; required_total?: number; mdns_enabled?: boolean; live_edge_enabled?: boolean; failing_checks?: Array<{ id: string; label: string; detail?: string }>; hints?: string[]; urls?: Record; }; const QUICK_NAV: Array<{ id: ViewKey; zh: string; en: string; icon: typeof Radio; accent: string }> = [ { id: "live_youtube", zh: "YouTube 无人直播", en: "YouTube Live", icon: Radio, accent: "from-rose-500/20 to-violet-500/10 text-rose-100 ring-rose-500/25" }, { id: "live_tiktok", zh: "TikTok HDMI", en: "TikTok HDMI", icon: MonitorPlay, accent: "from-cyan-500/20 to-sky-500/10 text-cyan-100 ring-cyan-500/25" }, { id: "android", zh: "安卓投屏", en: "Android", icon: Smartphone, accent: "from-emerald-500/20 to-teal-500/10 text-emerald-100 ring-emerald-500/25" }, { id: "services", zh: "服务编排", en: "Services", icon: Server, accent: "from-indigo-500/20 to-violet-500/10 text-indigo-100 ring-indigo-500/25" }, { id: "network", zh: "网络 / WG", en: "Network", icon: Wifi, accent: "from-amber-500/20 to-orange-500/10 text-amber-100 ring-amber-500/25" }, { id: "studio", zh: "工作室", en: "Studio", icon: Sparkles, accent: "from-fuchsia-500/20 to-pink-500/10 text-fuchsia-100 ring-fuchsia-500/25" }, ]; const PINNED_LINK_KEYS = [ "control_plane", "netdata", "filebrowser", "webtty", "neko_browser", "srs_http", "youtube_events", ] as const; function ringColor(pct: number) { if (pct >= 90) return "stroke-rose-400"; if (pct >= 75) return "stroke-amber-400"; return "stroke-cyan-400"; } function MetricRing({ label, value, sub, pct, icon: Icon, theme, }: { label: string; value: string; sub: string; pct?: number; icon: typeof Cpu; theme: PortalTheme; }) { const r = 18; const c = 2 * Math.PI * r; const dash = pct != null ? (pct / 100) * c : 0; return (

{label}

{value}

{sub}

{pct != null ? ( {pct}% ) : null}
); } function Section({ id, title, subtitle, defaultOpen = true, theme, children, badge, }: { id: string; title: string; subtitle?: string; defaultOpen?: boolean; theme: PortalTheme; children: ReactNode; badge?: ReactNode; }) { const [open, setOpen] = useState(defaultOpen); return (
{open ?
{children}
: null}
); } function DashboardSkeleton({ theme }: { theme: PortalTheme }) { return (
{Array.from({ length: 4 }).map((_, i) => (
))}
); } export function DashboardOverview({ dash, deployCheck, dashErr, loading, theme, t, onNavigate, nekoLinks, nekoPortsLabel, showStreamProbes, quickLinks, }: { dash: HubDashboard | null; deployCheck: DeploymentReport | null; dashErr: string | null; loading: boolean; theme: PortalTheme; t: TFn; onNavigate: (view: ViewKey) => void; nekoLinks: NekoBrowserLink[]; nekoPortsLabel: string; showStreamProbes: boolean; quickLinks: Record; }) { const onlineServices = dash?.services?.filter((s) => s.running).length ?? 0; const totalServices = dash?.services?.length ?? 0; const memTotal = dash?.snapshot?.memory?.total || 0; const memAvail = dash?.snapshot?.memory?.available ?? dash?.snapshot?.memory?.free ?? 0; const memUsed = Math.max(0, memTotal - memAvail); const diskTotal = dash?.snapshot?.disk?.total || 0; const diskFree = dash?.snapshot?.disk?.free || 0; const diskUsed = Math.max(0, diskTotal - diskFree); const liveRows = dash?.live?.processes || []; const digest = dash?.live_events?.process_digest || []; const liveActive = digest.filter((r) => { const s = (r.status || "").toLowerCase(); return s === "online" || s === "running" || s === "streaming"; }).length; const pinnedLinks = useMemo( () => PINNED_LINK_KEYS.map((key) => ({ key, url: quickLinks[key] })) .filter((row) => row.url && /^https?:\/\//i.test(row.url)), [quickLinks], ); const criticalServices = useMemo( () => (dash?.services || []) .filter((s) => ["live-console", "live-edge", "srs", "neko", "shellcrash"].some((id) => s.service_id.includes(id.replace("live-", "")) || s.service_id === id)) .slice(0, 6), [dash?.services], ); if (loading && !dash) return ; return (
{dashErr ? (
{t("无法加载仪表盘:", "Dashboard error: ")} {dashErr}
) : null} {/* Hero command strip */}
{dash?.stack?.hostname || "live"} {deployCheck?.ready ? ( {t("交付就绪", "Deploy ready")} ) : deployCheck ? ( {t("待验收", "Needs checks")} ) : null} {liveActive > 0 ? ( {liveActive} {t("路直播活跃", "live lanes active")} ) : null}

{dash?.stack?.mdns_host || "live.local"} {(dash?.stack?.ips || []).join(" · ") || "—"}

{dash?.stack?.machine} · {dash?.stack?.kernel} · Python {dash?.stack?.python || "—"}

{pinnedLinks.slice(0, 4).map((link) => ( {link.key.replace(/_/g, " ")} ))}
{/* Quick nav */}
{QUICK_NAV.map((item, i) => ( onNavigate(item.id)} className={`flex items-center gap-2 rounded-xl border bg-gradient-to-br px-3 py-3 text-left text-[11px] font-semibold ring-1 transition ${item.accent}`} > {t(item.zh, item.en)} ))}
{/* KPI rings */}
{/* Charts + 3D bento — fixed row height so both panels align */}
onNavigate("studio")} />
{/* Live pulse + network strip */}

{t("直播进程", "Live processes")}

{dash?.live?.backend_mode || "—"}
    {(liveRows.length ? liveRows : digest).slice(0, 8).map((p) => { const pm2 = "pm2" in p ? p.pm2 : ""; const label = ("label" in p && p.label) || pm2; const status = ("process_status" in p && p.process_status) || ("status" in p && p.status) || ""; const laneHint = `${pm2} ${label}`.toLowerCase(); const laneView: ViewKey | null = laneHint.includes("tiktok") || laneHint.includes("hdmi") ? "live_tiktok" : laneHint.includes("youtube") || laneHint.includes("yt") ? "live_youtube" : null; return (
  • onNavigate(laneView) : undefined} onKeyDown={ laneView ? (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onNavigate(laneView); } } : undefined } className={`flex items-center gap-3 rounded-xl border px-3 py-2 transition ${ theme === "light" ? "border-slate-200 bg-slate-50/80" : "border-white/[0.06] bg-black/25 hover:border-violet-400/20" } ${laneView ? "cursor-pointer hover:-translate-y-px hover:border-violet-400/30" : ""}`} >

    {label}

    {pm2}

    {statusLabel(status || "", t)}
  • ); })} {!liveRows.length && !digest.length ? (
  • {t("暂无直播进程", "No live processes")}
  • ) : null}
{dash?.network?.interfaces?.length ? (

{t("网络吞吐", "Network throughput")}

↓ {t("下行", "Down")}

{formatRate(dash.network.total_rx_bps)}

↑ {t("上行", "Up")}

{formatRate(dash.network.total_tx_bps)}

{(dash.network.interfaces || []).slice(0, 3).map((iface) => (
{iface.name} ↓ {formatRate(iface.rx_bps)} · ↑ {formatRate(iface.tx_bps)}
))}
) : null}
{/* Critical services mini grid */} {criticalServices.length ? (
{criticalServices.map((s) => (

{s.label}

{s.service_id}

{s.running ? t("运行中", "Running") : s.status}
))}
) : null} {/* Collapsible detail sections */}
{deployCheck ? (
{deployCheck.pass_count}/{deployCheck.total_count} } >

{deployCheck.ready ? t("必需检查已通过,控制面可交付。", "Required checks passed.") : t("部分检查未通过,请查看失败项。", "Some checks failed.")}

{Object.entries(deployCheck.urls || {}) .filter(([, url]) => !!url) .slice(0, 5) .map(([key, url]) => ( {key} ))}
{(deployCheck.failing_checks || []).length ? (
    {deployCheck.failing_checks!.slice(0, 4).map((item) => (
  • {item.label}: {item.detail || "—"}
  • ))}
) : null}
) : null} {dash?.hardware ? (

arch {String(dash.hardware.arch ?? "—")}

GPU {dash.hardware.gpu && typeof dash.hardware.gpu === "object" && (dash.hardware.gpu as { present?: boolean }).present ? t("有", "yes") : t("无", "no")}

hwaccels{" "} {((dash.hardware.ffmpeg_hwaccels as string[] | undefined) || []).join(", ") || "—"}

) : null} {dash?.stack?.neko_login ? (
{nekoLinks.map((link) => ( {link.label} · {link.accessLabel} ))}

{t("昵称随意,密码须与环境变量一致(默认 12345678)。", "Display name is free; password must match NEKO_* (default 12345678).")}

) : null} {showStreamProbes && dash?.probes ? (
{dash.probes.srs ? (

SRS

API {dash.probes.srs.api_ok ? "OK" : "—"} · :{dash.probes.srs.port} · RTT {dash.probes.srs.http?.latency_ms ?? dash.probes.srs.tcp?.latency_ms ?? "—"} ms

{dash.probes.srs.http?.error ? (

{dash.probes.srs.http.error}

) : null}
) : null} {dash.probes.neko ? (

Neko

{dash.probes.neko.running_count}/{dash.probes.neko.required_count} {t("在线", "online")} · HTTP {dash.probes.neko.http?.http_code ?? "—"}

{dash.probes.neko.http?.error ? (

{dash.probes.neko.http.error}

) : null}
) : null}
) : null} {(dash?.live_events?.pm2_tail?.length || digest.length) ? (
              {(dash?.live_events?.pm2_tail || []).slice(-12).join("\n") || t("暂无日志", "No logs")}
            
) : null}
); }