import { useMemo } from "react"; import Tooltip from "@mui/material/Tooltip"; import Box from "@mui/material/Box"; import Stack from "@mui/material/Stack"; import type { SxProps, Theme } from "@mui/material/styles"; import { useTranslation } from "react-i18next"; import type { CheckSnapshot } from "@/Types/Check"; import { MAX_RECENT_CHECKS } from "@/Types/Monitor"; import { computeBarHeights } from "@/Utils/DataUtils"; import { formatMs } from "@/Utils/TimeUtils"; import { ThemedChartTooltip } from "@/Pages/StatusPage/Status/themes/shared/ThemedChartTooltip"; 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 { padded, heights, avg, peak } = useMemo(() => { const source = checks.slice(-CELLS); const out: (CheckSnapshot | null)[] = [ ...source, ...Array.from({ length: Math.max(0, CELLS - source.length) }, () => null), ]; const heights = computeBarHeights(source); const valid = out.filter((c): c is CheckSnapshot => c !== null && c.responseTime > 0); const maxRt = valid.length ? Math.max(...valid.map((c) => c.responseTime)) : 0; const avgRt = valid.length ? valid.reduce((s, c) => s + c.responseTime, 0) / valid.length : 0; return { padded: out, heights, avg: formatMs(avgRt), peak: formatMs(maxRt) }; }, [checks]); return ( {padded.map((check, i) => { if (!check) { return ( ); } const height = heights[i] ?? MIN_HEIGHT_PCT; const tooltipContent = ; return ( ); })} {t("pages.statusPages.monitorsList.chart.avg", { value: avg })} {t("pages.statusPages.monitorsList.chart.max", { value: peak })} ); };