Added hover state to bars

This commit is contained in:
Daniel Cojocea committed 2024-09-04 12:57:08 -04:00
1 parent 0be386513f
commit a192b3d14c
10 files changed
+181 -50

No files matched your search

@@ -53,7 +53,6 @@ const Configure = () => {
const [monitor, setMonitor] = useState({}); const [monitor, setMonitor] = useState({});
const [errors, setErrors] = useState({}); const [errors, setErrors] = useState({});
const { monitorId } = useParams(); const { monitorId } = useParams();
console.log(monitor);
const idMap = { const idMap = {
"monitor-url": "url", "monitor-url": "url",
"monitor-name": "name", "monitor-name": "name",
@@ -5,35 +5,61 @@ import {
XAxis, XAxis,
CartesianGrid, CartesianGrid,
ResponsiveContainer, ResponsiveContainer,
Cell,
} from "recharts"; } from "recharts";
import { formatDate } from "../../../../Utils/timeUtils"; 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 = { let options = {
month: "short", month: "short",
year: undefined, year: undefined,
hour: undefined, hour: undefined,
minute: undefined, minute: undefined,
}; };
if (type === "day") delete options.hour;
return ( return (
<> <>
<text x={x} y={height} dy={-5} textAnchor="start" fill="#666"> <text x={x} y={height} dy={-3} textAnchor="start">
{formatDate(new Date(firstDataPoint.day), options)} {formatDate(new Date(firstDataPoint.time), options)}
</text> </text>
<text x={width} y={height} dy={-5} textAnchor="end" fill="#666"> <text x={width} y={height} dy={-3} textAnchor="end">
{formatDate(new Date(lastDataPoint.day), options)} {formatDate(new Date(lastDataPoint.time), options)}
</text> </text>
</> </>
); );
}; };
export const UpBarChart = ({ data, dateRange }) => { export const UpBarChart = ({ data, type, onBarHover }) => {
const theme = useTheme(); const theme = useTheme();
const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
return ( return (
<ResponsiveContainer width="100%" height={160}> <ResponsiveContainer width="100%" height={155}>
<BarChart width="100%" height="100%" data={data}> <BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
onBarHover({ time: null, totalChecks: null });
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
<XAxis <XAxis
stroke={theme.palette.border.dark} stroke={theme.palette.border.dark}
height={15} height={15}
@@ -46,26 +72,63 @@ export const UpBarChart = ({ data, dateRange }) => {
height="100%" height="100%"
firstDataPoint={data[0]} firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]} lastDataPoint={data[data.length - 1]}
dateRange={dateRange} type={type}
/> />
} }
/> />
<Bar <Bar
dataKey="totalChecks" dataKey="totalChecks"
fill={theme.palette.success.main}
barSize={5} barSize={5}
/> background={{ fill: "transparent" }}
>
{data.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={
hoveredBarIndex === index
? theme.palette.success.main
: chartHovered
? theme.palette.success.light
: theme.palette.success.main
}
onMouseEnter={() => {
setHoveredBarIndex(index);
onBarHover(entry);
}}
onMouseLeave={() => {
setHoveredBarIndex(null);
onBarHover({ time: null, totalChecks: null });
}}
/>
))}
</Bar>
</BarChart> </BarChart>
</ResponsiveContainer> </ResponsiveContainer>
); );
}; };
export const DownBarChart = ({ data }) => { export const DownBarChart = ({ data, type, onBarHover }) => {
const theme = useTheme(); const theme = useTheme();
const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
return ( return (
<ResponsiveContainer width="100%" height={160}> <ResponsiveContainer width="100%" height={155}>
<BarChart width="100%" height="100%" data={data}> <BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
onBarHover({ time: null, totalIncidents: null });
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
<XAxis <XAxis
stroke={theme.palette.border.dark} stroke={theme.palette.border.dark}
height={15} height={15}
@@ -78,14 +141,36 @@ export const DownBarChart = ({ data }) => {
height="100%" height="100%"
firstDataPoint={data[0]} firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]} lastDataPoint={data[data.length - 1]}
type={type}
/> />
} }
/> />
<Bar <Bar
dataKey="totalIncidents" dataKey="totalIncidents"
fill={theme.palette.error.text}
barSize={5} barSize={5}
/> background={{ fill: "transparent" }}
>
{data.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={
hoveredBarIndex === index
? theme.palette.error.text
: chartHovered
? theme.palette.error.light
: theme.palette.error.text
}
onMouseEnter={() => {
setHoveredBarIndex(index);
onBarHover(entry);
}}
onMouseLeave={() => {
setHoveredBarIndex(null);
onBarHover({ time: null, totalIncidents: null });
}}
/>
))}
</Bar>
</BarChart> </BarChart>
</ResponsiveContainer> </ResponsiveContainer>
); );
+61 -14
View File
@@ -210,13 +210,11 @@ const DetailsPage = ({ isAdmin }) => {
let totalChecks = 0; let totalChecks = 0;
let totalIncidents = 0; let totalIncidents = 0;
let totalResponseTime = 0; let totalResponseTime = 0;
let count = 0;
Object.keys(aggregateStats).forEach((entry) => { Object.keys(aggregateStats).forEach((entry) => {
totalChecks += aggregateStats[entry].totalChecks; totalChecks += aggregateStats[entry].totalChecks;
totalIncidents += aggregateStats[entry].totalIncidents; totalIncidents += aggregateStats[entry].totalIncidents;
totalResponseTime += aggregateStats[entry].totalIncidents; totalResponseTime += aggregateStats[entry].avgResponseTime;
count++;
}); });
let uptime = let uptime =
@@ -229,13 +227,17 @@ const DetailsPage = ({ isAdmin }) => {
return { return {
totalChecks: totalChecks, totalChecks: totalChecks,
totalIncidents: totalIncidents, totalIncidents: totalIncidents,
averageResponseTime: totalResponseTime / count, averageResponseTime:
uptime: uptime, Math.floor((totalResponseTime / aggregateStats.length) * 100) / 100,
uptime: Math.floor(uptime),
}; };
}; };
const monitorStats = calculateStats(); const monitorStats = calculateStats();
console.log(aggregateStats);
const [hoveredUptimeData, setHoveredUptimeData] = useState(null);
const [hoveredIncidentsData, setHoveredIncidentsData] = useState(null);
return ( return (
<Box className="monitor-details"> <Box className="monitor-details">
{loading ? ( {loading ? (
@@ -344,7 +346,6 @@ const DetailsPage = ({ isAdmin }) => {
onClose={closeCertificate} onClose={closeCertificate}
disableScrollLock disableScrollLock
marginThreshold={null} marginThreshold={null}
anchorReference={anchorEl}
anchorOrigin={{ anchorOrigin={{
vertical: "bottom", vertical: "bottom",
horizontal: "center", horizontal: "center",
@@ -481,12 +482,31 @@ const DetailsPage = ({ isAdmin }) => {
</IconBox> </IconBox>
<Typography component="h2">Uptime</Typography> <Typography component="h2">Uptime</Typography>
</Stack> </Stack>
<Stack justifyContent="space-between" my={theme.spacing(8)}> <Stack justifyContent="space-between">
<Box> <Box position="relative">
<Typography>Total Checks</Typography> <Typography>Total Checks</Typography>
<Typography component="span"> <Typography component="span">
{monitorStats.totalChecks} {hoveredUptimeData !== null
? hoveredUptimeData.totalChecks || 0
: monitorStats.totalChecks}
</Typography> </Typography>
{hoveredUptimeData !== null &&
hoveredUptimeData.time !== null && (
<Typography
component="h5"
position="absolute"
top="100%"
fontSize={11}
color={theme.palette.text.tertiary}
>
{formatDate(new Date(hoveredUptimeData.time), {
month: "short",
year: undefined,
minute: undefined,
hour: dateRange === "day" ? "numeric" : undefined,
})}
</Typography>
)}
</Box> </Box>
<Box> <Box>
<Typography>Uptime Percentage</Typography> <Typography>Uptime Percentage</Typography>
@@ -496,7 +516,11 @@ const DetailsPage = ({ isAdmin }) => {
</Typography> </Typography>
</Box> </Box>
</Stack> </Stack>
<UpBarChart data={aggregateStats} type={dateRange} /> <UpBarChart
data={aggregateStats}
type={dateRange}
onBarHover={setHoveredUptimeData}
/>
</ChartBox> </ChartBox>
<ChartBox> <ChartBox>
<Stack> <Stack>
@@ -505,13 +529,36 @@ const DetailsPage = ({ isAdmin }) => {
</IconBox> </IconBox>
<Typography component="h2">Incidents</Typography> <Typography component="h2">Incidents</Typography>
</Stack> </Stack>
<Box mb={theme.spacing(8)}> <Box position="relative">
<Typography>Total Incidents</Typography> <Typography>Total Incidents</Typography>
<Typography component="span"> <Typography component="span">
{monitorStats.totalIncidents} {hoveredIncidentsData !== null
? hoveredIncidentsData.totalIncidents || 0
: monitorStats.totalIncidents}
</Typography> </Typography>
{hoveredIncidentsData !== null &&
hoveredIncidentsData.time !== null && (
<Typography
component="h5"
position="absolute"
top="100%"
fontSize={11}
color={theme.palette.text.tertiary}
>
{formatDate(new Date(hoveredIncidentsData.time), {
month: "short",
year: undefined,
minute: undefined,
hour: dateRange === "day" ? "numeric" : undefined,
})}
</Typography>
)}
</Box> </Box>
<DownBarChart data={aggregateStats} /> <DownBarChart
data={aggregateStats}
type={dateRange}
onBarHover={setHoveredIncidentsData}
/>
</ChartBox> </ChartBox>
<ChartBox> <ChartBox>
<Stack> <Stack>
+6 -6
View File
@@ -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%", flex: "1 30%",
gap: theme.spacing(8),
height: 300, height: 300,
minWidth: 250, minWidth: 250,
padding: theme.spacing(8), padding: theme.spacing(8),
@@ -30,16 +31,15 @@ export const ChartBox = styled(Box)(({ theme }) => ({
}, },
"& .MuiStack-root": { "& .MuiStack-root": {
flexDirection: "row", flexDirection: "row",
alignItems: "center",
gap: theme.spacing(6), gap: theme.spacing(6),
}, },
"& > .MuiBox-root:not(:first-of-type)": {
marginTop: theme.spacing(8),
},
"& tspan, & text": { "& tspan, & text": {
fontSize: 11, fontSize: 11,
fill: theme.palette.text.tertiary, fill: theme.palette.text.tertiary,
}, },
"& path": {
transition: "fill 300ms ease",
},
})); }));
export const IconBox = styled(Box)(({ theme }) => ({ export const IconBox = styled(Box)(({ theme }) => ({
+1 -2
View File
@@ -48,8 +48,7 @@ const StatusBox = ({ title, value }) => {
borderColor={theme.palette.border.light} borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius} borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main} backgroundColor={theme.palette.background.main}
px={theme.spacing(12)} p={theme.spacing(8)}
py={theme.spacing(8)}
overflow="hidden" overflow="hidden"
sx={{ sx={{
"&:hover": { "&:hover": {
+4 -5
View File
@@ -44,7 +44,7 @@ const Monitors = ({ isAdmin }) => {
let loading = monitorState.isLoading && monitorState.monitors.length === 0; let loading = monitorState.isLoading && monitorState.monitors.length === 0;
return ( return (
<Stack className="monitors" gap={theme.spacing(12)}> <Stack className="monitors" gap={theme.spacing(8)}>
{loading ? ( {loading ? (
<SkeletonLayout /> <SkeletonLayout />
) : ( ) : (
@@ -79,7 +79,7 @@ const Monitors = ({ isAdmin }) => {
{monitorState.monitors?.length !== 0 && ( {monitorState.monitors?.length !== 0 && (
<> <>
<Stack <Stack
gap={theme.spacing(12)} gap={theme.spacing(8)}
direction="row" direction="row"
justifyContent="space-between" justifyContent="space-between"
> >
@@ -89,8 +89,7 @@ const Monitors = ({ isAdmin }) => {
</Stack> </Stack>
<Box <Box
flex={1} flex={1}
px={theme.spacing(16)} p={theme.spacing(10)}
py={theme.spacing(12)}
border={1} border={1}
borderColor={theme.palette.border.light} borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius} borderRadius={theme.shape.borderRadius}
@@ -99,7 +98,7 @@ const Monitors = ({ isAdmin }) => {
<Stack <Stack
direction="row" direction="row"
alignItems="center" alignItems="center"
mb={theme.spacing(12)} mb={theme.spacing(8)}
> >
<Typography <Typography
component="h2" component="h2"
+1 -1
View File
@@ -4,7 +4,7 @@ const text = {
primary: "#fafafa", primary: "#fafafa",
secondary: "#e6e6e6", secondary: "#e6e6e6",
tertiary: "#a1a1aa", tertiary: "#a1a1aa",
accent: "#e6e6e6", accent: "#8e8e8f",
disabled: "rgba(172, 172, 172, 0.3)", disabled: "rgba(172, 172, 172, 0.3)",
}; };
const background = { const background = {
+3 -3
View File
@@ -160,6 +160,7 @@ const Greeting = ({ type = "" }) => {
lineHeight={1} lineHeight={1}
fontWeight={500} fontWeight={500}
color={theme.palette.text.primary} color={theme.palette.text.primary}
mb={theme.spacing(1)}
> >
<Typography <Typography
component="span" component="span"
@@ -173,11 +174,10 @@ const Greeting = ({ type = "" }) => {
</Typography> </Typography>
</Typography> </Typography>
<Typography <Typography
sx={{ opacity: 0.8 }}
lineHeight={1} lineHeight={1}
fontSize={14} fontSize={14}
fontWeight={300} fontWeight={400}
color={theme.palette.text.secondary} color={theme.palette.text.accent}
> >
{append} — Here’s an overview of your {type} monitors. {append} — Here’s an overview of your {type} monitors.
</Typography> </Typography>
+3 -1
View File
@@ -72,5 +72,7 @@ export const formatDate = (date, customOptions) => {
}; };
// Return the date using the specified options // 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());
}; };
+1 -1
View File
@@ -1,3 +1,3 @@
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 17L17 7M17 7H7M17 7V17" stroke="#667085" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> <path d="M7 17L17 7M17 7H7M17 7V17" stroke="#667085" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 224 B

After

Width:  |  Height:  |  Size: 224 B