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

172 lines
5.2 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";
2024-07-26 12:39:18 -04:00
import { Box, Stack, Typography } from "@mui/material";
2024-07-17 17:44:29 -04:00
/**
* 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-26 12:39:18 -04:00
<Stack className="monitors" gap={theme.gap.large}>
<Stack direction="row" justifyContent="space-between" alignItems="center">
<Typography component="h1">
2024-07-23 12:45:00 -04:00
Hello, {authState.user.firstName}
2024-07-26 12:39:18 -04:00
</Typography>
2024-07-04 14:09:17 -07:00
<Button
level="primary"
label="Create new monitor"
onClick={() => {
navigate("/monitors/create");
}}
/>
2024-07-26 12:39:18 -04:00
</Stack>
<Stack
gap={theme.gap.large}
direction="row"
justifyContent="space-between"
>
2024-07-04 14:09:17 -07:00
<ServerStatus title="Up" value={up} state="up" />
<ServerStatus title="Down" value={down} state="down" />
<ServerStatus title="Paused" value={0} state="pause" />
2024-07-26 12:39:18 -04:00
</Stack>
<Stack
gap={theme.gap.large}
p={theme.gap.xl}
sx={{
border: `solid 1px ${theme.palette.otherColors.graishWhite}`,
borderRadius: `${theme.shape.borderRadius}px`,
backgroundColor: theme.palette.otherColors.white,
}}
>
<Stack direction="row" alignItems="center">
<Typography component="h2">Current monitors</Typography>
<Box className="current-monitors-counter">
{monitorState.monitors.length}
</Box>
{/* TODO - add search bar */}
</Stack>
2024-07-17 17:44:29 -04:00
<BasicTable data={data} paginated={true} />
2024-07-26 12:39:18 -04:00
</Stack>
</Stack>
2024-06-05 21:09:14 +03:30
);
};
2024-06-05 21:09:14 +03:30
export default Monitors;