import { useNavigate, useParams } from "react-router"; import { useTheme } from "@emotion/react"; import { useDispatch, useSelector } from "react-redux"; import { useEffect, useState } from "react"; import { Box, Stack, Tooltip, Typography, Button, FormControlLabel, Switch, } from "@mui/material"; import { monitorValidation } from "../../../Validation/validation"; import { createToast } from "../../../Utils/toastUtils"; import { logger } from "../../../Utils/Logger"; import { useTranslation } from "react-i18next"; import ConfigBox from "../../../Components/ConfigBox"; import { updateUptimeMonitor, pauseUptimeMonitor, getUptimeMonitorById, deleteUptimeMonitor, } from "../../../Features/UptimeMonitors/uptimeMonitorsSlice"; import TextInput from "../../../Components/Inputs/TextInput"; import { HttpAdornment } from "../../../Components/Inputs/TextInput/Adornments"; import PauseIcon from "../../../assets/icons/pause-icon.svg?react"; import ResumeIcon from "../../../assets/icons/resume-icon.svg?react"; import Select from "../../../Components/Inputs/Select"; import Checkbox from "../../../Components/Inputs/Checkbox"; import Breadcrumbs from "../../../Components/Breadcrumbs"; import PulseDot from "../../../Components/Animated/PulseDot"; import SkeletonLayout from "./skeleton"; import "./index.css"; import Dialog from "../../../Components/Dialog"; import NotificationIntegrationModal from "../../../Components/NotificationIntegrationModal/Components/NotificationIntegrationModal"; import { usePauseMonitor } from "../../../Hooks/useMonitorControls"; import PauseOutlinedIcon from "@mui/icons-material/PauseOutlined"; import PlayArrowOutlinedIcon from "@mui/icons-material/PlayArrowOutlined"; import { useMonitorUtils } from "../../../Hooks/useMonitorUtils"; /** * 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 MS_PER_MINUTE = 60000; const navigate = useNavigate(); const theme = useTheme(); const dispatch = useDispatch(); const { user } = useSelector((state) => state.auth); const { isLoading } = useSelector((state) => state.uptimeMonitors); const [monitor, setMonitor] = useState({}); const [errors, setErrors] = useState({}); const { monitorId } = useParams(); const idMap = { "monitor-url": "url", "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", "notify-email-default": "notification-email", }; const matchMethodOptions = [ { _id: "equal", name: "Equal" }, { _id: "include", name: "Include" }, { _id: "regex", name: "Regex" }, ]; const expectedValuePlaceholders = { regex: "^(success|ok)$", equal: "success", include: "ok", }; const [trigger, setTrigger] = useState(false); const triggerUpdate = () => { setTrigger(!trigger); }; const [pauseMonitor, isPausing, error] = usePauseMonitor({ monitorId: monitor?._id, triggerUpdate, }); useEffect(() => { const fetchMonitor = async () => { try { const action = await dispatch(getUptimeMonitorById({ monitorId })); if (getUptimeMonitorById.fulfilled.match(action)) { const monitor = action.payload.data; setMonitor(monitor); } else if (getUptimeMonitorById.rejected.match(action)) { throw new Error(action.error.message); } } catch (error) { logger.error("Error fetching monitor of id: " + monitorId); navigate("/not-found", { replace: true }); } }; fetchMonitor(); }, [monitorId, navigate, trigger]); const handleChange = (event, name) => { let { checked, value, id } = event.target; if (!name) name = idMap[id]; if (name.includes("notification-")) { name = name.replace("notification-", ""); let hasNotif = monitor.notifications.some( (notification) => notification.type === name ); setMonitor((prev) => { const notifs = [...prev.notifications]; if (hasNotif) { return { ...prev, notifications: notifs.filter((notif) => notif.type !== name), }; } else { return { ...prev, notifications: [ ...notifs, name === "email" ? { type: name, address: value } : // TODO - phone number { type: name, phone: value }, ], }; } }); } else { if (name === "interval") { value = value * MS_PER_MINUTE; } if (name === "ignoreTlsErrors") { value = checked; } 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 = async (event) => { event.preventDefault(); const action = await dispatch(updateUptimeMonitor({ monitor: monitor })); if (action.meta.requestStatus === "fulfilled") { createToast({ body: "Monitor updated successfully!" }); } else { createToast({ body: "Failed to update monitor." }); } }; const [isOpen, setIsOpen] = useState(false); const handleRemove = async (event) => { event.preventDefault(); const action = await dispatch(deleteUptimeMonitor({ monitor })); if (action.meta.requestStatus === "fulfilled") { navigate("/uptime"); } else { createToast({ body: "Failed to delete monitor." }); } }; 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" }, ]; // Parse the URL const parsedUrl = parseUrl(monitor?.url); const protocol = parsedUrl?.protocol?.replace(":", "") || ""; // Notification modal state const [isNotificationModalOpen, setIsNotificationModalOpen] = useState(false); const handleOpenNotificationModal = () => { setIsNotificationModalOpen(true); }; const handleClosenNotificationModal = () => { setIsNotificationModalOpen(false); }; const { determineState, statusColor } = useMonitorUtils(); const { t } = useTranslation(); return ( {Object.keys(monitor).length === 0 ? ( ) : ( <> {monitor.name} {monitor.url?.replace(/^https?:\/\//, "") || "..."} {t("editing")} {t("settingsGeneralSettings")} {t("distributedUptimeCreateSelectURL")} ) } id="monitor-url" label={t("urlMonitor")} placeholder="google.com" value={parsedUrl?.host || monitor?.url || ""} disabled={true} /> handleChange(event, "port")} error={errors["port"] ? true : false} helperText={errors["port"]} hidden={monitor.type !== "port"} /> {t("distributedUptimeCreateIncidentNotification")} {t("distributedUptimeCreateIncidentDescription")} {t("whenNewIncident")} {/* {Leaving components commented for future funtionality implimentation} */} {/* logger.warn("disabled")} isDisabled={true} /> */} notification.type === "email" ) || false } value={user?.email} onChange={(event) => handleChange(event)} /> {/* logger.warn("disabled")} isDisabled={true} /> */} {/* {monitor?.notifications?.some( (notification) => notification.type === "emails" ) ? ( logger.warn("disabled")} /> You can separate multiple emails with a comma ) : ( "" )} */} {t("ignoreTLSError")} {t("ignoreTLSErrorDescription")} handleChange(event, "ignoreTlsErrors")} sx={{ mr: theme.spacing(2) }} /> } label={t("tlsErrorIgnored")} /> {t("distributedUptimeCreateAdvancedSettings")} handleChange(event, "matchMethod")} items={matchMethodOptions} /> handleChange(event, "expectedValue")} error={errors["expectedValue"] ? true : false} helperText={errors["expectedValue"]} /> {t("uptimeCreate")} handleChange(event, "jsonPath")} error={errors["jsonPath"] ? true : false} helperText={errors["jsonPath"]} /> {t("uptimeCreateJsonPath")}  jmespath.org  {t("uptimeCreateJsonPathQuery")} )} )} setIsOpen(false)} confirmationButtonLabel="Delete" onConfirm={handleRemove} isLoading={isLoading} /> ); }; export default Configure;