Files
Checkmate/Client/src/Pages/Monitors/Home/index.jsx
T

151 lines
4.7 KiB
React
Raw Normal View History

2024-08-27 15:18:18 -07:00
import "./index.css";
import { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { getUptimeMonitorsByTeamId } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
import { useNavigate } from "react-router-dom";
import { useTheme } from "@emotion/react";
import BasicTable from "../../../Components/BasicTable";
2024-08-30 21:11:09 -04:00
import { Box, Button, Stack, Typography } from "@mui/material";
2024-08-27 15:18:18 -07:00
import PropTypes from "prop-types";
import SkeletonLayout from "./skeleton";
import Fallback from "./fallback";
2024-08-27 19:46:09 -07:00
import StatusBox from "./StatusBox";
2024-08-27 15:18:18 -07:00
import { buildData } from "./monitorData";
2024-08-28 10:28:02 -04:00
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-08-30 13:20:36 -04:00
import Greeting from "../../../Utils/greeting";
2024-08-27 15:18:18 -07:00
const Monitors = ({ isAdmin }) => {
const theme = useTheme();
const navigate = useNavigate();
const monitorState = useSelector((state) => state.uptimeMonitors);
const authState = useSelector((state) => state.auth);
const dispatch = useDispatch({});
useEffect(() => {
dispatch(getUptimeMonitorsByTeamId(authState.authToken));
}, [authState.authToken, dispatch]);
const monitorStats = monitorState.monitors.reduce(
(acc, monitor) => {
if (monitor.isActive === false) {
acc["paused"] += 1;
} else if (monitor.status === true) {
acc["up"] += 1;
} else {
acc["down"] += 1;
}
return acc;
},
{ paused: 0, up: 0, down: 0 }
);
2024-08-27 15:18:18 -07:00
const data = buildData(monitorState.monitors, isAdmin, navigate);
let loading = monitorState.isLoading && monitorState.monitors.length === 0;
2024-08-28 10:28:02 -04:00
const now = new Date();
const hour = now.getHours();
let greeting = "";
let emoji = "";
if (hour < 12) {
greeting = "morning";
emoji = "🌅";
} else if (hour < 18) {
greeting = "afternoon";
emoji = "🌞";
} else {
greeting = "evening";
emoji = "🌙";
}
2024-08-27 15:18:18 -07:00
return (
2024-08-28 14:42:38 -04:00
<Stack className="monitors" gap={theme.spacing(12)}>
2024-08-27 15:18:18 -07:00
{loading ? (
<SkeletonLayout />
) : (
<>
2024-08-28 10:28:02 -04:00
<Box>
<Breadcrumbs list={[{ name: `monitors`, path: "/monitors" }]} />
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
mt={theme.spacing(5)}
2024-08-27 15:18:18 -07:00
>
2024-08-30 13:20:36 -04:00
<Greeting type="uptime" />
2024-08-28 10:28:02 -04:00
{monitorState.monitors?.length !== 0 && (
<Button
2024-08-30 21:11:09 -04:00
variant="contained"
color="common"
2024-08-28 10:28:02 -04:00
onClick={() => {
navigate("/monitors/create");
}}
sx={{ fontWeight: 500 }}
2024-08-30 21:11:09 -04:00
>
Create monitor
</Button>
2024-08-28 10:28:02 -04:00
)}
</Stack>
</Box>
2024-08-28 16:50:42 -04:00
{isAdmin && monitorState.monitors?.length === 0 && (
2024-08-27 15:18:18 -07:00
<Fallback isAdmin={isAdmin} />
)}
{monitorState.monitors?.length !== 0 && (
<>
<Stack
2024-08-28 14:42:38 -04:00
gap={theme.spacing(12)}
2024-08-27 15:18:18 -07:00
direction="row"
justifyContent="space-between"
>
<StatusBox title="up" value={monitorStats.up} />
<StatusBox title="down" value={monitorStats.down} />
<StatusBox title="paused" value={monitorStats.paused} />
2024-08-27 15:18:18 -07:00
</Stack>
<Box
flex={1}
2024-08-29 21:02:15 -04:00
px={theme.spacing(16)}
py={theme.spacing(12)}
2024-08-27 15:18:18 -07:00
border={1}
2024-08-28 14:42:38 -04:00
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main}
2024-08-27 15:18:18 -07:00
>
2024-08-28 14:42:38 -04:00
<Stack
direction="row"
alignItems="center"
mb={theme.spacing(12)}
>
<Typography
component="h2"
color={theme.palette.text.secondary}
2024-08-29 21:02:15 -04:00
letterSpacing={-0.5}
2024-08-28 14:42:38 -04:00
>
2024-08-29 21:02:15 -04:00
Actively monitoring
2024-08-28 14:42:38 -04:00
</Typography>
<Box
className="current-monitors-counter"
color={theme.palette.text.primary}
border={1}
borderColor={theme.palette.border.light}
2024-08-28 22:14:55 -04:00
backgroundColor={theme.palette.background.accent}
2024-08-28 14:42:38 -04:00
>
2024-08-27 15:18:18 -07:00
{monitorState.monitors.length}
</Box>
{/* TODO - add search bar */}
</Stack>
<BasicTable data={data} paginated={true} table={"monitors"} />
</Box>
</>
)}
</>
)}
</Stack>
);
};
Monitors.propTypes = {
isAdmin: PropTypes.bool,
};
export default Monitors;