Files
Checkmate/Client/src/Pages/Monitors/index.jsx
T

173 lines
5.1 KiB
React
Raw Normal View History

2024-06-05 21:09:14 +03:30
import "./index.css";
2024-06-25 13:30:52 +03:30
import { useEffect } from "react";
2024-07-04 14:09:17 -07:00
import { useSelector, useDispatch } from "react-redux";
2024-06-25 13:30:52 +03:30
import { getMonitorsByUserId } from "../../Features/Monitors/monitorsSlice";
2024-07-04 14:09:17 -07:00
import { useNavigate } from "react-router-dom";
import Button from "../../Components/Button";
import ServerStatus from "../../Components/Charts/Servers/ServerStatus";
2024-07-08 13:42:45 -04:00
import { useTheme } from "@emotion/react";
2024-07-17 17:44:29 -04:00
import ArrowDownwardRoundedIcon from "@mui/icons-material/ArrowDownwardRounded";
import OpenInNewPage from "../../assets/icons/open-in-new-page.svg?react";
import BasicTable from "../../Components/BasicTable";
import { StatusLabel } from "../../Components/Label";
import ResponseTimeChart from "../../Components/Charts/ResponseTimeChart";
/**
* 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.
*/
const Host = ({ params }) => {
return (
<div className="host-row">
2024-07-17 20:33:33 -04:00
<a href={params.url} target="_blank" rel="noreferrer">
2024-07-17 17:44:29 -04:00
<OpenInNewPage />
</a>
<div className="host-name">{params.title}</div>
<div
className="host-percentage"
style={{ color: params.percentageColor }}
>
{params.precentage}%
</div>
</div>
);
};
const Monitors = () => {
2024-07-08 13:42:45 -04:00
const theme = useTheme();
2024-07-04 14:09:17 -07:00
const navigate = useNavigate();
2024-06-25 13:30:52 +03:30
const monitorState = useSelector((state) => state.monitors);
2024-07-04 14:09:17 -07:00
const authState = useSelector((state) => state.auth);
2024-06-25 13:30:52 +03:30
const dispatch = useDispatch();
useEffect(() => {
2024-07-04 14:09:17 -07:00
dispatch(getMonitorsByUserId(authState.authToken));
2024-06-25 13:30:52 +03:30
}, []);
2024-07-04 14:09:17 -07:00
const up = monitorState.monitors.reduce((acc, cur) => {
return cur.status === true ? acc + 1 : acc;
}, 0);
const down = monitorState.monitors.length - up;
2024-07-17 17:44:29 -04:00
const data = {
cols: [
{ id: 1, name: "Host" },
{
id: 2,
name: (
<>
Status
<span>
<ArrowDownwardRoundedIcon />
</span>
</>
),
},
{ id: 3, name: "Response Time" },
2024-07-25 15:45:58 -04:00
{ id: 4, name: "Type" },
{ id: 5, name: "Actions" },
2024-07-17 17:44:29 -04:00
],
rows: [],
};
data.rows = monitorState.monitors.map((monitor, idx) => {
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 {
id: monitor._id,
handleClick: () => navigate(`/monitors/${monitor._id}`),
data: [
{ id: idx, data: <Host params={params} /> },
{
id: idx + 1,
data: (
<StatusLabel
status={params.status}
dot={params.statusDotColor}
customStyles={{
backgroundColor: params.backgroundColor,
}}
/>
),
},
{ id: idx + 2, data: <ResponseTimeChart checks={monitor.checks} /> },
2024-07-25 15:45:58 -04:00
{ id: idx + 3, data: monitor.type },
{ id: idx + 4, data: "TODO" },
2024-07-17 17:44:29 -04:00
],
};
});
2024-06-05 21:09:14 +03:30
return (
2024-07-08 13:42:45 -04:00
<div
className="monitors"
style={{
padding: `${theme.content.pY} ${theme.content.pX}`,
2024-07-23 20:20:18 +03:30
backgroundColor: "var(--env-var-color-30)",
2024-07-08 13:42:45 -04:00
}}
>
2024-07-04 14:09:17 -07:00
<div className="monitors-bar">
<div className="monitors-bar-title">
2024-07-23 12:45:00 -04:00
Hello, {authState.user.firstName}
2024-07-04 14:09:17 -07:00
</div>
<Button
level="primary"
label="Create new monitor"
onClick={() => {
navigate("/monitors/create");
}}
2024-07-08 13:42:45 -04:00
sx={{ padding: "6px 25px", fontSize: "13px" }}
2024-07-04 14:09:17 -07:00
/>
</div>
<div className="monitors-stats">
<ServerStatus title="Up" value={up} state="up" />
<ServerStatus title="Down" value={down} state="down" />
<ServerStatus title="Paused" value={0} state="pause" />
</div>
<div className="current-monitors">
<div className="current-monitors-bar">
<div className="current-monitors-title-holder">
<div className="current-monitors-title">Current monitors</div>
<div className="current-monitors-counter">
{monitorState.monitors.length}
</div>
</div>
<div className="current-monitors-search-bar">
2024-07-17 17:44:29 -04:00
{/* TODO - add search bar */}
2024-07-04 14:09:17 -07:00
</div>
</div>
2024-07-05 14:38:16 -07:00
<div className="monitors-v-gaping" />
2024-07-17 17:44:29 -04:00
<BasicTable data={data} paginated={true} />
2024-07-04 14:09:17 -07:00
</div>
2024-06-05 21:09:14 +03:30
</div>
);
};
2024-06-05 21:09:14 +03:30
export default Monitors;