"use client" import { Card } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Progress } from "@/components/ui/progress" import { Cpu, MemoryStick, HardDrive, Network, Monitor, Thermometer, Fan, Battery, Server } from "lucide-react" import useSWR from "swr" const fetcher = (url: string) => fetch(url).then((res) => res.json()) interface CPUInfo { model?: string total_threads?: number cores_per_socket?: number sockets?: number current_mhz?: number max_mhz?: number virtualization?: string l1d_cache?: string l2_cache?: string l3_cache?: string } interface MotherboardInfo { manufacturer?: string model?: string version?: string serial?: string bios?: { vendor?: string version?: string date?: string } } interface MemoryModule { size: string type: string speed: string manufacturer?: string slot?: string } interface StorageDevice { name: string size: string model: string temperature: number health: string power_on_hours: number rotation_rate: number } interface NetworkCard { name: string type: string } interface GraphicsCard { name: string memory?: string temperature?: number power_draw?: string vendor: string } interface TemperatureSensor { name: string current: number high?: number critical?: number } interface FanSensor { name: string current_rpm: number } interface UPSInfo { model?: string status?: string battery_charge?: string time_left?: string load_percent?: string line_voltage?: string } interface HardwareData { cpu: CPUInfo motherboard: MotherboardInfo memory_modules: MemoryModule[] storage_devices: StorageDevice[] network_cards: NetworkCard[] graphics_cards: GraphicsCard[] sensors: { temperatures: TemperatureSensor[] fans: FanSensor[] } power: UPSInfo } export default function Hardware() { const { data: hardwareData, error } = useSWR("/api/hardware", fetcher, { refreshInterval: 5000, }) if (error) { return (

Failed to load hardware information

) } if (!hardwareData) { return (
) } const getHealthColor = (health: string) => { switch (health.toLowerCase()) { case "healthy": return "text-green-500" case "warning": return "text-yellow-500" case "critical": case "failed": return "text-red-500" default: return "text-muted-foreground" } } const getHealthBadge = (health: string) => { switch (health.toLowerCase()) { case "healthy": return Healthy case "warning": return Warning case "critical": case "failed": return Critical default: return Unknown } } const getTempColor = (temp: number, high?: number, critical?: number) => { if (critical && temp >= critical) return "text-red-500" if (high && temp >= high) return "text-yellow-500" if (temp >= 70) return "text-red-500" if (temp >= 60) return "text-yellow-500" return "text-green-500" } const getTempProgress = (temp: number, critical?: number) => { const max = critical || 100 return (temp / max) * 100 } const hasSensors = hardwareData.sensors.temperatures.length > 0 || hardwareData.sensors.fans.length > 0 const hasUPS = hardwareData.power && Object.keys(hardwareData.power).length > 0 const storageSummary = hardwareData.storage_devices.reduce( (acc, disk) => { const sizeMatch = disk.size.match(/(\d+\.?\d*)\s*([KMGT]B)/) if (sizeMatch) { let sizeInGB = Number.parseFloat(sizeMatch[1]) const unit = sizeMatch[2] if (unit === "TB") sizeInGB *= 1024 else if (unit === "MB") sizeInGB /= 1024 else if (unit === "KB") sizeInGB /= 1024 * 1024 acc.totalCapacity += sizeInGB } if (disk.rotation_rate === 0) acc.ssd++ else if (disk.rotation_rate > 0) acc.hdd++ return acc }, { totalCapacity: 0, ssd: 0, hdd: 0 }, ) return (
{/* System Information */}

System Information

{/* CPU */} {hardwareData.cpu.model && (

CPU

Model {hardwareData.cpu.model}
{hardwareData.cpu.sockets && hardwareData.cpu.cores_per_socket && (
Cores {hardwareData.cpu.sockets} × {hardwareData.cpu.cores_per_socket} ={" "} {hardwareData.cpu.sockets * hardwareData.cpu.cores_per_socket} cores
)} {hardwareData.cpu.total_threads && (
Threads {hardwareData.cpu.total_threads}
)} {hardwareData.cpu.current_mhz && (
Frequency {hardwareData.cpu.current_mhz.toFixed(0)} MHz
)} {hardwareData.cpu.l3_cache && (
L3 Cache {hardwareData.cpu.l3_cache}
)} {hardwareData.cpu.virtualization && (
Virtualization {hardwareData.cpu.virtualization}
)}
)} {/* Motherboard */} {hardwareData.motherboard.manufacturer && (

Motherboard

Manufacturer {hardwareData.motherboard.manufacturer}
{hardwareData.motherboard.model && (
Model {hardwareData.motherboard.model}
)} {hardwareData.motherboard.bios && ( <>
BIOS {hardwareData.motherboard.bios.vendor}
Version {hardwareData.motherboard.bios.version}
{hardwareData.motherboard.bios.date && (
Date {hardwareData.motherboard.bios.date}
)} )}
)}
{/* Memory Modules */} {hardwareData.memory_modules.length > 0 && (

Memory Modules

{hardwareData.memory_modules.length} installed
{hardwareData.memory_modules.map((module, index) => (
{module.slot &&
{module.slot}
}
Size {module.size}
Type {module.type}
Speed {module.speed}
{module.manufacturer && module.manufacturer !== "Unknown" && (
Manufacturer {module.manufacturer}
)}
))}
)} {/* Storage Summary - Simplified */} {hardwareData.storage_devices.length > 0 && (

Storage Summary

{hardwareData.storage_devices.length} devices

Total Capacity

{storageSummary.totalCapacity.toFixed(1)} GB

{storageSummary.ssd > 0 && (

SSD Drives

{storageSummary.ssd}

)} {storageSummary.hdd > 0 && (

HDD Drives

{storageSummary.hdd}

)}

For detailed storage information, see the Storage section

)} {/* Storage Devices */} {hardwareData.storage_devices.length > 0 && (

Storage Devices

{hardwareData.storage_devices.length} devices
{hardwareData.storage_devices.map((disk, index) => (
/dev/{disk.name} {getHealthBadge(disk.health)} {disk.rotation_rate === 0 && ( SSD )} {disk.rotation_rate > 0 && ( {disk.rotation_rate} RPM )}
Model {disk.model}
Size {disk.size}
{disk.temperature > 0 && (
Temperature {disk.temperature}°C
)} {disk.power_on_hours > 0 && (
Power On Hours {disk.power_on_hours.toLocaleString()}h
)}
))}
)} {/* Graphics Cards */} {hardwareData.graphics_cards.length > 0 && (

Graphics Cards

{hardwareData.graphics_cards.length} GPU{hardwareData.graphics_cards.length > 1 ? "s" : ""}
{hardwareData.graphics_cards.map((gpu, index) => (
{gpu.name}
Vendor {gpu.vendor}
{gpu.memory && (
Memory {gpu.memory}
)} {gpu.temperature && gpu.temperature > 0 && (
Temperature {gpu.temperature}°C
)} {gpu.power_draw && (
Power Draw {gpu.power_draw}
)}
))}
)} {/* Network Summary - Simplified */} {hardwareData.network_cards.length > 0 && (

Network Summary

{hardwareData.network_cards.length} interfaces
{hardwareData.network_cards.map((nic, index) => (
{nic.name} {nic.type}
))}

For detailed network information, see the Network section

)} {/* Network Cards */} {hardwareData.network_cards.length > 0 && (

Network Cards

{hardwareData.network_cards.length} NIC{hardwareData.network_cards.length > 1 ? "s" : ""}
{hardwareData.network_cards.map((nic, index) => (
{nic.name} {nic.type}
))}
)} {/* Sensors (Temperature & Fans) */} {hasSensors && (

Thermal & Fan Monitoring

{/* Temperatures */} {hardwareData.sensors.temperatures.length > 0 && (

Temperatures

{hardwareData.sensors.temperatures.map((sensor, index) => (
{sensor.name} {sensor.current.toFixed(1)}°C
))}
)} {/* Fans */} {hardwareData.sensors.fans.length > 0 && (

Fans

{hardwareData.sensors.fans.map((fan, index) => (
{fan.name}
{fan.current_rpm} RPM
))}
)}
)} {/* Power Supply / UPS */} {hasUPS && (

Power Supply / UPS

{hardwareData.power.model && (
Model {hardwareData.power.model}
)} {hardwareData.power.status && (
Status {hardwareData.power.status}
)} {hardwareData.power.battery_charge && (
Battery Charge {hardwareData.power.battery_charge}
)} {hardwareData.power.time_left && (
Time Left {hardwareData.power.time_left}
)} {hardwareData.power.load_percent && (
Load {hardwareData.power.load_percent}
)} {hardwareData.power.line_voltage && (
Line Voltage {hardwareData.power.line_voltage}
)}
)}
) }