import { useCallback, useEffect, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { Loader2, ShieldCheck } from "lucide-react"; import { open } from "@tauri-apps/plugin-dialog"; import { api, asUiError, type BackupKind, type BackupLocation, type RecoveryPoint, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { useHuman, useI18n, type Key, type Locale } from "@/i18n"; const TRIGGER_KEYS = ["manual", "automatic", "before-update", "before-restore", "before-migration", "before-bots", "before-repair", "before-dangerous-change"]; const DB_KEYS = ["characters", "auth", "world"]; type T = (k: Key, v?: Record) => string; function size(bytes: number): string { if (bytes >= 1 << 30) return `${(bytes / (1 << 30)).toFixed(1)} GB`; if (bytes >= 1 << 20) return `${(bytes / (1 << 20)).toFixed(1)} MB`; return `${Math.max(1, Math.round(bytes / 1024))} KB`; } function when(iso: string, t: T, locale: Locale): string { const d = new Date(iso); const today = new Date(); const time = d.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit" }); if (d.toDateString() === today.toDateString()) return t("bk.today", { time }); const y = new Date(today.getTime() - 86400000); if (d.toDateString() === y.toDateString()) return t("bk.yesterday", { time }); return `${d.toLocaleDateString(locale, { day: "numeric", month: "short", year: "numeric" })} ${time}`; } export function BackupsPage({ serverId }: { serverId: string }) { const { t, locale } = useI18n(); const human = useHuman(); const w = (iso: string) => when(iso, t, locale); const trigger = (x: string) => (TRIGGER_KEYS.includes(x) ? t(`bk.trigger.${x}` as Key) : x); const dbLabel = (x: string) => { const [realm, kind] = x.split("-"); if (kind && DB_KEYS.includes(kind)) return `${realm === "coa" ? "CoA" : "Wildcard"} · ${t(`bk.db.${kind}` as Key)}`; return DB_KEYS.includes(x) ? t(`bk.db.${x}` as Key) : x; }; const [points, setPoints] = useState(null); const [busy, setBusy] = useState(null); const [step, setStep] = useState(null); const [error, setError] = useState(null); const [notice, setNotice] = useState(null); const [restoring, setRestoring] = useState(null); const [running, setRunning] = useState(false); const [location, setLocation] = useState(null); const load = useCallback(async () => { setPoints(await api.backups(serverId)); setLocation(await api.backupLocation(serverId)); const s = await api.status(serverId); setRunning(s.observed.world.state !== "stopped" || s.observed.auth.state !== "stopped"); }, [serverId]); useEffect(() => { void load().catch((e) => setError(asUiError(e))); const un = listen("backup-progress", (e) => setStep(e.payload)); return () => { void un.then((f) => f()); }; }, [load]); async function run(label: string, fn: () => Promise) { setBusy(label); setError(null); setNotice(null); setStep(null); try { const msg = await fn(); if (msg) setNotice(msg); await load(); } catch (e) { setError(asUiError(e)); } finally { setBusy(null); setStep(null); } } const create = (kind: BackupKind) => run("create", async () => { await api.createBackup(serverId, kind); return t("bk.created"); }); const last = points?.[0]; return (

{t("bk.title")}

{t("q.backups")}

{last ? t("bk.last", { when: w(last.created_at) }) : t("bk.none")}

{t("bk.keptNext")}

{busy === "create" && {step ?? t("bk.working")}}
{location && (

{t("bk.folder")}

{location.path}{location.is_default ? ` (${t("bk.folderDefault")})` : ""}

{t("bk.folderNote")}

{!location.is_default && ( )}
)} {notice &&

{notice}

} {error && (

{error.human.code === "unknown" ? t("bk.failed") : human(error.human).title}

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

)} {restoring && (

{t("bk.restoreFrom", { what: trigger(restoring.trigger), when: w(restoring.created_at) })}

{t("bk.restoreNote")}

{running &&

{t("bk.stopFirst")}

}
{restoring.components.map((c) => c.name === "configs" ? ( ) : ( ), )}
)}
{points === null &&

{t("bk.loading")}

} {points?.length === 0 &&

{t("bk.empty")}

} {points?.map((p) => (

{p.label && p.trigger === "manual" && p.label !== "e2e" ? p.label : trigger(p.trigger)}

{w(p.created_at)} · {p.realm === "wildcard" ? "Wildcard" : "CoA"} · {t(`bk.kind.${p.kind}` as Key)} · {size(p.components.reduce((a, c) => a + c.bytes, p.mysql_snapshot?.bytes ?? 0))}

))}
); }