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; 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(null); const [remaining, setRemaining] = useState(GRACE); const [state, setState] = useState("idle"); const [action, setAction] = useState<"starting" | "aborting">(); const [error, setError] = useState(); 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 (

Choose a recovery point and a destination.

{snap && (

{snap.orbit_name}

{formatWhen(snap.taken_at)} · {formatBytes(snap.size_bytes)} · {snap.id.slice(0, 8)}

)} {managed && (

Destination

setDatabaseMode(value === "database")} options={[{ value: "database", label: "Restore to database" }, { value: "file", label: "Export file on hub" }]} />
)}
{managed && databaseMode ? ( ) : <>
{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."}
{error &&

{error}

}
}
); }