Files
Checkmate/client/src/Components/monitors/charts/HistogramPageSpeed.tsx
T

106 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-01-26 18:24:58 +00:00
import Stack from "@mui/material/Stack";
import { Area, AreaChart, CartesianGrid, ResponsiveContainer, Tooltip } from "recharts";
2026-02-20 18:13:39 +00:00
import { HistogramPageSpeedTooltip } from "@/Components/monitors/charts/HistogramPageSpeedTooltip";
2026-01-26 18:24:58 +00:00
import { useTheme } from "@mui/material/styles";
2026-01-27 19:23:34 +00:00
import type { CheckSnapshot } from "@/Types/Check";
2026-01-26 18:24:58 +00:00
import type { MonitorStatus } from "@/Types/Monitor";
import { getStatusColor } from "@/Utils/MonitorUtils";
2026-01-27 19:23:34 +00:00
const processData = (checks: CheckSnapshot[]) => {
2026-01-26 18:24:58 +00:00
if (checks.length === 0) return [];
const formattedData: { score: number; date: string }[] = [];
2026-01-27 19:23:34 +00:00
const calculateScore = (entry: CheckSnapshot) => {
2026-01-26 18:24:58 +00:00
const accessibility = entry?.accessibility || 0;
const bestPractices = entry?.bestPractices || 0;
const performance = entry?.performance || 0;
const seo = entry?.seo || 0;
2026-01-26 23:44:40 +00:00
return Math.floor((accessibility + bestPractices + performance + seo) / 4);
2026-01-26 18:24:58 +00:00
};
checks.forEach((entry) => {
const score = calculateScore(entry);
const date = entry.createdAt;
formattedData.push({ score, date });
});
return formattedData;
};
export const HistogramPageSpeed = ({
checks,
status,
}: {
2026-01-27 19:23:34 +00:00
checks: CheckSnapshot[];
2026-01-26 18:24:58 +00:00
status: MonitorStatus;
}) => {
const theme = useTheme();
const scores = processData(checks ?? []);
const color = getStatusColor(status, theme);
return (
<Stack
direction="row"
flexWrap="nowrap"
gap={theme.spacing(1.5)}
height="100px"
maxWidth="400px"
width="100%"
2026-01-26 18:24:58 +00:00
onClick={(event) => event.stopPropagation()}
overflow={"hidden"}
sx={{
cursor: "default",
position: "relative",
}}
>
<ResponsiveContainer
width="100%"
height={"100%"}
>
<AreaChart
data={scores}
margin={{ top: 10, bottom: -5 }}
style={{ cursor: "pointer" }}
>
<CartesianGrid
stroke={theme.palette.divider}
strokeWidth={1}
strokeOpacity={1}
fill="transparent"
vertical={false}
/>
<Tooltip
cursor={{ stroke: theme.palette.divider }}
content={<HistogramPageSpeedTooltip />}
/>
<defs>
<linearGradient
id={`pagespeed-chart-${status}`}
x1="0"
y1="0"
x2="0"
y2="1"
>
<stop
offset="0%"
stopColor={color}
stopOpacity={0.8}
/>
<stop
offset="100%"
stopColor={color}
stopOpacity={0}
/>
</linearGradient>
</defs>
<Area
type="monotone"
dataKey="score"
stroke={color}
fill={`url(#pagespeed-chart-${status})`}
/>
</AreaChart>
</ResponsiveContainer>
</Stack>
);
};