Add dashboard backup catalog and health tools
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useEffect, useState } from "react"
|
||||
import type { SandboxInventoryItem } from "../hooks/useSandboxInventory"
|
||||
|
||||
interface SandboxArchivePanelProps {
|
||||
@@ -8,14 +8,41 @@ interface SandboxArchivePanelProps {
|
||||
onRestoreComplete?: () => Promise<void> | void
|
||||
}
|
||||
|
||||
type BackupCatalogEntry = {
|
||||
id: string
|
||||
fileName: string
|
||||
sandboxName: string
|
||||
sourcePath: string
|
||||
size: number
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
function formatBytes(value: number) {
|
||||
if (value < 1024) return `${value} B`
|
||||
if (value < 1024 * 1024) return `${Math.ceil(value / 1024)} KiB`
|
||||
return `${(value / 1024 / 1024).toFixed(1)} MiB`
|
||||
}
|
||||
|
||||
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<File | null>(null)
|
||||
const [busy, setBusy] = useState<"backup" | "restore" | null>(null)
|
||||
const [catalogBackups, setCatalogBackups] = useState<BackupCatalogEntry[]>([])
|
||||
const [busy, setBusy] = useState<"backup" | "catalog" | "restore" | `restore-${string}` | `delete-${string}` | null>(null)
|
||||
const [message, setMessage] = useState("")
|
||||
|
||||
async function loadCatalog() {
|
||||
const response = await fetch("/api/backups", { cache: "no-store" })
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to load backup catalog")
|
||||
setCatalogBackups(Array.isArray(data.backups) ? data.backups : [])
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
loadCatalog().catch(() => undefined)
|
||||
}, [sandbox.id])
|
||||
|
||||
async function backupSandbox() {
|
||||
if (!backupPath.trim() || busy) return
|
||||
try {
|
||||
@@ -49,6 +76,27 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
}
|
||||
}
|
||||
|
||||
async function saveCatalogBackup() {
|
||||
if (!backupPath.trim() || busy) return
|
||||
try {
|
||||
setBusy("catalog")
|
||||
setMessage("")
|
||||
const response = await fetch("/api/backups", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sandboxId: sandbox.id, sourcePath: backupPath.trim() }),
|
||||
})
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to save backup to catalog")
|
||||
setMessage(`Saved ${data.backup.fileName} to the local backup catalog.`)
|
||||
await loadCatalog()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to save backup to catalog")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreSandbox() {
|
||||
if (!selectedArchive || !restorePath.trim() || busy) return
|
||||
try {
|
||||
@@ -73,6 +121,44 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreCatalogBackup(backupId: string) {
|
||||
if (!restorePath.trim() || busy) return
|
||||
try {
|
||||
setBusy(`restore-${backupId}`)
|
||||
setMessage("")
|
||||
const response = await fetch(`/api/backups/${encodeURIComponent(backupId)}/restore`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ sandboxId: sandbox.id, targetPath: restorePath.trim(), replace: restoreReplace }),
|
||||
})
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to restore catalog backup")
|
||||
setMessage(data.note || `Restored catalog backup into ${restorePath.trim()}.`)
|
||||
await onRestoreComplete?.()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to restore catalog backup")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteCatalogBackup(backupId: string) {
|
||||
if (busy) return
|
||||
try {
|
||||
setBusy(`delete-${backupId}`)
|
||||
setMessage("")
|
||||
const response = await fetch(`/api/backups/${encodeURIComponent(backupId)}`, { method: "DELETE" })
|
||||
const data = await response.json()
|
||||
if (!response.ok) throw new Error(data.error || "Failed to delete catalog backup")
|
||||
setMessage("Deleted catalog backup.")
|
||||
await loadCatalog()
|
||||
} catch (error) {
|
||||
setMessage(error instanceof Error ? error.message : "Failed to delete catalog backup")
|
||||
} finally {
|
||||
setBusy(null)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-start justify-between gap-4 max-lg:flex-col">
|
||||
@@ -102,14 +188,24 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]"
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
type="button"
|
||||
onClick={backupSandbox}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="rounded-sm bg-[var(--nvidia-green)] px-4 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === "backup" ? "Creating Backup..." : "Download Backup"}
|
||||
</button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={backupSandbox}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="rounded-sm bg-[var(--nvidia-green)] px-4 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === "backup" ? "Creating Backup..." : "Download Backup"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={saveCatalogBackup}
|
||||
disabled={!backupPath.trim() || busy !== null}
|
||||
className="action-button px-4 py-2"
|
||||
>
|
||||
{busy === "catalog" ? "Saving..." : "Save To Catalog"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 space-y-3">
|
||||
@@ -155,6 +251,59 @@ export default function SandboxArchivePanel({ sandbox, onRestoreComplete }: Sand
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-4 space-y-3">
|
||||
<div className="flex items-start justify-between gap-4 max-md:flex-col">
|
||||
<div>
|
||||
<h6 className="text-[11px] font-semibold uppercase tracking-wider text-[var(--foreground)]">Backup Catalog</h6>
|
||||
<p className="mt-1 text-xs text-[var(--foreground-dim)]">Host-side cold storage for cloning and redeploying sandboxes later.</p>
|
||||
</div>
|
||||
<button type="button" onClick={() => loadCatalog().catch((error) => setMessage(error.message))} className="action-button px-3 py-2">
|
||||
Refresh Catalog
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{catalogBackups.map((backup) => (
|
||||
<div key={backup.id} className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-3">
|
||||
<div className="flex items-start justify-between gap-4 max-lg:flex-col">
|
||||
<div className="min-w-0">
|
||||
<p className="truncate text-xs font-mono text-[var(--foreground)]">{backup.fileName}</p>
|
||||
<p className="mt-1 text-[10px] uppercase tracking-wider text-[var(--foreground-dim)]">
|
||||
{backup.sandboxName} / {backup.sourcePath} / {formatBytes(backup.size)} / {new Date(backup.createdAt).toLocaleString()}
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex shrink-0 flex-wrap gap-2">
|
||||
<a href={`/api/backups/${encodeURIComponent(backup.id)}/download`} className="action-button px-3 py-2">
|
||||
Download
|
||||
</a>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => restoreCatalogBackup(backup.id)}
|
||||
disabled={busy !== null || !restorePath.trim()}
|
||||
className="rounded-sm border border-[var(--nvidia-green)] bg-[var(--nvidia-green)] px-3 py-2 text-xs font-mono uppercase tracking-wider text-black disabled:opacity-50"
|
||||
>
|
||||
{busy === `restore-${backup.id}` ? "Restoring..." : "Restore Here"}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => deleteCatalogBackup(backup.id)}
|
||||
disabled={busy !== null}
|
||||
className="action-button px-3 py-2"
|
||||
>
|
||||
{busy === `delete-${backup.id}` ? "Deleting..." : "Delete"}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
{catalogBackups.length === 0 && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background-tertiary)] p-4 text-sm text-[var(--foreground-dim)]">
|
||||
No catalog backups saved yet.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{message && (
|
||||
<div className="rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] p-3 text-xs text-[var(--foreground-dim)] whitespace-pre-wrap">
|
||||
{message}
|
||||
|
||||
Reference in New Issue
Block a user