2026-08-29 09:10:46 -04:00
|
|
|
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";
|
|
|
|
|
|
2026-08-29 09:30:12 -04:00
|
|
|
import { Dot } from "@/Components/design-elements";
|
2026-08-29 09:10:46 -04:00
|
|
|
import { LAYOUT } from "@/Utils/Theme/constants";
|
|
|
|
|
import { typographyLevels } from "@/Utils/Theme/Palette";
|
|
|
|
|
import { getStatusColor, getMonitorPath } from "@/Utils/MonitorUtils";
|
2026-08-29 09:30:12 -04:00
|
|
|
import { formatStatusCode } from "@/Utils/statusCode";
|
2026-08-29 09:10:46 -04:00
|
|
|
import { formatDuration } from "@/Utils/TimeUtils";
|
|
|
|
|
import { DashboardCard, CardMessage } from "../DashboardCard";
|
|
|
|
|
import { CardRow, CardRowLabel } from "../CardPrimitives";
|
|
|
|
|
import { Sparkline } from "../Sparkline";
|
|
|
|
|
import { useMonitors } from "../../useDashboardData";
|
|
|
|
|
|
|
|
|
|
import type { Monitor, MonitorStatus } from "@/Types/Monitor";
|
|
|
|
|
|
|
|
|
|
// Ordering, not a severity scale — Checkmate has none. Every input already
|
|
|
|
|
// exists in the API response.
|
2026-08-29 09:30:12 -04:00
|
|
|
//
|
|
|
|
|
// `initializing` is deliberately excluded: on a fresh install every new monitor
|
|
|
|
|
// starts there, and counting it would open the card red claiming a fleet-wide
|
|
|
|
|
// outage that does not exist. It is "not checked yet", not "broken".
|
2026-08-29 09:10:46 -04:00
|
|
|
const STATUS_RANK: Partial<Record<MonitorStatus, number>> = {
|
|
|
|
|
down: 1,
|
|
|
|
|
breached: 2,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isProblem = (monitor: Monitor) => STATUS_RANK[monitor.status] !== undefined;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* How long the monitor has been in its current state, inferred from the oldest
|
2026-08-29 09:30:12 -04:00
|
|
|
* consecutive failing check.
|
|
|
|
|
*
|
|
|
|
|
* recentChecks is oldest-first — the server appends each snapshot with
|
|
|
|
|
* `$push … $slice: -N` — so the run of current failures is walked backwards
|
|
|
|
|
* from the end.
|
2026-08-29 09:10:46 -04:00
|
|
|
*/
|
|
|
|
|
const downSince = (monitor: Monitor): string | null => {
|
|
|
|
|
const checks = monitor.recentChecks ?? [];
|
|
|
|
|
let oldestFailing: string | null = null;
|
2026-08-29 09:30:12 -04:00
|
|
|
for (let index = checks.length - 1; index >= 0; index -= 1) {
|
|
|
|
|
if (checks[index].status === true) {
|
2026-08-29 09:10:46 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2026-08-29 09:30:12 -04:00
|
|
|
oldestFailing = checks[index].createdAt;
|
2026-08-29 09:10:46 -04:00
|
|
|
}
|
|
|
|
|
return oldestFailing;
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-29 09:30:12 -04:00
|
|
|
/** The newest retained check — recentChecks is oldest-first. */
|
|
|
|
|
const latestCheck = (monitor: Monitor) =>
|
|
|
|
|
monitor.recentChecks?.[monitor.recentChecks.length - 1];
|
|
|
|
|
|
2026-08-29 09:10:46 -04:00
|
|
|
export const CurrentlyDownCard = () => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const { t } = useTranslation();
|
2026-08-29 09:30:12 -04:00
|
|
|
const { data, isLoading, isValidating, error } = useMonitors();
|
2026-08-29 09:10:46 -04:00
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<DashboardCard
|
|
|
|
|
title={t("pages.dashboard.cards.currentlyDown.title")}
|
|
|
|
|
to="/incidents"
|
|
|
|
|
isLoading={isLoading && !data}
|
|
|
|
|
error={error}
|
2026-08-29 09:30:12 -04:00
|
|
|
isStale={isValidating && Boolean(data)}
|
2026-08-29 09:10:46 -04:00
|
|
|
action={
|
|
|
|
|
problems.length > 0 ? (
|
|
|
|
|
<Typography
|
|
|
|
|
fontSize={typographyLevels.s}
|
|
|
|
|
color={theme.palette.error.main}
|
|
|
|
|
>
|
|
|
|
|
{t("pages.dashboard.cards.currentlyDown.count", {
|
|
|
|
|
count: problems.length,
|
|
|
|
|
})}
|
|
|
|
|
</Typography>
|
|
|
|
|
) : null
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{problems.length === 0 ? (
|
|
|
|
|
<CardMessage text={t("pages.dashboard.cards.currentlyDown.empty")} />
|
|
|
|
|
) : (
|
|
|
|
|
<Stack gap={theme.spacing(LAYOUT.XS)}>
|
|
|
|
|
{problems.map(({ monitor, since }) => (
|
|
|
|
|
<CardRow
|
|
|
|
|
key={monitor.id}
|
|
|
|
|
to={`/${getMonitorPath(monitor.type)}/${monitor.id}`}
|
|
|
|
|
>
|
|
|
|
|
<Dot
|
|
|
|
|
color={getStatusColor(monitor.status, theme)}
|
|
|
|
|
size="md"
|
|
|
|
|
/>
|
|
|
|
|
<CardRowLabel
|
|
|
|
|
primary={monitor.name}
|
|
|
|
|
secondary={monitor.url}
|
|
|
|
|
/>
|
|
|
|
|
<Typography
|
|
|
|
|
fontSize={typographyLevels.s}
|
|
|
|
|
color={theme.palette.text.secondary}
|
|
|
|
|
flexShrink={0}
|
|
|
|
|
>
|
|
|
|
|
{monitor.type}
|
|
|
|
|
</Typography>
|
|
|
|
|
{since && (
|
|
|
|
|
<Typography
|
|
|
|
|
fontSize={typographyLevels.s}
|
|
|
|
|
color={theme.palette.error.main}
|
|
|
|
|
flexShrink={0}
|
|
|
|
|
minWidth={64}
|
|
|
|
|
textAlign="right"
|
|
|
|
|
>
|
|
|
|
|
{formatDuration(Date.now() - new Date(since).getTime())}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
<Box
|
|
|
|
|
width={80}
|
|
|
|
|
flexShrink={0}
|
|
|
|
|
display={{ xs: "none", md: "block" }}
|
|
|
|
|
>
|
|
|
|
|
<Sparkline
|
|
|
|
|
checks={monitor.recentChecks ?? []}
|
|
|
|
|
color={theme.palette.error.main}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
2026-08-29 09:30:12 -04:00
|
|
|
{/*
|
|
|
|
|
* Plain text rather than StatusCodeLabel: that component wraps
|
|
|
|
|
* its body in a Tooltip, and a focusable element nested inside
|
|
|
|
|
* the row's link is unreachable by keyboard — tabbing lands on
|
|
|
|
|
* the link and activating it navigates away.
|
|
|
|
|
*/}
|
|
|
|
|
<Typography
|
|
|
|
|
fontSize={typographyLevels.s}
|
|
|
|
|
color={theme.palette.text.secondary}
|
|
|
|
|
flexShrink={0}
|
|
|
|
|
minWidth={32}
|
|
|
|
|
textAlign="right"
|
|
|
|
|
>
|
|
|
|
|
{formatStatusCode(latestCheck(monitor)?.statusCode, t)}
|
|
|
|
|
</Typography>
|
2026-08-29 09:10:46 -04:00
|
|
|
</CardRow>
|
|
|
|
|
))}
|
|
|
|
|
</Stack>
|
|
|
|
|
)}
|
|
|
|
|
</DashboardCard>
|
|
|
|
|
);
|
|
|
|
|
};
|