"use client" import { useState, useEffect } from "react" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Badge } from "@/components/ui/badge" import { Loader2, CheckCircle2, AlertTriangle, XCircle, Activity, Cpu, MemoryStick, HardDrive, Disc, Network, Box, Settings, FileText, RefreshCw, Shield, } from "lucide-react" interface CategoryCheck { status: string reason?: string details?: any [key: string]: any } interface HealthDetails { overall: string summary: string details: { cpu: CategoryCheck memory: CategoryCheck storage: CategoryCheck disks: CategoryCheck network: CategoryCheck vms: CategoryCheck services: CategoryCheck logs: CategoryCheck updates: CategoryCheck security: CategoryCheck } timestamp: string } interface HealthStatusModalProps { open: boolean onOpenChange: (open: boolean) => void getApiUrl: (path: string) => string } const CATEGORIES = [ { key: "cpu", label: "CPU Usage & Temperature", Icon: Cpu }, { key: "memory", label: "Memory & Swap", Icon: MemoryStick }, { key: "storage", label: "Storage Mounts & Space", Icon: HardDrive }, { key: "disks", label: "Disk I/O & Errors", Icon: Disc }, { key: "network", label: "Network Interfaces", Icon: Network }, { key: "vms", label: "VMs & Containers", Icon: Box }, { key: "services", label: "PVE Services", Icon: Settings }, { key: "logs", label: "System Logs", Icon: FileText }, { key: "updates", label: "System Updates", Icon: RefreshCw }, { key: "security", label: "Security & Certificates", Icon: Shield }, ] export function HealthStatusModal({ open, onOpenChange, getApiUrl }: HealthStatusModalProps) { const [loading, setLoading] = useState(true) const [healthData, setHealthData] = useState(null) const [error, setError] = useState(null) useEffect(() => { if (open) { fetchHealthDetails() } }, [open]) const fetchHealthDetails = async () => { setLoading(true) setError(null) try { const response = await fetch(getApiUrl("/api/health/details")) if (!response.ok) { throw new Error("Failed to fetch health details") } const data = await response.json() console.log("[v0] Health data received:", data) setHealthData(data) } catch (err) { console.error("[v0] Error fetching health data:", err) setError(err instanceof Error ? err.message : "Unknown error") } finally { setLoading(false) } } const getStatusIcon = (status: string) => { const statusUpper = status?.toUpperCase() switch (statusUpper) { case "OK": return case "WARNING": return case "CRITICAL": return default: return } } const getStatusBadge = (status: string) => { const statusUpper = status?.toUpperCase() switch (statusUpper) { case "OK": return OK case "WARNING": return Warning case "CRITICAL": return Critical default: return Unknown } } const getHealthStats = () => { if (!healthData?.details) { return { total: 0, healthy: 0, warnings: 0, critical: 0 } } let healthy = 0 let warnings = 0 let critical = 0 CATEGORIES.forEach(({ key }) => { const categoryData = healthData.details[key as keyof typeof healthData.details] if (categoryData) { const status = categoryData.status?.toUpperCase() if (status === "OK") healthy++ else if (status === "WARNING") warnings++ else if (status === "CRITICAL") critical++ } }) return { total: CATEGORIES.length, healthy, warnings, critical } } const stats = getHealthStats() return (
System Health Status
{healthData && getStatusBadge(healthData.overall)}
Detailed health checks for all system components
{loading && (
)} {error && (

Error loading health status

{error}

)} {healthData && !loading && (
{/* Overall Stats Summary */}
{stats.total}
Total Checks
{stats.healthy}
Healthy
{stats.warnings}
Warnings
{stats.critical}
Critical
{healthData.summary && healthData.summary !== "All systems operational" && (
{healthData.summary}
)}
{CATEGORIES.map(({ key, label, Icon }) => { const categoryData = healthData.details[key as keyof typeof healthData.details] const status = categoryData?.status || "UNKNOWN" const reason = categoryData?.reason const details = categoryData?.details return (
{getStatusIcon(status)}

{label}

{status}
{reason &&

{reason}

} {details && typeof details === "object" && (
{Object.entries(details).map(([detailKey, detailValue]: [string, any]) => { if (typeof detailValue === "object" && detailValue !== null) { return (
{detailKey}: {detailValue.reason && ( {detailValue.reason} )}
) } return null })}
)}
) })}
{healthData.timestamp && (
Last updated: {new Date(healthData.timestamp).toLocaleString()}
)}
)}
) }