Files
Checkmate/Client/src/Components/Charts/MonitorDetailsAreaChart/index.jsx
T

168 lines
4.5 KiB
React
Raw Normal View History

2024-07-17 12:33:24 -07:00
import PropTypes from "prop-types";
2024-09-05 13:45:25 -04:00
import {
AreaChart,
Area,
XAxis,
Tooltip,
CartesianGrid,
ResponsiveContainer,
} from "recharts";
2024-09-04 21:41:16 -04:00
import { Box, Stack, Typography } from "@mui/material";
2024-08-27 12:18:25 -04:00
import { useTheme } from "@emotion/react";
import { useMemo } from "react";
2024-07-18 19:50:08 -04:00
import "./index.css";
2024-09-12 12:17:31 +08:00
import { useSelector } from "react-redux";
import { formatDateWithTz } from "../../../Utils/timeUtils";
2024-07-17 12:33:24 -07:00
const CustomToolTip = ({ active, payload, label }) => {
2024-09-12 12:17:31 +08:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-08-27 12:18:25 -04:00
2024-09-12 12:17:31 +08:00
const theme = useTheme();
if (active && payload && payload.length) {
return (
2024-08-27 12:18:25 -04:00
<Box
className="area-tooltip"
sx={{
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.dark,
borderRadius: theme.shape.borderRadius,
2024-09-04 21:41:16 -04:00
py: theme.spacing(2),
px: theme.spacing(4),
2024-08-27 12:18:25 -04:00
}}
>
<Typography
sx={{
2024-09-04 21:41:16 -04:00
color: theme.palette.text.tertiary,
fontSize: 12,
fontWeight: 500,
2024-08-27 12:18:25 -04:00
}}
>
2024-09-12 12:17:31 +08:00
{formatDateWithTz(label, "ddd, MMMM D, YYYY, h:mm A", uiTimezone)}
2024-08-27 12:18:25 -04:00
</Typography>
2024-09-04 21:41:16 -04:00
<Box mt={theme.spacing(1)}>
<Box
display="inline-block"
width={theme.spacing(4)}
height={theme.spacing(4)}
backgroundColor={theme.palette.primary.main}
sx={{ borderRadius: "50%" }}
/>
<Stack
display="inline-flex"
direction="row"
justifyContent="space-between"
ml={theme.spacing(3)}
sx={{
"& span": {
color: theme.palette.text.tertiary,
fontSize: 11,
fontWeight: 500,
},
}}
>
<Typography component="span" sx={{ opacity: 0.8 }}>
Response Time
</Typography>{" "}
<Typography component="span">
{payload[0].payload.originalResponseTime}
<Typography component="span" sx={{ opacity: 0.8 }}>
{" "}
ms
</Typography>
</Typography>
</Stack>
</Box>
{/* Display original value */}
2024-08-27 12:18:25 -04:00
</Box>
);
}
return null;
};
const MonitorDetailsAreaChart = ({ checks }) => {
2024-09-12 12:17:31 +08:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-07-17 12:33:24 -07:00
const formatDate = (timestamp) => {
2024-09-12 12:17:31 +08:00
return formatDateWithTz(timestamp, "HH:mm:ss", uiTimezone);
2024-07-17 12:33:24 -07:00
};
const memoizedChecks = useMemo(() => checks, [checks[0]]);
2024-09-05 17:19:59 -04:00
2024-09-04 21:09:18 -04:00
const theme = useTheme();
2024-07-17 12:33:24 -07:00
return (
2024-09-05 17:19:59 -04:00
<ResponsiveContainer width="100%" minWidth={25} height={220}>
2024-07-17 12:33:24 -07:00
<AreaChart
2024-09-04 21:41:16 -04:00
width="100%"
height="100%"
2024-09-05 17:19:59 -04:00
data={memoizedChecks}
2024-07-17 12:33:24 -07:00
margin={{
top: 10,
2024-07-18 19:50:08 -04:00
right: 0,
2024-07-17 12:33:24 -07:00
left: 0,
bottom: 0,
}}
>
2024-09-05 13:45:25 -04:00
<CartesianGrid
stroke={theme.palette.border.light}
strokeWidth={1}
strokeOpacity={1}
fill="transparent"
vertical={false}
/>
2024-09-04 21:09:18 -04:00
<defs>
<linearGradient id="colorUv" x1="0" y1="0" x2="0" y2="1">
<stop
offset="0%"
stopColor={theme.palette.primary.main}
stopOpacity={0.8}
/>
<stop
offset="100%"
stopColor={theme.palette.primary.light}
stopOpacity={0}
/>
</linearGradient>
</defs>
2024-07-17 12:33:24 -07:00
<XAxis
2024-09-04 21:41:16 -04:00
stroke={theme.palette.border.dark}
2024-07-17 12:33:24 -07:00
dataKey="createdAt"
tickFormatter={formatDate}
tick={{ fontSize: "13px" }}
2024-07-18 19:50:08 -04:00
tickLine={false}
2024-09-04 21:41:16 -04:00
height={18}
2024-07-17 12:33:24 -07:00
/>
<Tooltip
cursor={{ stroke: theme.palette.border.light }}
content={<CustomToolTip />}
/>
2024-07-17 12:33:24 -07:00
<Area
type="monotone"
dataKey="responseTime"
2024-09-04 21:09:18 -04:00
stroke={theme.palette.primary.main}
fill="url(#colorUv)"
strokeWidth={1.5}
activeDot={{ stroke: theme.palette.background.main, r: 5 }}
2024-07-17 12:33:24 -07:00
/>
</AreaChart>
</ResponsiveContainer>
);
};
MonitorDetailsAreaChart.propTypes = {
checks: PropTypes.array,
};
CustomToolTip.propTypes = {
active: PropTypes.bool,
payload: PropTypes.arrayOf(
PropTypes.shape({
payload: PropTypes.shape({
originalResponseTime: PropTypes.number.isRequired,
}).isRequired,
})
),
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
2024-07-17 12:33:24 -07:00
};
export default MonitorDetailsAreaChart;