Files
Checkmate/Client/src/Pages/Incidents/IncidentTable/index.jsx
T

231 lines
6.7 KiB
React
Raw Normal View History

import PropTypes from "prop-types";
import {
TableContainer,
Table,
TableHead,
TableRow,
TableCell,
TableBody,
Pagination,
PaginationItem,
Paper,
Typography,
Box,
} from "@mui/material";
2024-08-09 10:01:28 -07:00
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
import ArrowForwardRoundedIcon from "@mui/icons-material/ArrowForwardRounded";
import { useState, useEffect } from "react";
import { useSelector } from "react-redux";
2024-08-18 17:42:49 -07:00
import { networkService } from "../../../main";
import { StatusLabel } from "../../../Components/Label";
import { logger } from "../../../Utils/Logger";
2024-08-28 10:28:02 -04:00
import { useTheme } from "@emotion/react";
2024-09-12 12:17:31 +08:00
import { formatDateWithTz } from "../../../Utils/timeUtils";
2024-09-17 20:26:52 -04:00
import PlaceholderLight from "../../../assets/Images/data_placeholder.svg?react";
import PlaceholderDark from "../../../assets/Images/data_placeholder_dark.svg?react";
2024-09-12 11:37:09 +08:00
const IncidentTable = ({ monitors, selectedMonitor, filter }) => {
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-09-12 11:37:09 +08:00
const theme = useTheme();
const { authToken, user } = useSelector((state) => state.auth);
const mode = useSelector((state) => state.ui.mode);
const [checks, setChecks] = useState([]);
const [checksCount, setChecksCount] = useState(0);
const [paginationController, setPaginationController] = useState({
page: 0,
rowsPerPage: 14,
});
useEffect(() => {
setPaginationController((prevPaginationController) => ({
...prevPaginationController,
page: 0,
}));
}, [filter, selectedMonitor]);
2024-08-10 10:38:14 -07:00
useEffect(() => {
const fetchPage = async () => {
if (!monitors || Object.keys(monitors).length === 0) {
return;
}
try {
let res;
if (selectedMonitor === "0") {
res = await networkService.getChecksByTeam({
authToken: authToken,
teamId: user.teamId,
sortOrder: "desc",
limit: null,
dateRange: null,
filter: filter,
page: paginationController.page,
rowsPerPage: paginationController.rowsPerPage,
});
} else {
res = await networkService.getChecksByMonitor({
authToken: authToken,
monitorId: selectedMonitor,
sortOrder: "desc",
limit: null,
dateRange: null,
sitler: filter,
page: paginationController.page,
rowsPerPage: paginationController.rowsPerPage,
});
}
setChecks(res.data.data.checks);
setChecksCount(res.data.data.checksCount);
} catch (error) {
logger.error(error);
}
};
fetchPage();
}, [
authToken,
user,
monitors,
selectedMonitor,
filter,
paginationController.page,
paginationController.rowsPerPage,
]);
const handlePageChange = (_, newPage) => {
setPaginationController({
...paginationController,
page: newPage - 1, // 0-indexed
});
};
let paginationComponent = <></>;
if (checksCount > paginationController.rowsPerPage) {
paginationComponent = (
<Pagination
count={Math.ceil(checksCount / paginationController.rowsPerPage)}
page={paginationController.page + 1} //0-indexed
onChange={handlePageChange}
shape="rounded"
renderItem={(item) => (
<PaginationItem
slots={{
previous: ArrowBackRoundedIcon,
next: ArrowForwardRoundedIcon,
}}
{...item}
/>
)}
sx={{ mt: "auto" }}
/>
);
}
2024-08-09 10:01:28 -07:00
let sharedStyles = {
border: 1,
borderColor: theme.palette.border.light,
borderRadius: theme.shape.borderRadius,
backgroundColor: theme.palette.background.main,
p: theme.spacing(30),
};
2024-08-28 10:28:02 -04:00
return (
2024-08-09 10:01:28 -07:00
<>
{checks?.length === 0 && selectedMonitor === "0" ? (
2024-08-28 10:28:02 -04:00
<Box sx={{ ...sharedStyles }}>
2024-10-21 14:40:22 -03:00
<Box
textAlign="center"
pb={theme.spacing(20)}
>
2024-09-17 20:26:52 -04:00
{mode === "light" ? <PlaceholderLight /> : <PlaceholderDark />}
</Box>
2024-10-21 14:40:22 -03:00
<Typography
textAlign="center"
color={theme.palette.text.secondary}
>
2024-08-28 10:28:02 -04:00
No incidents recorded yet.
</Typography>
</Box>
) : checks?.length === 0 ? (
2024-08-28 10:28:02 -04:00
<Box sx={{ ...sharedStyles }}>
2024-10-21 14:40:22 -03:00
<Box
textAlign="center"
pb={theme.spacing(20)}
>
2024-09-17 20:26:52 -04:00
{mode === "light" ? <PlaceholderLight /> : <PlaceholderDark />}
</Box>
2024-10-21 14:40:22 -03:00
<Typography
textAlign="center"
color={theme.palette.text.secondary}
>
2024-08-12 10:02:54 -07:00
The monitor you have selected has no recorded incidents yet.
</Typography>
</Box>
) : (
<>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Monitor Name</TableCell>
<TableCell>Status</TableCell>
<TableCell>Date & Time</TableCell>
<TableCell>Status Code</TableCell>
<TableCell>Message</TableCell>
2024-08-09 10:01:28 -07:00
</TableRow>
</TableHead>
<TableBody>
{checks.map((check) => {
const status = check.status === true ? "up" : "down";
2024-09-12 12:17:31 +08:00
const formattedDate = formatDateWithTz(
check.createdAt,
"YYYY-MM-DD HH:mm:ss A",
uiTimezone
);
2024-10-21 14:40:22 -03:00
const getGenericStatus = (status) => {
if (typeof status === "number" && status >= 100 && status < 600) {
return Math.floor(status / 100) * 100;
}
return "cannot resolve";
};
return (
<TableRow key={check._id}>
<TableCell>{monitors[check.monitorId]?.name}</TableCell>
<TableCell>
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
</TableCell>
2024-09-12 11:37:09 +08:00
<TableCell>{formattedDate}</TableCell>
<TableCell>
2024-10-21 14:40:22 -03:00
<StatusLabel
status={getGenericStatus(check.statusCode)}
text={String(check.statusCode || "N/A")}
hasDot={false}
customStyles={{ textTransform: "capitalize" }}
/>
</TableCell>
<TableCell>{check.message}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</TableContainer>
{paginationComponent}
</>
)}
2024-08-09 10:01:28 -07:00
</>
);
};
IncidentTable.propTypes = {
monitors: PropTypes.object.isRequired,
selectedMonitor: PropTypes.string.isRequired,
filter: PropTypes.string.isRequired,
};
export default IncidentTable;