import { useState } from "react"; import { Box, Button, ButtonGroup, Stack, Typography } from "@mui/material"; import LoadingButton from "@mui/lab/LoadingButton"; import { useSelector, useDispatch } from "react-redux"; import { monitorValidation } from "../../../Validation/validation"; import { useNavigate } from "react-router-dom"; import { useTheme } from "@emotion/react"; import { createPageSpeed, checkEndpointResolution, } from "../../../Features/PageSpeedMonitor/pageSpeedMonitorSlice"; import { createToast } from "../../../Utils/toastUtils"; import { logger } from "../../../Utils/Logger"; import { ConfigBox } from "../../Monitors/styled"; import Radio from "../../../Components/Inputs/Radio"; import TextInput from "../../../Components/Inputs/TextInput"; import { HttpAdornment } from "../../../Components/Inputs/TextInput/Adornments"; import Field from "../../../Components/Inputs/Field"; import Select from "../../../Components/Inputs/Select"; import Checkbox from "../../../Components/Inputs/Checkbox"; import Breadcrumbs from "../../../Components/Breadcrumbs"; import "./index.css"; const CreatePageSpeed = () => { const MS_PER_MINUTE = 60000; const { user, authToken } = useSelector((state) => state.auth); const { isLoading } = useSelector((state) => state.pageSpeedMonitors); const dispatch = useDispatch(); const navigate = useNavigate(); const theme = useTheme(); const idMap = { "monitor-url": "url", "monitor-name": "name", "monitor-checks-http": "type", "monitor-checks-ping": "type", "notify-email-default": "notification-email", }; const [monitor, setMonitor] = useState({ url: "", name: "", type: "pagespeed", notifications: [], interval: 3, }); const [https, setHttps] = useState(true); const [errors, setErrors] = useState({}); const handleChange = (event, name) => { const { 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 { setMonitor((prev) => ({ ...prev, [name]: value, })); const { error } = monitorValidation.validate( { [name]: value }, { abortEarly: false } ); setErrors((prev) => { const updatedErrors = { ...prev }; if (error) updatedErrors[name] = error.details[0].message; else delete updatedErrors[name]; return updatedErrors; }); } }; const handleCreateMonitor = async (event) => { event.preventDefault(); //obj to submit let form = { url: `http${https ? "s" : ""}://` + monitor.url, name: monitor.name === "" ? monitor.url : monitor.name, type: monitor.type, interval: monitor.interval * MS_PER_MINUTE, }; const { error } = monitorValidation.validate(form, { abortEarly: false, }); if (error) { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); createToast({ body: "Error validation data." }); } else { const checkEndpointAction = await dispatch( checkEndpointResolution({ authToken, monitorURL: form.url }) ); if (checkEndpointAction.meta.requestStatus === "rejected") { createToast({ body: "The endpoint you entered doesn't resolve. Check the URL again.", }); setErrors({ url: "The entered URL is not reachable." }); return; } form = { ...form, description: form.name, teamId: user.teamId, userId: user._id, notifications: monitor.notifications, }; const action = await dispatch(createPageSpeed({ authToken, monitor: form })); if (action.meta.requestStatus === "fulfilled") { createToast({ body: "Monitor created successfully!" }); navigate("/pagespeed"); } else { createToast({ body: "Failed to create monitor." }); } } }; //select values 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" }, ]; return ( Create your{" "} pagespeed monitor General settings Here you can select the URL of the host, together with the type of monitor. } placeholder="google.com" value={monitor.url} onChange={handleChange} error={errors["url"] ? true : false} helperText={errors["url"]} /> Checks to perform You can always add or remove checks after adding your site. handleChange(event)} /> {errors["type"] ? ( {errors["type"]} ) : ( "" )} Incident notifications When there is an incident, notify users. When there is a new incident, logger.warn("disabled")} isDisabled={true} /> notification.type === "email" )} 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