import { useQuery } from '@tanstack/react-query' import { Link } from '@tanstack/react-router' import { devicesApi, metricsApi, type DeviceResponse } from '@/lib/api' import { cn } from '@/lib/utils' import { formatUptime } from '@/lib/utils' interface SiteHealthGridProps { tenantId: string siteId: string } function cpuColor(pct: number | null): string { if (pct == null) return 'bg-elevated' if (pct >= 90) return 'bg-error' if (pct >= 70) return 'bg-warning' return 'bg-success' } function memColor(pct: number | null): string { if (pct == null) return 'bg-elevated' if (pct >= 90) return 'bg-error' if (pct >= 70) return 'bg-warning' return 'bg-success' } function StatusDot({ status }: { status: string }) { const styles: Record = { online: 'bg-online shadow-[0_0_6px_hsl(var(--online)/0.3)]', offline: 'bg-offline shadow-[0_0_6px_hsl(var(--offline)/0.3)]', unknown: 'bg-unknown', } return ( ) } function borderColor(status: string): string { if (status === 'online') return 'border-success/50' if (status === 'offline') return 'border-error/50' return 'border-warning/50' } export function SiteHealthGrid({ tenantId, siteId }: SiteHealthGridProps) { const { data: deviceData, isLoading: devicesLoading } = useQuery({ queryKey: ['site-devices', tenantId, siteId], queryFn: () => devicesApi.list(tenantId, { site_id: siteId, page_size: 100 }), }) // Fleet summary has CPU/memory data const { data: fleetData } = useQuery({ queryKey: ['fleet-summary', tenantId], queryFn: () => metricsApi.fleetSummary(tenantId), }) if (devicesLoading) { return (
{Array.from({ length: 6 }).map((_, i) => (
))}
) } const devices = deviceData?.items ?? [] if (devices.length === 0) { return (

No devices assigned to this site. Assign devices from the fleet page.

) } // Build a map of device metrics from fleet summary const metricsMap = new Map() if (fleetData) { for (const fd of fleetData) { metricsMap.set(fd.id, { cpu: fd.last_cpu_load, mem: fd.last_memory_used_pct }) } } return (
{devices.map((device: DeviceResponse) => { const metrics = metricsMap.get(device.id) const cpu = metrics?.cpu ?? null const mem = metrics?.mem ?? null return (
{device.hostname}
{/* CPU bar */}
CPU {cpu != null ? `${Math.round(cpu)}%` : '--'}
{cpu != null && (
)}
{/* Memory bar */}
Memory {mem != null ? `${Math.round(mem)}%` : '--'}
{mem != null && (
)}
{/* Uptime */}
Uptime: {formatUptime(device.uptime_seconds)}
) })}
) }