import Stack from "@mui/material/Stack"; import Box from "@mui/material/Box"; import Typography from "@mui/material/Typography"; import { useTheme } from "@mui/material/styles"; import { useTranslation } from "react-i18next"; import { useMemo } from "react"; import { Dot } from "@/Components/design-elements"; import { LAYOUT } from "@/Utils/Theme/constants"; import { getStatusColor, getMonitorPath } from "@/Utils/MonitorUtils"; import { formatStatusCode } from "@/Utils/statusCode"; import { formatDuration } from "@/Utils/TimeUtils"; import { DashboardCard, CardMessage } from "../DashboardCard"; import { CardRow } from "../CardPrimitives"; import { Sparkline } from "../Sparkline"; import { useMonitors } from "../../useDashboardData"; import { latestCheck } from "../../checks"; import type { Monitor, MonitorStatus } from "@/Types/Monitor"; const SPARKLINE_WIDTH = 80; const STATUS_RANK: Partial> = { down: 1, breached: 2, }; const isProblem = (monitor: Monitor) => STATUS_RANK[monitor.status] !== undefined; /** * How long the monitor has been down, from the oldest consecutive failing * check. recentChecks is oldest-first, so the run is walked back from the end. */ const downSince = (monitor: Monitor): string | null => { const checks = monitor.recentChecks ?? []; let oldestFailing: string | null = null; for (let index = checks.length - 1; index >= 0; index -= 1) { if (checks[index].status !== false) { break; } oldestFailing = checks[index].createdAt; } return oldestFailing; }; export const CurrentlyDownCard = () => { const theme = useTheme(); const { t } = useTranslation(); const { data, isLoading, isValidating, error } = useMonitors(); const problems = useMemo(() => { const monitors = data?.monitors ?? []; return monitors .filter(isProblem) .map((monitor) => ({ monitor, since: downSince(monitor) })) .sort((a, b) => { const rank = (STATUS_RANK[a.monitor.status] ?? 99) - (STATUS_RANK[b.monitor.status] ?? 99); if (rank !== 0) { return rank; } // Tie-break on longest downtime: the older timestamp ranks first. return new Date(a.since ?? 0).getTime() - new Date(b.since ?? 0).getTime(); }); }, [data]); return ( 0 ? ( {t("pages.dashboard.cards.currentlyDown.count", { count: problems.length, })} ) : null } > {problems.length === 0 ? ( ) : ( {problems.map(({ monitor, since }) => ( {monitor.name} {since && ( {formatDuration(Date.now() - new Date(since).getTime())} )} {/* Not StatusCodeLabel: its Tooltip is focusable, and a focusable element inside the row's link is unreachable by keyboard. */} {monitor.type} ยท{" "} {formatStatusCode(latestCheck(monitor)?.statusCode, t)} ))} )} ); };