import { useCallback, useEffect, useState } from "react"; import { listen } from "@tauri-apps/api/event"; import { Loader2, ShieldCheck } from "lucide-react"; import { api, asUiError, type BackupKind, type RecoveryPoint, type UiError } from "@/lib/api"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; const TRIGGER: Record = { manual: "Manual backup", automatic: "Automatic backup", "before-update": "Before update", "before-restore": "Safety copy before a restore", "before-migration": "Before database update", "before-bots": "Before installing companions", "before-repair": "Before repair", "before-dangerous-change": "Before a risky change", }; const KIND: Record = { quick: "Characters, accounts and settings", full: "Everything, including the world database", config: "Settings only", database: "Characters and accounts", }; const DB_LABEL: Record = { characters: "Characters", auth: "Accounts", world: "World data" }; 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): string { const d = new Date(iso); const today = new Date(); const time = d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); if (d.toDateString() === today.toDateString()) return `Today ${time}`; const y = new Date(today.getTime() - 86400000); if (d.toDateString() === y.toDateString()) return `Yesterday ${time}`; return `${d.toLocaleDateString([], { day: "numeric", month: "short", year: "numeric" })} ${time}`; } export function BackupsPage({ serverId }: { serverId: string }) { 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 load = useCallback(async () => { setPoints(await api.backups(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 "Backup created."; }); const last = points?.[0]; return (

Backups

Is my data safe?

{last ? `Last backup: ${when(last.created_at)}` : "No backups yet"}

Backups are kept next to your server and never overwrite your live data.

{busy === "create" && {step ?? "Working…"}}
{notice &&

{notice}

} {error && (

{error.human.code === "unknown" ? "That did not work" : error.human.title}

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

)} {restoring && (

Restore from {TRIGGER[restoring.trigger] ?? restoring.trigger}, {when(restoring.created_at)}

Nothing is deleted. Your current data is saved first, and a replaced database is kept alongside under a new name.

{running &&

Stop the server first — databases can only be restored while it is stopped.

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

Loading…

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

Your backups will appear here.

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

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

{when(p.created_at)} · {KIND[p.kind]} · {size(p.components.reduce((a, c) => a + c.bytes, 0))}

))}
); }