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

285 lines
8.7 KiB
React
Raw Normal View History

2024-07-19 14:46:26 -04:00
import { useNavigate, useParams } from "react-router";
import { useTheme } from "@emotion/react";
import { useDispatch, useSelector } from "react-redux";
import { useEffect, useState } from "react";
2024-07-19 14:35:11 -04:00
import Button from "../../../Components/Button";
2024-07-19 17:48:47 -04:00
import Field from "../../../Components/Inputs/Field";
import RadioButton from "../../../Components/RadioButton";
2024-07-24 14:09:24 -04:00
import { Box, MenuItem, Stack, Typography } from "@mui/material";
2024-07-19 14:35:11 -04:00
import WestRoundedIcon from "@mui/icons-material/WestRounded";
2024-07-19 16:42:19 -04:00
import GreenCheck from "../../../assets/icons/checkbox-green.svg?react";
import RedCheck from "../../../assets/icons/checkbox-red.svg?react";
import PauseCircleOutlineIcon from "@mui/icons-material/PauseCircleOutline";
2024-07-19 14:35:11 -04:00
import "./index.css";
2024-07-19 18:04:58 -04:00
import { monitorValidation } from "../../../Validation/validation";
2024-07-24 14:09:24 -04:00
import Select from "../../../Components/Inputs/Select";
2024-07-28 16:51:47 -04:00
import { formatDurationRounded } from "../../../Utils/timeUtils";
2024-07-19 16:42:19 -04:00
/**
* Helper function to get duration since last check
* @param {Array} checks Array of check objects.
* @returns {number} Timestamp of the most recent check.
*/
const getLastChecked = (checks) => {
if (!checks || checks.length === 0) {
return 0; // Handle case when no checks are available
}
return new Date() - new Date(checks[0].createdAt);
};
2024-07-19 14:35:11 -04:00
/**
2024-07-19 14:46:26 -04:00
* Configure page displays monitor configurations and allows for editing actions.
2024-07-19 14:35:11 -04:00
* @component
*/
const Configure = () => {
2024-07-19 17:48:47 -04:00
const MS_PER_MINUTE = 60000;
2024-07-19 14:35:11 -04:00
const navigate = useNavigate();
const theme = useTheme();
2024-07-19 14:46:26 -04:00
const dispatch = useDispatch();
2024-07-19 17:48:47 -04:00
const { authToken } = useSelector((state) => state.auth);
2024-07-30 11:25:14 -07:00
const { monitors } = useSelector((state) => state.uptimeMonitors);
2024-07-19 17:48:47 -04:00
const { monitorId } = useParams();
2024-07-19 14:46:26 -04:00
2024-07-19 18:04:58 -04:00
const idMap = {
"monitor-url": "url",
"monitor-name": "name",
2024-07-19 18:30:44 -04:00
"monitor-checks-http": "type",
"monitor-checks-ping": "type",
2024-07-19 18:04:58 -04:00
};
const [config, setConfig] = useState();
2024-07-19 14:46:26 -04:00
const [monitor, setMonitor] = useState();
2024-07-19 17:48:47 -04:00
const [errors, setErrors] = useState({});
2024-07-19 14:46:26 -04:00
useEffect(() => {
2024-07-19 19:05:02 -04:00
const fetchMonitor = () => {
const data = monitors.find((monitor) => monitor._id === monitorId);
2024-07-19 18:04:58 -04:00
setConfig(data);
2024-07-19 17:48:47 -04:00
setMonitor({
name: data.name,
url: data.url.replace(/^https?:\/\//, ""),
type: data.type,
interval: data.interval / MS_PER_MINUTE,
});
2024-07-19 14:46:26 -04:00
};
fetchMonitor();
}, [monitorId, authToken]);
2024-07-19 14:35:11 -04:00
2024-07-19 18:30:44 -04:00
const handleChange = (event, name) => {
2024-07-19 18:04:58 -04:00
const { value, id } = event.target;
2024-07-19 18:30:44 -04:00
if (!name) name = idMap[id];
2024-07-19 18:04:58 -04:00
setMonitor((prev) => ({
...prev,
[name]: value,
}));
const validation = monitorValidation.validate(
{ [name]: value },
{ abortEarly: false }
);
setErrors((prev) => {
const updatedErrors = { ...prev };
if (validation.error)
updatedErrors[name] = validation.error.details[0].message;
else delete updatedErrors[name];
return updatedErrors;
});
};
const handleSubmit = (event) => {
event.preventDefault();
// TODO
};
2024-07-24 14:09:24 -04:00
const frequencies = [
{ _id: 1, name: "1 minute" },
{ _id: 2, name: "2 minutes" },
{ _id: 3, name: "3 minutes" },
{ _id: 4, name: "4 minutes" },
{ _id: 5, name: "5 minutes" },
];
2024-07-19 17:48:47 -04:00
2024-07-19 14:35:11 -04:00
return (
2024-07-26 12:39:18 -04:00
<Box className="configure-monitor">
2024-07-19 14:35:11 -04:00
<Button
level="tertiary"
label="Back"
img={<WestRoundedIcon />}
onClick={() => navigate(-1)}
sx={{
backgroundColor: "#f4f4f4",
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
pr: theme.gap.small,
},
}}
/>
2024-07-19 17:48:47 -04:00
<form className="configure-monitor-form" noValidate spellCheck="false">
2024-07-22 13:48:26 -04:00
<Stack direction="row" gap={theme.gap.small} mt={theme.gap.small}>
{config?.status ? <GreenCheck /> : <RedCheck />}
<Box>
<Typography component="h1" sx={{ lineHeight: 1 }}>
{config?.url.replace(/^https?:\/\//, "") || "..."}
</Typography>
<Typography mt={theme.gap.small}>
<Typography
component="span"
2024-07-19 17:48:47 -04:00
sx={{
2024-07-22 13:48:26 -04:00
color: config?.status
? "var(--env-var-color-17)"
: "var(--env-var-color-24)",
2024-07-19 17:48:47 -04:00
}}
2024-07-22 13:48:26 -04:00
>
Your site is {config?.status ? "up" : "down"}.
</Typography>{" "}
Checking every {formatDurationRounded(config?.interval)}. Last
time checked{" "}
{formatDurationRounded(getLastChecked(config?.checks))} ago.
</Typography>
</Box>
2024-07-19 16:42:19 -04:00
<Stack
direction="row"
2024-07-22 13:48:26 -04:00
gap={theme.gap.medium}
sx={{
ml: "auto",
alignSelf: "flex-end",
}}
2024-07-19 16:42:19 -04:00
>
2024-07-22 13:48:26 -04:00
<Button
level="tertiary"
label="Pause"
img={<PauseCircleOutlineIcon />}
sx={{
backgroundColor: "#f4f4f4",
pl: theme.gap.small,
pr: theme.gap.medium,
"& svg": {
pr: theme.gap.xs,
},
}}
/>
<Button
level="error"
label="Remove"
sx={{
boxShadow: "none",
px: theme.gap.ml,
}}
/>
2024-07-19 16:42:19 -04:00
</Stack>
2024-07-22 13:48:26 -04:00
</Stack>
<Stack
className="config-box"
direction="row"
justifyContent="space-between"
gap={theme.gap.xxl}
>
<Box>
<Typography component="h2">General settings</Typography>
<Typography component="p" sx={{ mt: theme.gap.small }}>
Here you can select the URL of the host, together with the type of
monitor.
</Typography>
</Box>
<Stack gap={theme.gap.xl}>
<Field
type="url"
id="monitor-url"
label="URL to monitor"
placeholder="google.com"
value={monitor?.url || ""}
onChange={handleChange}
error={errors["url"]}
/>
<Field
type="text"
id="monitor-name"
label="Friendly name"
isOptional={true}
placeholder="Google"
value={monitor?.name || ""}
onChange={handleChange}
error={errors["name"]}
/>
</Stack>
</Stack>
<Stack
className="config-box"
direction="row"
justifyContent="space-between"
gap={theme.gap.xxl}
>
<Box>
<Typography component="h2">Checks to perform</Typography>
<Typography component="p" sx={{ mt: theme.gap.small }}>
You can always add or remove checks after adding your site.
</Typography>
</Box>
<Stack gap={theme.gap.xl}>
<RadioButton
id="monitor-checks-http"
title="HTTP/website monitoring"
desc="Use HTTP(s) to monitor your website or API endpoint."
size="small"
value="http"
checked={monitor?.type === "http"}
onChange={handleChange}
/>
<RadioButton
id="monitor-checks-ping"
title="Ping monitoring"
desc="Check whether your server is available or not."
size="small"
value="ping"
checked={monitor?.type === "ping"}
onChange={handleChange}
/>
<Box className="error-container">
{errors["type"] ? (
<Typography component="p" className="input-error">
{errors["type"]}
</Typography>
) : (
""
)}
2024-07-19 17:48:47 -04:00
</Box>
2024-07-19 16:42:19 -04:00
</Stack>
2024-07-22 13:48:26 -04:00
</Stack>
<Stack
className="config-box"
direction="row"
justifyContent="space-between"
gap={theme.gap.xxl}
>
<Box>
<Typography component="h2">Advanced settings</Typography>
</Box>
<Stack gap={theme.gap.xl}>
2024-07-24 14:09:24 -04:00
<Select
id="monitor-interval-configure"
label="Check frequency"
value={monitor?.interval || 1}
onChange={(event) => handleChange(event, "interval")}
items={frequencies}
/>
2024-07-19 16:42:19 -04:00
</Stack>
</Stack>
2024-07-22 13:48:26 -04:00
<Stack direction="row" justifyContent="flex-end">
<Button
type="submit"
level="primary"
label="Save"
sx={{
minWidth: "80px",
}}
/>
2024-07-19 16:42:19 -04:00
</Stack>
2024-07-19 17:48:47 -04:00
</form>
2024-07-26 12:39:18 -04:00
</Box>
2024-07-19 14:35:11 -04:00
);
};
export default Configure;