|
| 1 | +/** |
| 2 | + * BackupPanel — 数据备份/恢复面板(TD-2026-08-20-G 清偿:REQ-107 TRUST-1 |
| 3 | + * backup_create/backup_restore 后端能力此前全应用无 UI,数据备份不可达)。 |
| 4 | + * |
| 5 | + * @ai-context: 创建备份(data_dir → backups/backup-<时间>.zip,含 SQLite+图+音频) |
| 6 | + * + 从备份 zip 恢复(tauri-plugin-dialog 文件选择 + 显式确认—— |
| 7 | + * 恢复覆盖当前数据,entropy.db 改名 .pre-restore 兜底,完成后提示重启)。 |
| 8 | + * @ai-context: 备份目录即应用数据目录内,提示用户可另行拷贝到外部存储(TRUST-1 |
| 9 | + * 本地优先:数据不出本机,备份也仅在本机卷)。 |
| 10 | + */ |
| 11 | +import { useState } from "react"; |
| 12 | +import { invoke } from "@tauri-apps/api/core"; |
| 13 | +import { open } from "@tauri-apps/plugin-dialog"; |
| 14 | + |
| 15 | +interface BackupSummary { |
| 16 | + archivePath: string; |
| 17 | + fileCount: number; |
| 18 | + totalBytes: number; |
| 19 | +} |
| 20 | + |
| 21 | +/** 字节 → 可读大小(纯函数)。 */ |
| 22 | +function fmtBytes(b: number): string { |
| 23 | + if (b < 1024) return `${b} B`; |
| 24 | + if (b < 1024 * 1024) return `${(b / 1024).toFixed(1)} KB`; |
| 25 | + if (b < 1024 * 1024 * 1024) return `${(b / 1024 / 1024).toFixed(1)} MB`; |
| 26 | + return `${(b / 1024 / 1024 / 1024).toFixed(2)} GB`; |
| 27 | +} |
| 28 | + |
| 29 | +export default function BackupPanel() { |
| 30 | + const [busy, setBusy] = useState(false); |
| 31 | + const [error, setError] = useState(""); |
| 32 | + const [info, setInfo] = useState(""); |
| 33 | + |
| 34 | + const createBackup = async () => { |
| 35 | + setBusy(true); |
| 36 | + setError(""); |
| 37 | + setInfo(""); |
| 38 | + try { |
| 39 | + const s = await invoke<BackupSummary>("backup_create"); |
| 40 | + setInfo(`备份完成:${s.fileCount} 个文件 · ${fmtBytes(s.totalBytes)} · ${s.archivePath}`); |
| 41 | + } catch (e) { |
| 42 | + setError(`备份失败: ${e}`); |
| 43 | + } finally { |
| 44 | + setBusy(false); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const restore = async () => { |
| 49 | + // 文件选择(zip) |
| 50 | + const selected = await open({ multiple: false, filters: [{ name: "备份文件", extensions: ["zip"] }] }); |
| 51 | + if (typeof selected !== "string" || !selected) return; |
| 52 | + // 显式确认(恢复覆盖当前数据——TRUST-1 高影响操作) |
| 53 | + if (!window.confirm("恢复将覆盖当前全部数据(现有数据库改名 .pre-restore 兜底)。\n继续?")) { |
| 54 | + return; |
| 55 | + } |
| 56 | + setBusy(true); |
| 57 | + setError(""); |
| 58 | + setInfo(""); |
| 59 | + try { |
| 60 | + const n = await invoke<number>("backup_restore", { archivePath: selected }); |
| 61 | + setInfo(`恢复完成(${n} 个文件)。请重启应用使数据生效。`); |
| 62 | + } catch (e) { |
| 63 | + setError(`恢复失败: ${e}`); |
| 64 | + } finally { |
| 65 | + setBusy(false); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div style={{ marginTop: 10 }}> |
| 71 | + <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 6 }}> |
| 72 | + <span style={{ fontSize: 12, fontWeight: 600 }}>🗄 数据备份</span> |
| 73 | + <button |
| 74 | + onClick={() => void createBackup()} |
| 75 | + disabled={busy} |
| 76 | + style={{ padding: "3px 10px", fontSize: 11, border: "1px solid #0d9488", borderRadius: 6, background: "#f0fdfa", color: "#0f766e", cursor: busy ? "wait" : "pointer" }} |
| 77 | + title="备份 SQLite + 会话图片 + 会话音频为 zip" |
| 78 | + > |
| 79 | + {busy ? "处理中…" : "创建备份"} |
| 80 | + </button> |
| 81 | + <button |
| 82 | + onClick={() => void restore()} |
| 83 | + disabled={busy} |
| 84 | + style={{ padding: "3px 10px", fontSize: 11, border: "1px solid #fca5a5", borderRadius: 6, background: "#fff", color: "#dc2626", cursor: busy ? "wait" : "pointer" }} |
| 85 | + title="从备份 zip 恢复(覆盖当前数据,需重启生效)" |
| 86 | + > |
| 87 | + 从备份恢复… |
| 88 | + </button> |
| 89 | + </div> |
| 90 | + {error && <div style={{ fontSize: 11, color: "#dc2626" }}>{error}</div>} |
| 91 | + {!error && info && <div style={{ fontSize: 11, color: "#0f766e" }}>{info}</div>} |
| 92 | + <div style={{ fontSize: 10, color: "#9ca3af" }}> |
| 93 | + 备份保存在应用数据目录 backups/(本地优先:数据不出本机);如需异地保存请自行拷贝备份文件 |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments