'use client'; import { use, useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import StatusBadge from '@/components/StatusBadge'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Skeleton from '@/components/ui/Skeleton'; import { ArrowLeft, Play, Square, RotateCw, HardDrive, Cpu, MemoryStick } from 'lucide-react'; interface VMDetails { vmid: string; name: string; status: string; mem: string; bootdisk: string; config: Record; stats: { cpu: number; mem: { used: number; total: number; percent: number; }; disk: { percent: number; }; }; } export default function VMDetailPage({ params, }: { params: Promise<{ id: string }>; }) { const resolvedParams = use(params); const router = useRouter(); const [vm, setVm] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [actionLoading, setActionLoading] = useState(false); const [toast, setToast] = useState<{ message: string; type: 'success' | 'error' } | null>(null); const fetchVMDetails = async () => { try { const res = await fetch(`/api/vms/${resolvedParams.id}`); if (!res.ok) throw new Error('Failed to fetch VM details'); const data = await res.json(); setVm(data); setError(null); } catch (err) { setError(err instanceof Error ? err.message : 'Unknown error'); } finally { setLoading(false); } }; const handleAction = async (action: 'start' | 'stop' | 'restart') => { setActionLoading(true); try { const res = await fetch(`/api/vms/${resolvedParams.id}/action`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action }), }); if (!res.ok) throw new Error('Failed to perform action'); setToast({ message: `VM ${action}ing...`, type: 'success' }); setTimeout(() => setToast(null), 3000); setTimeout(fetchVMDetails, 2000); } catch (err) { setToast({ message: err instanceof Error ? err.message : 'Action failed', type: 'error' }); setTimeout(() => setToast(null), 3000); } finally { setActionLoading(false); } }; useEffect(() => { fetchVMDetails(); const interval = setInterval(fetchVMDetails, 5000); return () => clearInterval(interval); }, [resolvedParams.id]); if (loading) { return (
); } if (error || !vm) { return (

Error: {error || 'VM not found'}

); } return (
{/* Toast Notification */} {toast && (
{toast.message}
)}
{/* Back Button */} {/* VM Header */}

{vm.name}

VM ID: {vm.vmid}

{/* Stats Grid */}

CPU Usage

{vm.stats.cpu}%

Memory Usage

{vm.stats.mem.percent}%

{vm.stats.mem.used} MB / {vm.stats.mem.total} MB

Disk Usage

{vm.stats.disk.percent}%

{/* Control Panel */}

Control Panel

{/* Configuration */}

Configuration

{Object.entries(vm.config).map(([key, value]) => (

{key}

{value}

))}
); }