"use client"; import { useEffect, useRef, useState } from "react"; type ServerStatus = "checking" | "downloading" | "loading" | "online" | "error" | "offline"; type Device = "cpu" | "cuda" | null; // Polling intervals: poll quickly until the server is online, then slow down. const FAST_INTERVAL_MS = 3000; // while checking / loading const SLOW_INTERVAL_MS = 30000; // once online export default function Header() { const [status, setStatus] = useState("checking"); const [device, setDevice] = useState(null); const [message, setMessage] = useState(); const intervalRef = useRef | null>(null); useEffect(() => { const checkHealth = async () => { try { const res = await fetch("/api/health", { cache: "no-store" }); const data = await res.json(); const newStatus: ServerStatus = (data.status as ServerStatus) ?? "offline"; setStatus(newStatus); setDevice((data.device as Device) ?? null); setMessage(data.message); // Switch to slow polling once we know the server is online if (newStatus === "online" && intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = setInterval(checkHealth, SLOW_INTERVAL_MS); } // Switch to fast polling if we detect the server went offline/loading if ( (newStatus === "offline" || newStatus === "downloading" || newStatus === "loading") && intervalRef.current ) { clearInterval(intervalRef.current); intervalRef.current = setInterval(checkHealth, FAST_INTERVAL_MS); } } catch { setStatus("offline"); setDevice(null); setMessage(undefined); } }; // Start with a fast poll — the server may still be loading the model checkHealth(); intervalRef.current = setInterval(checkHealth, FAST_INTERVAL_MS); return () => { if (intervalRef.current) clearInterval(intervalRef.current); }; }, []); const statusConfig: Record< ServerStatus, { color: string; label: string; pulse: boolean; ring: string } > = { checking: { color: "bg-yellow-500", label: "Checking…", pulse: true, ring: "border-yellow-500/30", }, loading: { color: "bg-blue-400", label: "Loading model…", pulse: true, ring: "border-blue-400/30", }, downloading: { color: "bg-sky-400", label: "Downloading model…", pulse: true, ring: "border-sky-400/30", }, online: { color: "bg-green-500", label: "Server Online", pulse: false, ring: "border-green-500/30", }, error: { color: "bg-orange-500", label: "Model Error", pulse: false, ring: "border-orange-500/30", }, offline: { color: "bg-red-500", label: "Server Offline", pulse: false, ring: "border-red-500/30", }, }; const cfg = statusConfig[status]; // Device badge — only shown once the server is online and device is known const deviceBadge = status === "online" && device ? ( {device.toUpperCase()} ) : null; return (
🎙

VibePod

Powered by VibeVoice 0.5B

{deviceBadge}
{cfg.pulse && ( )} {cfg.label}
); }