"use client" import { useMemo, useState } from "react" import type { SandboxInventoryItem } from "../hooks/useSandboxInventory" export default function SandboxFilesPanel({ sandbox, embedded = false, showHeader = true, }: { sandbox: SandboxInventoryItem embedded?: boolean showHeader?: boolean }) { const [selectedFile, setSelectedFile] = useState(null) const [uploadPath, setUploadPath] = useState("/sandbox/") const [downloadPath, setDownloadPath] = useState("/sandbox/") const [busy, setBusy] = useState<"upload" | "download" | null>(null) const [message, setMessage] = useState("") const suggestedUploadPath = useMemo(() => { if (!selectedFile) return uploadPath return uploadPath.endsWith("/") ? `${uploadPath}${selectedFile.name}` : uploadPath }, [selectedFile, uploadPath]) async function uploadFile() { if (!selectedFile || busy) return try { setBusy("upload") setMessage("") const form = new FormData() form.set("file", selectedFile) form.set("path", uploadPath) const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/files/upload`, { method: "POST", body: form, }) const data = await response.json() if (!response.ok) throw new Error(data.error || "Failed to upload file") setMessage(data.note || `Uploaded to ${data.uploaded?.path || suggestedUploadPath}.`) setDownloadPath(data.uploaded?.path || suggestedUploadPath) } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to upload file") } finally { setBusy(null) } } async function downloadFile() { if (!downloadPath.trim() || busy) return try { setBusy("download") setMessage("") const response = await fetch(`/api/sandbox/${encodeURIComponent(sandbox.id)}/files/download?${new URLSearchParams({ path: downloadPath.trim() })}`) if (!response.ok) { const data = await response.json().catch(() => ({})) throw new Error(data.error || "Failed to download file") } 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] || downloadPath.split("/").filter(Boolean).pop() || "download.bin" 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(`Downloaded ${downloadPath.trim()}.`) } catch (error) { setMessage(error instanceof Error ? error.message : "Failed to download file") } finally { setBusy(null) } } return (
{showHeader && (

{sandbox.name} - FILE TRANSFER

Move files through scoped sandbox paths.

)}
Upload

Destination must be under /sandbox or /tmp.

setSelectedFile(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)] file:px-3 file:py-2 file:text-xs file:font-mono file:uppercase file:text-[var(--foreground)]" />
setUploadPath(event.target.value)} placeholder="/sandbox/file.txt" className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" /> {selectedFile && (

Target: {suggestedUploadPath}

)}
Download

Enter a regular file path under /sandbox or /tmp.

setDownloadPath(event.target.value)} placeholder="/sandbox/file.txt" className="w-full rounded-sm border border-[var(--border-subtle)] bg-[var(--background)] px-3 py-2 text-xs font-mono text-[var(--foreground)] focus:outline-none focus:border-[var(--nvidia-green)]" />
{message && (
{message}
)}
) }