import { useEffect, useState } from "react"; import { useTheme } from "@emotion/react"; import { Box, Stack, Tooltip, Typography } from "@mui/material"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate, useParams } from "react-router"; import { deletePageSpeed, getPagespeedMonitorById, getPageSpeedByTeamId, updatePageSpeed, pausePageSpeed, } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice"; import { monitorValidation } from "../../../Validation/validation"; import { createToast } from "../../../Utils/toastUtils"; import { logger } from "../../../Utils/Logger"; import { ConfigBox } from "../../Monitors/styled"; import TextInput from "../../../Components/Inputs/TextInput"; import Select from "../../../Components/Inputs/Select"; import Checkbox from "../../../Components/Inputs/Checkbox"; import PauseCircleOutlineIcon from "@mui/icons-material/PauseCircleOutline"; import Breadcrumbs from "../../../Components/Breadcrumbs"; import PulseDot from "../../../Components/Animated/PulseDot"; import LoadingButton from "@mui/lab/LoadingButton"; import PlayCircleOutlineRoundedIcon from "@mui/icons-material/PlayCircleOutlineRounded"; import SkeletonLayout from "./skeleton"; import useUtils from "../../Monitors/utils"; import "./index.css"; import Dialog from "../../../Components/Dialog"; const PageSpeedConfigure = () => { const theme = useTheme(); const navigate = useNavigate(); const dispatch = useDispatch(); const MS_PER_MINUTE = 60000; const { user, authToken } = useSelector((state) => state.auth); const { isLoading } = useSelector((state) => state.pageSpeedMonitors); const { monitorId } = useParams(); const [monitor, setMonitor] = useState({}); const [errors, setErrors] = useState({}); const { statusColor, pagespeedStatusMsg, determineState } = useUtils(); const [buttonLoading, setButtonLoading] = useState(false); const idMap = { "monitor-url": "url", "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", "notify-email-default": "notification-email", }; const frequencies = [ { _id: 3, name: "3 minutes" }, { _id: 5, name: "5 minutes" }, { _id: 10, name: "10 minutes" }, { _id: 20, name: "20 minutes" }, { _id: 60, name: "1 hour" }, { _id: 1440, name: "1 day" }, { _id: 10080, name: "1 week" }, ]; useEffect(() => { const fetchMonitor = async () => { try { const action = await dispatch(getPagespeedMonitorById({ authToken, monitorId })); if (getPagespeedMonitorById.fulfilled.match(action)) { const monitor = action.payload.data; setMonitor(monitor); } else if (getPagespeedMonitorById.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(); }, [dispatch, authToken, monitorId, navigate]); const handleChange = (event, name) => { let { 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; } 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 handlePause = async () => { try { const action = await dispatch(pausePageSpeed({ authToken, monitorId })); if (pausePageSpeed.fulfilled.match(action)) { const monitor = action.payload.data; setMonitor(monitor); } else if (pausePageSpeed.rejected.match(action)) { throw new Error(action.error.message); } } catch (error) { logger.error("Error pausing monitor: " + monitorId); createToast({ body: "Failed to pause monitor" }); } }; const handleSave = async (event) => { event.preventDefault(); const action = await dispatch(updatePageSpeed({ authToken, monitor: monitor })); if (action.meta.requestStatus === "fulfilled") { createToast({ body: "Monitor updated successfully!" }); dispatch(getPageSpeedByTeamId(authToken)); } else { createToast({ body: "Failed to update monitor." }); } }; const [isOpen, setIsOpen] = useState(false); const handleRemove = async (event) => { event.preventDefault(); setButtonLoading(true); const action = await dispatch(deletePageSpeed({ authToken, monitor })); if (action.meta.requestStatus === "fulfilled") { navigate("/pagespeed"); } else { createToast({ body: "Failed to delete monitor." }); } setButtonLoading(false); }; return ( {Object.keys(monitor).length === 0 ? ( ) : ( <> {monitor.name} {monitor.url?.replace(/^https?:\/\//, "") || "..."} Editing... {monitor?.isActive ? ( <> Pause ) : ( <> Resume )} setIsOpen(true)} sx={{ ml: theme.spacing(6), }} > Remove General settings Here you can select the URL of the host, together with the type of monitor. .Mui-disabled)": { backgroundColor: theme.palette.background.accent, }, }} > Incident notifications When there is an incident, notify users. When there is a new incident, 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 ) : ( "" )} Advanced settings