import { useEffect, useState } from "react"; import { open } from "@tauri-apps/plugin-dialog"; import { ChevronDown, ChevronRight, Loader2 } from "lucide-react"; import { applyServerUpdate, clearServerUpdateResult, useServerUpdate } from "@/lib/serverUpdate"; import { api, asUiError, type UiError, type UpdatePreview, type UpdateTxn } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { ClientCard } from "@/screens/ClientCard"; import { DiagnosticsCard } from "@/screens/DiagnosticsCard"; import { AboutCard } from "@/screens/AboutCard"; import { hasKey, useHuman, useI18n, type Key } from "@/i18n"; import { LanguagePicker } from "@/components/LanguagePicker"; import { RealmStartupSettings } from "@/screens/RealmStartupSettings"; import { RepairCard } from "@/screens/RepairCard"; 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 { t } = useI18n(); const upd = useServerUpdate(serverId); const human = useHuman(); const [preview, setPreview] = useState(null); const [pending, setPending] = useState(null); const [localBusy, setBusy] = useState<"check" | "rollback" | null>(null); // an update keeps running in the background when this tab is left, so its state is the shared one const busy = upd.applying ? "update" : localBusy; const progress = upd.applying ? { step: upd.step ?? "Starting", percent: upd.percent } : null; const [localError, setError] = useState(null); const error = localError ?? upd.uiError; const [localDone, setDone] = useState(null); const done = upd.result && "committed" in upd.result ? t("upd.updatedTo", { v: upd.result.committed }) : localDone; const [choices, setChoices] = useState>({}); const [advanced, setAdvanced] = useState(false); const [source, setSource] = useState(""); useEffect(() => { void api.pendingUpdate(serverId).then(setPending).catch(() => {}); }, [serverId]); // the update finished (possibly while another tab was open): drop the stale preview, show what is left to decide useEffect(() => { if (!upd.result) return; setPreview(null); if ("pending" in upd.result) setPending(upd.result.pending); else void api.pendingUpdate(serverId).then(setPending).catch(() => {}); }, [upd.result, serverId]); async function check() { setBusy("check"); setError(null); setDone(null); clearServerUpdateResult(serverId); 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() { setError(null); setDone(null); await applyServerUpdate(serverId, { choices, source: source.trim() || undefined }); } async function rollback(txn: UpdateTxn) { if (!window.confirm(t("upd.confirmBack"))) return; setBusy("rollback"); setError(null); try { await api.rollbackUpdate(serverId, txn.id); setPending(null); setDone(t("upd.wentBack")); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); } } async function browse() { const p = await open({ directory: true, multiple: false, title: t("upd.dialogTitle") }); if (typeof p === "string") setSource(p); } return (

{t("settings.title")}

{t("q.settings")}

{pending && (

{pending.state === "needs-decision" ? t("upd.pendingBad") : t("upd.pendingUnfinished")}

{pending.message ?? t("upd.safeBack")}

{t("upd.backNote")}

)}

{t("settings.language")}

{t("settings.languageHint")}

{t("upd.title")}

{t("upd.text")}

{advanced && (
setSource(e.target.value)} placeholder={t("upd.folderHint")} 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 : human(error.human).message}

)} {preview && preview.from_version === preview.to_version && preview.items.every((i) => i.action === "skip") && ( // The same version with no file to change: the database scripts of this package are all in the ledger already.

{t("upd.current", { v: preview.to_version })}

)} {preview && !(preview.from_version === preview.to_version && preview.items.every((i) => i.action === "skip")) && (

{preview.from_version ? t("upd.availableFrom", { to: preview.to_version, from: preview.from_version }) : t("upd.available", { to: preview.to_version })}

{preview.migrations > 0 ? t("upd.summaryDb", { size: mb(preview.download_bytes), files: preview.items.filter((i) => i.action !== "skip").length, db: preview.migrations }) : t("upd.summary", { size: mb(preview.download_bytes), files: preview.items.filter((i) => i.action !== "skip").length })}

    {preview.items.filter((i) => i.action !== "skip").map((i) => (
  • {i.path} {t(`upd.act.${i.action}` as Key)}
    {i.action === "conflict" && (
    {t("upd.conflict")} {t("upd.conflict")} {(["keep", "replace"] as const).map((c) => ( ))}
    )}
  • ))}

{t("upd.stopNote")}

)} {busy === "update" && progress && (
{hasKey(`ustep.${progress.step}`) ? t(`ustep.${progress.step}` as Key) : progress.step} {progress.percent}%
)}
); }