Files
Checkmate/client/src/Pages/Maintenance/MaintenanceTable/index.jsx
T

237 lines
5.5 KiB
React
Raw Normal View History

2024-10-04 14:10:16 +08:00
import PropTypes from "prop-types";
2025-01-22 12:40:29 -08:00
import { Box } from "@mui/material";
2025-09-27 14:54:55 -07:00
import DataTable from "@/Components/v1/Table/index.jsx";
import Pagination from "@/Components/v1/Table/TablePagination/index.jsx";
import Icon from "@/Components/v1/Icon";
2025-09-25 10:11:41 -07:00
import ActionsMenu from "./ActionsMenu/index.jsx";
2025-01-22 12:40:29 -08:00
import { memo } from "react";
2024-10-04 15:25:41 +08:00
import { useDispatch, useSelector } from "react-redux";
2026-01-12 23:57:39 +00:00
import { formatDurationRounded } from "../../../Utils/timeUtils.js";
2025-09-27 14:54:55 -07:00
import { StatusLabel } from "@/Components/v1/Label/index.jsx";
2026-01-12 23:57:39 +00:00
import { setRowsPerPage } from "../../../Features/UI/uiSlice.js";
2025-03-17 01:16:42 +05:30
import { useTranslation } from "react-i18next";
import { useTheme } from "@emotion/react";
import { useNavigate } from "react-router-dom";
2024-10-04 15:44:35 +08:00
import dayjs from "dayjs";
2024-10-04 14:10:16 +08:00
/**
* Component for pagination actions (first, previous, next, last).
*
* @component
* @param {Object} props
* @param {number} props.count - Total number of items.
* @param {number} props.page - Current page number.
* @param {number} props.rowsPerPage - Number of rows per page.
* @param {function} props.onPageChange - Callback function to handle page change.
*
* @returns {JSX.Element} Pagination actions component.
*/
const MaintenanceTable = ({
page,
setPage,
sort,
setSort,
maintenanceWindows,
maintenanceWindowCount,
updateCallback,
2024-10-04 14:10:16 +08:00
}) => {
2025-06-16 09:44:21 +08:00
const rowsPerPage = useSelector((state) => state?.ui?.maintenance?.rowsPerPage ?? 5);
const dispatch = useDispatch();
const theme = useTheme();
const navigate = useNavigate();
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
2024-10-04 14:10:16 +08:00
2025-01-22 12:40:29 -08:00
const handleChangeRowsPerPage = (event) => {
dispatch(
setRowsPerPage({
value: parseInt(event.target.value, 10),
table: "maintenance",
})
);
setPage(0);
};
2025-05-27 09:10:32 -07:00
2025-03-17 01:16:42 +05:30
const { t } = useTranslation();
2025-01-22 12:40:29 -08:00
const headers = [
{
id: "name",
content: (
<Box onClick={() => handleSort("name")}>
2025-03-17 01:16:42 +05:30
{t("maintenanceWindowName")}
2025-01-22 12:40:29 -08:00
<span
style={{
visibility: sort.field === "name" ? "visible" : "hidden",
}}
>
{sort.order === "asc" ? (
2026-01-15 23:39:26 -05:00
<Icon
name="ArrowUp"
size={18}
/>
2025-01-22 12:40:29 -08:00
) : (
2026-01-15 23:39:26 -05:00
<Icon
name="ArrowDown"
size={18}
/>
2025-01-22 12:40:29 -08:00
)}
</span>
</Box>
),
render: (row) => row.name,
},
{
id: "status",
content: (
<Box onClick={() => handleSort("status")}>
{" "}
2025-03-17 01:16:42 +05:30
{t("status")}
2025-01-22 12:40:29 -08:00
<span
style={{
visibility: sort.field === "active" ? "visible" : "hidden",
}}
>
{sort.order === "asc" ? (
2026-01-15 23:39:26 -05:00
<Icon
name="ArrowUp"
size={18}
/>
2025-01-22 12:40:29 -08:00
) : (
2026-01-15 23:39:26 -05:00
<Icon
name="ArrowDown"
size={18}
/>
2025-01-22 12:40:29 -08:00
)}
</span>
</Box>
),
render: (row) => {
const status = row.active ? "up" : "paused";
const text = row.active ? "active" : "paused";
return (
<StatusLabel
status={status}
text={text}
customStyles={{ textTransform: "capitalize" }}
/>
);
},
},
{
id: "nextWindow",
2025-03-17 01:16:42 +05:30
content: t("nextWindow"),
2025-01-22 12:40:29 -08:00
render: (row) => {
return getTimeToNextWindow(row.start, row.end, row.repeat);
},
},
{
id: "repeat",
2025-03-17 01:16:42 +05:30
content: t("repeat"),
2025-01-22 12:40:29 -08:00
render: (row) => {
return row.repeat === 0 ? "N/A" : formatDurationRounded(row.repeat);
},
},
{
id: "actions",
2025-03-17 01:16:42 +05:30
content: t("actions"),
2025-01-22 12:40:29 -08:00
render: (row) => (
<ActionsMenu
maintenanceWindow={row}
updateCallback={updateCallback}
/>
),
},
];
const getTimeToNextWindow = (startTime, endTime, repeat) => {
//1. Advance time closest to next window as possible
const now = dayjs();
let start = dayjs(startTime);
let end = dayjs(endTime);
if (repeat > 0) {
// Advance time closest to next window as possible
while (start.isBefore(now) && end.isBefore(now)) {
start = start.add(repeat, "milliseconds");
end = end.add(repeat, "milliseconds");
}
}
2024-10-04 15:44:35 +08:00
//Check if we are in a window
if (now.isAfter(start) && now.isBefore(end)) {
return "In maintenance window";
}
2024-10-04 15:44:35 +08:00
if (start.isAfter(now)) {
const diffInMinutes = start.diff(now, "minutes");
const diffInHours = start.diff(now, "hours");
const diffInDays = start.diff(now, "days");
2024-10-04 15:44:35 +08:00
if (diffInMinutes < 60) {
return diffInMinutes + " minutes";
} else if (diffInHours < 24) {
return diffInHours + " hours";
} else if (diffInDays < 7) {
return diffInDays + " days";
} else {
return diffInDays + " days";
}
}
};
2024-10-04 15:44:35 +08:00
const handleSort = async (field) => {
let order = "";
if (sort.field !== field) {
order = "desc";
} else {
order = sort.order === "asc" ? "desc" : "asc";
}
setSort({ field, order });
};
2024-10-04 14:10:16 +08:00
return (
<>
2025-01-22 12:40:29 -08:00
<DataTable
config={{
rowSX: {
cursor: "pointer",
"&:hover td": {
backgroundColor: theme.palette.tertiary.main,
transition: "background-color .3s ease",
},
},
onRowClick: (row) => {
navigate(`/maintenance/create/${row._id}`);
},
}}
2025-01-22 12:40:29 -08:00
headers={headers}
data={maintenanceWindows}
/>
<Pagination
itemCount={maintenanceWindowCount}
page={page}
rowsPerPage={rowsPerPage}
handleChangePage={handleChangePage}
handleChangeRowsPerPage={handleChangeRowsPerPage}
/>
</>
);
2024-10-04 14:10:16 +08:00
};
MaintenanceTable.propTypes = {
isAdmin: PropTypes.bool,
page: PropTypes.number,
setPage: PropTypes.func,
rowsPerPage: PropTypes.number,
setRowsPerPage: PropTypes.func,
sort: PropTypes.object,
setSort: PropTypes.func,
maintenanceWindows: PropTypes.array,
maintenanceWindowCount: PropTypes.number,
updateCallback: PropTypes.func,
2024-10-04 14:10:16 +08:00
};
const MemoizedMaintenanceTable = memo(MaintenanceTable);
export default MemoizedMaintenanceTable;