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

132 lines
3.2 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";
import { useState } from "react";
import useChecksFetch from "../../Hooks/useChecksFetch";
import PropTypes from "prop-types";
2025-03-12 00:56:28 +05:30
import { useTranslation } from "react-i18next";
2025-01-29 15:06:41 -08:00
const IncidentTable = ({
shouldRender,
monitors,
selectedMonitor,
filter,
dateRange,
}) => {
//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-01-29 15:06:41 -08:00
const { isLoading, networkError, checks, checksCount } = useChecksFetch({
selectedMonitor,
2025-03-05 00:58:43 +05:30
selectedMonitorType,
2025-01-29 15:06:41 -08:00
filter,
dateRange,
page,
rowsPerPage,
});
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);
};
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-01-29 15:06:41 -08:00
];
if (!shouldRender || isLoading) return <TableSkeleton />;
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 = {
shouldRender: PropTypes.bool,
monitors: PropTypes.object,
selectedMonitor: PropTypes.string,
filter: PropTypes.string,
dateRange: PropTypes.string,
};
export default IncidentTable;