import { useState } from "react"; import { Check, Loader2, TriangleAlert, X } from "lucide-react"; import { api, asUiError, type DiagCheck, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { hasKey, useHuman, useI18n, type Key } from "@/i18n"; const ICON = { ok: Check, warn: TriangleAlert, fail: X } as const; const TONE = { ok: "text-ok", warn: "text-warn", fail: "text-bad" } as const; const WORD = { ok: "diag.word.ok", warn: "diag.word.warn", fail: "diag.word.fail" } as const; type T = (k: Key, v?: Record) => string; /** The backend writes each check in English; recognise the known ones and show them in the current language. */ function checkText(t: T, c: DiagCheck): { title: string; detail: string } { const title = hasKey(`dg.title.${c.id}`) ? t(`dg.title.${c.id}` as Key) : c.title; const d = c.detail; const rules: [RegExp, (m: RegExpMatchArray) => string][] = [ [/^All expected server parts were found\.$/, () => t("dg.files.ok")], [/^Some server parts are missing\.$/, () => t("dg.files.partial")], [/^This is a custom server build; some features are limited\.$/, () => t("dg.files.custom")], [/^This folder does not look like a CoA server\.$/, () => t("dg.files.bad")], [/^Running$/, () => t("dg.svc.running")], [/^Port (\d+) is used by another program \(process (\d+)\)\.$/, (m) => t("dg.svc.port", { port: m[1], pid: m[2] })], [/^Starting, not answering yet\.$/, () => t("dg.svc.starting")], [/^Not running\.$/, () => t("dg.svc.stopped")], [/^Configuration files can be read\.$/, () => t("dg.configs.ok")], [/^Cannot read: (.*)$/, (m) => t("dg.configs.bad", { files: m[1] })], [/^Unusable values for: (.*)$/, (m) => t("dg.values.bad", { keys: m[1] })], [/^(\d+) GB free$/, (m) => t("dg.disk.ok", { gb: m[1] })], [/^Only (\d+) GB free; backups and updates need room\.$/, (m) => t("dg.disk.low", { gb: m[1] })], [/^The server folder is writable\.$/, () => t("dg.perm.ok")], [/^The Manager cannot write to the server folder/, () => t("dg.perm.bad")], [/^The saved game folder was not found; choose it again in Settings\.$/, () => t("dg.client.missing")], [/^Database and server console are not reachable from the network\.$/, () => t("dg.exposure.ok")], [/^Reachable from the network: (.*)\.$/, (m) => t("dg.exposure.bad", { what: m[1] })], ]; for (const [re, f] of rules) { const m = d.match(re); if (m) return { title, detail: f(m) }; } return { title, detail: d }; } export function DiagnosticsCard({ serverId }: { serverId: string }) { const { t, tn } = useI18n(); const human = useHuman(); const [checks, setChecks] = useState(null); const [files, setFiles] = useState<{ path: string; kind: string }[] | null>(null); const [busy, setBusy] = useState(null); const [note, setNote] = useState(null); const [error, setError] = useState(null); async function run(label: string, fn: () => Promise) { setBusy(label); setError(null); setNote(null); try { await fn(); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); } } const problems = checks?.filter((c) => c.level !== "ok").length ?? 0; return (

{t("diag.title")}

{t("diag.text")}

{checks && (

{problems === 0 ? t("diag.allGood") : tn("diag.problems", problems)}

    {checks.map((c) => { const Icon = ICON[c.level]; return (
  • {checkText(t, c).title} — {t(WORD[c.level])}

    {checkText(t, c).detail}

  • ); })}
)} {files && (
{files.length === 0 ? (

{t("diag.filesOk")}

) : ( <>

{t("diag.filesDiffer", { n: files.length })}

    {files.map((f) => (
  • {f.path} — {f.kind === "missing" ? t("diag.fileMissing") : t("diag.fileChanged")}
  • ))}
)}
)} {note &&

{note}

} {error &&

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

}
); }