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

169 lines
5.2 KiB
React
Raw Normal View History

2024-08-22 19:36:48 -04:00
import { useTheme } from "@emotion/react";
2024-08-22 20:15:49 -04:00
import { Box, Stack, Tooltip, Typography } from "@mui/material";
2024-09-12 12:44:29 +08:00
import { formatDateWithTz } from "../../../Utils/timeUtils";
2024-08-23 14:34:26 -04:00
import { useEffect, useState } from "react";
2024-08-22 20:15:49 -04:00
import "./index.css";
2024-09-12 12:44:29 +08:00
import { useSelector } from "react-redux";
2024-08-22 19:36:48 -04:00
const BarChart = ({ checks = [] }) => {
const theme = useTheme();
2024-08-23 14:34:26 -04:00
const [animate, setAnimate] = useState(false);
2024-09-12 12:44:29 +08:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-08-23 14:34:26 -04:00
useEffect(() => {
setAnimate(true);
});
2024-08-22 19:36:48 -04:00
2024-08-23 14:25:55 -04:00
// set responseTime to average if there's only one check
if (checks.length === 1) {
checks[0] = { ...checks[0], responseTime: 50 };
}
if (checks.length !== 25) {
const placeholders = Array(25 - checks.length).fill("placeholder");
checks = [...checks, ...placeholders];
}
2024-08-22 19:36:48 -04:00
return (
2024-08-23 13:41:06 -04:00
<Stack
direction="row"
flexWrap="nowrap"
2024-08-27 12:18:25 -04:00
gap={theme.spacing(1.5)}
2024-08-23 13:41:06 -04:00
height="50px"
width="fit-content"
onClick={(event) => event.stopPropagation()}
sx={{
cursor: "default",
}}
>
2024-08-23 14:25:55 -04:00
{checks.map((check, index) =>
check === "placeholder" ? (
2024-08-22 19:36:48 -04:00
<Box
2024-08-23 14:25:55 -04:00
key={`${check}-${index}`}
2024-08-22 20:15:49 -04:00
position="relative"
2024-08-27 12:18:25 -04:00
width={theme.spacing(4.5)}
2024-08-22 20:15:49 -04:00
height="100%"
2024-08-27 12:18:25 -04:00
backgroundColor={theme.palette.background.fill}
2024-08-22 19:36:48 -04:00
sx={{
2024-08-27 12:18:25 -04:00
borderRadius: theme.spacing(1.5),
2024-08-22 19:36:48 -04:00
}}
2024-08-23 14:25:55 -04:00
/>
) : (
<Tooltip
title={
<>
<Typography>
2024-09-12 12:44:29 +08:00
{formatDateWithTz(
check.createdAt,
"ddd, MMMM D, YYYY, HH:mm A",
uiTimezone
)}
2024-08-23 14:25:55 -04:00
</Typography>
2024-08-27 12:18:25 -04:00
<Box mt={theme.spacing(2)}>
2024-08-23 14:25:55 -04:00
<Box
display="inline-block"
2024-08-27 12:18:25 -04:00
width={theme.spacing(4)}
height={theme.spacing(4)}
2024-08-23 14:25:55 -04:00
backgroundColor={
check.status
2024-08-27 12:18:25 -04:00
? theme.palette.success.main
: theme.palette.error.text
2024-08-23 14:25:55 -04:00
}
sx={{ borderRadius: "50%" }}
/>
<Stack
display="inline-flex"
direction="row"
justifyContent="space-between"
2024-08-27 12:18:25 -04:00
ml={theme.spacing(2)}
gap={theme.spacing(12)}
2024-08-23 14:25:55 -04:00
>
<Typography component="span" sx={{ opacity: 0.8 }}>
Response Time
</Typography>
<Typography component="span">
{check.originalResponseTime}
<Typography component="span" sx={{ opacity: 0.8 }}>
{" "}
ms
</Typography>
</Typography>
</Stack>
</Box>
</>
}
placement="top"
key={`check-${check?._id}`}
slotProps={{
popper: {
className: "bar-tooltip",
modifiers: [
{
name: "offset",
options: {
offset: [0, -10],
},
},
],
2024-08-27 12:18:25 -04:00
sx: {
"& .MuiTooltip-tooltip": {
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.dark,
borderRadius: theme.shape.borderRadius,
boxShadow: theme.shape.boxShadow,
px: theme.spacing(4),
py: theme.spacing(2),
},
"& .MuiTooltip-tooltip p": {
fontSize: 12,
color: theme.palette.text.tertiary,
fontWeight: 500,
},
"& .MuiTooltip-tooltip span": {
fontSize: 11,
color: theme.palette.text.tertiary,
fontWeight: 600,
},
},
2024-08-23 14:25:55 -04:00
},
}}
2024-08-22 20:15:49 -04:00
>
<Box
2024-08-23 14:25:55 -04:00
position="relative"
width="9px"
height="100%"
2024-08-22 20:15:49 -04:00
backgroundColor={
2024-08-27 12:18:25 -04:00
check.status ? theme.palette.success.bg : theme.palette.error.bg
2024-08-22 20:15:49 -04:00
}
sx={{
2024-08-27 12:18:25 -04:00
borderRadius: theme.spacing(1.5),
2024-08-23 14:25:55 -04:00
"&:hover > .MuiBox-root": {
2024-08-23 14:34:26 -04:00
filter: "brightness(0.8)",
},
2024-08-22 20:15:49 -04:00
}}
2024-08-23 14:25:55 -04:00
>
<Box
position="absolute"
bottom={0}
width="100%"
2024-08-23 14:34:26 -04:00
height={`${animate ? check.responseTime : 0}%`}
2024-08-23 14:25:55 -04:00
backgroundColor={
check.status
2024-08-27 12:18:25 -04:00
? theme.palette.success.main
: theme.palette.error.text
2024-08-23 14:25:55 -04:00
}
sx={{
2024-08-27 12:18:25 -04:00
borderRadius: theme.spacing(1.5),
2024-08-23 14:34:26 -04:00
transition: "height 600ms cubic-bezier(0.4, 0, 0.2, 1)",
2024-08-23 14:25:55 -04:00
}}
/>
</Box>
</Tooltip>
)
)}
2024-08-22 19:36:48 -04:00
</Stack>
);
};
export default BarChart;