import { useEffect, useState } from 'react' import { MaintenanceConfig, MonitorState, MonitorTarget } from '@/types/config' import { activeNotices, overallSummary, upcomingNotices } from '@/util/status' import { PodRow, StatusIcon } from './brand' import Notice from './Notice' function useNow() { const [now, setNow] = useState(() => Math.round(Date.now() / 1000)) useEffect(() => { const t = setInterval(() => { if (document.visibilityState === 'visible') setNow(Math.round(Date.now() / 1000)) }, 1000) return () => clearInterval(t) }, []) return now } function ago(seconds: number) { if (seconds < 5) return 'just now' if (seconds < 90) return `${seconds} seconds ago` return `${Math.round(seconds / 60)} minutes ago` } export default function OverallStatus({ state, monitors, maintenances, }: { state: MonitorState monitors: MonitorTarget[] maintenances: MaintenanceConfig[] }) { const now = useNow() const [openedAt] = useState(now) const [showUpcoming, setShowUpcoming] = useState(false) // Reload when the data is more than five minutes old (the worker writes every minute). useEffect(() => { if (state.lastUpdate > 0 && now - state.lastUpdate > 300 && now - openedAt > 30) { window.location.reload() } }, [now, state.lastUpdate, openedAt]) const summary = overallSummary(state, monitors, maintenances) const active = activeNotices(maintenances) const upcoming = upcomingNotices(maintenances) const shape = summary.tone === 'success' ? 'dot' : summary.tone === 'neutral' ? 'ring' : summary.title.startsWith('Maintenance') ? 'spin' : 'tri' return (

{summary.title}

{state.lastUpdate > 0 ? ( <> Last checked {ago(now - state.lastUpdate)} ยท checks run every minute ) : ( 'The first checks run within a minute of deploying.' )}

{active.length > 0 && (
{active.map((n, i) => ( ))}
)} {upcoming.length > 0 && (
{showUpcoming && (
{upcoming.map((n, i) => ( ))}
)}
)}
) }