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

144 lines
4.9 KiB
React
Raw Normal View History

2024-08-27 15:18:18 -07:00
import "./index.css";
2024-09-18 17:22:58 -04:00
import { useEffect, useState } from "react";
2024-08-27 15:18:18 -07:00
import { useSelector, useDispatch } from "react-redux";
import { getUptimeMonitorsByTeamId } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice";
import { useNavigate } from "react-router-dom";
import { useTheme } from "@emotion/react";
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-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-09-04 16:15:14 -07:00
import MonitorTable from "./MonitorTable";
2024-09-18 17:14:03 -04:00
import Search from "../../../Components/Inputs/Search";
2024-09-19 11:07:38 +08:00
import useDebounce from "../../../Utils/debounce";
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);
2024-09-18 17:22:58 -04:00
const [search, setSearch] = useState("");
2024-08-27 15:18:18 -07:00
const dispatch = useDispatch({});
2024-09-19 11:07:38 +08:00
const debouncedFilter = useDebounce(search, 500);
2024-08-27 15:18:18 -07:00
useEffect(() => {
dispatch(getUptimeMonitorsByTeamId(authState.authToken));
}, [authState.authToken, dispatch]);
2024-09-05 12:47:27 -07:00
let loading =
monitorState?.isLoading &&
monitorState?.monitorsSummary?.monitors?.length === 0;
2024-09-18 17:14:03 -04:00
2024-08-27 15:18:18 -07:00
return (
2024-09-04 12:57:08 -04:00
<Stack className="monitors" gap={theme.spacing(8)}>
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-09-05 14:27:13 -07:00
{monitorState?.monitorsSummary?.monitors?.length !== 0 && (
2024-08-28 10:28:02 -04:00
<Button
2024-08-30 21:11:09 -04:00
variant="contained"
color="primary"
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-09-05 12:47:27 -07:00
{isAdmin && monitorState?.monitorsSummary?.monitors?.length === 0 && (
2024-08-27 15:18:18 -07:00
<Fallback isAdmin={isAdmin} />
)}
2024-09-05 14:28:07 -07:00
{monitorState?.monitorsSummary?.monitors?.length !== 0 && (
2024-08-27 15:18:18 -07:00
<>
<Stack
2024-09-04 12:57:08 -04:00
gap={theme.spacing(8)}
2024-08-27 15:18:18 -07:00
direction="row"
justifyContent="space-between"
>
2024-09-05 12:47:27 -07:00
<StatusBox
title="up"
value={monitorState?.monitorsSummary?.monitorCounts?.up ?? 0}
/>
<StatusBox
title="down"
value={
monitorState?.monitorsSummary?.monitorCounts?.down ?? 0
}
/>
<StatusBox
title="paused"
value={
monitorState?.monitorsSummary?.monitorCounts?.paused ?? 0
}
/>
2024-08-27 15:18:18 -07:00
</Stack>
<Box
flex={1}
2024-09-18 17:14:03 -04:00
px={theme.spacing(10)}
py={theme.spacing(8)}
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"
2024-09-04 12:57:08 -04:00
mb={theme.spacing(8)}
2024-08-28 14:42:38 -04:00
>
2024-09-16 12:53:59 -04:00
<Typography
component="h2"
variant="h2"
fontWeight={500}
letterSpacing={-0.2}
>
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
>
{monitorState?.monitorsSummary?.monitorCounts?.total || 0}
2024-08-27 15:18:18 -07:00
</Box>
2024-09-18 17:14:03 -04:00
<Box width="25%" minWidth={150} ml="auto">
2024-09-18 17:22:58 -04:00
<Search
options={monitorState?.monitorsSummary.monitors}
2024-09-18 20:27:55 -04:00
filteredBy="name"
2024-09-18 17:22:58 -04:00
value={search}
2024-09-19 11:07:38 +08:00
handleInputChange={setSearch}
2024-09-18 17:22:58 -04:00
/>
2024-09-18 17:14:03 -04:00
</Box>
2024-08-27 15:18:18 -07:00
</Stack>
2024-09-19 11:07:38 +08:00
<MonitorTable isAdmin={isAdmin} filter={debouncedFilter} />
2024-08-27 15:18:18 -07:00
</Box>
</>
)}
</>
)}
</Stack>
);
};
Monitors.propTypes = {
isAdmin: PropTypes.bool,
};
export default Monitors;