"use client" import { useEffect, useMemo, useState } from 'react' import Sidebar from './components/Sidebar' import SandboxList from './components/SandboxList' import ConfigurationPanel from './components/ConfigurationPanel' import InferenceEndpointPanel from './components/InferenceEndpointPanel' import HelpPanel from './components/HelpPanel' import WizardPanel from './components/WizardPanel' import { useSandboxInventory } from './hooks/useSandboxInventory' import { createHydrationSafeDashboardSessionState, buildOperatorTerminalRoute, loadDashboardSessionState, persistDashboardSessionState, updateDashboardSessionSelection, } from './lib/dashboardSession' export default function Dashboard() { const [theme, setTheme] = useState<'light' | 'dark'>('dark') const [dashboardSession, setDashboardSession] = useState(() => createHydrationSafeDashboardSessionState()) const [isCreateMode, setIsCreateMode] = useState(false) const [isDestroyMode, setIsDestroyMode] = useState(false) const [activeView, setActiveView] = useState<'settings' | 'sandboxes' | 'help' | 'wizards'>('sandboxes') const [deletingSandboxId, setDeletingSandboxId] = useState(null) const [showDeleteConfirm, setShowDeleteConfirm] = useState(false) const [lifecycleMessage, setLifecycleMessage] = useState(null) const [deleteInProgress, setDeleteInProgress] = useState(false) const inventoryEnabled = activeView === 'sandboxes' || activeView === 'wizards' || activeView === 'help' || isCreateMode || isDestroyMode const { sandboxes, nemoclaw, loading, error, refresh } = useSandboxInventory({ enabled: inventoryEnabled, }) useEffect(() => { setDashboardSession(loadDashboardSessionState()) }, []) useEffect(() => { persistDashboardSessionState(dashboardSession) }, [dashboardSession]) useEffect(() => { document.documentElement.dataset.theme = theme }, [theme]) const selectedSandbox = useMemo( () => sandboxes.find((sandbox) => sandbox.id === dashboardSession.selectedSandboxId) ?? null, [sandboxes, dashboardSession.selectedSandboxId] ) const deletingSandbox = useMemo( () => sandboxes.find((sandbox) => sandbox.id === deletingSandboxId) ?? null, [sandboxes, deletingSandboxId] ) const toggleTheme = () => { setTheme(theme === 'light' ? 'dark' : 'light') } useEffect(() => { if (!inventoryEnabled || loading || isCreateMode || isDestroyMode) return if (sandboxes.length === 0) { if (dashboardSession.selectedSandboxId) { setDashboardSession((current) => updateDashboardSessionSelection(current, null)) } return } const selectedStillExists = sandboxes.some((sandbox) => sandbox.id === dashboardSession.selectedSandboxId) if (selectedStillExists) return const nextSelection = sandboxes.find((sandbox) => sandbox.isDefault)?.id || sandboxes[0]?.id || null setDashboardSession((current) => updateDashboardSessionSelection(current, nextSelection)) }, [ dashboardSession.selectedSandboxId, inventoryEnabled, isCreateMode, isDestroyMode, loading, sandboxes, ]) const handleSandboxSelect = (id: string | null) => { if (isDestroyMode && id) { setDeletingSandboxId(id) setShowDeleteConfirm(true) } else { setDashboardSession((current) => updateDashboardSessionSelection(current, id)) } } const clearSelection = () => { setDashboardSession((current) => updateDashboardSessionSelection(current, null)) } const sleep = (ms: number) => new Promise((resolve) => window.setTimeout(resolve, ms)) const refreshUntilSandboxVisible = async (sandboxRef: string) => { for (let attempt = 0; attempt < 8; attempt += 1) { const latest = await refresh({ force: true }) const sandbox = latest.find((item) => item.id === sandboxRef || item.name === sandboxRef) if (sandbox) return sandbox await sleep(1500) } return null } const refreshUntilSandboxGone = async (sandboxId: string, sandboxName: string) => { for (let attempt = 0; attempt < 10; attempt += 1) { const latest = await refresh({ force: true }) const stillPresent = latest.find((item) => item.id === sandboxId || item.name === sandboxName) if (!stillPresent) return { latest, gone: true } await sleep(1500) } return { latest: await refresh({ force: true }), gone: false } } const handleCreateSuccess = async (createdSandboxId: string) => { setIsCreateMode(false) setIsDestroyMode(false) setActiveView('sandboxes') setLifecycleMessage(`Refreshing inventory for ${createdSandboxId}...`) const created = await refreshUntilSandboxVisible(createdSandboxId) setLifecycleMessage(created ? `Sandbox ${created.name} is ready.` : `Sandbox ${createdSandboxId} was created, but inventory has not reported it yet.`) setDashboardSession((current) => updateDashboardSessionSelection(current, created?.id ?? null)) } const confirmDelete = async () => { if (!deletingSandboxId || deleteInProgress) return const sandbox = sandboxes.find((item) => item.id === deletingSandboxId) const sandboxName = sandbox?.name ?? deletingSandboxId try { setDeleteInProgress(true) setLifecycleMessage(`Destroying sandbox ${sandboxName}...`) const response = await fetch('/api/sandbox/delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ sandboxName }), }) const data = await response.json() if (!response.ok) throw new Error([data.error, data.stdout, data.stderr].filter(Boolean).join('\n\n') || 'Failed to destroy sandbox') const { gone } = await refreshUntilSandboxGone(deletingSandboxId, sandboxName) setDashboardSession((current) => updateDashboardSessionSelection(current, current.selectedSandboxId === deletingSandboxId ? null : current.selectedSandboxId)) setLifecycleMessage(gone ? `Sandbox ${sandboxName} destroyed.` : `Delete started for ${sandboxName}. Inventory still reports it while cleanup finishes.`) setShowDeleteConfirm(false) setDeletingSandboxId(null) setIsDestroyMode(false) } catch (err) { setLifecycleMessage(err instanceof Error ? err.message : 'Failed to destroy sandbox') } finally { setDeleteInProgress(false) } } const cancelDelete = () => { setShowDeleteConfirm(false) setDeletingSandboxId(null) } return (
sandbox.status === 'running').length} sandboxesTotal={sandboxes.length} isCreateMode={isCreateMode} isDestroyMode={isDestroyMode} activeView={activeView} onSandboxesClick={() => { setActiveView('sandboxes') setIsCreateMode(false) setIsDestroyMode(false) }} onCreateClick={() => { setActiveView('sandboxes') setIsCreateMode(true) setIsDestroyMode(false) clearSelection() setLifecycleMessage(null) }} onDestroyClick={() => { setActiveView('sandboxes') setIsDestroyMode(true) setIsCreateMode(false) setLifecycleMessage(null) }} onTerminalClick={() => { const nextUrl = buildOperatorTerminalRoute({ sandboxId: selectedSandbox?.id, dashboardSessionId: dashboardSession.dashboardSessionId, }) window.open(nextUrl, '_blank', 'noopener,noreferrer') }} terminalDisabled={!selectedSandbox} onSettingsClick={() => { setActiveView('settings') setIsCreateMode(false) setIsDestroyMode(false) clearSelection() }} onWizardsClick={() => { setActiveView('wizards') setIsCreateMode(false) setIsDestroyMode(false) clearSelection() }} onHelpClick={() => { setActiveView('help') setIsCreateMode(false) setIsDestroyMode(false) clearSelection() }} onExitMode={() => { setIsCreateMode(false) setIsDestroyMode(false) clearSelection() }} onLogout={async () => { await fetch('/api/auth/logout', { method: 'POST' }).catch(() => null) window.location.href = '/login' }} />
{activeView === 'settings' ? (

INFERENCE ENDPOINTS

Sandboxes

{sandboxes.length}

Running

{sandboxes.filter((sandbox) => sandbox.status === 'running').length}

OpenClaw

{nemoclaw?.available ? 'available' : 'not detected'}

) : activeView === 'help' ? ( ) : activeView === 'wizards' ? ( ) : isCreateMode ? (

CREATE SANDBOX

Choose a blueprint, name the sandbox, and create it.

) : isDestroyMode ? (

DESTROY SANDBOX

Click a sandbox below to destroy it.

{lifecycleMessage && (
{lifecycleMessage}
)} {inventoryEnabled && loading ? (
INITIALIZING...
) : inventoryEnabled && error ? (

Inventory Unavailable

{error}

) : ( )}
) : ( <>

{sandboxes.filter((sandbox) => sandbox.status === 'running').length} online / {sandboxes.length} total

OPENSHELL CONTROL

Manage local OpenShell sandboxes and operator access.

{lifecycleMessage && (
{lifecycleMessage}
)} {inventoryEnabled && loading ? (
INITIALIZING...
) : inventoryEnabled && error ? (

Inventory Unavailable

{error}

) : sandboxes.length === 0 ? (

No Sandboxes Detected

No live OpenShell sandboxes reported yet

) : ( )} )}
{showDeleteConfirm && (

WARNING: DESTRUCTIVE ACTION

Destroying {deletingSandbox?.name ?? 'this sandbox'} will permanently delete it and it will not be recoverable. Are you sure?

{lifecycleMessage && (
{lifecycleMessage}
)}
)}
) }