import { useEffect, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { open } from "@tauri-apps/plugin-dialog"; import { Check, ChevronDown, ChevronRight, Loader2 } from "lucide-react"; import { api, asUiError, type InstallStep, type Preflight, type ServerSummary, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useT, type Key } from "@/i18n"; // The backend reports progress with these English step names; map the known ones to translations. const STEP_KEYS: Record = { "Preparing your server": "install.progressTitle", "Checking your computer": "install.step.checking", "Downloading server": "install.step.downloading", Unpacking: "install.step.unpacking", "Preparing database": "install.step.database", Finishing: "install.step.finishing", }; type Phase = "choose" | "installing" | "done"; export function InstallServer(props: { canCancel: boolean; onCancel: () => void; onDone: (s: ServerSummary) => void | Promise }) { const t = useT(); const human = useHuman(); const [dest, setDest] = useState("C:\\Games\\CoA Server"); const [pre, setPre] = useState(null); const [phase, setPhase] = useState("choose"); const [step, setStep] = useState({ step: "Preparing your server", percent: 0, detail: null }); const [error, setError] = useState(null); const [showAdvanced, setShowAdvanced] = useState(false); const [pkg, setPkg] = useState(""); const [installed, setInstalled] = useState(null); const [showDetails, setShowDetails] = useState(false); useEffect(() => { const t = setTimeout(() => void api.installPreflight(dest).then(setPre).catch(() => setPre(null)), 250); return () => clearTimeout(t); }, [dest]); useEffect(() => { const un = listen("install-progress", (e) => setStep(e.payload)); return () => { void un.then((f) => f()); }; }, []); async function browse() { const picked = await open({ directory: true, multiple: false, title: t("install.dialogTitle") }); if (typeof picked === "string") setDest(picked.replace(/[\\/]+$/, "") + (picked.toLowerCase().includes("coa") ? "" : "\\CoA Server")); } async function browsePackage() { const picked = await open({ directory: true, multiple: false, title: t("install.pkgDialogTitle") }); if (typeof picked === "string") setPkg(picked); } async function install() { setError(null); setPhase("installing"); setStep({ step: "Preparing your server", percent: 0, detail: null }); try { const s = await api.installNew(dest, pkg.trim() || undefined); setInstalled(s); setPhase("done"); } catch (e) { setError(asUiError(e)); setPhase("choose"); } } if (phase === "installing") { return (

{t("install.progressTitle")}

{t("install.progressText")}

{STEP_KEYS[step.step] ? t(STEP_KEYS[step.step]) : step.step} {step.percent}%
{step.detail &&

{step.detail}

} {step.step === "Downloading server" && ( )}
); } if (phase === "done" && installed) { return (

{t("install.doneTitle")}

{t("install.doneText")}

); } return (

{t("install.title")}

{t("install.subtitle")}

setDest(e.target.value)} className="selectable flex-1 rounded-md border border-line bg-card px-3 py-2.5 text-[15px] outline-none focus:border-gold" />
{pre && pre.problems.length > 0 && ( {pre.problems.map((p) => (

{p.message}

))}
)} {pre?.ok &&

{t("install.ready", { gb: Math.round(pre.free_bytes / (1 << 30)) })}

} {error && (

{error.human.code === "unknown" ? t("install.failedTitle") : human(error.human).title}

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

{t("install.nothingInstalled")}

{showDetails &&
{error.technical}
}
)}
{showAdvanced && (
setPkg(e.target.value)} placeholder={t("install.localPkgHint")} className="selectable flex-1 rounded-md border border-line bg-card px-3 py-2 text-sm outline-none focus:border-gold" />
)}
{props.canCancel && ( )}
); }