UI: translate Backups page

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ДмитрийandClaude Sonnet 5.5 committed 2026-09-30 11:54:18 +05:00
1 parent 0b975a7c42
commit b5bfeb20e7
6 files changed
+274 -52

No files matched your search

+44 -52
View File
@@ -4,26 +4,13 @@ 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";
import { useHuman, useI18n, type Key, type Locale } from "@/i18n";
const TRIGGER: Record<string, string> = {
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 TRIGGER_KEYS = ["manual", "automatic", "before-update", "before-restore", "before-migration", "before-bots", "before-repair", "before-dangerous-change"];
const KIND: Record<BackupKind, string> = {
quick: "Characters, accounts and settings",
full: "Everything, including the world database",
config: "Settings only",
database: "Characters and accounts",
};
const DB_KEYS = ["characters", "auth", "world"];
const DB_LABEL: Record<string, string> = { characters: "Characters", auth: "Accounts", world: "World data" };
type T = (k: Key, v?: Record<string, string | number>) => string;
function size(bytes: number): string {
if (bytes >= 1 << 30) return `${(bytes / (1 << 30)).toFixed(1)} GB`;
@@ -31,17 +18,22 @@ function size(bytes: number): string {
return `${Math.max(1, Math.round(bytes / 1024))} KB`;
}
function when(iso: string): string {
function when(iso: string, t: T, locale: Locale): 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 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 `Yesterday ${time}`;
return `${d.toLocaleDateString([], { day: "numeric", month: "short", year: "numeric" })} ${time}`;
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) => (DB_KEYS.includes(x) ? t(`bk.db.${x}` as Key) : x);
const [points, setPoints] = useState<RecoveryPoint[] | null>(null);
const [busy, setBusy] = useState<string | null>(null);
const [step, setStep] = useState<string | null>(null);
@@ -84,54 +76,54 @@ export function BackupsPage({ serverId }: { serverId: string }) {
const create = (kind: BackupKind) =>
run("create", async () => {
await api.createBackup(serverId, kind);
return "Backup created.";
return t("bk.created");
});
const last = points?.[0];
return (
<div className="max-w-3xl">
<h1 className="text-2xl font-semibold">Backups</h1>
<p className="mt-1 text-muted">Is my data safe?</p>
<h1 className="text-2xl font-semibold">{t("bk.title")}</h1>
<p className="mt-1 text-muted">{t("q.backups")}</p>
<Card className="mt-6 p-6">
<div className="flex items-center gap-3">
<ShieldCheck className={last ? "h-6 w-6 text-ok" : "h-6 w-6 text-muted"} aria-hidden />
<div>
<p className="font-medium">{last ? `Last backup: ${when(last.created_at)}` : "No backups yet"}</p>
<p className="text-sm text-muted">Backups are kept next to your server and never overwrite your live data.</p>
<p className="font-medium">{last ? t("bk.last", { when: w(last.created_at) }) : t("bk.none")}</p>
<p className="text-sm text-muted">{t("bk.keptNext")}</p>
</div>
</div>
<div className="mt-5 flex flex-wrap items-center gap-3">
<Button variant="primary" disabled={!!busy} onClick={() => void create("quick")}>
{busy === "create" && <Loader2 className="h-4 w-4 animate-spin" aria-hidden />}
Back up now
{t("bk.backUpNow")}
</Button>
<Button disabled={!!busy} onClick={() => void create("full")} title="Also saves the large world database">
Full backup
<Button disabled={!!busy} onClick={() => void create("full")} title={t("bk.fullHint")}>
{t("bk.full")}
</Button>
<Button variant="ghost" disabled={!!busy} onClick={() => void create("config")}>
Settings only
{t("bk.settingsOnly")}
</Button>
{busy === "create" && <span className="text-sm text-muted" role="status">{step ?? "Working…"}</span>}
{busy === "create" && <span className="text-sm text-muted" role="status">{step ?? t("bk.working")}</span>}
</div>
</Card>
{notice && <p className="mt-3 text-sm text-ok" role="status">{notice}</p>}
{error && (
<Card className="mt-4 border-bad/40 p-4" role="alert">
<p className="font-medium text-bad">{error.human.code === "unknown" ? "That did not work" : error.human.title}</p>
<p className="mt-1 text-sm text-muted">{error.human.code === "unknown" ? error.technical : error.human.message}</p>
<p className="font-medium text-bad">{error.human.code === "unknown" ? t("bk.failed") : human(error.human).title}</p>
<p className="mt-1 text-sm text-muted">{error.human.code === "unknown" ? error.technical : human(error.human).message}</p>
</Card>
)}
{restoring && (
<Card className="mt-4 border-gold/40 p-5">
<p className="font-medium">Restore from {TRIGGER[restoring.trigger] ?? restoring.trigger}, {when(restoring.created_at)}</p>
<p className="font-medium">{t("bk.restoreFrom", { what: trigger(restoring.trigger), when: w(restoring.created_at) })}</p>
<p className="mt-1 text-sm text-muted">
Nothing is deleted. Your current data is saved first, and a replaced database is kept alongside under a new name.
{t("bk.restoreNote")}
</p>
{running && <p className="mt-2 text-sm text-warn">Stop the server first — databases can only be restored while it is stopped.</p>}
{running && <p className="mt-2 text-sm text-warn">{t("bk.stopFirst")}</p>}
<div className="mt-4 flex flex-wrap gap-2">
{restoring.components.map((c) =>
c.name === "configs" ? (
@@ -143,11 +135,11 @@ export function BackupsPage({ serverId }: { serverId: string }) {
void run("restore", async () => {
await api.restoreConfigs(serverId, restoring.id);
setRestoring(null);
return "Settings restored. A safety copy of the previous settings was saved.";
return t("bk.settingsRestored");
})
}
>
Restore settings
{t("bk.restoreSettings")}
</Button>
) : (
<Button
@@ -155,39 +147,39 @@ export function BackupsPage({ serverId }: { serverId: string }) {
size="sm"
disabled={!!busy || running}
onClick={() => {
if (!window.confirm(`Restore the ${DB_LABEL[c.name] ?? c.name} database from this backup? Progress made since ${when(restoring.created_at)} will be set aside (not deleted).`)) return;
if (!window.confirm(t("bk.confirmDb", { db: dbLabel(c.name), when: w(restoring.created_at) }))) return;
void run("restore", async () => {
const r = await api.restoreDatabase(serverId, restoring.id, c.name);
setRestoring(null);
return `Restored ${r.tables_restored} tables. The previous data is kept as ${r.previous_schema}.`;
return t("bk.dbRestored", { n: r.tables_restored, schema: r.previous_schema });
});
}}
>
Restore {DB_LABEL[c.name]?.toLowerCase() ?? c.name}
{t("bk.restoreDb", { db: dbLabel(c.name) })}
</Button>
),
)}
<Button size="sm" variant="ghost" onClick={() => setRestoring(null)}>
Cancel
{t("common.cancel")}
</Button>
</div>
</Card>
)}
<div className="mt-6 divide-y divide-line rounded-card border border-line bg-card/60">
{points === null && <p className="p-4 text-muted">Loading…</p>}
{points?.length === 0 && <p className="p-4 text-muted">Your backups will appear here.</p>}
{points === null && <p className="p-4 text-muted">{t("bk.loading")}</p>}
{points?.length === 0 && <p className="p-4 text-muted">{t("bk.empty")}</p>}
{points?.map((p) => (
<div key={p.id} className="flex items-center justify-between gap-4 p-4">
<div>
<p className="font-medium">{p.label && p.trigger === "manual" && p.label !== "e2e" ? p.label : (TRIGGER[p.trigger] ?? p.trigger)}</p>
<p className="font-medium">{p.label && p.trigger === "manual" && p.label !== "e2e" ? p.label : trigger(p.trigger)}</p>
<p className="text-sm text-muted">
{when(p.created_at)} · {KIND[p.kind]} · {size(p.components.reduce((a, c) => a + c.bytes, 0))}
{w(p.created_at)} · {t(`bk.kind.${p.kind}` as Key)} · {size(p.components.reduce((a, c) => a + c.bytes, 0))}
</p>
</div>
<div className="flex shrink-0 gap-1">
<Button size="sm" disabled={!!busy} onClick={() => setRestoring(p)}>
Restore
{t("bk.restore")}
</Button>
<Button
size="sm"
@@ -196,25 +188,25 @@ export function BackupsPage({ serverId }: { serverId: string }) {
onClick={() =>
void run("verify", async () => {
const v = await api.verifyBackup(serverId, p.id);
return v.ok ? "This backup is intact." : `Problem found: ${v.problems.join("; ")}`;
return v.ok ? t("bk.intact") : t("bk.problem", { details: v.problems.join("; ") });
})
}
>
Verify
{t("bk.verify")}
</Button>
<Button
size="sm"
variant="ghost"
disabled={!!busy}
onClick={() => {
if (window.confirm("Delete this backup? Only this backup is removed; your server is not affected.")) {
if (window.confirm(t("bk.confirmDelete"))) {
void run("delete", async () => {
await api.deleteBackup(serverId, p.id);
});
}
}}
>
Delete
{t("bk.delete")}
</Button>
</div>
</div>