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

167 lines
5.3 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 Button from "../../../Components/Button";
import { useTheme } from "@emotion/react";
import BasicTable from "../../../Components/BasicTable";
import { Box, Stack, Typography } from "@mui/material";
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-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 up = monitorState.monitors.reduce((acc, cur) => {
return cur.status === true ? acc + 1 : acc;
}, 0);
const down = monitorState.monitors.length - up;
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-28 10:28:02 -04:00
<Box>
2024-08-28 14:42:38 -04:00
<Typography
component="h1"
lineHeight={1}
color={theme.palette.text.primary}
>
2024-08-28 10:28:02 -04:00
<Typography
component="span"
fontSize="inherit"
2024-08-28 14:42:38 -04:00
color={theme.palette.text.secondary}
2024-08-28 10:28:02 -04:00
>
Good {greeting},{" "}
</Typography>
<Typography
component="span"
fontSize="inherit"
fontWeight="inherit"
>
{authState.user.firstName} {emoji}
</Typography>
</Typography>
<Typography
sx={{ opacity: 0.8 }}
lineHeight={1}
fontWeight={300}
2024-08-28 14:42:38 -04:00
color={theme.palette.text.secondary}
2024-08-28 10:28:02 -04:00
>
Here’s an overview of your uptime monitors.
</Typography>
</Box>
{monitorState.monitors?.length !== 0 && (
<Button
level="primary"
label="Create monitor"
onClick={() => {
navigate("/monitors/create");
}}
sx={{ fontWeight: 500 }}
/>
)}
</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"
>
2024-08-27 19:46:09 -07:00
<StatusBox title="up" value={up} />
<StatusBox title="down" value={down} />
<StatusBox title="paused" value={0} />
2024-08-27 15:18:18 -07:00
</Stack>
<Box
flex={1}
2024-08-28 14:42:38 -04:00
p={theme.spacing(16)}
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}
>
Current monitors
</Typography>
<Box
className="current-monitors-counter"
color={theme.palette.text.primary}
border={1}
borderColor={theme.palette.border.light}
backgroundColor={theme.palette.unresolved.bg}
>
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;