import { useEffect, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { open } from "@tauri-apps/plugin-dialog"; import { ChevronDown, ChevronRight, Loader2 } from "lucide-react"; import { api, asUiError, type UiError, type UpdatePreview, type UpdateTxn } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; const ACTION_TEXT: Record = { create: "New file", replace: "Updated", "merge-config": "New settings added", skip: "Unchanged", conflict: "Needs your decision", }; function mb(bytes: number) { return bytes >= 1 << 20 ? `${(bytes / (1 << 20)).toFixed(1)} MB` : `${Math.max(1, Math.round(bytes / 1024))} KB`; } export function SettingsHome({ serverId }: { serverId: string }) { const [preview, setPreview] = useState(null); const [pending, setPending] = useState(null); const [busy, setBusy] = useState<"check" | "update" | "rollback" | null>(null); const [progress, setProgress] = useState<{ step: string; percent: number } | null>(null); const [error, setError] = useState(null); const [done, setDone] = useState(null); const [choices, setChoices] = useState>({}); const [advanced, setAdvanced] = useState(false); const [source, setSource] = useState(""); useEffect(() => { void api.pendingUpdate(serverId).then(setPending).catch(() => {}); const un = listen<{ step: string; percent: number }>("update-progress", (e) => setProgress(e.payload)); return () => { void un.then((f) => f()); }; }, [serverId]); async function check() { setBusy("check"); setError(null); setDone(null); setPreview(null); try { const p = await api.checkUpdate(serverId, source.trim() || undefined); setPreview(p); setChoices(Object.fromEntries(p.conflicts.map((c) => [c, "keep" as const]))); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); } } async function update() { setBusy("update"); setError(null); setProgress({ step: "Starting", percent: 0 }); try { const out = await api.applyUpdate(serverId, choices, source.trim() || undefined); setPreview(null); if (out.txn.state === "committed") setDone(`Updated to version ${out.txn.to_version}.`); else setPending(out.txn); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); setProgress(null); void api.pendingUpdate(serverId).then(setPending).catch(() => {}); } } async function rollback(t: UpdateTxn) { if (!window.confirm("Go back to the previous version? Your characters and accounts are not changed.")) return; setBusy("rollback"); setError(null); try { await api.rollbackUpdate(serverId, t.id); setPending(null); setDone("Went back to the previous version. Your data was not touched."); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); } } async function browse() { const p = await open({ directory: true, multiple: false, title: "Choose an update package folder" }); if (typeof p === "string") setSource(p); } return (

Settings

How does Manager behave?

{pending && (

{pending.state === "needs-decision" ? "The updated server did not start correctly" : "An earlier update did not finish"}

{pending.message ?? "You can safely go back to the previous version."}

Going back restores program files and settings. Characters and accounts are never rolled back automatically.

)}

Server updates

Updates are checked for authenticity, backed up first, and can be undone.

{advanced && (
setSource(e.target.value)} placeholder="Update package folder (leave empty to use the official source)" className="selectable flex-1 rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-gold" />
)} {done &&

{done}

} {error && (

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

)} {preview && (

Version {preview.to_version} is available{preview.from_version ? ` (you have ${preview.from_version})` : ""}

Download {mb(preview.download_bytes)} · {preview.items.filter((i) => i.action !== "skip").length} file(s) change {preview.migrations > 0 ? ` · ${preview.migrations} database update(s)` : ""}

    {preview.items.filter((i) => i.action !== "skip").map((i) => (
  • {i.path} {ACTION_TEXT[i.action]}
    {i.action === "conflict" && (
    This file was modified outside CoA Server Manager This file was modified outside CoA Server Manager. {(["keep", "replace"] as const).map((c) => ( ))}
    )}
  • ))}

The server is stopped during the update and started again to check that it works.

)} {busy === "update" && progress && (
{progress.step} {progress.percent}%
)}
); }