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 InstallEnvironment, type InstallRequirements, type InstallStep, type Preflight, type ServerSummary, type UiError } from "@/lib/api"; import { formatBytes } from "@/lib/utils"; 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 [req, setReq] = 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); // What this computer installs. A repack (Windows) needs nothing more than a folder; Docker (Linux) also needs the game data // folder and a package, and Docker itself. const [env, setEnv] = useState(null); const [gameData, setGameData] = useState(""); const docker = env?.flavor === "docker"; function loadEnvironment(first: boolean) { void api.installEnvironment().then((e) => { setEnv(e); if (first && e.flavor === "docker") setDest(e.default_dir); }).catch(() => setEnv(null)); } useEffect(() => loadEnvironment(true), []); // how big the server is, from the package's signed list (re-read when another package source is entered) useEffect(() => { const timer = setTimeout(() => void api.installRequirements(pkg.trim() || undefined).then(setReq).catch(() => setReq(null)), 400); return () => clearTimeout(timer); }, [pkg]); // room for the unpacked server plus the download that is removed afterwards const needed = req ? req.unpacked_bytes + req.download_bytes : undefined; useEffect(() => { const timer = setTimeout(() => void api.installPreflight(dest, needed, docker ? gameData.trim() : undefined).then(setPre).catch(() => setPre(null)), 250); return () => clearTimeout(timer); }, [dest, needed, docker, gameData]); 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") ? "" : docker ? "/CoaServer" : "\\CoA Server")); } async function browseGameData() { const picked = await open({ directory: true, multiple: false, title: t("install.data.dialogTitle") }); if (typeof picked === "string") setGameData(picked); } 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, docker ? gameData.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" />
{docker && (
setGameData(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" />

{t("install.data.hint")}

setPkg(e.target.value)} placeholder={t("install.linuxPkg.hint")} className="selectable flex-1 rounded-md border border-line bg-card px-3 py-2.5 text-[15px] outline-none focus:border-gold" />
)} {docker && env?.docker_problem && (

{t("install.docker.title")}

{t("install.docker.text")}

{env.docker_problem}
)} {pre && pre.problems.some((p) => !(docker && !gameData.trim() && p.code.startsWith("data"))) && ( {pre.problems.filter((p) => !(docker && !gameData.trim() && p.code.startsWith("data"))).map((p) => (

{p.message}

))}
)} {req && (

{t("install.size", { size: formatBytes(req.unpacked_bytes), download: formatBytes(req.download_bytes), peak: formatBytes(req.unpacked_bytes + req.download_bytes) })}

)} {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}
}
)} {!docker &&
{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 && ( )}
); }