|
| 1 | +// Restoring a copy (spec 14.2): the owner picks the file and types its |
| 2 | +// passphrase; the daemon opens it and says what it holds; only after the |
| 3 | +// owner confirms does Botloft restart and swap it in. |
| 4 | + |
| 5 | +import { FolderOpen, RotateCcw } from "lucide-react"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { useT } from "../../i18n"; |
| 8 | +import { when } from "../../lib/format"; |
| 9 | +import type { BackupManifest } from "../../lib/protocol.gen"; |
| 10 | +import { useApi, useHost } from "../../store/context"; |
| 11 | +import { Button } from "../../ui/Button"; |
| 12 | +import { Callout } from "../../ui/Callout"; |
| 13 | +import { TextField } from "../../ui/Field"; |
| 14 | +import { backupError } from "./BackupSettings"; |
| 15 | + |
| 16 | +export function BackupRestore() { |
| 17 | + const b = useT().account.backup; |
| 18 | + const api = useApi(); |
| 19 | + const host = useHost(); |
| 20 | + const [file, setFile] = useState<string | null>(null); |
| 21 | + const [passphrase, setPassphrase] = useState(""); |
| 22 | + const [manifest, setManifest] = useState<BackupManifest | null>(null); |
| 23 | + const [busy, setBusy] = useState(false); |
| 24 | + const [failed, setFailed] = useState<{ title: string; text: string } | null>(null); |
| 25 | + |
| 26 | + const pick = async () => { |
| 27 | + const picked = await host.pickFile(b.pickTitle, b.kind, ["botloft"]); |
| 28 | + if (picked) { |
| 29 | + setFile(picked); |
| 30 | + setManifest(null); |
| 31 | + setFailed(null); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const open = async () => { |
| 36 | + if (!file) { |
| 37 | + return; |
| 38 | + } |
| 39 | + setBusy(true); |
| 40 | + setFailed(null); |
| 41 | + try { |
| 42 | + setManifest(await api.call("backup.stage", { path: file, passphrase })); |
| 43 | + } catch (failure) { |
| 44 | + setFailed({ title: b.openFailed, text: backupError(failure, b.reasons) }); |
| 45 | + } |
| 46 | + setBusy(false); |
| 47 | + }; |
| 48 | + |
| 49 | + const restore = async () => { |
| 50 | + setBusy(true); |
| 51 | + setFailed(null); |
| 52 | + try { |
| 53 | + await api.call("backup.confirm"); |
| 54 | + await host.restartDaemon(); |
| 55 | + } catch (failure) { |
| 56 | + setFailed({ title: b.restoreFailed, text: backupError(failure, b.reasons) }); |
| 57 | + setBusy(false); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + const cancel = async () => { |
| 62 | + await api.call("backup.cancel").catch(() => null); |
| 63 | + setManifest(null); |
| 64 | + setFile(null); |
| 65 | + setPassphrase(""); |
| 66 | + }; |
| 67 | + |
| 68 | + const folders = (manifest?.crews ?? []).flatMap((crew) => |
| 69 | + crew.workFolder ? [crew.workFolder] : [], |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div className="flex flex-col gap-3"> |
| 74 | + {!manifest && ( |
| 75 | + <> |
| 76 | + <div className="flex items-center gap-3"> |
| 77 | + <Button icon={FolderOpen} onClick={pick} disabled={busy}> |
| 78 | + {b.pick} |
| 79 | + </Button> |
| 80 | + {file && ( |
| 81 | + <span className="min-w-0 truncate font-mono text-muted text-xs" title={file}> |
| 82 | + {file} |
| 83 | + </span> |
| 84 | + )} |
| 85 | + </div> |
| 86 | + {file && ( |
| 87 | + <div className="flex items-end gap-3"> |
| 88 | + <div className="min-w-0 flex-1"> |
| 89 | + <TextField |
| 90 | + type="password" |
| 91 | + autoComplete="current-password" |
| 92 | + label={b.copyPassphrase} |
| 93 | + value={passphrase} |
| 94 | + onChange={(event) => setPassphrase(event.target.value)} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + <Button disabled={busy || passphrase.length === 0} onClick={open}> |
| 98 | + {busy ? b.opening : b.open} |
| 99 | + </Button> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </> |
| 103 | + )} |
| 104 | + {manifest && ( |
| 105 | + <div className="flex flex-col gap-2 rounded-lg border border-line bg-sunken px-3 py-2.5 text-sm"> |
| 106 | + <p className="font-medium">{b.from(when(manifest.createdAt))}</p> |
| 107 | + <ul className="flex flex-col gap-0.5 text-ink-soft"> |
| 108 | + {manifest.crews.map((crew) => ( |
| 109 | + <li key={crew.name}>{b.crew(crew.name, crew.bots.length)}</li> |
| 110 | + ))} |
| 111 | + </ul> |
| 112 | + {folders.length > 0 && ( |
| 113 | + <div className="text-muted text-xs"> |
| 114 | + <p>{b.workFolders}</p> |
| 115 | + <ul className="mt-1 font-mono" data-selectable> |
| 116 | + {folders.map((folder) => ( |
| 117 | + <li key={folder} className="break-all"> |
| 118 | + {folder} |
| 119 | + </li> |
| 120 | + ))} |
| 121 | + </ul> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + <p className="text-warn text-xs">{b.replaces}</p> |
| 125 | + <div className="mt-1 flex flex-wrap gap-2"> |
| 126 | + <Button variant="danger" icon={RotateCcw} disabled={busy} onClick={restore}> |
| 127 | + {busy ? b.restoring : b.restore} |
| 128 | + </Button> |
| 129 | + <Button variant="ghost" disabled={busy} onClick={cancel}> |
| 130 | + {b.cancel} |
| 131 | + </Button> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + )} |
| 135 | + {failed && ( |
| 136 | + <Callout tone="danger" title={failed.title}> |
| 137 | + {failed.text} |
| 138 | + </Callout> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments