import { useMemo } from "react"; import Tooltip from "@mui/material/Tooltip"; import Box from "@mui/material/Box"; import Stack from "@mui/material/Stack"; import Typography from "@mui/material/Typography"; import type { SxProps, Theme } from "@mui/material/styles"; import { useTranslation } from "react-i18next"; import type { CheckSnapshot } from "@/Types/Check"; import { formatDateWithTz } from "@/Utils/TimeUtils"; import { MAX_RECENT_CHECKS } from "@/Types/Monitor"; import { useStatusPageTheme } from "../StatusPageThemeProvider"; const CELLS = MAX_RECENT_CHECKS; const MIN_HEIGHT_PCT = 6; export type BarKind = "up" | "down" | "empty"; interface Props { checks: CheckSnapshot[]; containerSx: SxProps; barSx: (kind: BarKind, heightPct: number) => SxProps; statsSx: SxProps; statsGap?: number; } const tone = (check: CheckSnapshot): Exclude => check.status ? "up" : "down"; export const ThemedHistogram = ({ checks, containerSx, barSx, statsSx, statsGap = 1, }: Props) => { const { t } = useTranslation(); const { timezone } = useStatusPageTheme(); const { padded, max, avg, peak } = useMemo(() => { const source = checks.slice(-CELLS); const out: (CheckSnapshot | null)[] = [ ...source, ...Array.from({ length: Math.max(0, CELLS - source.length) }, () => null), ]; const valid = out.filter((c): c is CheckSnapshot => c !== null && c.responseTime > 0); const maxRt = valid.length ? Math.max(...valid.map((c) => c.responseTime)) : 1; const avgRt = valid.length ? Math.round(valid.reduce((s, c) => s + c.responseTime, 0) / valid.length) : 0; return { padded: out, max: maxRt, avg: avgRt, peak: valid.length ? maxRt : 0 }; }, [checks]); return ( {padded.map((check, i) => { if (!check) { return ( ); } const height = Math.max( MIN_HEIGHT_PCT, Math.round((check.responseTime / max) * 100) ); const tooltipContent = ( {check.status ? `${check.responseTime} ms` : t("pages.statusPages.monitorsList.chart.downTooltip")} {formatDateWithTz( check.createdAt, "ddd, MMMM D, YYYY, HH:mm A", timezone )} ); return ( ); })} {t("pages.statusPages.monitorsList.chart.avg", { value: avg })} {t("pages.statusPages.monitorsList.chart.max", { value: peak })} ); };