Files
Checkmate/Client/src/Components/MonitorTable/index.jsx
T

187 lines
5.6 KiB
React
Raw Normal View History

2024-07-05 13:30:09 -07:00
import "./index.css";
2024-07-06 23:07:04 -07:00
import { useState } from "react";
2024-07-05 13:30:09 -07:00
import PropTypes from "prop-types";
import OpenIt from "../../assets/Images/Icon-open-in-tab-gray.png";
import ResponseTimeChart from "../Charts/ResponseTimeChart";
import {
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
2024-07-06 23:07:04 -07:00
TableFooter,
TablePagination,
Paper,
} from "@mui/material";
2024-07-08 13:42:45 -04:00
import SouthRoundedIcon from "@mui/icons-material/SouthRounded";
import OpenInNewPage from "../../assets/icons/open-in-new-page.svg?react"
/**
* Host component.
* This subcomponent receives a params object and displays the host details.
*
* @component
* @param {Object} params - An object containing the following properties:
* @param {string} params.url - The URL of the host.
* @param {string} params.title - The name of the host.
* @param {string} params.percentageColor - The color of the percentage text.
* @param {number} params.precentage - The percentage to display.
* @returns {React.ElementType} Returns a div element with the host details.
*/
2024-07-05 13:30:09 -07:00
const Host = ({ params }) => {
return (
<div className="host-row">
<a href={params.url} target="_blank">
2024-07-08 13:42:45 -04:00
<OpenInNewPage />
2024-07-05 13:30:09 -07:00
</a>
<div className="host-name">{params.title}</div>
2024-07-05 13:30:09 -07:00
<div
className="host-percentage"
style={{ color: params.percentageColor }}
2024-07-05 13:30:09 -07:00
>
{params.precentage}%
2024-07-05 13:30:09 -07:00
</div>
</div>
);
};
/**
* Status component.
* This subcomponent receives a params object and displays the status details of a monitor.
*
* @component
* @param {Object} params - An object containing the following properties:
* @param {string} params.backgroundColor - The background color of the status box.
* @param {string} params.statusDotColor - The color of the status dot.
* @param {string} params.status - The status text to display.
* @returns {React.ElementType} Returns a div element with the host status.
*/
2024-07-05 13:30:09 -07:00
const Status = ({ params }) => {
return (
<div className="host-status">
<div
className="host-status-details"
style={{ backgroundColor: params.backgroundColor }}
2024-07-05 13:30:09 -07:00
>
<div
className="host-status-dot"
style={{ backgroundColor: params.statusDotColor }}
2024-07-05 13:30:09 -07:00
/>
2024-07-08 13:42:45 -04:00
<span
className="host-status-text"
style={{ textTransform: "capitalize" }}
>
{params.status}
</span>
2024-07-05 13:30:09 -07:00
</div>
</div>
);
};
/**
* MonitorTable component.
* Takes an array of monitor objects and displays them in a table.
* Each row in the table represents a monitor and includes the host, status, response time, and action.
*
* @component
* @param {Object[]} monitors - An array of monitor objects. Each object should have the following properties:
* @param {string} monitors[].url - The URL of the monitor.
* @param {string} monitors[].name - The name of the monitor.
* @param {boolean} monitors[].status - The status of the monitor. True if the monitor is up, false otherwise.
* @param {Object[]} monitors[].checks - An array of check objects for the response time chart.
* @returns {React.Component} Returns a table with the monitor data.
*/
2024-07-05 13:30:09 -07:00
const MonitorTable = ({ monitors = [] }) => {
2024-07-06 23:07:04 -07:00
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(5);
2024-07-06 23:07:04 -07:00
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
const handleChangeRowsPerPage = (event) => {
setRowsPerPage(parseInt(event.target.value, 10));
setPage(0);
};
const mappedRows = monitors
.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
.map((monitor) => {
const params = {
url: monitor.url,
title: monitor.name,
precentage: 100,
percentageColor:
monitor.status === true
? "var(--env-var-color-17)"
: "var(--env-var-color-19)",
status: monitor.status === true ? "up" : "down",
backgroundColor:
monitor.status === true
? "var(--env-var-color-20)"
: "var(--env-var-color-21)",
statusDotColor:
monitor.status === true
? "var(--env-var-color-17)"
: "var(--env-var-color-19)",
};
return (
<TableRow key={monitor._id}>
<TableCell>
<Host params={params} />
</TableCell>
<TableCell>
<Status params={params} />
</TableCell>
<TableCell>
<ResponseTimeChart checks={monitor.checks} />
</TableCell>
<TableCell>TODO Add Actions</TableCell>
</TableRow>
);
});
2024-07-05 13:30:09 -07:00
return (
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Host</TableCell>
2024-07-08 13:42:45 -04:00
<TableCell>
Status
<span>
<SouthRoundedIcon />
</span>
</TableCell>
<TableCell>Response Time</TableCell>
2024-07-08 13:42:45 -04:00
<TableCell>Actions</TableCell>
</TableRow>
</TableHead>
<TableBody>{mappedRows}</TableBody>
2024-07-06 23:07:04 -07:00
<TableFooter>
<TableRow>
<TablePagination
rowsPerPageOptions={[5, 10, 25]}
count={monitors.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</TableRow>
</TableFooter>
</Table>
</TableContainer>
2024-07-05 13:30:09 -07:00
);
};
MonitorTable.propTypes = {
monitors: PropTypes.arrayOf(PropTypes.object).isRequired,
};
Status.propTypes = { params: PropTypes.object.isRequired };
Host.propTypes = { params: PropTypes.object.isRequired };
export default MonitorTable;