Update virtual-machines.tsx

This commit is contained in:
MacRimi
2025-10-20 23:24:38 +02:00
parent 2f0fabea7a
commit 3c654ab495

View File

@@ -20,7 +20,7 @@ import {
Container, Container,
} from "lucide-react" } from "lucide-react"
import useSWR from "swr" import useSWR from "swr"
import { MetricsDialog } from "./metrics-dialog" // Import MetricsDialog import { MetricsView } from "./metrics-dialog"
interface VMData { interface VMData {
vmid: number vmid: number
@@ -720,42 +720,14 @@ export function VirtualMachines() {
setSelectedMetric(null) setSelectedMetric(null)
}} }}
> >
<DialogContent className="max-w-4xl max-h-[95vh] p-0 flex flex-col overflow-hidden"> <DialogContent className="max-w-4xl max-h-[95vh] flex flex-col p-0">
{currentView === "main" ? ( {currentView === "main" ? (
<> <>
<DialogHeader className="pb-4 border-b border-border px-6 pt-6 flex-shrink-0"> <DialogHeader className="pb-4 border-b border-border px-6 pt-6">
<DialogTitle className="flex flex-col sm:flex-row sm:items-center gap-3"> <DialogTitle className="flex flex-col sm:flex-row sm:items-center gap-3">
{/* Desktop layout */}
<div className="hidden sm:flex items-center gap-3 flex-wrap">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<Server className="h-5 w-5 flex-shrink-0" /> <Server className="h-5 w-5 flex-shrink-0" />
<span className="text-lg truncate">{selectedVM?.name}</span> <span className="text-lg truncate">{selectedVM?.name}</span>
<span className="text-sm text-muted-foreground">ID: {selectedVM?.vmid}</span>
</div>
{selectedVM && (
<>
<Badge variant="outline" className={`${getTypeBadge(selectedVM.type).color} flex-shrink-0`}>
{getTypeBadge(selectedVM.type).icon}
{getTypeBadge(selectedVM.type).label}
</Badge>
<Badge variant="outline" className={`${getStatusColor(selectedVM.status)} flex-shrink-0`}>
{selectedVM.status.toUpperCase()}
</Badge>
{selectedVM.status === "running" && (
<span className="text-sm text-muted-foreground">
Uptime: {formatUptime(selectedVM.uptime)}
</span>
)}
</>
)}
</div>
{/* Mobile layout */}
<div className="flex sm:hidden flex-col gap-2">
<div className="flex items-center gap-2">
<Server className="h-5 w-5 flex-shrink-0" />
<span className="text-lg truncate">{selectedVM?.name}</span>
<span className="text-sm text-muted-foreground">ID: {selectedVM?.vmid}</span>
</div> </div>
{selectedVM && ( {selectedVM && (
<div className="flex items-center gap-2 flex-wrap"> <div className="flex items-center gap-2 flex-wrap">
@@ -766,14 +738,8 @@ export function VirtualMachines() {
<Badge variant="outline" className={`${getStatusColor(selectedVM.status)} flex-shrink-0`}> <Badge variant="outline" className={`${getStatusColor(selectedVM.status)} flex-shrink-0`}>
{selectedVM.status.toUpperCase()} {selectedVM.status.toUpperCase()}
</Badge> </Badge>
{selectedVM.status === "running" && (
<span className="text-sm text-muted-foreground">
Uptime: {formatUptime(selectedVM.uptime)}
</span>
)}
</div> </div>
)} )}
</div>
</DialogTitle> </DialogTitle>
</DialogHeader> </DialogHeader>
@@ -786,14 +752,19 @@ export function VirtualMachines() {
Basic Information Basic Information
</h3> </h3>
<div className="grid grid-cols-2 lg:grid-cols-3 gap-4"> <div className="grid grid-cols-2 lg:grid-cols-3 gap-4">
<div>
<div className="text-xs text-muted-foreground mb-1">Name</div>
<div className="font-semibold text-foreground">{selectedVM.name}</div>
</div>
<div>
<div className="text-xs text-muted-foreground mb-1">VMID</div>
<div className="font-semibold text-foreground">{selectedVM.vmid}</div>
</div>
<div> <div>
<div className="text-xs text-muted-foreground mb-1">CPU Usage</div> <div className="text-xs text-muted-foreground mb-1">CPU Usage</div>
<div <div
className="cursor-pointer hover:opacity-80 transition-opacity" className="cursor-pointer hover:opacity-80 transition-opacity"
onClick={(e) => { onClick={() => handleMetricClick("cpu")}
e.stopPropagation()
handleMetricClick("cpu")
}}
> >
<div className={`font-semibold mb-1 ${getUsageColor(selectedVM.cpu * 100)}`}> <div className={`font-semibold mb-1 ${getUsageColor(selectedVM.cpu * 100)}`}>
{(selectedVM.cpu * 100).toFixed(1)}% {(selectedVM.cpu * 100).toFixed(1)}%
@@ -808,10 +779,7 @@ export function VirtualMachines() {
<div className="text-xs text-muted-foreground mb-1">Memory</div> <div className="text-xs text-muted-foreground mb-1">Memory</div>
<div <div
className="cursor-pointer hover:opacity-80 transition-opacity" className="cursor-pointer hover:opacity-80 transition-opacity"
onClick={(e) => { onClick={() => handleMetricClick("memory")}
e.stopPropagation()
handleMetricClick("memory")
}}
> >
<div <div
className={`font-semibold mb-1 ${getUsageColor((selectedVM.mem / selectedVM.maxmem) * 100)}`} className={`font-semibold mb-1 ${getUsageColor((selectedVM.mem / selectedVM.maxmem) * 100)}`}
@@ -829,10 +797,7 @@ export function VirtualMachines() {
<div className="text-xs text-muted-foreground mb-1">Disk</div> <div className="text-xs text-muted-foreground mb-1">Disk</div>
<div <div
className="cursor-pointer hover:opacity-80 transition-opacity" className="cursor-pointer hover:opacity-80 transition-opacity"
onClick={(e) => { onClick={() => handleMetricClick("disk")}
e.stopPropagation()
handleMetricClick("disk")
}}
> >
<div <div
className={`font-semibold mb-1 ${getUsageColor((selectedVM.disk / selectedVM.maxdisk) * 100)}`} className={`font-semibold mb-1 ${getUsageColor((selectedVM.disk / selectedVM.maxdisk) * 100)}`}
@@ -846,14 +811,15 @@ export function VirtualMachines() {
/> />
</div> </div>
</div> </div>
<div>
<div className="text-xs text-muted-foreground mb-1">Uptime</div>
<div className="font-semibold text-foreground">{formatUptime(selectedVM.uptime)}</div>
</div>
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Disk I/O</div> <div className="text-xs text-muted-foreground mb-1">Disk I/O</div>
<div <div
className="cursor-pointer hover:opacity-80 transition-opacity" className="cursor-pointer hover:opacity-80 transition-opacity"
onClick={(e) => { onClick={() => handleMetricClick("disk")}
e.stopPropagation()
handleMetricClick("disk")
}}
> >
<div className="text-sm text-green-500 flex items-center gap-1"> <div className="text-sm text-green-500 flex items-center gap-1">
<span></span> <span></span>
@@ -869,10 +835,7 @@ export function VirtualMachines() {
<div className="text-xs text-muted-foreground mb-1">Network I/O</div> <div className="text-xs text-muted-foreground mb-1">Network I/O</div>
<div <div
className="cursor-pointer hover:opacity-80 transition-opacity" className="cursor-pointer hover:opacity-80 transition-opacity"
onClick={(e) => { onClick={() => handleMetricClick("network")}
e.stopPropagation()
handleMetricClick("network")
}}
> >
<div className="text-sm text-green-500 flex items-center gap-1"> <div className="text-sm text-green-500 flex items-center gap-1">
<span></span> <span></span>
@@ -887,36 +850,40 @@ export function VirtualMachines() {
</div> </div>
</div> </div>
{detailsLoading ? (
<div className="text-center py-8 text-muted-foreground">Loading configuration...</div>
) : vmDetails?.config ? (
<>
<div> <div>
<h3 className="text-sm font-semibold text-muted-foreground mb-3 uppercase tracking-wide"> <h3 className="text-sm font-semibold text-muted-foreground mb-3 uppercase tracking-wide">
Resources Resources
</h3> </h3>
<div className="grid grid-cols-2 lg:grid-cols-3 gap-4"> <div className="grid grid-cols-2 lg:grid-cols-3 gap-4">
{vmDetails?.config.cores && ( {vmDetails.config.cores && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">CPU Cores</div> <div className="text-xs text-muted-foreground mb-1">CPU Cores</div>
<div className="font-semibold text-blue-500">{vmDetails.config.cores}</div> <div className="font-semibold text-blue-500">{vmDetails.config.cores}</div>
</div> </div>
)} )}
{vmDetails?.config.sockets && ( {vmDetails.config.sockets && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">CPU Sockets</div> <div className="text-xs text-muted-foreground mb-1">CPU Sockets</div>
<div className="font-semibold text-foreground">{vmDetails.config.sockets}</div> <div className="font-semibold text-foreground">{vmDetails.config.sockets}</div>
</div> </div>
)} )}
{vmDetails?.config.memory && ( {vmDetails.config.memory && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Memory</div> <div className="text-xs text-muted-foreground mb-1">Memory</div>
<div className="font-semibold text-blue-500">{vmDetails.config.memory} MB</div> <div className="font-semibold text-blue-500">{vmDetails.config.memory} MB</div>
</div> </div>
)} )}
{vmDetails?.config.swap && ( {vmDetails.config.swap && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Swap</div> <div className="text-xs text-muted-foreground mb-1">Swap</div>
<div className="font-semibold text-foreground">{vmDetails.config.swap} MB</div> <div className="font-semibold text-foreground">{vmDetails.config.swap} MB</div>
</div> </div>
)} )}
{vmDetails?.config.rootfs && ( {vmDetails.config.rootfs && (
<div className="col-span-2 lg:col-span-3"> <div className="col-span-2 lg:col-span-3">
<div className="text-xs text-muted-foreground mb-1">Root Filesystem</div> <div className="text-xs text-muted-foreground mb-1">Root Filesystem</div>
<div className="font-medium text-foreground text-sm break-all font-mono"> <div className="font-medium text-foreground text-sm break-all font-mono">
@@ -924,7 +891,7 @@ export function VirtualMachines() {
</div> </div>
</div> </div>
)} )}
{Object.keys(vmDetails?.config || {}) {Object.keys(vmDetails.config)
.filter((key) => key.match(/^(scsi|sata|ide|virtio)\d+$/)) .filter((key) => key.match(/^(scsi|sata|ide|virtio)\d+$/))
.map((diskKey) => ( .map((diskKey) => (
<div key={diskKey} className="col-span-2 lg:col-span-3"> <div key={diskKey} className="col-span-2 lg:col-span-3">
@@ -932,7 +899,7 @@ export function VirtualMachines() {
{diskKey.toUpperCase().replace(/(\d+)/, " $1")} {diskKey.toUpperCase().replace(/(\d+)/, " $1")}
</div> </div>
<div className="font-medium text-foreground text-sm break-all font-mono"> <div className="font-medium text-foreground text-sm break-all font-mono">
{vmDetails?.config[diskKey]} {vmDetails.config[diskKey]}
</div> </div>
</div> </div>
))} ))}
@@ -944,7 +911,7 @@ export function VirtualMachines() {
Network Network
</h3> </h3>
<div className="grid grid-cols-1 lg:grid-cols-2 gap-4"> <div className="grid grid-cols-1 lg:grid-cols-2 gap-4">
{Object.keys(vmDetails?.config || {}) {Object.keys(vmDetails.config)
.filter((key) => key.match(/^net\d+$/)) .filter((key) => key.match(/^net\d+$/))
.map((netKey) => ( .map((netKey) => (
<div key={netKey} className="col-span-1"> <div key={netKey} className="col-span-1">
@@ -952,23 +919,25 @@ export function VirtualMachines() {
Network Interface {netKey.replace("net", "")} Network Interface {netKey.replace("net", "")}
</div> </div>
<div className="font-medium text-green-500 text-sm break-all font-mono"> <div className="font-medium text-green-500 text-sm break-all font-mono">
{vmDetails?.config[netKey]} {vmDetails.config[netKey]}
</div> </div>
</div> </div>
))} ))}
{vmDetails?.config.nameserver && ( {vmDetails.config.nameserver && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">DNS Nameserver</div> <div className="text-xs text-muted-foreground mb-1">DNS Nameserver</div>
<div className="font-medium text-foreground font-mono">{vmDetails.config.nameserver}</div> <div className="font-medium text-foreground font-mono">
{vmDetails.config.nameserver}
</div>
</div> </div>
)} )}
{vmDetails?.config.searchdomain && ( {vmDetails.config.searchdomain && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Search Domain</div> <div className="text-xs text-muted-foreground mb-1">Search Domain</div>
<div className="font-medium text-foreground">{vmDetails.config.searchdomain}</div> <div className="font-medium text-foreground">{vmDetails.config.searchdomain}</div>
</div> </div>
)} )}
{vmDetails?.config.hostname && ( {vmDetails.config.hostname && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Hostname</div> <div className="text-xs text-muted-foreground mb-1">Hostname</div>
<div className="font-medium text-foreground">{vmDetails.config.hostname}</div> <div className="font-medium text-foreground">{vmDetails.config.hostname}</div>
@@ -982,7 +951,7 @@ export function VirtualMachines() {
Options Options
</h3> </h3>
<div className="grid grid-cols-2 lg:grid-cols-3 gap-4"> <div className="grid grid-cols-2 lg:grid-cols-3 gap-4">
{vmDetails?.config.onboot !== undefined && ( {vmDetails.config.onboot !== undefined && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Start on Boot</div> <div className="text-xs text-muted-foreground mb-1">Start on Boot</div>
<Badge <Badge
@@ -997,7 +966,7 @@ export function VirtualMachines() {
</Badge> </Badge>
</div> </div>
)} )}
{vmDetails?.config.unprivileged !== undefined && ( {vmDetails.config.unprivileged !== undefined && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Unprivileged</div> <div className="text-xs text-muted-foreground mb-1">Unprivileged</div>
<Badge <Badge
@@ -1012,25 +981,25 @@ export function VirtualMachines() {
</Badge> </Badge>
</div> </div>
)} )}
{vmDetails?.config.ostype && ( {vmDetails.config.ostype && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">OS Type</div> <div className="text-xs text-muted-foreground mb-1">OS Type</div>
<div className="font-medium text-foreground">{vmDetails.config.ostype}</div> <div className="font-medium text-foreground">{vmDetails.config.ostype}</div>
</div> </div>
)} )}
{vmDetails?.config.arch && ( {vmDetails.config.arch && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Architecture</div> <div className="text-xs text-muted-foreground mb-1">Architecture</div>
<div className="font-medium text-foreground">{vmDetails.config.arch}</div> <div className="font-medium text-foreground">{vmDetails.config.arch}</div>
</div> </div>
)} )}
{vmDetails?.config.boot && ( {vmDetails.config.boot && (
<div> <div>
<div className="text-xs text-muted-foreground mb-1">Boot Order</div> <div className="text-xs text-muted-foreground mb-1">Boot Order</div>
<div className="font-medium text-foreground">{vmDetails.config.boot}</div> <div className="font-medium text-foreground">{vmDetails.config.boot}</div>
</div> </div>
)} )}
{vmDetails?.config.features && ( {vmDetails.config.features && (
<div className="col-span-2 lg:col-span-3"> <div className="col-span-2 lg:col-span-3">
<div className="text-xs text-muted-foreground mb-1">Features</div> <div className="text-xs text-muted-foreground mb-1">Features</div>
<div className="font-medium text-foreground text-sm">{vmDetails.config.features}</div> <div className="font-medium text-foreground text-sm">{vmDetails.config.features}</div>
@@ -1039,11 +1008,13 @@ export function VirtualMachines() {
</div> </div>
</div> </div>
</> </>
) : null}
</>
)} )}
</div> </div>
</div> </div>
<div className="border-t border-border bg-background px-6 py-4 flex-shrink-0"> <div className="border-t border-border bg-background px-6 py-4 mt-auto">
<h3 className="text-sm font-semibold text-muted-foreground mb-3 uppercase tracking-wide"> <h3 className="text-sm font-semibold text-muted-foreground mb-3 uppercase tracking-wide">
Control Actions Control Actions
</h3> </h3>
@@ -1083,18 +1054,19 @@ export function VirtualMachines() {
</div> </div>
</div> </div>
</> </>
) : currentView === "metrics" && selectedMetric ? ( ) : (
<MetricsDialog /* Render metrics view when currentView is "metrics" */
vmid={selectedVM?.vmid || 0} selectedVM &&
vmName={selectedVM?.name || ""} selectedMetric && (
vmType={selectedVM?.type || "qemu"} <MetricsView
metric={selectedMetric} vmid={selectedVM.vmid}
onBack={() => { vmName={selectedVM.name}
setCurrentView("main") vmType={selectedVM.type as "qemu" | "lxc"}
setSelectedMetric(null) metricType={selectedMetric}
}} onBack={handleBackToMain}
/> />
) : null} )
)}
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</div> </div>