Files
Checkmate/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx
T

315 lines
8.7 KiB
React
Raw Normal View History

2024-09-11 12:46:41 -04:00
import PropTypes from "prop-types";
2024-09-10 14:17:59 -04:00
import {
AreaChart,
Area,
XAxis,
Tooltip,
CartesianGrid,
ResponsiveContainer,
2024-09-11 12:46:41 -04:00
Text,
2024-09-10 14:17:59 -04:00
} from "recharts";
import { useTheme } from "@emotion/react";
2024-09-17 21:03:47 -04:00
import { useMemo, useState } from "react";
2024-09-10 17:43:43 -04:00
import { Box, Stack, Typography } from "@mui/material";
2024-09-14 12:42:31 -04:00
import { formatDateWithTz } from "../../../../Utils/timeUtils";
import { useSelector } from "react-redux";
2024-09-10 14:17:59 -04:00
const config = {
2024-09-11 12:46:41 -04:00
seo: {
id: "seo",
text: "SEO",
color: "unresolved",
2024-09-10 14:17:59 -04:00
},
performance: {
id: "performance",
2024-09-10 17:43:43 -04:00
text: "performance",
2024-09-10 14:17:59 -04:00
color: "success",
},
2024-09-11 12:46:41 -04:00
bestPractices: {
id: "bestPractices",
text: "best practices",
color: "warning",
},
accessibility: {
id: "accessibility",
text: "accessibility",
color: "primary",
2024-09-10 14:17:59 -04:00
},
};
2024-09-11 12:46:41 -04:00
/**
* Custom tooltip for the area chart.
* @param {Object} props
* @param {boolean} props.active - Whether the tooltip is active.
* @param {Array} props.payload - The payload data for the tooltip.
* @param {string} props.label - The label for the tooltip.
* @returns {JSX.Element|null} The tooltip element or null if not active.
*/
2024-09-11 15:47:46 -04:00
const CustomToolTip = ({ active, payload, label, config }) => {
2024-09-10 17:43:43 -04:00
const theme = useTheme();
2024-09-14 12:42:31 -04:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-09-10 17:43:43 -04:00
if (active && payload && payload.length) {
return (
<Box
sx={{
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.dark,
borderRadius: theme.shape.borderRadius,
py: theme.spacing(2),
px: theme.spacing(4),
}}
>
<Typography
sx={{
color: theme.palette.text.tertiary,
fontSize: 12,
fontWeight: 500,
}}
>
2024-09-14 12:42:31 -04:00
{formatDateWithTz(label, "ddd, MMMM D, YYYY, h:mm A", uiTimezone)}
2024-09-10 17:43:43 -04:00
</Typography>
2024-09-11 12:46:41 -04:00
{Object.keys(config)
.reverse()
.map((key) => {
const { color } = config[key];
const dotColor = theme.palette[color].main;
2024-09-10 17:43:43 -04:00
2024-09-11 12:46:41 -04:00
return (
<Stack
key={`${key}-tooltip`}
direction="row"
alignItems="center"
gap={theme.spacing(3)}
mt={theme.spacing(1)}
sx={{
"& span": {
color: theme.palette.text.tertiary,
fontSize: 11,
fontWeight: 500,
},
}}
2024-09-10 17:43:43 -04:00
>
2024-09-11 12:46:41 -04:00
<Box
width={theme.spacing(4)}
height={theme.spacing(4)}
backgroundColor={dotColor}
sx={{ borderRadius: "50%" }}
/>
<Typography
component="span"
textTransform="capitalize"
sx={{ opacity: 0.8 }}
>
{config[key].text}
</Typography>{" "}
<Typography component="span">
{payload[0].payload[key]}
</Typography>
</Stack>
);
})}
2024-09-10 17:43:43 -04:00
</Box>
);
}
return null;
};
2024-09-11 12:46:41 -04:00
CustomToolTip.propTypes = {
active: PropTypes.bool,
payload: PropTypes.array,
label: PropTypes.string,
2024-09-14 11:20:28 +08:00
config: PropTypes.object,
2024-09-11 12:46:41 -04:00
};
/**
* Processes data to insert gaps with null values based on the interval.
* @param {Array} data
* @param {number} interval - The interval in milliseconds for gaps.
* @returns {Array} The formatted data with gaps.
*/
2024-09-10 17:43:43 -04:00
const processDataWithGaps = (data, interval) => {
if (data.length === 0) return [];
let formattedData = [];
let last = new Date(data[0].createdAt).getTime();
// Helper function to add a null entry
const addNullEntry = (timestamp) => {
formattedData.push({
accessibility: "N/A",
bestPractices: "N/A",
performance: "N/A",
seo: "N/A",
createdAt: timestamp,
2024-09-10 14:17:59 -04:00
});
};
2024-09-10 17:43:43 -04:00
data.forEach((entry) => {
const current = new Date(entry.createdAt).getTime();
if (current - last > interval * 2) {
// Insert null entries for each interval
let temp = last + interval;
while (temp < current) {
addNullEntry(new Date(temp).toISOString());
temp += interval;
}
}
formattedData.push(entry);
last = current;
});
2024-09-10 14:17:59 -04:00
2024-09-10 17:43:43 -04:00
return formattedData;
};
2024-09-11 12:46:41 -04:00
/**
* Custom tick component to render ticks on the XAxis.
*
* @param {Object} props
* @param {number} props.x - The x coordinate for the tick.
* @param {number} props.y - The y coordinate for the tick.
* @param {Object} props.payload - The data object containing the tick value.
* @param {number} props.index - The index of the tick in the array of ticks.
*
* @returns {JSX.Element|null} The tick element or null if the tick should be hidden.
*/
const CustomTick = ({ x, y, payload, index }) => {
const theme = useTheme();
2024-09-14 12:42:31 -04:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-09-11 12:46:41 -04:00
// Render nothing for the first tick
if (index === 0) return null;
return (
<Text
x={x}
y={y + 8}
textAnchor="middle"
fill={theme.palette.text.tertiary}
fontSize={11}
fontWeight={400}
>
2024-09-14 12:42:31 -04:00
{formatDateWithTz(payload?.value, "h:mm a", uiTimezone)}
2024-09-11 12:46:41 -04:00
</Text>
);
};
CustomTick.propTypes = {
x: PropTypes.number,
y: PropTypes.number,
payload: PropTypes.shape({
value: PropTypes.string.isRequired,
}),
index: PropTypes.number,
};
/**
* A chart displaying pagespeed details over time.
* @param {Object} props
* @param {Array} props.data - The data to display in the chart.
* @param {number} props.interval - The interval in milliseconds for processing gaps.
* @returns {JSX.Element} The area chart component.
*/
2024-09-11 15:47:46 -04:00
const PagespeedDetailsAreaChart = ({ data, interval, metrics }) => {
2024-09-10 14:17:59 -04:00
const theme = useTheme();
2024-09-17 21:03:47 -04:00
const [isHovered, setIsHovered] = useState(false);
2024-09-10 17:43:43 -04:00
const memoizedData = useMemo(
() => processDataWithGaps(data, interval),
2024-09-11 15:47:46 -04:00
[data[0]]
2024-09-10 17:43:43 -04:00
);
2024-09-10 14:17:59 -04:00
2024-09-11 15:47:46 -04:00
const filteredConfig = Object.keys(config).reduce((result, key) => {
if (metrics[key]) {
result[key] = config[key];
}
return result;
}, {});
2024-09-10 14:17:59 -04:00
return (
2024-09-10 17:43:43 -04:00
<ResponsiveContainer width="100%" minWidth={25} height={215}>
2024-09-10 14:17:59 -04:00
<AreaChart
width="100%"
height="100%"
data={memoizedData}
margin={{ top: 10 }}
2024-09-17 21:03:47 -04:00
onMouseMove={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
2024-09-10 14:17:59 -04:00
>
<CartesianGrid
stroke={theme.palette.border.light}
strokeWidth={1}
strokeOpacity={1}
fill="transparent"
vertical={false}
/>
<XAxis
stroke={theme.palette.border.dark}
dataKey="createdAt"
2024-09-11 12:46:41 -04:00
tick={<CustomTick />}
axisLine={false}
2024-09-10 14:17:59 -04:00
tickLine={false}
height={18}
2024-09-11 12:46:41 -04:00
minTickGap={0}
interval="equidistantPreserveStart"
2024-09-10 17:43:43 -04:00
/>
<Tooltip
cursor={{ stroke: theme.palette.border.light }}
2024-09-11 15:47:46 -04:00
content={<CustomToolTip config={filteredConfig} />}
2024-09-10 14:17:59 -04:00
/>
<defs>
2024-09-11 15:47:46 -04:00
{Object.values(filteredConfig).map(({ id, color }) => {
2024-09-10 14:17:59 -04:00
const startColor = theme.palette[color].main;
const endColor = theme.palette[color].light;
return (
<linearGradient id={id} x1="0" y1="0" x2="0" y2="1" key={id}>
<stop offset="0%" stopColor={startColor} stopOpacity={0.8} />
<stop offset="100%" stopColor={endColor} stopOpacity={0} />
</linearGradient>
);
})}
</defs>
2024-09-11 15:47:46 -04:00
{Object.keys(filteredConfig).map((key) => {
const { color } = filteredConfig[key];
2024-09-10 14:17:59 -04:00
const strokeColor = theme.palette[color].main;
2024-09-10 17:43:43 -04:00
const bgColor = theme.palette.background.main;
2024-09-10 14:17:59 -04:00
return (
<Area
2024-09-10 17:43:43 -04:00
connectNulls
2024-09-10 14:17:59 -04:00
key={key}
dataKey={key}
stackId={1}
stroke={strokeColor}
2024-09-17 21:03:47 -04:00
strokeWidth={isHovered ? 2.5 : 1.5}
2024-09-11 15:47:46 -04:00
fill={`url(#${filteredConfig[key].id})`}
2024-09-10 17:43:43 -04:00
activeDot={{ stroke: bgColor, fill: strokeColor, r: 4.5 }}
2024-09-10 14:17:59 -04:00
/>
);
})}
</AreaChart>
</ResponsiveContainer>
);
};
2024-09-11 12:46:41 -04:00
PagespeedDetailsAreaChart.propTypes = {
data: PropTypes.arrayOf(
PropTypes.shape({
createdAt: PropTypes.string.isRequired,
accessibility: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
bestPractices: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
performance: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
seo: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
})
).isRequired,
interval: PropTypes.number.isRequired,
2024-09-14 11:20:28 +08:00
metrics: PropTypes.object,
2024-09-11 12:46:41 -04:00
};
2024-09-10 14:17:59 -04:00
export default PagespeedDetailsAreaChart;