2026-04-23 22:49:50 -04:00
|
|
|
import Tooltip from "@mui/material/Tooltip";
|
|
|
|
|
import Box from "@mui/material/Box";
|
|
|
|
|
import Stack from "@mui/material/Stack";
|
|
|
|
|
import Typography from "@mui/material/Typography";
|
2026-04-27 18:21:31 -04:00
|
|
|
import type { SxProps, Theme } from "@mui/material/styles";
|
2026-04-23 22:49:50 -04:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import type { CheckSnapshot } from "@/Types/Check";
|
|
|
|
|
import { formatCheckTimestamp } from "./timeFormat";
|
|
|
|
|
|
|
|
|
|
const CELLS = 25;
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
export type HeatCellKind = "fast" | "med" | "slow" | "down" | "empty";
|
|
|
|
|
|
2026-04-23 22:49:50 -04:00
|
|
|
interface Props {
|
|
|
|
|
checks: CheckSnapshot[];
|
2026-04-27 18:21:31 -04:00
|
|
|
/** sx-based API — preferred. Themes pass their own container + cell recipes. */
|
|
|
|
|
containerSx?: SxProps<Theme>;
|
|
|
|
|
cellSx?: (kind: HeatCellKind) => SxProps<Theme>;
|
|
|
|
|
/** Legacy className-based API — kept for unmigrated themes. Will be removed once all themes move to sx. */
|
|
|
|
|
classPrefix?: string;
|
2026-04-23 22:49:50 -04:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
const classify = (check: CheckSnapshot): Exclude<HeatCellKind, "empty"> => {
|
2026-04-23 22:49:50 -04:00
|
|
|
if (!check.status) return "down";
|
|
|
|
|
const rt = check.responseTime ?? 0;
|
|
|
|
|
if (rt > 500) return "slow";
|
|
|
|
|
if (rt > 250) return "med";
|
|
|
|
|
return "fast";
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
export const ThemedHeatmap = ({ checks, containerSx, cellSx, classPrefix }: Props) => {
|
2026-04-23 22:49:50 -04:00
|
|
|
const { t } = useTranslation();
|
2026-04-27 18:21:31 -04:00
|
|
|
const useSxApi = Boolean(cellSx);
|
2026-04-23 22:49:50 -04:00
|
|
|
|
|
|
|
|
const source = checks.slice(0, CELLS).reverse();
|
|
|
|
|
const padded: (CheckSnapshot | null)[] = [
|
|
|
|
|
...Array.from({ length: Math.max(0, CELLS - source.length) }, () => null),
|
|
|
|
|
...source,
|
|
|
|
|
];
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
const containerProps = useSxApi
|
|
|
|
|
? { sx: containerSx }
|
|
|
|
|
: { className: `${classPrefix}-heatmap` };
|
|
|
|
|
|
|
|
|
|
const cellProps = (kind: HeatCellKind) =>
|
|
|
|
|
useSxApi
|
|
|
|
|
? { sx: cellSx!(kind) }
|
|
|
|
|
: { className: `${classPrefix}-heatmap-cell ${classPrefix}-${kind}` };
|
|
|
|
|
|
2026-04-23 22:49:50 -04:00
|
|
|
return (
|
|
|
|
|
<Box
|
2026-04-27 18:21:31 -04:00
|
|
|
{...containerProps}
|
2026-04-23 22:49:50 -04:00
|
|
|
role="img"
|
|
|
|
|
aria-label={t("pages.statusPages.monitorsList.chart.heatmapAria")}
|
|
|
|
|
>
|
|
|
|
|
{padded.map((check, i) => {
|
|
|
|
|
if (!check) {
|
|
|
|
|
return (
|
|
|
|
|
<Box
|
|
|
|
|
key={`empty-${i}`}
|
2026-04-27 18:21:31 -04:00
|
|
|
{...cellProps("empty")}
|
2026-04-23 22:49:50 -04:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-27 18:21:31 -04:00
|
|
|
const kind = classify(check);
|
2026-04-23 22:49:50 -04:00
|
|
|
const tooltipContent = (
|
|
|
|
|
<Stack gap={0.25}>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
fontWeight={600}
|
|
|
|
|
>
|
|
|
|
|
{check.status
|
|
|
|
|
? `${check.responseTime} ms`
|
|
|
|
|
: t("pages.statusPages.monitorsList.chart.downTooltip")}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{ opacity: 0.8 }}
|
|
|
|
|
>
|
|
|
|
|
{formatCheckTimestamp(check.createdAt as unknown as string)}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip
|
|
|
|
|
key={check.id ?? i}
|
|
|
|
|
title={tooltipContent}
|
|
|
|
|
arrow
|
|
|
|
|
placement="top"
|
|
|
|
|
>
|
|
|
|
|
<Box
|
2026-04-27 18:21:31 -04:00
|
|
|
{...cellProps(kind)}
|
2026-04-23 22:49:50 -04:00
|
|
|
aria-label={`${check.responseTime} ms, ${check.status ? "up" : "down"}`}
|
|
|
|
|
/>
|
|
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
};
|