'use client'; import { useEffect, useState } from 'react'; import { getHealth, type AgentHealth } from '@/lib/api'; import { cn } from '@/lib/utils'; type HealthState = 'loading' | 'healthy' | 'error'; const deriveHealthState = (health: AgentHealth | null): HealthState => { if (!health) return 'loading'; if (health.ok === false) return 'error'; const status = health.status?.toLowerCase(); if (status && (status.includes('error') || status.includes('down'))) { return 'error'; } return 'healthy'; }; const StatusChip = ({ state }: { state: HealthState }) => { const config = { healthy: { label: 'Healthy', icon: '✓', className: 'border-emerald-500/50 bg-emerald-500/10 text-emerald-400', }, loading: { label: 'Checking', icon: '●', className: 'border-amber-500/50 bg-amber-500/10 text-amber-400 animate-pulse', }, error: { label: 'Error', icon: '✕', className: 'border-rose-500/50 bg-rose-500/10 text-rose-400', }, } satisfies Record< HealthState, { label: string; icon: string; className: string } >; const { label, icon, className } = config[state]; return ( {icon} {label} ); }; export function HealthCard({ initialHealth, className, }: { initialHealth: AgentHealth | null; className?: string; }) { const [health, setHealth] = useState(initialHealth); const [state, setState] = useState(() => deriveHealthState(initialHealth) ); useEffect(() => { let cancelled = false; const check = async () => { try { const nextHealth = await getHealth(); if (cancelled) return; setHealth(nextHealth); setState(deriveHealthState(nextHealth)); } catch { if (!cancelled) { setState('error'); } } }; check(); const interval = setInterval(check, 30_000); return () => { cancelled = true; clearInterval(interval); }; }, []); return ( 💚 Health Status {health?.status ?? (state === 'healthy' ? 'ok' : 'unknown')} Last Checked {health?.timestamp ?? 'Just now'} ); }