import { useEffect, useRef, useState } from "react"; import { open } from "@tauri-apps/plugin-dialog"; import { listen } from "@tauri-apps/api/event"; import { Check, Loader2 } from "lucide-react"; import { api, asUiError, REMOTE_CLIENT_ID, type ClientDownloadCheck, type ClientPlan, type ClientStep, 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 } from "@/i18n"; /** * - setup: no client linked yet: pick an existing folder or download the client. * - update: a client the Manager keeps up to date has a newer version. * - track: an existing client the Manager has not looked at yet: compare it once, then keep it up to date. */ export type ClientDialogMode = "setup" | "update" | "track"; type Phase = "choose" | "confirm" | "scanning" | "preview" | "working" | "done" | "stopped" | "failed"; export function ClientDialog(props: { serverId: string; mode: ClientDialogMode; onClose: () => void; /** The linked client or its version changed; the caller refreshes what it shows. */ onChanged: () => void; /** Update mode only: start the game without updating. */ onPlayAnyway?: () => void; }) { const t = useT(); const human = useHuman(); const { serverId, mode } = props; const [phase, setPhase] = useState(mode === "setup" ? "choose" : "scanning"); const [step, setStep] = useState(null); const [parent, setParent] = useState(null); const [check, setCheck] = useState(null); const [plan, setPlan] = useState(null); const [keep, setKeep] = useState(true); const [error, setError] = useState(null); const cancelled = useRef(false); const working = phase === "scanning" || phase === "working"; useEffect(() => { let un: (() => void) | undefined; let gone = false; listen("client-progress", (e) => setStep(e.payload)) .then((u) => (gone ? u() : (un = u))) .catch(() => undefined); return () => { gone = true; un?.(); }; }, []); useEffect(() => { if (mode !== "setup") void startPlan(); }, []); // eslint-disable-line react-hooks/exhaustive-deps useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape" && !working) close(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }); function close() { props.onChanged(); props.onClose(); } function fail(e: unknown) { if (cancelled.current) { setPhase("stopped"); } else { setError(asUiError(e)); setPhase("failed"); } } async function useExisting() { const picked = await open({ directory: true, multiple: false, title: t("client.dialogTitle") }); if (typeof picked !== "string") return; setError(null); try { await api.setClient(serverId, picked); setPhase("done"); } catch (e) { setError(asUiError(e)); setPhase("failed"); } } async function pickParent() { const picked = await open({ directory: true, multiple: false, title: t("client.dl.pickParent") }); if (typeof picked !== "string") return; setError(null); try { setCheck(await api.clientDownloadCheck(picked)); setParent(picked); setPhase("confirm"); } catch (e) { setError(asUiError(e)); setPhase("failed"); } } async function startDownload() { if (!parent) return; cancelled.current = false; setStep(null); setPhase("working"); try { await api.clientDownload(serverId, parent); setPhase("done"); } catch (e) { fail(e); } } async function startPlan() { cancelled.current = false; setStep(null); setError(null); setPhase("scanning"); try { const p = await api.clientPlan(serverId); setPlan(p); setKeep(p.items.some((i) => i.kind === "modified")); setPhase("preview"); } catch (e) { fail(e); } } async function startSync() { cancelled.current = false; setStep(null); setPhase("working"); try { await api.clientSync(serverId, keep); setPhase("done"); } catch (e) { fail(e); } } function stop() { cancelled.current = true; void api.clientCancel(); } const modified = plan?.items.filter((i) => i.kind === "modified") ?? []; const pct = step && step.total > 0 ? Math.min(100, Math.floor((step.done / step.total) * 100)) : 0; const title = mode === "setup" ? t("client.setup.title") : mode === "update" ? t("client.upd.title") : t("client.track.title"); return (

{title}

{phase === "choose" && ( <>

{t("client.setup.intro")}

)} {phase === "confirm" && check && ( <>

{t("client.dl.confirmText", { v: check.version, size: formatBytes(check.needed_bytes) })}

{check.dest}

{check.free_bytes < check.needed_bytes + 512 * 1024 * 1024 ? t("client.dl.notEnough", { need: formatBytes(check.needed_bytes), free: formatBytes(check.free_bytes) }) : t("client.dl.free", { free: formatBytes(check.free_bytes) })}

)} {(phase === "scanning" || phase === "working") && (
{phase === "scanning" || step?.phase === "scan" ? t("client.dl.scanning") : t("client.dl.downloading")} {pct}%
{step?.file ?? ""} {step ? t("client.dl.progress", { done: formatBytes(step.done), total: formatBytes(step.total) }) : ""} {step && step.phase === "download" && step.bytes_per_sec > 0 ? ` ยท ${t("client.dl.speed", { speed: formatBytes(step.bytes_per_sec) })}` : ""}
)} {phase === "preview" && plan && ( <> {plan.items.length === 0 ? (

{t("client.upd.upToDate", { v: plan.version })}

) : ( <> {mode === "update" &&

{t("client.upd.available", { v: plan.version })}

}

{t("client.upd.summary", { n: plan.items.length, size: formatBytes(plan.download_bytes) })}

)} {mode === "track" &&

{t("client.track.text")}

} {modified.length > 0 && (

{t("client.upd.modifiedTitle", { n: modified.length })}

{t("client.upd.modifiedText")}

    {modified.slice(0, 8).map((i) => (
  • {i.path}
  • ))} {modified.length > 8 &&
  • {t("client.upd.more", { n: modified.length - 8 })}
  • }
)}
{mode === "update" && props.onPlayAnyway && ( )}
)} {phase === "done" && ( <>

{mode === "setup" ? t("client.dl.done") : t("client.upd.done")}

{mode === "setup" ? t(serverId === REMOTE_CLIENT_ID ? "remote.clientReady" : "client.dl.doneText") : t("client.upd.doneText")}

)} {phase === "stopped" && ( <>

{t("client.dl.cancelled")}

{mode === "setup" ? ( parent && ) : ( )}
)} {phase === "failed" && error && ( <>

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

{mode === "setup" ? : }
)}
); }