From a192b3d14c396439497dde7859a92e1227719760 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Wed, 4 Sep 2024 12:57:08 -0400 Subject: [PATCH] Added hover state to bars --- Client/src/Pages/Monitors/Configure/index.jsx | 1 - .../Pages/Monitors/Details/Charts/index.jsx | 117 +++++++++++++++--- Client/src/Pages/Monitors/Details/index.jsx | 75 ++++++++--- Client/src/Pages/Monitors/Details/styled.jsx | 12 +- Client/src/Pages/Monitors/Home/StatusBox.jsx | 3 +- Client/src/Pages/Monitors/Home/index.jsx | 9 +- Client/src/Utils/Theme/darkTheme.js | 2 +- Client/src/Utils/greeting.jsx | 6 +- Client/src/Utils/timeUtils.js | 4 +- Client/src/assets/icons/top-right-arrow.svg | 2 +- 10 files changed, 181 insertions(+), 50 deletions(-) diff --git a/Client/src/Pages/Monitors/Configure/index.jsx b/Client/src/Pages/Monitors/Configure/index.jsx index 2e719a31a..637c71ddc 100644 --- a/Client/src/Pages/Monitors/Configure/index.jsx +++ b/Client/src/Pages/Monitors/Configure/index.jsx @@ -53,7 +53,6 @@ const Configure = () => { const [monitor, setMonitor] = useState({}); const [errors, setErrors] = useState({}); const { monitorId } = useParams(); - console.log(monitor); const idMap = { "monitor-url": "url", "monitor-name": "name", diff --git a/Client/src/Pages/Monitors/Details/Charts/index.jsx b/Client/src/Pages/Monitors/Details/Charts/index.jsx index 54d9716fa..44c7ba163 100644 --- a/Client/src/Pages/Monitors/Details/Charts/index.jsx +++ b/Client/src/Pages/Monitors/Details/Charts/index.jsx @@ -5,35 +5,61 @@ import { XAxis, CartesianGrid, ResponsiveContainer, + Cell, } from "recharts"; import { formatDate } from "../../../../Utils/timeUtils"; +import { useState } from "react"; -const CustomLabels = ({ x, width, height, firstDataPoint, lastDataPoint }) => { +const CustomLabels = ({ + x, + width, + height, + firstDataPoint, + lastDataPoint, + type, +}) => { let options = { month: "short", year: undefined, hour: undefined, minute: undefined, }; + if (type === "day") delete options.hour; return ( <> - - {formatDate(new Date(firstDataPoint.day), options)} + + {formatDate(new Date(firstDataPoint.time), options)} - - {formatDate(new Date(lastDataPoint.day), options)} + + {formatDate(new Date(lastDataPoint.time), options)} ); }; -export const UpBarChart = ({ data, dateRange }) => { +export const UpBarChart = ({ data, type, onBarHover }) => { const theme = useTheme(); + const [chartHovered, setChartHovered] = useState(false); + const [hoveredBarIndex, setHoveredBarIndex] = useState(null); + return ( - - + + { + setChartHovered(true); + onBarHover({ time: null, totalChecks: null }); + }} + onMouseLeave={() => { + setChartHovered(false); + setHoveredBarIndex(null); + onBarHover(null); + }} + > { height="100%" firstDataPoint={data[0]} lastDataPoint={data[data.length - 1]} - dateRange={dateRange} + type={type} /> } /> + background={{ fill: "transparent" }} + > + {data.map((entry, index) => ( + { + setHoveredBarIndex(index); + onBarHover(entry); + }} + onMouseLeave={() => { + setHoveredBarIndex(null); + onBarHover({ time: null, totalChecks: null }); + }} + /> + ))} + ); }; -export const DownBarChart = ({ data }) => { +export const DownBarChart = ({ data, type, onBarHover }) => { const theme = useTheme(); + const [chartHovered, setChartHovered] = useState(false); + const [hoveredBarIndex, setHoveredBarIndex] = useState(null); + return ( - - + + { + setChartHovered(true); + onBarHover({ time: null, totalIncidents: null }); + }} + onMouseLeave={() => { + setChartHovered(false); + setHoveredBarIndex(null); + onBarHover(null); + }} + > { height="100%" firstDataPoint={data[0]} lastDataPoint={data[data.length - 1]} + type={type} /> } /> + background={{ fill: "transparent" }} + > + {data.map((entry, index) => ( + { + setHoveredBarIndex(index); + onBarHover(entry); + }} + onMouseLeave={() => { + setHoveredBarIndex(null); + onBarHover({ time: null, totalIncidents: null }); + }} + /> + ))} + ); diff --git a/Client/src/Pages/Monitors/Details/index.jsx b/Client/src/Pages/Monitors/Details/index.jsx index 6f717143e..f3239fb42 100644 --- a/Client/src/Pages/Monitors/Details/index.jsx +++ b/Client/src/Pages/Monitors/Details/index.jsx @@ -210,13 +210,11 @@ const DetailsPage = ({ isAdmin }) => { let totalChecks = 0; let totalIncidents = 0; let totalResponseTime = 0; - let count = 0; Object.keys(aggregateStats).forEach((entry) => { totalChecks += aggregateStats[entry].totalChecks; totalIncidents += aggregateStats[entry].totalIncidents; - totalResponseTime += aggregateStats[entry].totalIncidents; - count++; + totalResponseTime += aggregateStats[entry].avgResponseTime; }); let uptime = @@ -229,13 +227,17 @@ const DetailsPage = ({ isAdmin }) => { return { totalChecks: totalChecks, totalIncidents: totalIncidents, - averageResponseTime: totalResponseTime / count, - uptime: uptime, + averageResponseTime: + Math.floor((totalResponseTime / aggregateStats.length) * 100) / 100, + uptime: Math.floor(uptime), }; }; const monitorStats = calculateStats(); - console.log(aggregateStats); + + const [hoveredUptimeData, setHoveredUptimeData] = useState(null); + const [hoveredIncidentsData, setHoveredIncidentsData] = useState(null); + return ( {loading ? ( @@ -344,7 +346,6 @@ const DetailsPage = ({ isAdmin }) => { onClose={closeCertificate} disableScrollLock marginThreshold={null} - anchorReference={anchorEl} anchorOrigin={{ vertical: "bottom", horizontal: "center", @@ -481,12 +482,31 @@ const DetailsPage = ({ isAdmin }) => { Uptime - - + + Total Checks - {monitorStats.totalChecks} + {hoveredUptimeData !== null + ? hoveredUptimeData.totalChecks || 0 + : monitorStats.totalChecks} + {hoveredUptimeData !== null && + hoveredUptimeData.time !== null && ( + + {formatDate(new Date(hoveredUptimeData.time), { + month: "short", + year: undefined, + minute: undefined, + hour: dateRange === "day" ? "numeric" : undefined, + })} + + )} Uptime Percentage @@ -496,7 +516,11 @@ const DetailsPage = ({ isAdmin }) => { - + @@ -505,13 +529,36 @@ const DetailsPage = ({ isAdmin }) => { Incidents - + Total Incidents - {monitorStats.totalIncidents} + {hoveredIncidentsData !== null + ? hoveredIncidentsData.totalIncidents || 0 + : monitorStats.totalIncidents} + {hoveredIncidentsData !== null && + hoveredIncidentsData.time !== null && ( + + {formatDate(new Date(hoveredIncidentsData.time), { + month: "short", + year: undefined, + minute: undefined, + hour: dateRange === "day" ? "numeric" : undefined, + })} + + )} - + diff --git a/Client/src/Pages/Monitors/Details/styled.jsx b/Client/src/Pages/Monitors/Details/styled.jsx index 1ed6ca8ee..3c178cb00 100644 --- a/Client/src/Pages/Monitors/Details/styled.jsx +++ b/Client/src/Pages/Monitors/Details/styled.jsx @@ -1,7 +1,8 @@ -import { Box, styled } from "@mui/material"; +import { Box, Stack, styled } from "@mui/material"; -export const ChartBox = styled(Box)(({ theme }) => ({ +export const ChartBox = styled(Stack)(({ theme }) => ({ flex: "1 30%", + gap: theme.spacing(8), height: 300, minWidth: 250, padding: theme.spacing(8), @@ -30,16 +31,15 @@ export const ChartBox = styled(Box)(({ theme }) => ({ }, "& .MuiStack-root": { flexDirection: "row", - alignItems: "center", gap: theme.spacing(6), }, - "& > .MuiBox-root:not(:first-of-type)": { - marginTop: theme.spacing(8), - }, "& tspan, & text": { fontSize: 11, fill: theme.palette.text.tertiary, }, + "& path": { + transition: "fill 300ms ease", + }, })); export const IconBox = styled(Box)(({ theme }) => ({ diff --git a/Client/src/Pages/Monitors/Home/StatusBox.jsx b/Client/src/Pages/Monitors/Home/StatusBox.jsx index 08b2e88d9..ece8cde16 100644 --- a/Client/src/Pages/Monitors/Home/StatusBox.jsx +++ b/Client/src/Pages/Monitors/Home/StatusBox.jsx @@ -48,8 +48,7 @@ const StatusBox = ({ title, value }) => { borderColor={theme.palette.border.light} borderRadius={theme.shape.borderRadius} backgroundColor={theme.palette.background.main} - px={theme.spacing(12)} - py={theme.spacing(8)} + p={theme.spacing(8)} overflow="hidden" sx={{ "&:hover": { diff --git a/Client/src/Pages/Monitors/Home/index.jsx b/Client/src/Pages/Monitors/Home/index.jsx index e5c9a765f..64c01b7ac 100644 --- a/Client/src/Pages/Monitors/Home/index.jsx +++ b/Client/src/Pages/Monitors/Home/index.jsx @@ -44,7 +44,7 @@ const Monitors = ({ isAdmin }) => { let loading = monitorState.isLoading && monitorState.monitors.length === 0; return ( - + {loading ? ( ) : ( @@ -79,7 +79,7 @@ const Monitors = ({ isAdmin }) => { {monitorState.monitors?.length !== 0 && ( <> @@ -89,8 +89,7 @@ const Monitors = ({ isAdmin }) => { { { lineHeight={1} fontWeight={500} color={theme.palette.text.primary} + mb={theme.spacing(1)} > { {append} — Here’s an overview of your {type} monitors. diff --git a/Client/src/Utils/timeUtils.js b/Client/src/Utils/timeUtils.js index fa4a27d95..f9f916d04 100644 --- a/Client/src/Utils/timeUtils.js +++ b/Client/src/Utils/timeUtils.js @@ -72,5 +72,7 @@ export const formatDate = (date, customOptions) => { }; // Return the date using the specified options - return date.toLocaleString("en-US", options); + return date + .toLocaleString("en-US", options) + .replace(/\b(AM|PM)\b/g, (match) => match.toLowerCase()); }; diff --git a/Client/src/assets/icons/top-right-arrow.svg b/Client/src/assets/icons/top-right-arrow.svg index 471b9454c..d569cdd7e 100644 --- a/Client/src/assets/icons/top-right-arrow.svg +++ b/Client/src/assets/icons/top-right-arrow.svg @@ -1,3 +1,3 @@ - +