Files
Checkmate/client/src/Pages/Incidents/Components/IncidentTable/index.jsx
T

195 lines
5.1 KiB
React
Raw Normal View History

2025-01-29 15:06:41 -08:00
//Components
import Table from "../../../../Components/Table";
import TableSkeleton from "../../../../Components/Table/skeleton";
import Pagination from "../../../../Components/Table/TablePagination";
import { StatusLabel } from "../../../../Components/Label";
import { HttpStatusLabel } from "../../../../Components/HttpStatusLabel";
import GenericFallback from "../../../../Components/GenericFallback";
import NetworkError from "../../../../Components/GenericFallback/NetworkError";
//Utils
import { formatDateWithTz } from "../../../../Utils/timeUtils";
import { useSelector } from "react-redux";
2025-06-16 13:51:15 +05:30
import { useState } from "react";
2025-01-29 15:06:41 -08:00
import PropTypes from "prop-types";
2025-03-12 00:56:28 +05:30
import { useTranslation } from "react-i18next";
2025-06-17 13:13:21 +05:30
import { useFetchChecksTeam } from "../../../../Hooks/checkHooks";
import { useFetchChecksByMonitor } from "../../../../Hooks/checkHooks";
2025-07-04 01:25:33 +05:30
import { useResolveIncident } from "../../../../Hooks/checkHooks";
2025-07-03 02:15:37 +05:30
import { Button, Typography } from "@mui/material";
2025-01-29 15:06:41 -08:00
const IncidentTable = ({
2025-07-03 02:15:37 +05:30
isLoading,
2025-01-29 15:06:41 -08:00
monitors,
selectedMonitor,
filter,
dateRange,
2025-07-03 02:15:37 +05:30
updateTrigger,
setUpdateTrigger,
2025-01-29 15:06:41 -08:00
}) => {
//Redux state
const uiTimezone = useSelector((state) => state.ui.timezone);
//Local state
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
2025-03-05 00:58:43 +05:30
const selectedMonitorDetails = monitors?.[selectedMonitor];
const selectedMonitorType = selectedMonitorDetails?.type;
2025-07-04 01:25:33 +05:30
//Hooks
const [resolveIncident, resolveLoading] = useResolveIncident();
2025-06-12 15:47:59 +08:00
2025-06-16 13:51:15 +05:30
const [checksMonitor, checksCountMonitor, isLoadingMonitor, networkErrorMonitor] =
useFetchChecksByMonitor({
monitorId: selectedMonitor === "0" ? undefined : selectedMonitor,
type: selectedMonitorType,
2025-06-23 22:07:03 +05:30
status: false,
2025-06-16 13:51:15 +05:30
sortOrder: "desc",
limit: null,
dateRange,
2025-06-20 15:39:13 +05:30
filter: filter === "resolved" ? "all" : filter,
2025-06-24 14:44:02 +05:30
ack: filter === "resolved" ? true : false,
2025-06-16 13:51:15 +05:30
page: page,
rowsPerPage: rowsPerPage,
2025-06-17 13:27:51 +05:30
enabled: selectedMonitor !== "0",
2025-07-03 02:15:37 +05:30
updateTrigger,
2025-06-16 13:51:15 +05:30
});
const [checksTeam, checksCountTeam, isLoadingTeam, networkErrorTeam] =
useFetchChecksTeam({
2025-06-23 22:07:03 +05:30
status: false,
2025-06-16 13:51:15 +05:30
sortOrder: "desc",
limit: null,
dateRange,
2025-06-20 15:39:13 +05:30
filter: filter === "resolved" ? "all" : filter,
2025-06-24 14:44:02 +05:30
ack: filter === "resolved" ? true : false,
2025-06-16 13:51:15 +05:30
page: page,
rowsPerPage: rowsPerPage,
2025-06-17 13:27:51 +05:30
enabled: selectedMonitor === "0",
2025-07-03 02:15:37 +05:30
updateTrigger,
2025-06-16 13:51:15 +05:30
});
2025-06-17 13:27:51 +05:30
const checks = selectedMonitor === "0" ? checksTeam : checksMonitor;
const checksCount = selectedMonitor === "0" ? checksCountTeam : checksCountMonitor;
2025-07-03 02:15:37 +05:30
isLoading = isLoadingTeam || isLoadingMonitor;
2025-06-17 13:27:51 +05:30
const networkError = selectedMonitor === "0" ? networkErrorTeam : networkErrorMonitor;
2025-01-29 15:06:41 -08:00
2025-03-12 00:56:28 +05:30
const { t } = useTranslation();
2025-01-29 15:06:41 -08:00
//Handlers
const handleChangePage = (_, newPage) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
setRowsPerPage(event.target.value);
};
2025-07-04 01:25:33 +05:30
const handleResolveIncident = (checkId) => {
resolveIncident(checkId, setUpdateTrigger);
2025-07-03 02:15:37 +05:30
};
2025-01-29 15:06:41 -08:00
const headers = [
{
id: "monitorName",
2025-03-12 00:56:28 +05:30
content: t("incidentsTableMonitorName"),
2025-01-29 15:06:41 -08:00
render: (row) => monitors[row.monitorId]?.name ?? "N/A",
},
{
id: "status",
2025-03-12 00:56:28 +05:30
content: t("incidentsTableStatus"),
2025-01-29 15:06:41 -08:00
render: (row) => {
const status = row.status === true ? "up" : "down";
return (
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
);
},
},
{
id: "dateTime",
2025-03-12 00:56:28 +05:30
content: t("incidentsTableDateTime"),
2025-01-29 15:06:41 -08:00
render: (row) => {
const formattedDate = formatDateWithTz(
row.createdAt,
"YYYY-MM-DD HH:mm:ss A",
uiTimezone
);
return formattedDate;
},
},
{
id: "statusCode",
2025-03-12 00:56:28 +05:30
content: t("incidentsTableStatusCode"),
2025-01-29 15:06:41 -08:00
render: (row) => <HttpStatusLabel status={row.statusCode} />,
},
2025-03-12 00:56:28 +05:30
{ id: "message", content: t("incidentsTableMessage"), render: (row) => row.message },
2025-07-03 02:15:37 +05:30
{
id: "action",
content: t("actions"),
render: (row) => {
return row.ack === false ? (
<Button
variant="contained"
color="accent"
onClick={() => {
handleResolveIncident(row._id);
}}
>
{t("incidentsTableActionResolve")}
</Button>
) : (
<Typography>
{t("incidentsTableResolvedAt")}{" "}
{formatDateWithTz(row.ackAt, "YYYY-MM-DD HH:mm:ss A", uiTimezone)}
</Typography>
);
},
},
2025-01-29 15:06:41 -08:00
];
2025-07-03 02:15:37 +05:30
if (isLoading || resolveLoading) return <TableSkeleton />;
2025-01-29 15:06:41 -08:00
if (networkError) {
return (
<GenericFallback>
<NetworkError />
</GenericFallback>
);
}
if (!isLoading && typeof checksCount === "undefined") {
2025-03-12 00:56:28 +05:30
return <GenericFallback>{t("incidentsTableNoIncidents")}</GenericFallback>;
2025-01-29 15:06:41 -08:00
}
return (
<>
<Table
headers={headers}
data={checks}
/>
<Pagination
2025-03-12 00:56:28 +05:30
paginationLabel={t("incidentsTablePaginationLabel")}
2025-01-29 15:06:41 -08:00
itemCount={checksCount}
page={page}
rowsPerPage={rowsPerPage}
handleChangePage={handleChangePage}
handleChangeRowsPerPage={handleChangeRowsPerPage}
/>
</>
);
};
IncidentTable.propTypes = {
2025-07-03 02:15:37 +05:30
isLoading: PropTypes.bool,
2025-01-29 15:06:41 -08:00
monitors: PropTypes.object,
selectedMonitor: PropTypes.string,
filter: PropTypes.string,
dateRange: PropTypes.string,
2025-07-03 02:15:37 +05:30
updateTrigger: PropTypes.bool,
setUpdateTrigger: PropTypes.func,
2025-01-29 15:06:41 -08:00
};
export default IncidentTable;