Code review of the dashboard branch. Four correctness bugs, one shared
helper, and the comment density brought down.
Correctness:
- CurrentlyDownCard: downSince() broke its backwards walk only on
`status === true`, so a check with an undefined status kept the run
going and reported a downtime reaching back to the start of the
retained window. Break on `status !== false` instead, matching what
the doc comment already claimed.
- RankingCards: the lowest-uptime card barred the uptime percentage
itself, so the worst monitor drew the shortest bar - a chart that read
as "best uptime" directly against its own title and sort order. Bar
the shortfall instead.
- useCardTransitions: the FLIP rAF and its nested 260ms timeout were
never cancelled. Navigating away mid-animation wrote styles to
unmounted nodes, and a second move while the first was settling had
its inversion wiped by the earlier timeout, so the card jumped rather
than travelled. Handles are now tracked and cancelled on unmount and
at the start of each move.
- BreakdownCard: the bar scale took buckets[0].total, silently assuming
the caller sorted descending. Compute the max instead - both callers
do sort today, but nothing in the signature said they had to.
Redundancy:
- "newest check is the last element of recentChecks" was implemented
twice with its own copy of the explanation. Now one `latestCheck`
helper in checks.ts. RankingCards averages over all checks, which is
a different operation, and is left alone.
- DEFAULT_CARD_IDS is `readonly` and copied into state rather than
assigned by reference.
Comments and conventions:
- 283 comment lines down to ~220. Design rationale ("two lines rather
than one wide row", "the header controls stay invisible until...")
is gone; comments recording non-obvious API facts - recentChecks is
oldest-first, snake_case here but camelCase there, credential fields
must not reach the DOM - are kept and shortened.
- DashboardCard: display/flexDirection/height hoisted out of sx to
native props (frontend rule 1). A stale comment claiming a "34px
control height" is corrected; the codebase settled on 32.
Gates: tsc clean, build passes, format-check clean, lint unchanged from
develop (12 errors, all pre-existing in untouched files).
158 lines
4.5 KiB
TypeScript
158 lines
4.5 KiB
TypeScript
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<Record<MonitorStatus, number>> = {
|
|
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 (
|
|
<DashboardCard
|
|
title={t("pages.dashboard.cards.currentlyDown.title")}
|
|
to="/incidents"
|
|
isLoading={isLoading && !data}
|
|
error={error}
|
|
isStale={isValidating && Boolean(data)}
|
|
action={
|
|
problems.length > 0 ? (
|
|
<Typography 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"
|
|
/>
|
|
<Stack
|
|
flex={1}
|
|
minWidth={0}
|
|
gap={theme.spacing(LAYOUT.XXS)}
|
|
>
|
|
<Stack
|
|
direction="row"
|
|
alignItems="baseline"
|
|
justifyContent="space-between"
|
|
gap={theme.spacing(LAYOUT.SM)}
|
|
>
|
|
<Typography
|
|
color={theme.palette.text.primary}
|
|
noWrap
|
|
>
|
|
{monitor.name}
|
|
</Typography>
|
|
{since && (
|
|
<Typography
|
|
color={theme.palette.error.main}
|
|
flexShrink={0}
|
|
>
|
|
{formatDuration(Date.now() - new Date(since).getTime())}
|
|
</Typography>
|
|
)}
|
|
</Stack>
|
|
<Stack
|
|
direction="row"
|
|
alignItems="center"
|
|
justifyContent="space-between"
|
|
gap={theme.spacing(LAYOUT.SM)}
|
|
>
|
|
{/* Not StatusCodeLabel: its Tooltip is focusable, and a
|
|
focusable element inside the row's link is unreachable
|
|
by keyboard. */}
|
|
<Typography
|
|
color={theme.palette.text.secondary}
|
|
noWrap
|
|
>
|
|
{monitor.type} ·{" "}
|
|
{formatStatusCode(latestCheck(monitor)?.statusCode, t)}
|
|
</Typography>
|
|
<Box
|
|
width={SPARKLINE_WIDTH}
|
|
flexShrink={0}
|
|
>
|
|
<Sparkline
|
|
checks={monitor.recentChecks ?? []}
|
|
color={theme.palette.error.main}
|
|
/>
|
|
</Box>
|
|
</Stack>
|
|
</Stack>
|
|
</CardRow>
|
|
))}
|
|
</Stack>
|
|
)}
|
|
</DashboardCard>
|
|
);
|
|
};
|