import { useEffect, useState } from "react"; import { Link, useNavigate } from "react-router-dom"; import { api, type IntegrationSummary, type MaintenanceWindow, type ServerSummary } from "../api/client"; import { alertBadge, useAlertsReport } from "../hooks/useAlertsReport"; import { useEnvironment } from "../utils/environment"; import WallView, { REFRESH_MS, wallHeadline, type WallData } from "../components/WallView"; /** * A full-screen status board for a spare monitor or tablet: how things are, in type big enough to read across a room, refreshing * by itself. Nothing on it can be clicked into, so it can be left alone; Esc (or the corner link) goes back to the app. */ export default function Wall() { const navigate = useNavigate(); const environment = useEnvironment(); const { report, failed, reload } = useAlertsReport(); const [servers, setServers] = useState(null); const [integrations, setIntegrations] = useState(null); const [windows, setWindows] = useState([]); const [updatedAt, setUpdatedAt] = useState(null); const [idle, setIdle] = useState(false); useEffect(() => { let alive = true; const load = () => { reload(); Promise.allSettled([api.servers.summary(), api.integrations.list(), api.maintenance.list()]).then(([s, i, m]) => { if (!alive) return; if (s.status === "fulfilled") setServers(s.value); if (i.status === "fulfilled") setIntegrations(i.value.integrations); if (m.status === "fulfilled") setWindows(m.value.windows); setUpdatedAt(new Date()); }); }; load(); const t = setInterval(load, REFRESH_MS); return () => { alive = false; clearInterval(t); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Esc leaves; the pointer hides itself when nobody is using it. useEffect(() => { const onKey = (e: KeyboardEvent) => e.key === "Escape" && !document.fullscreenElement && navigate("/"); let timer: ReturnType; const onMove = () => { setIdle(false); clearTimeout(timer); timer = setTimeout(() => setIdle(true), 5000); }; onMove(); window.addEventListener("keydown", onKey); window.addEventListener("mousemove", onMove); return () => { window.removeEventListener("keydown", onKey); window.removeEventListener("mousemove", onMove); clearTimeout(timer); }; }, [navigate]); const badge = alertBadge(report); const blind = report?.couldntCheck.length ?? 0; const level: WallData["level"] = !report ? "unsure" : badge ? badge.level : blind > 0 ? "unsure" : "ok"; const active = (report?.alerts ?? []).filter((a) => !a.silenced && a.severity !== "info").sort((a, b) => (a.severity === b.severity ? 0 : a.severity === "critical" ? -1 : 1)); const failing = (integrations ?? []).filter((i) => i.enabled && i.health?.status === "failing"); const data: WallData = { level, headline: wallHeadline(level, badge?.count ?? 0, { loaded: !!report, failed }), servers: servers ? { online: servers.online, total: servers.online + servers.offline.length } : null, alerts: report ? { critical: report.counts.critical, warning: report.counts.warning } : null, maintenance: windows.length, integrationsFailing: integrations ? failing.length : null, problems: active.map((a) => ({ id: a.id, severity: a.severity as "critical" | "warning", message: a.message })), windows: windows.map((w) => ({ id: w.id, name: w.targetName, endsAt: w.endsAt })), integrations: (integrations ?? []) .filter((i) => i.enabled) .sort((a, b) => Number(b.health?.status === "failing") - Number(a.health?.status === "failing") || a.name.localeCompare(b.name)) .map((i) => ({ id: i.id, name: i.name, type: i.type, status: i.health?.status === "failing" ? "failing" : i.health?.status === "ok" ? "ok" : "unknown" })), environmentName: environment?.name || null, updatedAt, }; return ( Exit (Esc) } /> ); }