Files
Checkmate/client/src/Pages/Uptime/Create/index.jsx
T

486 lines
12 KiB
React
Raw Normal View History

2024-12-03 15:29:19 +08:00
//Components
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-11-26 12:21:42 +08:00
import TextInput from "../../../Components/Inputs/TextInput";
import { HttpAdornment } from "../../../Components/Inputs/TextInput/Adornments";
2024-12-03 15:29:19 +08:00
import Radio from "../../../Components/Inputs/Radio";
import Select from "../../../Components/Inputs/Select";
2024-12-18 12:20:18 -08:00
import ConfigBox from "../../../Components/ConfigBox";
2025-06-09 12:55:45 +08:00
import NotificationsConfig from "../../../Components/NotificationConfig";
2025-06-12 12:44:46 +08:00
import Button from "@mui/material/Button";
import ButtonGroup from "@mui/material/ButtonGroup";
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
import Switch from "@mui/material/Switch";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "../../../Components/Inputs/Checkbox";
2025-06-12 12:44:46 +08:00
// Utils
import { useTheme } from "@emotion/react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { monitorValidation } from "../../../Validation/validation";
import { createToast } from "../../../Utils/toastUtils";
import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications";
import { useCreateMonitor } from "../../../Hooks/monitorHooks";
2025-06-09 12:55:45 +08:00
2024-07-17 20:15:45 -04:00
const CreateMonitor = () => {
2025-06-09 12:55:45 +08:00
// Local state
const [errors, setErrors] = useState({});
const [https, setHttps] = useState(true);
const [useAdvancedMatching, setUseAdvancedMatching] = useState(false);
const [monitor, setMonitor] = useState({
url: "",
name: "",
type: "http",
matchMethod: "equal",
expectedValue: "",
jsonPath: "",
2025-06-09 12:55:45 +08:00
notifications: [],
interval: 1,
ignoreTlsErrors: false,
});
// Setup
const theme = useTheme();
const { t } = useTranslation();
const [notifications, notificationsAreLoading, error] = useGetNotificationsByTeamId();
2025-06-12 12:44:46 +08:00
const [createMonitor, isCreating] = useCreateMonitor();
2025-06-09 12:55:45 +08:00
const MS_PER_MINUTE = 60000;
2024-12-03 15:29:19 +08:00
const SELECT_VALUES = [
{ _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" },
];
const matchMethodOptions = [
{ _id: "equal", name: "Equal" },
{ _id: "include", name: "Include" },
{ _id: "regex", name: "Regex" },
];
const expectedValuePlaceholders = {
2025-03-28 13:06:40 -04:00
regex: "^(success|ok)$",
equal: "success",
include: "ok",
};
const monitorTypeMaps = {
http: {
label: "URL to monitor",
placeholder: "google.com",
namePlaceholder: "Google",
},
ping: {
label: "IP address to monitor",
placeholder: "1.1.1.1",
namePlaceholder: "Google",
},
docker: {
label: "Container ID",
placeholder: "abc123",
namePlaceholder: "My Container",
},
2024-12-27 11:16:58 -08:00
port: {
label: "URL to monitor",
placeholder: "localhost",
namePlaceholder: "Localhost:5173",
},
};
2025-06-09 12:55:45 +08:00
const BREADCRUMBS = [
2024-12-10 14:23:46 +08:00
{ name: "uptime", path: "/uptime" },
{ name: "create", path: `/uptime/create` },
2024-12-03 15:29:19 +08:00
];
2025-06-09 12:55:45 +08:00
// Handlers
2024-11-27 15:48:06 +08:00
2025-06-09 12:55:45 +08:00
const onSubmit = async (event) => {
event.preventDefault();
2025-06-09 12:55:45 +08:00
const { notifications, ...rest } = monitor;
let form = {
2025-06-09 12:55:45 +08:00
...rest,
url:
2024-12-03 15:29:19 +08:00
//prepending protocol for url
monitor.type === "http"
? `http${https ? "s" : ""}://` + monitor.url
: monitor.url,
2024-12-27 11:16:58 -08:00
port: monitor.type === "port" ? monitor.port : undefined,
2025-02-01 22:14:27 +08:00
name: monitor.name || monitor.url.substring(0, 50),
type: monitor.type,
interval: monitor.interval * MS_PER_MINUTE,
};
2024-06-27 17:26:00 -04:00
2025-06-09 12:55:45 +08:00
// If not using advanced matching, remove advanced settings
if (!useAdvancedMatching) {
form.matchMethod = undefined;
form.expectedValue = undefined;
form.jsonPath = undefined;
2025-02-13 00:54:24 +00:00
}
const { error } = monitorValidation.validate(form, {
abortEarly: false,
});
2024-07-18 12:11:27 -07:00
if (error) {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
2024-12-03 15:29:19 +08:00
createToast({ body: "Please check the form for errors." });
return;
}
form = {
...form,
2025-02-01 22:14:27 +08:00
description: monitor.name || monitor.url,
2024-12-03 15:29:19 +08:00
notifications: monitor.notifications,
};
2025-06-09 12:55:45 +08:00
2025-06-13 14:54:59 +08:00
await createMonitor({ monitor: form, redirect: "/uptime" });
2024-12-03 15:29:19 +08:00
};
2025-06-09 12:55:45 +08:00
const onChange = (event) => {
const { name, value, checked } = event.target;
let newValue = value;
if (name === "ignoreTlsErrors") {
newValue = checked;
}
if (name === "useAdvancedMatching") {
setUseAdvancedMatching(checked);
return;
}
2025-06-09 12:55:45 +08:00
const updatedMonitor = {
2024-12-03 15:29:19 +08:00
...monitor,
2025-06-09 12:55:45 +08:00
[name]: newValue,
2025-02-01 22:14:27 +08:00
};
2025-06-09 12:55:45 +08:00
setMonitor(updatedMonitor);
2024-12-03 15:29:19 +08:00
const { error } = monitorValidation.validate(
2025-06-09 12:55:45 +08:00
{ type: monitor.type, [name]: newValue },
2024-12-03 15:29:19 +08:00
{ abortEarly: false }
);
2025-06-09 12:55:45 +08:00
setErrors((prev) => ({
2024-12-03 15:29:19 +08:00
...prev,
2025-06-09 12:55:45 +08:00
...(error ? { [name]: error.details[0].message } : { [name]: undefined }),
2024-12-03 15:29:19 +08:00
}));
};
2024-06-27 16:44:56 -04:00
return (
2025-06-09 12:55:45 +08:00
<Stack gap={theme.spacing(10)}>
<Breadcrumbs list={BREADCRUMBS} />
<Typography
component="h1"
variant="h1"
>
<Typography
component="span"
fontSize="inherit"
>
{t("createYour")}{" "}
</Typography>
<Typography
component="span"
variant="h2"
fontSize="inherit"
fontWeight="inherit"
>
{t("monitor")}
</Typography>
</Typography>
<Stack
component="form"
2025-06-09 12:55:45 +08:00
noValidate
gap={theme.spacing(12)}
mt={theme.spacing(6)}
2025-06-09 12:55:45 +08:00
onSubmit={onSubmit}
>
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
{t("distributedUptimeCreateChecks")}
</Typography>
<Typography component="p">
2025-03-17 01:16:42 +05:30
{t("distributedUptimeCreateChecksDescription")}
</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<Stack gap={theme.spacing(6)}>
<Radio
2025-06-09 12:55:45 +08:00
name="type"
2025-03-17 01:16:42 +05:30
title={t("websiteMonitoring")}
desc={t("websiteMonitoringDescription")}
size="small"
value="http"
checked={monitor.type === "http"}
2025-06-09 12:55:45 +08:00
onChange={onChange}
/>
{monitor.type === "http" ? (
<ButtonGroup sx={{ ml: theme.spacing(16) }}>
<Button
variant="group"
filled={https.toString()}
onClick={() => setHttps(true)}
>
2025-03-17 01:16:42 +05:30
{t("https")}
</Button>
<Button
variant="group"
filled={(!https).toString()}
onClick={() => setHttps(false)}
>
2025-03-17 01:16:42 +05:30
{t("http")}
</Button>
</ButtonGroup>
) : (
""
)}
</Stack>
<Radio
2025-06-09 12:55:45 +08:00
name="type"
2025-03-17 01:16:42 +05:30
title={t("pingMonitoring")}
desc={t("pingMonitoringDescription")}
size="small"
value="ping"
checked={monitor.type === "ping"}
2025-06-09 12:55:45 +08:00
onChange={onChange}
/>
<Radio
2025-06-09 12:55:45 +08:00
name="type"
2025-03-17 01:16:42 +05:30
title={t("dockerContainerMonitoring")}
desc={t("dockerContainerMonitoringDescription")}
size="small"
value="docker"
checked={monitor.type === "docker"}
2025-06-09 12:55:45 +08:00
onChange={onChange}
/>
2024-12-27 11:16:58 -08:00
<Radio
2025-06-09 12:55:45 +08:00
name="type"
2025-03-17 01:16:42 +05:30
title={t("portMonitoring")}
desc={t("portMonitoringDescription")}
2024-12-27 11:16:58 -08:00
size="small"
value="port"
checked={monitor.type === "port"}
2025-06-09 12:55:45 +08:00
onChange={onChange}
2024-12-27 11:16:58 -08:00
/>
{errors["type"] ? (
<Box className="error-container">
<Typography
component="p"
className="input-error"
2024-11-10 17:00:13 -05:00
color={theme.palette.error.contrastText}
>
{errors["type"]}
</Typography>
</Box>
) : (
""
)}
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
{t("settingsGeneralSettings")}
</Typography>
2025-06-08 08:14:17 +08:00
<Typography component="p">
{t(`uptimeGeneralInstructions.${monitor.type}`)}
</Typography>
</Box>
<Stack gap={theme.spacing(15)}>
<TextInput
2025-06-09 12:55:45 +08:00
name="url"
type={monitor.type === "http" ? "url" : "text"}
startAdornment={
monitor.type === "http" ? <HttpAdornment https={https} /> : null
}
label={monitorTypeMaps[monitor.type].label || "URL to monitor"}
https={https}
placeholder={monitorTypeMaps[monitor.type].placeholder || ""}
value={monitor.url}
2025-06-09 12:55:45 +08:00
onChange={onChange}
error={errors["url"] ? true : false}
helperText={errors["url"]}
/>
<TextInput
2025-06-09 12:55:45 +08:00
name="port"
type="number"
2025-03-17 01:16:42 +05:30
label={t("portToMonitor")}
placeholder="5173"
value={monitor.port}
2025-06-09 12:55:45 +08:00
onChange={onChange}
error={errors["port"] ? true : false}
helperText={errors["port"]}
hidden={monitor.type !== "port"}
/>
<TextInput
2025-06-09 12:55:45 +08:00
name="name"
type="text"
2025-03-17 01:16:42 +05:30
label={t("displayName")}
isOptional={true}
placeholder={monitorTypeMaps[monitor.type].namePlaceholder || ""}
value={monitor.name}
2025-06-09 12:55:45 +08:00
onChange={onChange}
error={errors["name"] ? true : false}
helperText={errors["name"]}
/>
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
2025-06-09 14:22:11 +08:00
<Typography component="h2">{t("notificationConfig.title")}</Typography>
<Typography component="p">{t("notificationConfig.description")}</Typography>
</Box>
2025-06-09 12:55:45 +08:00
<NotificationsConfig
notifications={notifications}
setMonitor={setMonitor}
/>
</ConfigBox>
2025-05-08 15:28:44 +05:30
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
{t("ignoreTLSError")}
</Typography>
2025-05-08 15:47:20 +05:30
<Typography component="p">{t("ignoreTLSErrorDescription")}</Typography>
2025-05-08 15:28:44 +05:30
</Box>
<Stack>
<FormControlLabel
2025-05-12 21:46:52 +05:30
sx={{ marginLeft: 0 }}
2025-05-08 15:28:44 +05:30
control={
<Switch
2025-06-09 12:55:45 +08:00
name="ignoreTlsErrors"
2025-05-08 15:28:44 +05:30
checked={monitor.ignoreTlsErrors}
2025-06-09 12:55:45 +08:00
onChange={onChange}
2025-05-08 15:28:44 +05:30
sx={{ mr: theme.spacing(2) }}
/>
}
2025-05-08 15:47:20 +05:30
label={t("tlsErrorIgnored")}
2025-05-08 15:28:44 +05:30
/>
</Stack>
</ConfigBox>
<ConfigBox>
<Box>
2025-05-27 09:10:32 -07:00
<Typography
component="h2"
variant="h2"
>
2025-03-19 09:02:30 -07:00
{t("distributedUptimeCreateAdvancedSettings")}
</Typography>
</Box>
<Stack gap={theme.spacing(12)}>
<Select
2025-06-09 12:55:45 +08:00
name="interval"
label="Check frequency"
value={monitor.interval || 1}
2025-06-09 12:55:45 +08:00
onChange={onChange}
2024-12-03 15:29:19 +08:00
items={SELECT_VALUES}
/>
<Checkbox
name="useAdvancedMatching"
label={t("advancedMatching")}
isChecked={useAdvancedMatching}
onChange={onChange}
/>
{monitor.type === "http" && useAdvancedMatching && (
2025-02-24 14:33:25 -08:00
<>
2025-02-13 00:54:24 +00:00
<Select
2025-06-09 12:55:45 +08:00
name="matchMethod"
2025-02-13 00:54:24 +00:00
label="Match Method"
value={monitor.matchMethod || "equal"}
2025-06-09 12:55:45 +08:00
onChange={onChange}
items={matchMethodOptions}
2025-02-13 00:54:24 +00:00
/>
<Stack>
<TextInput
2025-06-09 12:55:45 +08:00
name="expectedValue"
2025-02-13 00:54:24 +00:00
type="text"
label="Expected value"
isOptional={true}
2025-02-24 14:33:25 -08:00
placeholder={
expectedValuePlaceholders[monitor.matchMethod || "equal"]
}
2025-02-13 00:54:24 +00:00
value={monitor.expectedValue}
2025-06-09 12:55:45 +08:00
onChange={onChange}
2025-02-13 00:54:24 +00:00
error={errors["expectedValue"] ? true : false}
helperText={errors["expectedValue"]}
/>
<Typography
component="span"
color={theme.palette.primary.contrastTextTertiary}
opacity={0.8}
>
2025-03-17 01:16:42 +05:30
{t("uptimeCreate")}
2025-02-13 00:54:24 +00:00
</Typography>
</Stack>
<Stack>
<TextInput
2025-06-09 12:55:45 +08:00
name="jsonPath"
2025-02-13 00:54:24 +00:00
type="text"
label="JSON Path"
isOptional={true}
2025-03-25 20:54:50 -04:00
placeholder="data.status"
2025-02-13 00:54:24 +00:00
value={monitor.jsonPath}
2025-06-09 12:55:45 +08:00
onChange={onChange}
2025-02-13 00:54:24 +00:00
error={errors["jsonPath"] ? true : false}
helperText={errors["jsonPath"]}
/>
<Typography
component="span"
color={theme.palette.primary.contrastTextTertiary}
opacity={0.8}
>
2025-03-17 01:16:42 +05:30
{t("uptimeCreateJsonPath")}&nbsp;
2025-02-24 14:33:25 -08:00
<Typography
component="a"
href="https://jmespath.org/"
target="_blank"
color="info"
>
2025-02-13 00:54:24 +00:00
jmespath.org
</Typography>
2025-03-17 01:16:42 +05:30
&nbsp;{t("uptimeCreateJsonPathQuery")}
2025-02-13 00:54:24 +00:00
</Typography>
</Stack>
</>
2025-02-24 14:33:25 -08:00
)}
</Stack>
</ConfigBox>
<Stack
direction="row"
justifyContent="flex-end"
>
2025-02-13 15:20:12 -08:00
<Button
2025-06-09 12:55:45 +08:00
type="submit"
variant="contained"
2025-01-14 08:09:03 -05:00
color="accent"
2024-12-03 15:29:19 +08:00
disabled={!Object.values(errors).every((value) => value === undefined)}
2025-06-13 10:52:05 +08:00
loading={isCreating}
>
2025-03-17 01:16:42 +05:30
{t("createMonitor")}
2025-02-13 15:20:12 -08:00
</Button>
</Stack>
</Stack>
2025-06-09 12:55:45 +08:00
</Stack>
);
2024-06-18 22:39:58 +03:30
};
export default CreateMonitor;