import Stack from "@mui/material/Stack"; import Typography from "@mui/material/Typography"; import { useTheme } from "@mui/material/styles"; import { useTranslation } from "react-i18next"; import { useMemo } from "react"; import { LAYOUT } from "@/Utils/Theme/constants"; import { typographyLevels } from "@/Utils/Theme/Palette"; import { DashboardCard, CardMessage } from "../DashboardCard"; import { CardBar, CardRow } from "../CardPrimitives"; import { useMonitors } from "../../useDashboardData"; interface GroupBucket { group: string; total: number; down: number; } export const MonitorsByGroupCard = () => { const theme = useTheme(); const { t } = useTranslation(); 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. const buckets = useMemo(() => { const byGroup = new Map(); for (const monitor of data?.monitors ?? []) { if (!monitor.group) { continue; } const bucket = byGroup.get(monitor.group) ?? { group: monitor.group, 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]); const max = buckets[0]?.total ?? 0; return ( {buckets.length === 0 ? ( ) : ( {buckets.map((bucket) => ( {bucket.group} {bucket.down > 0 && ( {t("pages.dashboard.cards.monitorsByType.down", { count: bucket.down, })} )} {bucket.total} 0 ? theme.palette.error.main : theme.palette.primary.main } /> ))} )} ); };