import { useCallback, useEffect, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { Loader2 } from "lucide-react"; import { api, asUiError, type DashboardStatus, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useT } from "@/i18n"; /** SQUID's bot dashboard: installed from the release that matches the bots, started on a free port, shown here. */ export function DashboardPage({ serverId }: { serverId: string }) { const t = useT(); const human = useHuman(); const [status, setStatus] = useState(null); const [busy, setBusy] = useState<"install" | "start" | "stop" | null>(null); const [step, setStep] = useState(null); const [error, setError] = useState(null); const load = useCallback(async () => { try { setStatus(await api.dashboardStatus(serverId)); } catch (e) { setError(asUiError(e)); } }, [serverId]); useEffect(() => { void load(); const un = listen("dashboard-progress", (e) => setStep(e.payload)); return () => { void un.then((f) => f()); }; }, [load]); async function run(kind: "install" | "start" | "stop", fn: () => Promise) { setBusy(kind); setError(null); setStep(null); try { setStatus(await fn()); } catch (e) { setError(asUiError(e)); await load(); } finally { setBusy(null); setStep(null); } } const install = () => run("install", () => api.dashboardInstall(serverId)); return (

{t("dash.title")}

{t("dash.intro")}

{!status && !error &&

{t("overview.checking")}

} {status && ( {!status.installed ? ( <>

{t("dash.notInstalled")}

{status.squid_tag ? t("dash.willInstall", { v: status.squid_tag }) : t("dash.noSquid")}

) : ( <>

{t("dash.versions", { dash: status.tag ?? "?", bots: status.squid_tag ?? "?" })} {" ยท "} {status.running ? t("dash.running") : t("dash.stopped")}

{!status.matches && status.squid_tag && (

{t("dash.mismatch", { bots: status.squid_tag })}

)}
{status.running ? ( <> ) : ( )} {!status.matches && status.squid_tag && ( )}
)} {busy === "install" &&

{step ?? t("bk.working")}

}
)} {error && (

{error.human.code === "unknown" ? error.technical : human(error.human).message}

)} {status?.running && (