import { useTheme } from "@emotion/react"; import PropTypes from "prop-types"; import { BarChart, Bar, XAxis, ResponsiveContainer, Cell, RadialBarChart, RadialBar, } from "recharts"; import { memo, useMemo, useState } from "react"; import { useSelector } from "react-redux"; import { formatDateWithTz } from "../../../../Utils/timeUtils"; const CustomLabels = ({ x, width, height, firstDataPoint, lastDataPoint, type }) => { const uiTimezone = useSelector((state) => state.ui.timezone); const dateFormat = type === "day" ? "MMM D, h:mm A" : "MMM D"; return ( <> {formatDateWithTz(new Date(firstDataPoint.time), dateFormat, uiTimezone)} {formatDateWithTz(new Date(lastDataPoint.time), dateFormat, uiTimezone)} ); }; CustomLabels.propTypes = { x: PropTypes.number.isRequired, width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), firstDataPoint: PropTypes.object.isRequired, lastDataPoint: PropTypes.object.isRequired, type: PropTypes.string.isRequired, }; const UpBarChart = memo(({ data, type, onBarHover }) => { const theme = useTheme(); const [chartHovered, setChartHovered] = useState(false); const [hoveredBarIndex, setHoveredBarIndex] = useState(null); const getColorRange = (uptime) => { return uptime > 80 ? { main: theme.palette.success.main, light: theme.palette.success.light } : uptime > 50 ? { main: theme.palette.warning.main, light: theme.palette.warning.light } : { main: theme.palette.error.contrastText, light: theme.palette.error.light }; }; // TODO - REMOVE THIS LATER const reversedData = useMemo(() => [...data].reverse(), [data]); return ( { setChartHovered(true); onBarHover({ time: null, totalChecks: 0, uptimePercentage: 0 }); }} onMouseLeave={() => { setChartHovered(false); setHoveredBarIndex(null); onBarHover(null); }} > } /> {reversedData.map((entry, index) => { let { main, light } = getColorRange(entry.uptimePercentage); return ( { setHoveredBarIndex(index); onBarHover(entry); }} onMouseLeave={() => { setHoveredBarIndex(null); onBarHover({ time: null, totalChecks: 0, uptimePercentage: 0, }); }} /> ); })} ); }); // Add display name for the component UpBarChart.displayName = "UpBarChart"; // Validate props using PropTypes UpBarChart.propTypes = { data: PropTypes.arrayOf(PropTypes.object), type: PropTypes.string, onBarHover: PropTypes.func, }; export { UpBarChart }; const DownBarChart = memo(({ data, type, onBarHover }) => { const theme = useTheme(); const [chartHovered, setChartHovered] = useState(false); const [hoveredBarIndex, setHoveredBarIndex] = useState(null); // TODO - REMOVE THIS LATER const reversedData = useMemo(() => [...data].reverse(), [data]); return ( { setChartHovered(true); onBarHover({ time: null, totalIncidents: 0 }); }} onMouseLeave={() => { setChartHovered(false); setHoveredBarIndex(null); onBarHover(null); }} > } /> {reversedData.map((entry, index) => ( { setHoveredBarIndex(index); onBarHover(entry); }} onMouseLeave={() => { setHoveredBarIndex(null); onBarHover({ time: null, totalIncidents: 0 }); }} /> ))} ); }); DownBarChart.displayName = "DownBarChart"; DownBarChart.propTypes = { data: PropTypes.arrayOf(PropTypes.object), type: PropTypes.string, onBarHover: PropTypes.func, }; export { DownBarChart }; const ResponseGaugeChart = ({ data }) => { const theme = useTheme(); let max = 1000; // max ms const memoizedData = useMemo( () => [{ response: max, fill: "transparent", background: false }, ...data], [data[0].response] ); let responseTime = Math.floor(memoizedData[1].response); let responseProps = responseTime <= 200 ? { category: "Excellent", main: theme.palette.success.main, bg: theme.palette.success.contrastText, } : responseTime <= 500 ? { category: "Fair", main: theme.palette.success.main, bg: theme.palette.success.contrastText, } : responseTime <= 600 ? { category: "Acceptable", main: theme.palette.warning.main, bg: theme.palette.warning.dark, } : { category: "Poor", main: theme.palette.error.contrastText, bg: theme.palette.error.dark, }; return ( low high {responseProps.category} {responseTime} ms ); }; ResponseGaugeChart.propTypes = { data: PropTypes.arrayOf(PropTypes.object).isRequired, }; export { ResponseGaugeChart };