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 [errors, setErrors] = useState({});
const { monitorId } = useParams();
console.log(monitor);
const idMap = {
"monitor-url": "url",
"monitor-name": "name",
@@ -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 (
<>
<text x={x} y={height} dy={-5} textAnchor="start" fill="#666">
{formatDate(new Date(firstDataPoint.day), options)}
<text x={x} y={height} dy={-3} textAnchor="start">
{formatDate(new Date(firstDataPoint.time), options)}
</text>
<text x={width} y={height} dy={-5} textAnchor="end" fill="#666">
{formatDate(new Date(lastDataPoint.day), options)}
<text x={width} y={height} dy={-3} textAnchor="end">
{formatDate(new Date(lastDataPoint.time), options)}
</text>
</>
);
};
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 (
<ResponsiveContainer width="100%" height={160}>
<BarChart width="100%" height="100%" data={data}>
<ResponsiveContainer width="100%" height={155}>
<BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
onBarHover({ time: null, totalChecks: null });
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
<XAxis
stroke={theme.palette.border.dark}
height={15}
@@ -46,26 +72,63 @@ export const UpBarChart = ({ data, dateRange }) => {
height="100%"
firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]}
dateRange={dateRange}
type={type}
/>
}
/>
<Bar
dataKey="totalChecks"
fill={theme.palette.success.main}
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>
</ResponsiveContainer>
);
};
export const DownBarChart = ({ data }) => {
export const DownBarChart = ({ data, type, onBarHover }) => {
const theme = useTheme();
const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
return (
<ResponsiveContainer width="100%" height={160}>
<BarChart width="100%" height="100%" data={data}>
<ResponsiveContainer width="100%" height={155}>
<BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
onBarHover({ time: null, totalIncidents: null });
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
<XAxis
stroke={theme.palette.border.dark}
height={15}
@@ -78,14 +141,36 @@ export const DownBarChart = ({ data }) => {
height="100%"
firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]}
type={type}
/>
}
/>
<Bar
dataKey="totalIncidents"
fill={theme.palette.error.text}
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>
</ResponsiveContainer>
);
+61 -14
View File
@@ -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 (
<Box className="monitor-details">
{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 }) => {
</IconBox>
<Typography component="h2">Uptime</Typography>
</Stack>
<Stack justifyContent="space-between" my={theme.spacing(8)}>
<Box>
<Stack justifyContent="space-between">
<Box position="relative">
<Typography>Total Checks</Typography>
<Typography component="span">
{monitorStats.totalChecks}
{hoveredUptimeData !== null
? hoveredUptimeData.totalChecks || 0
: monitorStats.totalChecks}
</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>
<Typography>Uptime Percentage</Typography>
@@ -496,7 +516,11 @@ const DetailsPage = ({ isAdmin }) => {
</Typography>
</Box>
</Stack>
<UpBarChart data={aggregateStats} type={dateRange} />
<UpBarChart
data={aggregateStats}
type={dateRange}
onBarHover={setHoveredUptimeData}
/>
</ChartBox>
<ChartBox>
<Stack>
@@ -505,13 +529,36 @@ const DetailsPage = ({ isAdmin }) => {
</IconBox>
<Typography component="h2">Incidents</Typography>
</Stack>
<Box mb={theme.spacing(8)}>
<Box position="relative">
<Typography>Total Incidents</Typography>
<Typography component="span">
{monitorStats.totalIncidents}
{hoveredIncidentsData !== null
? hoveredIncidentsData.totalIncidents || 0
: monitorStats.totalIncidents}
</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>
<DownBarChart data={aggregateStats} />
<DownBarChart
data={aggregateStats}
type={dateRange}
onBarHover={setHoveredIncidentsData}
/>
</ChartBox>
<ChartBox>
<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%",
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 }) => ({
+1 -2
View File
@@ -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": {
+4 -5
View File
@@ -44,7 +44,7 @@ const Monitors = ({ isAdmin }) => {
let loading = monitorState.isLoading && monitorState.monitors.length === 0;
return (
<Stack className="monitors" gap={theme.spacing(12)}>
<Stack className="monitors" gap={theme.spacing(8)}>
{loading ? (
<SkeletonLayout />
) : (
@@ -79,7 +79,7 @@ const Monitors = ({ isAdmin }) => {
{monitorState.monitors?.length !== 0 && (
<>
<Stack
gap={theme.spacing(12)}
gap={theme.spacing(8)}
direction="row"
justifyContent="space-between"
>
@@ -89,8 +89,7 @@ const Monitors = ({ isAdmin }) => {
</Stack>
<Box
flex={1}
px={theme.spacing(16)}
py={theme.spacing(12)}
p={theme.spacing(10)}
border={1}
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
@@ -99,7 +98,7 @@ const Monitors = ({ isAdmin }) => {
<Stack
direction="row"
alignItems="center"
mb={theme.spacing(12)}
mb={theme.spacing(8)}
>
<Typography
component="h2"
+1 -1
View File
@@ -4,7 +4,7 @@ const text = {
primary: "#fafafa",
secondary: "#e6e6e6",
tertiary: "#a1a1aa",
accent: "#e6e6e6",
accent: "#8e8e8f",
disabled: "rgba(172, 172, 172, 0.3)",
};
const background = {
+3 -3
View File
@@ -160,6 +160,7 @@ const Greeting = ({ type = "" }) => {
lineHeight={1}
fontWeight={500}
color={theme.palette.text.primary}
mb={theme.spacing(1)}
>
<Typography
component="span"
@@ -173,11 +174,10 @@ const Greeting = ({ type = "" }) => {
</Typography>
</Typography>
<Typography
sx={{ opacity: 0.8 }}
lineHeight={1}
fontSize={14}
fontWeight={300}
color={theme.palette.text.secondary}
fontWeight={400}
color={theme.palette.text.accent}
>
{append} — Here’s an overview of your {type} monitors.
</Typography>
+3 -1
View File
@@ -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());
};
+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">
<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>

Before

Width:  |  Height:  |  Size: 224 B

After

Width:  |  Height:  |  Size: 224 B