import { useEffect, useState, useMemo } from 'react'
import {
fetchHealth,
fetchSources,
fetchAlerts,
fetchEnvStatus,
fetchEnvActive,
fetchSWPC,
type MeshHealth,
type SourceHealth,
type Alert,
type EnvStatus,
type EnvEvent,
type BandConditionsStatus,
} from '@/lib/api'
import { useWebSocket } from '@/hooks/useWebSocket'
import {
AlertTriangle,
AlertCircle,
Info,
CheckCircle,
Radio,
Cpu,
Activity,
MapPin,
Zap,
Cloud,
Flame,
Mountain,
Droplets,
Car,
Construction,
Satellite,
Sun,
} from 'lucide-react'
function HealthGauge({ health }: { health: MeshHealth }) {
const score = health.score
const tier = health.tier
const getColor = (s: number) => {
if (s >= 80) return '#22c55e'
if (s >= 60) return '#f59e0b'
return '#ef4444'
}
const color = getColor(score)
const circumference = 2 * Math.PI * 45
const progress = (score / 100) * circumference
return (
)
}
function PillarBar({ label, value }: { label: string; value: number }) {
const getColor = (v: number) => {
if (v >= 80) return 'bg-green-500'
if (v >= 60) return 'bg-amber-500'
return 'bg-red-500'
}
return (
{label}
{value.toFixed(1)}
)
}
function AlertItem({ alert }: { alert: Alert }) {
const getSeverityStyles = (severity: string) => {
switch (severity.toLowerCase()) {
case 'critical':
case 'emergency':
case 'immediate':
return { bg: 'bg-red-500/10', border: 'border-red-500', icon: AlertCircle, iconColor: 'text-red-500' }
case 'warning':
case 'priority':
return { bg: 'bg-amber-500/10', border: 'border-amber-500', icon: AlertTriangle, iconColor: 'text-amber-500' }
case 'routine':
default:
return { bg: 'bg-blue-500/10', border: 'border-blue-500', icon: Info, iconColor: 'text-blue-500' }
}
}
const styles = getSeverityStyles(alert.severity)
const Icon = styles.icon
return (
{alert.message}
{alert.timestamp || 'Just now'}
)
}
function SourceCard({ source }: { source: SourceHealth }) {
const getStatusColor = () => {
if (!source.is_loaded) return 'bg-red-500'
if (source.last_error) return 'bg-amber-500'
return 'bg-green-500'
}
return (
{source.name}
{source.node_count} nodes · {source.type}
)
}
function StatCard({ icon: Icon, label, value, subvalue }: { icon: typeof Radio; label: string; value: string | number; subvalue?: string }) {
return (
{label}
{value}
{subvalue &&
{subvalue}
}
)
}
// Band Conditions Card
function BandConditionsCard({ bandConditions }: { bandConditions: BandConditionsStatus | null }) {
const getRatingEmoji = (rating?: string) => {
switch (rating) {
case 'Good': return '\ud83d\udfe2' // green circle
case 'Fair': return '\ud83d\udfe1' // yellow circle
case 'Poor': return '\ud83d\udd34' // red circle
default: return '\u2014'
}
}
const getSlotEmoji = (label?: string) => {
if (!label) return ''
return label.includes('Night') ? '\ud83c\udf19' : '\u2600\ufe0f'
}
if (!bandConditions?.enabled || !bandConditions?.ratings) {
return (
)
}
const bands = ['80-40m', '30-20m', '17-15m', '12-10m'] as const
return (
RF Propagation
{/* Slot label */}
{getSlotEmoji(bandConditions.slot_label)}
{bandConditions.slot_label}
{/* Band conditions header */}
\ud83d\udce1 Band Conditions:
{/* Band rows */}
{bands.map(band => {
const rating = bandConditions.ratings?.[band]
return (
{band}
{getRatingEmoji(rating)} {rating || '\u2014'}
)
})}
{/* Footer: source and time */}
{bandConditions.source && (
{bandConditions.source === 'swpc_local' ? 'SWPC' : 'HamQSL'}
)}
{bandConditions.sent_at && (
{new Date(bandConditions.sent_at * 1000).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
)}
)
}
// Source icon mapping
const SOURCE_ICONS: Record = {
nws: { icon: Cloud, color: 'text-blue-400', label: 'NWS' },
swpc: { icon: Sun, color: 'text-yellow-400', label: 'SWPC' },
ducting: { icon: Radio, color: 'text-cyan-400', label: 'Tropo' },
nifc: { icon: Flame, color: 'text-orange-400', label: 'NIFC' },
firms: { icon: Satellite, color: 'text-red-400', label: 'FIRMS' },
avalanche: { icon: Mountain, color: 'text-slate-300', label: 'Avy' },
usgs: { icon: Droplets, color: 'text-blue-300', label: 'USGS' },
traffic: { icon: Car, color: 'text-purple-400', label: 'Traffic' },
roads: { icon: Construction, color: 'text-amber-400', label: '511' },
}
// Severity badge colors (3-level system + legacy support)
const SEVERITY_COLORS: Record = {
// New 3-level system
routine: 'bg-blue-500/20 text-blue-400 border-blue-500/30',
priority: 'bg-amber-500/20 text-amber-400 border-amber-500/30',
immediate: 'bg-red-600/20 text-red-300 border-red-600/30',
// NWS native (for raw event display)
info: 'bg-blue-500/20 text-blue-400 border-blue-500/30',
advisory: 'bg-blue-500/20 text-blue-400 border-blue-500/30',
moderate: 'bg-amber-500/20 text-amber-400 border-amber-500/30',
watch: 'bg-amber-500/20 text-amber-400 border-amber-500/30',
warning: 'bg-amber-500/20 text-amber-400 border-amber-500/30',
severe: 'bg-red-500/20 text-red-400 border-red-500/30',
extreme: 'bg-red-600/20 text-red-300 border-red-600/30',
critical: 'bg-red-600/20 text-red-300 border-red-600/30',
emergency: 'bg-red-700/20 text-red-200 border-red-700/30',
}
function EventFeedItem({ event, isLocal }: { event: EnvEvent; isLocal?: boolean }) {
const sourceConfig = SOURCE_ICONS[event.source] || { icon: Info, color: 'text-slate-400', label: event.source }
const Icon = sourceConfig.icon
const severityStyle = SEVERITY_COLORS[event.severity?.toLowerCase()] || SEVERITY_COLORS.info
// Format timestamp
const formatTime = (ts: number) => {
const date = new Date(ts * 1000)
const now = new Date()
const diffMs = now.getTime() - date.getTime()
const diffMins = Math.floor(diffMs / 60000)
if (diffMins < 1) return 'just now'
if (diffMins < 60) return `${diffMins}m ago`
if (diffMins < 1440) return `${Math.floor(diffMins / 60)}h ago`
return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' })
}
// Build display title: prefer event_type + area_desc, fall back to headline
const eventType = (event as Record).event_type as string | undefined
const areaDesc = (event as Record).area_desc as string | undefined
const description = (event as Record).description as string | undefined
let title = event.headline
if (eventType && areaDesc) {
// Shorten area description (remove "County" repetition)
const shortArea = areaDesc.replace(/ County/g, '').split(';')[0]
title = `${eventType} — ${shortArea}`
} else if (eventType) {
title = eventType
}
// Get first sentence of description as subtitle
const subtitle = description ? description.split('. ')[0] : null
return (
{event.severity || 'info'}
{isLocal && (
LOCAL
)}
{sourceConfig.label}
{formatTime(event.fetched_at)}
{title}
{subtitle && (
{subtitle}
)}
)
}
// Live Event Feed Card
function LiveEventFeed({ events, envStatus }: { events: EnvEvent[]; envStatus: EnvStatus | null }) {
// Severity order for sorting
const severityOrder: Record = { immediate: 0, priority: 1, routine: 2 }
const sortedEvents = useMemo(() => {
// Dedup by event_id
const seen = new Set()
const deduped = events.filter(e => {
if (!e.event_id) return true
if (seen.has(e.event_id)) return false
seen.add(e.event_id)
return true
})
// Sort: local first, then by severity, then by time
return deduped.sort((a, b) => {
const aLocal = (a as Record).is_local ? 1 : 0
const bLocal = (b as Record).is_local ? 1 : 0
if (aLocal !== bLocal) return bLocal - aLocal // local first
const aSev = severityOrder[a.severity?.toLowerCase() || 'routine'] ?? 2
const bSev = severityOrder[b.severity?.toLowerCase() || 'routine'] ?? 2
if (aSev !== bSev) return aSev - bSev // higher severity first
return (b.fetched_at || 0) - (a.fetched_at || 0) // newest first
})
}, [events])
// Calculate feed health summary
const feedSummary = useMemo(() => {
if (!envStatus?.feeds) return null
const total = envStatus.feeds.length
const active = envStatus.feeds.filter(f => f.is_loaded && !f.last_error).length
const errors = envStatus.feeds.filter(f => f.last_error).map(f => f.source)
const lastFetch = Math.max(...envStatus.feeds.map(f => f.last_fetch || 0))
const secAgo = lastFetch ? Math.floor((Date.now() / 1000) - lastFetch) : null
return { total, active, errors, secAgo }
}, [envStatus])
return (
Live Event Feed
{sortedEvents.length > 0 ? (
{sortedEvents.map((event, i) => (
).is_local as boolean | undefined}
/>
))}
) : (
No active events
All clear
)}
{/* Feed health summary */}
{feedSummary && (
0 ? 'text-amber-400' : 'text-slate-500'}`}>
{feedSummary.active} of {feedSummary.total} feeds active
{feedSummary.secAgo !== null && ` · Last update ${feedSummary.secAgo}s ago`}
{feedSummary.errors.length > 0 && (
· {feedSummary.errors.join(', ')}: error
)}
)}
)
}
export default function Dashboard() {
const [health, setHealth] = useState(null)
const [sources, setSources] = useState([])
const [alerts, setAlerts] = useState([])
const [envStatus, setEnvStatus] = useState(null)
const [envEvents, setEnvEvents] = useState([])
const [bandConditions, setBandConditions] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState(null)
const { lastHealth, lastMessage } = useWebSocket()
useEffect(() => {
Promise.all([
fetchHealth(),
fetchSources(),
fetchAlerts(),
fetchEnvStatus(),
fetchEnvActive().catch(() => []),
fetchSWPC().catch(() => null),
])
.then(([h, src, a, e, events, bc]) => {
setHealth(h)
setSources(src)
setAlerts(a)
setEnvStatus(e)
setEnvEvents(events)
setBandConditions(bc as BandConditionsStatus)
setLoading(false)
document.title = 'Dashboard — MeshAI'
})
.catch((err) => {
setError(err.message)
setLoading(false)
document.title = 'Dashboard — MeshAI'
})
}, [])
// Update health from WebSocket
useEffect(() => {
if (lastHealth) {
setHealth(lastHealth)
}
}, [lastHealth])
// Handle WebSocket env_update messages
useEffect(() => {
if (lastMessage?.type === 'env_update' && lastMessage.event) {
setEnvEvents(prev => {
// Add new event, dedupe by event_id
const newEvent = lastMessage.event as EnvEvent
const filtered = prev.filter(e => e.event_id !== newEvent.event_id)
return [newEvent, ...filtered].slice(0, 100) // Keep last 100
})
}
}, [lastMessage])
if (loading) {
return (
)
}
if (error) {
return (
)
}
return (
{/* Top row: Health + Alerts + Stats */}
{/* Mesh Health */}
Mesh Health
{health && (
<>
>
)}
{/* Alerts + Stats */}
{/* Active Alerts */}
Active Alerts
{alerts.length > 0 ? (
{alerts.map((alert, i) => (
))}
) : (() => {
const highSeverityEnv = envEvents
.filter(e => e.severity === 'immediate' || e.severity === 'priority')
.sort((a, b) => {
const ord: Record
= { immediate: 0, priority: 1 }
const diff = (ord[a.severity] ?? 2) - (ord[b.severity] ?? 2)
if (diff !== 0) return diff
return (b.fetched_at || 0) - (a.fetched_at || 0)
})
.slice(0, 5)
if (highSeverityEnv.length > 0) {
return (
{highSeverityEnv.map((ev, i) => {
const sevStyle = ev.severity === 'immediate'
? { bg: 'bg-red-500/10', border: 'border-red-500', icon: AlertCircle, iconColor: 'text-red-500' }
: { bg: 'bg-amber-500/10', border: 'border-amber-500', icon: AlertTriangle, iconColor: 'text-amber-500' }
const Icon = sevStyle.icon
return (
ENV
{ev.severity}
{ev.headline}
{ev.source} · {new Date(ev.fetched_at * 1000).toLocaleTimeString()}
)
})}
)
}
return (
No active alerts
)
})()}
{/* Quick Stats */}
{/* Middle row: Sources + RF Propagation + Live Feed */}
{/* Mesh Sources */}
Mesh Sources ({sources.length})
{sources.length > 0 ? (
{sources.map((source, i) => (
))}
) : (
No sources configured
)}
{/* RF Propagation */}
{/* Live Event Feed */}
)
}