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

466 lines
13 KiB
React
Raw Normal View History

2024-06-05 21:09:14 +03:30
import "./index.css";
import { useEffect, useState } from "react";
2024-07-04 14:09:17 -07:00
import { useSelector, useDispatch } from "react-redux";
2024-08-01 17:04:11 -04:00
import {
deleteUptimeMonitor,
getUptimeMonitorsByUserId,
} from "../../Features/UptimeMonitors/uptimeMonitorsSlice";
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";
2024-08-01 17:04:11 -04:00
import { createToast } from "../../Utils/toastUtils";
2024-07-17 17:44:29 -04:00
import ResponseTimeChart from "../../Components/Charts/ResponseTimeChart";
import {
Box,
IconButton,
Menu,
MenuItem,
2024-08-01 17:04:11 -04:00
Modal,
2024-08-07 16:45:49 -04:00
Skeleton,
Stack,
Typography,
} from "@mui/material";
import Settings from "../../assets/icons/settings-bold.svg?react";
2024-08-19 11:35:12 -07:00
import PropTypes from "prop-types";
const ActionsMenu = ({ monitor }) => {
const [anchorEl, setAnchorEl] = useState(null);
const [actions, setActions] = useState({});
const [isOpen, setIsOpen] = useState(false);
const dispatch = useDispatch();
const theme = useTheme();
const authState = useSelector((state) => state.auth);
const handleRemove = async (event) => {
event.preventDefault();
event.stopPropagation();
let monitor = { _id: actions.id };
const action = await dispatch(
deleteUptimeMonitor({ authToken: authState.authToken, monitor })
);
if (action.meta.requestStatus === "fulfilled") {
setIsOpen(false); // close modal
dispatch(getUptimeMonitorsByUserId(authState.authToken));
createToast({ body: "Monitor deleted successfully." });
} else {
createToast({ body: "Failed to delete monitor." });
}
};
const openMenu = (event, id, url) => {
event.preventDefault();
event.stopPropagation();
setAnchorEl(event.currentTarget);
setActions({ id: id, url: url });
};
const openRemove = (e) => {
closeMenu(e);
setIsOpen(true);
};
const closeMenu = (e) => {
e.stopPropagation();
setAnchorEl(null);
};
const navigate = useNavigate();
return (
<>
<IconButton
aria-label="monitor actions"
onClick={(event) => {
event.stopPropagation();
openMenu(
event,
monitor._id,
monitor.type === "ping" ? null : monitor.url
);
}}
sx={{
"&:focus": {
outline: "none",
},
}}
>
<Settings />
</IconButton>
<Menu
className="actions-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={(e) => closeMenu(e)}
>
{actions.url !== null ? (
<MenuItem
onClick={(e) => {
closeMenu(e);
e.stopPropagation();
window.open(actions.url, "_blank", "noreferrer");
}}
>
Open site
</MenuItem>
) : (
""
)}
<MenuItem
onClick={(e) => {
e.stopPropagation();
navigate(`/monitors/${actions.id}`);
}}
>
Details
</MenuItem>
{/* TODO - pass monitor id to Incidents page */}
<MenuItem disabled>Incidents</MenuItem>
<MenuItem
onClick={(e) => {
e.stopPropagation();
navigate(`/monitors/configure/${actions.id}`);
}}
>
Configure
</MenuItem>
<MenuItem
onClick={(e) => {
e.stopPropagation();
openRemove(e);
}}
>
Remove
</MenuItem>
</Menu>
<Modal
aria-labelledby="modal-delete-monitor"
aria-describedby="delete-monitor-confirmation"
open={isOpen}
onClose={() => setIsOpen(false)}
disablePortal
>
<Stack
gap={theme.gap.xs}
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 400,
bgcolor: "white",
border: "solid 1px #f2f2f2",
borderRadius: `${theme.shape.borderRadius}px`,
boxShadow: 24,
p: "30px",
"&:focus": {
outline: "none",
},
}}
>
<Typography id="modal-delete-monitor" component="h2">
Do you really want to delete this monitor?
</Typography>
<Typography id="delete-monitor-confirmation">
Once deleted, this monitor cannot be retrieved.
</Typography>
<Stack
direction="row"
gap={theme.gap.small}
mt={theme.gap.large}
justifyContent="flex-end"
>
<Button
level="tertiary"
label="Cancel"
onClick={() => setIsOpen(false)}
/>
<Button
level="error"
label="Delete"
onClick={(e) => handleRemove(e)}
/>
</Stack>
</Stack>
</Modal>
</>
);
};
2024-07-17 17:44:29 -04:00
2024-08-19 11:35:12 -07:00
ActionsMenu.propTypes = {
monitor: PropTypes.shape({
_id: PropTypes.string,
url: PropTypes.string,
type: PropTypes.string,
}).isRequired,
};
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.
2024-08-02 17:47:22 -04:00
* @param {number} params.percentage - The percentage to display.
2024-07-17 17:44:29 -04:00
* @returns {React.ElementType} Returns a div element with the host details.
*/
const Host = ({ params }) => {
return (
<Stack direction="row" alignItems="center" className="host">
<IconButton
aria-label="monitor link"
2024-08-02 10:57:12 -07:00
onClick={(event) => {
event.stopPropagation();
window.open(params.url, "_blank", "noreferrer");
}}
sx={{
"&:focus": {
outline: "none",
},
2024-08-16 18:38:40 -04:00
mr: "5px",
2024-08-02 10:57:12 -07:00
}}
>
<OpenInNewPage
style={{
marginTop: "-1px",
marginRight: "-1px",
}}
/>
</IconButton>
2024-08-02 13:54:44 -04:00
<Box>
{params.title}
<Typography component="span" sx={{ color: params.percentageColor }}>
2024-08-02 17:47:22 -04:00
{params.percentage}%
2024-08-02 13:54:44 -04:00
</Typography>
</Box>
</Stack>
2024-07-17 17:44:29 -04:00
);
};
2024-08-19 11:35:12 -07:00
Host.propTypes = {
params: PropTypes.shape({
url: PropTypes.string,
title: PropTypes.string,
percentageColor: PropTypes.string,
percentage: PropTypes.number,
}).isRequired,
};
2024-08-07 19:49:08 -04:00
/**
* Renders a skeleton layout.
*
* @returns {JSX.Element}
*/
const SkeletonLayout = () => {
const theme = useTheme();
return (
<>
<Stack direction="row" justifyContent="space-between" alignItems="center">
<Skeleton variant="rounded" width="50%" height={36} />
<Skeleton variant="rounded" width="15%" height={36} />
</Stack>
<Stack
gap={theme.gap.large}
direction="row"
justifyContent="space-between"
>
<Skeleton variant="rounded" width="100%" height={100} />
<Skeleton variant="rounded" width="100%" height={100} />
<Skeleton variant="rounded" width="100%" height={100} />
</Stack>
<Stack gap={theme.gap.large} p={theme.gap.xl} backgroundColor="#f9fafb">
<Skeleton variant="rounded" width="50%" height={25} />
<Skeleton variant="rounded" width="100%" height={300} />
<Skeleton variant="rounded" width="100%" height={100} />
</Stack>
</>
);
};
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-07-30 11:25:14 -07:00
const monitorState = useSelector((state) => state.uptimeMonitors);
2024-07-04 14:09:17 -07:00
const authState = useSelector((state) => state.auth);
2024-08-01 17:04:11 -04:00
const dispatch = useDispatch({});
2024-06-25 13:30:52 +03:30
useEffect(() => {
2024-07-30 11:25:14 -07:00
dispatch(getUptimeMonitorsByUserId(authState.authToken));
2024-08-08 11:20:54 -07:00
}, [authState.authToken, dispatch]);
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: (
2024-08-15 11:14:43 -07:00
<Box width="max-content">
2024-07-17 17:44:29 -04:00
Status
2024-08-15 11:14:43 -07:00
<span>
<ArrowDownwardRoundedIcon />
</span>
</Box>
2024-07-17 17:44:29 -04:00
),
},
{ 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: [],
};
2024-08-15 11:14:43 -07:00
data.rows = monitorState.monitors.map((monitor, idx) => {
2024-07-17 17:44:29 -04:00
const params = {
url: monitor.url,
title: monitor.name,
2024-08-02 17:47:22 -04:00
percentage: 100,
2024-07-17 17:44:29 -04:00
percentageColor:
monitor.status === true
? "var(--env-var-color-17)"
: "var(--env-var-color-19)",
2024-07-30 01:59:58 -04:00
status: monitor.status === true ? "up" : "down",
2024-07-17 17:44:29 -04:00
};
2024-07-26 18:56:39 -07:00
// Reverse checks so latest check is on the right
const reversedChecks = monitor.checks.slice().reverse();
2024-07-17 17:44:29 -04:00
return {
id: monitor._id,
// disabled for now
2024-08-18 22:52:58 -07:00
handleClick: () => {
navigate(`/monitors/${monitor._id}`);
},
2024-07-17 17:44:29 -04:00
data: [
{ id: idx, data: <Host params={params} /> },
{
id: idx + 1,
data: (
<StatusLabel
status={params.status}
2024-07-30 01:59:58 -04:00
text={params.status}
customStyles={{ textTransform: "capitalize" }}
2024-07-17 17:44:29 -04:00
/>
),
},
2024-07-26 18:56:39 -07:00
{ id: idx + 2, data: <ResponseTimeChart checks={reversedChecks} /> },
2024-07-31 15:53:01 -04:00
{
id: idx + 3,
data: (
<span style={{ textTransform: "uppercase" }}>{monitor.type}</span>
),
},
{
id: idx + 4,
2024-08-18 22:52:58 -07:00
data: <ActionsMenu monitor={monitor} />,
},
2024-07-17 17:44:29 -04:00
],
};
});
2024-08-07 19:49:08 -04:00
let loading = monitorState.isLoading && monitorState.monitors.length === 0;
2024-08-07 16:45:49 -04:00
2024-06-05 21:09:14 +03:30
return (
2024-08-20 17:03:19 -04:00
<Stack className="monitors" gap={theme.gap.large}>
2024-08-07 19:49:08 -04:00
{loading ? (
<SkeletonLayout />
) : (
<>
<Stack
direction="row"
justifyContent="space-between"
alignItems="center"
>
<Typography
component="h1"
sx={{ lineHeight: 1, alignSelf: "flex-end" }}
>
2024-08-07 16:45:49 -04:00
Hello, {authState.user.firstName}
</Typography>
{monitorState.monitors?.length !== 0 && (
<Button
level="primary"
2024-08-20 17:03:19 -04:00
label="Create monitor"
onClick={() => {
navigate("/monitors/create");
}}
2024-08-20 17:03:19 -04:00
sx={{ fontWeight: 500 }}
/>
)}
2024-08-07 16:45:49 -04:00
</Stack>
{monitorState.monitors?.length === 0 ? (
<Stack
alignItems="center"
backgroundColor={theme.palette.otherColors.white}
p={theme.gap.xxl}
gap={theme.gap.xs}
border={1}
borderRadius={`${theme.shape.borderRadius}px`}
borderColor={theme.palette.otherColors.graishWhite}
>
<Typography component="h2">No monitors found</Typography>
<Typography>
It looks like you don’t have any monitors set up yet.
</Typography>
<Button
level="primary"
label="Create your first monitor"
onClick={() => {
navigate("/monitors/create");
}}
sx={{ mt: theme.gap.large }}
/>
2024-08-07 19:49:08 -04:00
</Stack>
) : (
<>
<Stack
gap={theme.gap.large}
direction="row"
justifyContent="space-between"
>
<ServerStatus title="Up" value={up} state="up" />
<ServerStatus title="Down" value={down} state="down" />
<ServerStatus title="Paused" value={0} state="pause" />
</Stack>
<Box
2024-08-22 15:00:44 -04:00
flex={1}
p={theme.gap.lgplus}
border={1}
borderColor={theme.palette.otherColors.graishWhite}
backgroundColor={theme.palette.otherColors.white}
sx={{
borderRadius: `${theme.shape.borderRadius}px`,
}}
>
<Stack direction="row" alignItems="center" mb={theme.gap.large}>
<Typography component="h2">Current monitors</Typography>
<Box className="current-monitors-counter">
{monitorState.monitors.length}
</Box>
{/* TODO - add search bar */}
</Stack>
<BasicTable data={data} paginated={true} />
</Box>
</>
)}
2024-08-07 19:49:08 -04:00
</>
)}
2024-07-26 12:39:18 -04:00
</Stack>
2024-06-05 21:09:14 +03:30
);
};
2024-06-05 21:09:14 +03:30
export default Monitors;