"use client" import { useState } from "react" import type { SandboxInventoryItem } from "../hooks/useSandboxInventory" interface SandboxArchivePanelProps { sandbox: SandboxInventoryItem onRestoreComplete?: () => Promise | void } export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: SandboxArchivePanelProps) { const [backupPath, setBackupPath] = useState("/sandbox") const [restorePath, setRestorePath] = useState("/sandbox") const [restoreReplace, setRestoreReplace] = useState(false) const [selectedArchive, setSelectedArchive] = useState(null) const [busy, setBusy] = useState<"backup" | "restore" | null>(null) const [message, setMessage] = useState("") async function backupSandbox() { if (!backupPath.trim() || busy) return try { setBusy("backup") setMessage("") const pathToBackup = backupPath.trim() const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/backup?${new URLSearchParams({ path: pathToBackup })}`) if (!response.ok) { const data = await response.json().catch(() => ({})) throw new Error(data.error || "Failed to create sandbox backup") } const blob = await response.blob() const contentDisposition = response.headers.get("content-disposition") || "" const fileName = decodeURIComponent(contentDisposition.match(/filename\*=UTF-8''([^;]+)/)?.[1] || "") || contentDisposition.match(/filename="([^"]+)"/)?.[1] || `${sandbox.name}-backup.tar.gz` const url = window.URL.createObjectURL(blob) const anchor = document.createElement("a") anchor.href = url anchor.download = fileName document.body.appendChild(anchor) anchor.click() anchor.remove() window.URL.revokeObjectURL(url) setMessage(`Created backup for ${pathToBackup}: ${fileName}.`) } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to create sandbox backup") } finally { setBusy(null) } } async function restoreSandbox() { if (!selectedArchive || !restorePath.trim() || busy) return try { setBusy("restore") setMessage("") const form = new FormData() form.set("archive", selectedArchive) form.set("targetPath", restorePath.trim()) form.set("replace", restoreReplace ? "true" : "false") const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/restore`, { method: "POST", body: form, }) const data = await response.json() if (!response.ok) throw new Error(data.error || "Failed to restore sandbox backup") setMessage(data.note || `Restored ${selectedArchive.name} into ${restorePath.trim()}.`) await onRestoreComplete?.() } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to restore sandbox backup") } finally { setBusy(null) } } return (
Backup / Restore

Export sandbox contents as a compressed archive, or restore an archive into this sandbox.

tar.gz
Backup

Archive a directory for cold storage or cloning.

Restore

Merge into the target directory, or replace it first.

setSelectedArchive(event.target.files?.[0] || null)} className="block w-full text-xs text-[var(--foreground-dim)] file:mr-3 file:rounded-sm file:border file:border-[var(--border-subtle)] file:bg-[var(--background-tertiary)] file:px-3 file:py-2 file:text-xs file:font-mono file:uppercase file:text-[var(--foreground)]" />
{message && (
{message}
)}
) }