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

198 lines
5.0 KiB
React
Raw Normal View History

// Components
2025-01-24 13:09:57 -08:00
import Breadcrumbs from "../../../Components/Breadcrumbs";
import Greeting from "../../../Utils/greeting";
2025-01-24 13:09:57 -08:00
import StatusBoxes from "./Components/StatusBoxes";
import UptimeDataTable from "./Components/UptimeDataTable";
import Pagination from "../../../Components/Table/TablePagination";
2025-01-30 14:27:11 -08:00
import CreateMonitorHeader from "../../../Components/MonitorCreateHeader";
import Fallback from "../../../Components/Fallback";
2025-01-29 15:06:41 -08:00
import GenericFallback from "../../../Components/GenericFallback";
import SearchComponent from "./Components/SearchComponent";
import MonitorCountHeader from "../../../Components/MonitorCountHeader";
2025-01-24 13:09:57 -08:00
// MUI Components
2025-01-29 15:06:41 -08:00
import { Stack, Box, Button, Typography } from "@mui/material";
2025-01-24 13:09:57 -08:00
// Utils
2025-01-24 13:09:57 -08:00
import { useState, useCallback } from "react";
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
2025-01-24 13:09:57 -08:00
import { useTheme } from "@emotion/react";
import { useNavigate } from "react-router-dom";
2025-01-27 10:00:58 -08:00
import useMonitorsFetch from "./Hooks/useMonitorsFetch";
2025-01-24 13:09:57 -08:00
import { useSelector, useDispatch } from "react-redux";
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
import PropTypes from "prop-types";
2024-08-27 15:18:18 -07:00
2024-12-10 14:23:46 +08:00
const BREADCRUMBS = [{ name: `Uptime`, path: "/uptime" }];
2025-01-24 13:09:57 -08:00
const CreateMonitorButton = ({ shouldRender }) => {
// Utils
const navigate = useNavigate();
if (shouldRender === false) {
return;
}
return (
<Box alignSelf="flex-end">
<Button
variant="contained"
color="accent"
onClick={() => {
navigate("/uptime/create");
}}
>
Create new
</Button>
</Box>
);
};
CreateMonitorButton.propTypes = {
shouldRender: PropTypes.bool,
};
2024-12-12 09:15:39 -05:00
const UptimeMonitors = () => {
// Redux state
2025-01-24 13:09:57 -08:00
const { authToken, user } = useSelector((state) => state.auth);
2025-01-08 11:53:13 -08:00
const rowsPerPage = useSelector((state) => state.ui.monitors.rowsPerPage);
2025-01-24 13:09:57 -08:00
// Local state
const [search, setSearch] = useState(undefined);
const [page, setPage] = useState(undefined);
const [sort, setSort] = useState(undefined);
const [isSearching, setIsSearching] = useState(false);
2025-01-08 13:47:42 -08:00
const [isLoading, setIsLoading] = useState(false);
2024-12-11 10:46:54 +08:00
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
// Utils
const theme = useTheme();
const isAdmin = useIsAdmin();
2025-01-24 13:09:57 -08:00
const dispatch = useDispatch();
2025-01-14 08:36:48 -05:00
2025-01-24 13:09:57 -08:00
// Handlers
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
dispatch(
setRowsPerPage({
value: parseInt(event.target.value, 10),
table: "monitors",
})
);
setPage(0);
};
2024-09-18 17:14:03 -04:00
2025-01-08 15:13:35 -08:00
const triggerUpdate = useCallback(() => {
setMonitorUpdateTrigger((prev) => !prev);
2025-01-08 15:13:35 -08:00
}, []);
2025-01-24 13:09:57 -08:00
const teamId = user.teamId;
2025-01-29 12:32:09 -08:00
const {
monitorsAreLoading,
monitors,
filteredMonitors,
monitorsSummary,
networkError,
} = useMonitorsFetch({
authToken,
teamId,
limit: 25,
page: page,
rowsPerPage: rowsPerPage,
filter: search,
field: sort?.field,
order: sort?.order,
triggerUpdate: monitorUpdateTrigger,
});
2025-01-30 14:31:36 -08:00
const totalMonitors = monitorsSummary?.totalMonitors ?? 0;
2025-01-29 12:32:09 -08:00
2025-01-29 15:06:41 -08:00
if (networkError) {
return (
<GenericFallback>
<Typography
variant="h1"
marginY={theme.spacing(4)}
color={theme.palette.primary.contrastTextTertiary}
>
Network error
</Typography>
<Typography>Please check your connection</Typography>
</GenericFallback>
);
}
2025-01-29 12:32:09 -08:00
if (!isLoading && !monitorsAreLoading && totalMonitors === 0) {
return (
<Fallback
vowelStart={true}
title="uptime monitor"
2025-02-06 19:37:00 -08:00
checks={[
"Check if websites, servers, or services are online and responsive",
"Detect and alert teams about downtime or performance issues",
"Monitor HTTP(s) endpoints, ping responses, Docker containers and ports",
"Track historical uptime and reliability trends",
]}
link="/uptime/create"
isAdmin={isAdmin}
/>
);
}
return (
<Stack
className="monitors"
2025-01-27 13:17:48 -08:00
gap={theme.spacing(10)}
>
2025-01-24 13:09:57 -08:00
<Breadcrumbs list={BREADCRUMBS} />
<CreateMonitorHeader
isAdmin={isAdmin}
shouldRender={!isLoading}
path="/uptime/create"
/>
2025-01-24 13:09:57 -08:00
<Greeting type="uptime" />
<StatusBoxes
monitorsSummary={monitorsSummary}
shouldRender={!monitorsAreLoading}
/>
<Stack direction={"row"}>
<MonitorCountHeader
shouldRender={monitors?.length > 0 && !monitorsAreLoading}
monitorCount={totalMonitors}
heading={"Uptime monitors"}
></MonitorCountHeader>
<SearchComponent
monitors={monitors}
onSearchChange={setSearch}
setIsSearching={setIsSearching}
/>
</Stack>
2025-01-24 13:09:57 -08:00
<UptimeDataTable
isAdmin={isAdmin}
isLoading={isLoading}
setIsLoading={setIsLoading}
filteredMonitors={filteredMonitors}
monitors={monitors}
monitorCount={totalMonitors}
sort={sort}
setSort={setSort}
setSearch={setSearch}
isSearching={isSearching}
monitorsAreLoading={monitorsAreLoading}
triggerUpdate={triggerUpdate}
/>
<Pagination
itemCount={totalMonitors}
paginationLabel="monitors"
page={page}
rowsPerPage={rowsPerPage}
handleChangePage={handleChangePage}
handleChangeRowsPerPage={handleChangeRowsPerPage}
/>
</Stack>
);
2024-08-27 15:18:18 -07:00
};
2024-12-10 14:23:46 +08:00
export default UptimeMonitors;