2026-08-29 09:37:34 -04:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
|
2026-08-29 10:03:59 -04:00
|
|
|
import { BreakdownCard, type BreakdownBucket } from "./BreakdownCard";
|
2026-08-29 09:37:34 -04:00
|
|
|
import { useMonitors } from "../../useDashboardData";
|
|
|
|
|
|
|
|
|
|
export const MonitorsByGroupCard = () => {
|
|
|
|
|
const { data, isLoading, isValidating, error } = useMonitors();
|
|
|
|
|
|
|
|
|
|
// `group` is a plain string | null on the monitor — there is no groups
|
|
|
|
|
// endpoint, so the set is whatever monitors actually declare.
|
2026-08-29 10:03:59 -04:00
|
|
|
const buckets = useMemo<BreakdownBucket[]>(() => {
|
|
|
|
|
const byGroup = new Map<string, BreakdownBucket>();
|
2026-08-29 09:37:34 -04:00
|
|
|
for (const monitor of data?.monitors ?? []) {
|
|
|
|
|
if (!monitor.group) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const bucket = byGroup.get(monitor.group) ?? {
|
2026-08-29 10:03:59 -04:00
|
|
|
key: monitor.group,
|
|
|
|
|
label: monitor.group,
|
2026-08-29 09:37:34 -04:00
|
|
|
total: 0,
|
|
|
|
|
down: 0,
|
|
|
|
|
};
|
|
|
|
|
bucket.total += 1;
|
|
|
|
|
if (monitor.status === "down" || monitor.status === "breached") {
|
|
|
|
|
bucket.down += 1;
|
|
|
|
|
}
|
|
|
|
|
byGroup.set(monitor.group, bucket);
|
|
|
|
|
}
|
|
|
|
|
return [...byGroup.values()].sort((a, b) => b.total - a.total);
|
|
|
|
|
}, [data]);
|
|
|
|
|
|
|
|
|
|
return (
|
2026-08-29 10:03:59 -04:00
|
|
|
<BreakdownCard
|
|
|
|
|
titleKey="pages.dashboard.cards.monitorsByGroup.title"
|
|
|
|
|
emptyKey="pages.dashboard.cards.monitorsByGroup.empty"
|
2026-08-29 09:37:34 -04:00
|
|
|
to="/uptime"
|
2026-08-29 10:03:59 -04:00
|
|
|
buckets={buckets}
|
2026-08-29 09:37:34 -04:00
|
|
|
isLoading={isLoading && !data}
|
|
|
|
|
isStale={isValidating && Boolean(data)}
|
2026-08-29 10:03:59 -04:00
|
|
|
error={error}
|
|
|
|
|
/>
|
2026-08-29 09:37:34 -04:00
|
|
|
);
|
|
|
|
|
};
|