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

245 lines
6.7 KiB
React
Raw Normal View History

2025-03-14 11:05:33 -07:00
// Required Data
// 1. Monitor summary
// 2. List of monitors filtered by search term with 25 checks each
// 2a.List of monitors must have the total number of monitors that match.
// 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";
2025-03-28 01:19:13 +05:30
import Filter from "./Components/Filter";
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-24 13:09:57 -08:00
import { useSelector, useDispatch } from "react-redux";
import { setRowsPerPage } from "../../../Features/UI/uiSlice";
import PropTypes from "prop-types";
2025-06-12 12:44:46 +08:00
import {
useFetchMonitorsWithSummary,
useFetchMonitorsWithChecks,
} from "../../../Hooks/monitorHooks";
2025-03-17 01:16:42 +05:30
import { useTranslation } from "react-i18next";
2025-06-12 12:44:46 +08:00
2025-03-14 11:05:33 -07:00
const TYPES = ["http", "ping", "docker", "port"];
2025-01-24 13:09:57 -08:00
const CreateMonitorButton = ({ shouldRender }) => {
// Utils
const navigate = useNavigate();
2025-05-09 14:40:07 -04:00
const { t } = useTranslation();
2025-01-24 13:09:57 -08:00
if (shouldRender === false) {
return;
}
return (
<Box alignSelf="flex-end">
<Button
variant="contained"
color="accent"
onClick={() => {
navigate("/uptime/create");
}}
>
2025-05-09 14:40:07 -04:00
{t("createNew")}
2025-01-24 13:09:57 -08:00
</Button>
</Box>
);
};
CreateMonitorButton.propTypes = {
shouldRender: PropTypes.bool,
};
2024-12-12 09:15:39 -05:00
const UptimeMonitors = () => {
// Redux state
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);
2024-12-11 10:46:54 +08:00
const [monitorUpdateTrigger, setMonitorUpdateTrigger] = useState(false);
2025-04-02 15:00:00 +05:30
const [selectedTypes, setSelectedTypes] = useState(undefined);
const [selectedState, setSelectedState] = useState(undefined);
const [selectedStatus, setSelectedStatus] = useState(undefined);
2025-04-03 15:07:42 +05:30
const [toFilterStatus, setToFilterStatus] = useState(undefined);
const [toFilterActive, setToFilterActive] = useState(undefined);
2024-12-11 10:46:54 +08:00
// Utils
const theme = useTheme();
const isAdmin = useIsAdmin();
2025-01-24 13:09:57 -08:00
const dispatch = useDispatch();
2025-03-17 01:16:42 +05:30
const { t } = useTranslation();
2025-01-14 08:36:48 -05:00
2025-05-09 14:40:07 -04:00
const BREADCRUMBS = [{ name: t("menu.uptime"), path: "/uptime" }];
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
2025-03-14 11:05:33 -07:00
const [monitors, monitorsSummary, monitorsWithSummaryIsLoading, networkError] =
useFetchMonitorsWithSummary({
types: TYPES,
2025-03-20 09:11:07 -07:00
monitorUpdateTrigger,
2025-03-14 11:05:33 -07:00
});
2025-04-02 15:00:00 +05:30
const handleReset = () => {
setSelectedState(undefined);
setSelectedTypes(undefined);
setSelectedStatus(undefined);
2025-04-03 15:07:42 +05:30
setToFilterStatus(undefined);
setToFilterActive(undefined);
2025-04-02 15:00:00 +05:30
};
2025-04-03 15:07:42 +05:30
const filterLookup = new Map([
[toFilterStatus, "status"],
2025-04-09 22:34:59 +05:30
[toFilterActive, "isActive"],
2025-04-03 15:07:42 +05:30
]);
2025-04-02 15:00:00 +05:30
2025-04-03 15:07:42 +05:30
const activeFilter = [...filterLookup].find(([key]) => key !== undefined);
const field = activeFilter?.[1] || sort?.field;
const filter = activeFilter?.[0] || search;
const effectiveTypes = selectedTypes?.length ? selectedTypes : TYPES;
2025-03-14 11:05:33 -07:00
const [
monitorsWithChecks,
monitorsWithChecksCount,
monitorsWithChecksIsLoading,
monitorsWithChecksNetworkError,
] = useFetchMonitorsWithChecks({
2025-04-03 15:07:42 +05:30
types: effectiveTypes,
2025-01-29 12:32:09 -08:00
limit: 25,
page: page,
rowsPerPage: rowsPerPage,
2025-03-28 01:19:13 +05:30
filter: filter,
field: field,
2025-01-29 12:32:09 -08:00
order: sort?.order,
2025-03-20 09:11:07 -07:00
monitorUpdateTrigger,
2025-01-29 12:32:09 -08:00
});
2025-03-14 11:05:33 -07:00
2025-05-06 00:41:40 +05:30
const isLoading = monitorsWithSummaryIsLoading || monitorsWithChecksIsLoading;
2025-01-29 15:06:41 -08:00
if (networkError) {
return (
<GenericFallback>
<Typography
variant="h1"
marginY={theme.spacing(4)}
color={theme.palette.primary.contrastTextTertiary}
>
2025-06-07 19:30:55 +02:00
{t("common.toasts.networkError")}
2025-01-29 15:06:41 -08:00
</Typography>
2025-06-07 19:30:55 +02:00
<Typography>{t("common.toasts.checkConnection")}</Typography>
2025-01-29 15:06:41 -08:00
</GenericFallback>
);
}
2025-02-07 09:19:01 -08:00
if (
2025-03-14 11:05:33 -07:00
!isLoading &&
(monitorsSummary?.totalMonitors === 0 ||
typeof monitorsSummary?.totalMonitors === "undefined")
2025-02-07 09:19:01 -08:00
) {
return (
<Fallback
vowelStart={true}
title="uptime monitor"
2025-02-06 19:37:00 -08:00
checks={[
2025-02-06 19:42:09 -08:00
"Check if websites or servers are online & responsive",
"Alert teams about downtime or performance issues",
"Monitor HTTP endpoints, pings, containers & ports",
2025-02-06 19:37:00 -08:00
"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}
2025-05-06 00:41:40 +05:30
isLoading={isLoading}
path="/uptime/create"
2025-04-27 03:05:14 +05:30
bulkPath="/uptime/bulk-import"
/>
2025-01-24 13:09:57 -08:00
<Greeting type="uptime" />
<StatusBoxes
monitorsSummary={monitorsSummary}
2025-03-14 11:05:33 -07:00
shouldRender={!monitorsWithSummaryIsLoading}
2025-01-24 13:09:57 -08:00
/>
<Stack direction={"row"}>
<MonitorCountHeader
2025-03-14 11:05:33 -07:00
shouldRender={monitors?.length > 0 && !monitorsWithSummaryIsLoading}
monitorCount={monitorsSummary?.totalMonitors}
></MonitorCountHeader>
2025-03-28 01:19:13 +05:30
<Filter
selectedTypes={selectedTypes}
setSelectedTypes={setSelectedTypes}
2025-04-02 15:00:00 +05:30
selectedStatus={selectedStatus}
setSelectedStatus={setSelectedStatus}
selectedState={selectedState}
setSelectedState={setSelectedState}
2025-03-28 01:19:13 +05:30
setToFilterStatus={setToFilterStatus}
setToFilterActive={setToFilterActive}
2025-04-02 15:00:00 +05:30
handleReset={handleReset}
2025-03-28 01:19:13 +05:30
/>
<SearchComponent
monitors={monitors}
onSearchChange={setSearch}
setIsSearching={setIsSearching}
/>
</Stack>
2025-01-24 13:09:57 -08:00
<UptimeDataTable
isAdmin={isAdmin}
2025-03-14 11:05:33 -07:00
isSearching={isSearching}
filteredMonitors={monitorsWithChecks}
2025-01-24 13:09:57 -08:00
sort={sort}
setSort={setSort}
2025-03-14 11:05:33 -07:00
monitorsAreLoading={monitorsWithChecksIsLoading}
2025-03-20 09:11:07 -07:00
triggerUpdate={triggerUpdate}
2025-01-24 13:09:57 -08:00
/>
<Pagination
2025-03-14 11:05:33 -07:00
itemCount={monitorsWithChecksCount}
2025-01-24 13:09:57 -08:00
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;