Files
tessera/web/src/components/reconstitute/ReconstituteWizard.tsx
Adnan Zahir c6dd52c700 feat: rename restore targets and show the application next to them
- PUT /api/v1/restore-targets/:id changes only the display name (admin, validated, audited); ids,
  identity, relations and jobs are untouched
- Relations console: Application column, inline rename, application in the target pickers
- Refresh screen: the application on each card
- registering a target suggests '<application> · <database>'

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 14:57:46 +07:00

204 lines
8.6 KiB
TypeScript

import { useEffect, useState } from "react";
import { motion } from "motion/react";
import type { Snapshot } from "../../api";
import { DatabaseRestore } from "./DatabaseRestore";
import { formatBytes, formatWhen } from "../../lib/format";
import { Button } from "../ui/Button";
import { Hint } from "../ui/EmptyState";
import { TelemetryPanel } from "../ui/Telemetry";
import { Segmented } from "../ui/Segmented";
import { Select } from "../ui/Select";
const GRACE = 12;
export function ReconstituteWizard({
snapshots,
selected,
scopedExport,
onStart,
onPoll,
onAbort,
onBusyChange,
}: {
snapshots: Snapshot[];
selected: string | null;
scopedExport?: boolean;
onStart: (snapId: string, target: string) => Promise<{
session_id: string;
session: { seconds_remaining: number; state: string };
}>;
onPoll: (id: string) => Promise<{ state: string; seconds_remaining: number }>;
onAbort: (id: string) => Promise<void>;
onBusyChange: (busy: boolean) => void;
}) {
const [snapId, setSnapId] = useState(selected ?? snapshots[0]?.id ?? "");
const [databaseMode, setDatabaseMode] = useState(true);
const [target, setTarget] = useState(scopedExport ? "restored.dump" : "./restore/out");
const [sessionId, setSessionId] = useState<string | null>(null);
const [remaining, setRemaining] = useState(GRACE);
const [state, setState] = useState("idle");
const [action, setAction] = useState<"starting" | "aborting">();
const [error, setError] = useState<string>();
const [reconnecting, setReconnecting] = useState(false);
const snap = snapshots.find((s) => s.id === snapId);
const managed = snap?.capture?.restore_ready ? snap.capture : undefined;
const inCountdown = state === "countdown" || state === "confirming";
const busy = !!action || inCountdown || state === "restoring";
useEffect(() => { onBusyChange(busy); }, [busy, onBusyChange]);
useEffect(() => {
if (!sessionId) return;
let cancelled = false;
let timer: number;
const poll = async () => {
try {
const s = await onPoll(sessionId);
if (cancelled) return;
setReconnecting(false);
setState(s.state);
setRemaining(s.seconds_remaining ?? 0);
if (["completed", "aborted", "failed", "unavailable"].includes(s.state)) {
setAction(undefined);
return;
}
} catch {
if (cancelled) return;
setReconnecting(true);
}
timer = window.setTimeout(poll, 400);
};
timer = window.setTimeout(poll, 400);
return () => {
cancelled = true;
clearTimeout(timer);
};
}, [sessionId, onPoll]);
const radius = 52;
const circumference = 2 * Math.PI * radius;
const progress = Math.max(0, Math.min(1, remaining / GRACE));
return (
<div className="restore-layout">
<div className="restore-top">
<p className="restore-lead">Choose a recovery point and a destination.</p>
{snap && (
<div className="restore-snap">
<span className="restore-snap-glyph" aria-hidden="true">◇</span>
<div className="min-w-0">
<p className="restore-snap-name">{snap.orbit_name}</p>
<p className="restore-snap-meta">{formatWhen(snap.taken_at)} · {formatBytes(snap.size_bytes)} · {snap.id.slice(0, 8)}</p>
</div>
</div>
)}
<label className="block text-sm">
<span className="field-label">Snapshot</span>
<Select
value={snapId}
onChange={(e) => { setSnapId(e.target.value); setDatabaseMode(true); }}
disabled={busy}
>
{snapshots.map((s) => (
<option key={s.id} value={s.id}>
{s.orbit_name} · {formatWhen(s.taken_at)} · {formatBytes(s.size_bytes)}
</option>
))}
</Select>
</label>
{managed && (
<div>
<p className="field-label mb-1.5">Destination</p>
<Segmented label="Recovery destination" value={databaseMode ? "database" : "file"} disabled={busy}
onChange={(value) => setDatabaseMode(value === "database")}
options={[{ value: "database", label: "Restore to database" }, { value: "file", label: "Export file on hub" }]} />
</div>
)}
</div>
{managed && databaseMode ? (
<DatabaseRestore key={snapId} snapshot={snapId} application={managed.application_id} applicationName={snap?.orbit_name} engine={managed.engine} />
) : <>
<div className="restore-fields">
<label className="block text-sm">
<span className="field-label">{scopedExport ? "Export filename on hub" : "Export file path on hub"}</span>
<input className="field field-mono" value={target} onChange={(e) => setTarget(e.target.value)} disabled={busy} />
</label>
<Hint>{scopedExport ? "The hub writes to its exports directory after the countdown." : "The hub writes after the countdown. An existing file at the destination may be overwritten."}</Hint>
<div className="flex gap-2.5">
<Button
variant="danger"
className="flex-1"
disabled={!sessionId || !inCountdown || !!action}
onClick={async () => {
if (!sessionId) return;
setAction("aborting");
setError(undefined);
try {
await onAbort(sessionId);
} catch {
setAction(undefined);
setError("Could not cancel the file export. Check the current session status.");
}
}}
>
Abort
</Button>
<Button
variant="primary"
className="flex-1"
disabled={!snap || !target.trim() || busy}
onClick={async () => {
setAction("starting");
setError(undefined);
try {
const res = await onStart(snapId, target);
setSessionId(res.session_id);
setState(res.session.state);
setRemaining(res.session.seconds_remaining);
} catch {
setError("Could not confirm that the hub started writing to the destination.");
} finally {
setAction(undefined);
}
}}
>
Begin file export
</Button>
</div>
{error && <p role="alert" className="text-orbit-red text-sm">{error}</p>}
</div>
<aside className="restore-status">
<p className="restore-status-label">Countdown</p>
{inCountdown ? (
<motion.div className="restore-count" initial={{ opacity: 0 }} animate={{ opacity: 1 }}>
<div className="restore-ring">
<svg viewBox="0 0 120 120" width="132" height="132" aria-hidden="true">
<circle cx="60" cy="60" r={radius} fill="none" stroke="#2a2218" strokeWidth="5" />
<motion.circle cx="60" cy="60" r={radius} fill="none" stroke="#e8b57e" strokeWidth="5" strokeLinecap="round"
strokeDasharray={circumference} initial={false} animate={{ strokeDashoffset: circumference * (1 - progress) }} />
</svg>
<span className="restore-ring-text"><b>{remaining}</b><i>seconds</i></span>
</div>
<p className="restore-count-note">Reconstituting in {remaining}s.<br />Abort restores nothing.</p>
</motion.div>
) : <p className="restore-idle">No export is running. Begin an export to start the countdown.</p>}
{reconnecting && <p role="status" className="text-orbit-amber text-sm">Reconnecting to the export session…</p>}
{state === "restoring" && <p role="status" className="text-orbit-amber text-sm">Exporting snapshot…</p>}
{state === "unavailable" && (
<p role="alert" className="text-orbit-red text-sm">The hub no longer has this export session. Check the destination before retrying.</p>
)}
{state === "completed" && <p className="text-orbit-green text-sm">File export complete.</p>}
{state === "aborted" && <p className="text-star-300 text-sm">File export cancelled.</p>}
{state === "failed" && <p className="text-orbit-red text-sm">File export failed. Check telemetry.</p>}
{sessionId && (
<TelemetryPanel label="Session">
{JSON.stringify({ sessionId, state, remaining }, null, 2)}
</TelemetryPanel>
)}
</aside>
</>}
</div>
);
}