// Components import Box from "@mui/material/Box"; import Stack from "@mui/material/Stack"; import Tooltip from "@mui/material/Tooltip"; import Typography from "@mui/material/Typography"; import Button from "@mui/material/Button"; import FormControlLabel from "@mui/material/FormControlLabel"; import Switch from "@mui/material/Switch"; import ConfigBox from "../../../Components/ConfigBox"; import Breadcrumbs from "../../../Components/Breadcrumbs"; import TextInput from "../../../Components/Inputs/TextInput"; import { HttpAdornment } from "../../../Components/Inputs/TextInput/Adornments"; import Select from "../../../Components/Inputs/Select"; import Dialog from "../../../Components/Dialog"; import PulseDot from "../../../Components/Animated/PulseDot"; import Checkbox from "../../../Components/Inputs/Checkbox"; // Utils import { useParams } from "react-router"; import { useTheme } from "@emotion/react"; import { useState } from "react"; import { monitorValidation } from "../../../Validation/validation"; import { createToast } from "../../../Utils/toastUtils"; import { useTranslation } from "react-i18next"; import PauseOutlinedIcon from "@mui/icons-material/PauseOutlined"; import PlayArrowOutlinedIcon from "@mui/icons-material/PlayArrowOutlined"; import { useMonitorUtils } from "../../../Hooks/useMonitorUtils"; import { useGetNotificationsByTeamId } from "../../../Hooks/useNotifications"; import { useDeleteMonitor, useUpdateMonitor, usePauseMonitor, useFetchMonitorById, } from "../../../Hooks/monitorHooks"; import NotificationsConfig from "../../../Components/NotificationConfig"; /** * Parses a URL string and returns a URL object. * * @param {string} url - The URL string to parse. * @returns {URL} - The parsed URL object if valid, otherwise an empty string. */ const parseUrl = (url) => { try { return new URL(url); } catch (error) { return null; } }; /** * Configure page displays monitor configurations and allows for editing actions. * @component */ const Configure = () => { const { monitorId } = useParams(); // Local state const [form, setForm] = useState({ ignoreTlsErrors: false, interval: 60000, matchMethod: "equal", expectedValue: "", jsonPath: "", notifications: [], port: "", type: "http", }); const [useAdvancedMatching, setUseAdvancedMatching] = useState(false); const [updateTrigger, setUpdateTrigger] = useState(false); const [isOpen, setIsOpen] = useState(false); const [errors, setErrors] = useState({}); const triggerUpdate = () => { setUpdateTrigger(!updateTrigger); }; // Network const [notifications, notificationsAreLoading, notificationsError] = useGetNotificationsByTeamId(); const [pauseMonitor, isPausing, pauseError] = usePauseMonitor({}); const [deleteMonitor, isDeleting] = useDeleteMonitor(); const [updateMonitor, isUpdating] = useUpdateMonitor(); const [isLoading] = useFetchMonitorById({ monitorId, setMonitor: setForm, updateTrigger, }); const MS_PER_MINUTE = 60000; const theme = useTheme(); const matchMethodOptions = [ { _id: "equal", name: "Equal" }, { _id: "include", name: "Include" }, { _id: "regex", name: "Regex" }, ]; 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" }, ]; const expectedValuePlaceholders = { regex: "^(success|ok)$", equal: "success", include: "ok", }; // Handlers const handlePause = async () => { const res = await pauseMonitor({ monitorId: form?._id, triggerUpdate }); if (typeof res !== "undefined") { triggerUpdate(); } }; const handleRemove = async (event) => { event.preventDefault(); await deleteMonitor({ monitor: form, redirect: "/uptime" }); }; const onChange = (event) => { let { name, value, checked } = event.target; if (name === "ignoreTlsErrors") { value = checked; } if (name === "useAdvancedMatching") { setForm((prevForm) => { return { ...prevForm, matchMethod: "equal", }; }); setUseAdvancedMatching(!useAdvancedMatching); return; } if (name === "interval") { value = value * MS_PER_MINUTE; } setForm({ ...form, [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 onSubmit = async (e) => { e.preventDefault(); const toSubmit = { _id: form?._id, url: form?.url, name: form?.name, type: form?.type, matchMethod: form?.matchMethod, expectedValue: form?.expectedValue, jsonPath: form?.jsonPath, interval: form?.interval, teamId: form?.teamId, userId: form?.userId, port: form?.port, ignoreTlsErrors: form?.ignoreTlsErrors, }; if (!useAdvancedMatching) { toSubmit.matchMethod = ""; toSubmit.expectedValue = ""; toSubmit.jsonPath = ""; } const validation = monitorValidation.validate(toSubmit, { abortEarly: false, }); if (validation.error) { const newErrors = {}; validation.error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); createToast({ body: "Please check the form for errors." }); return; } toSubmit.notifications = form?.notifications; await updateMonitor({ monitor: toSubmit, redirect: "/uptime" }); }; // Parse the URL const parsedUrl = parseUrl(form?.url); const protocol = parsedUrl?.protocol?.replace(":", "") || ""; const { determineState, statusColor } = useMonitorUtils(); const { t } = useTranslation(); return ( {form?.name} {form?.url?.replace(/^https?:\/\//, "") || "..."} {t("editing")} {t("settingsGeneralSettings")} {t("distributedUptimeCreateSelectURL")} } id="monitor-url" label={t("urlMonitor")} placeholder="google.com" value={parsedUrl?.host || form?.url || ""} disabled={true} /> {t("notificationConfig.title")} {t("notificationConfig.description")} {t("ignoreTLSError")} {t("ignoreTLSErrorDescription")} } label={t("tlsErrorIgnored")} /> {t("distributedUptimeCreateAdvancedSettings")} {t("uptimeCreate")} {t("uptimeCreateJsonPath")}  jmespath.org  {t("uptimeCreateJsonPathQuery")} )} setIsOpen(false)} confirmationButtonLabel="Delete" onConfirm={handleRemove} isLoading={isLoading} /> ); }; export default Configure;