import Stack from "@mui/material/Stack"; import Typography from "@mui/material/Typography"; import Breadcrumbs from "../../../Components/Breadcrumbs/index.jsx"; import SettingsTimeZone from "./SettingsTimeZone.jsx"; import SettingsUI from "./SettingsUI.jsx"; import SettingsURL from "./SettingsURL.jsx"; import SettingsPagespeed from "./SettingsPagespeed.jsx"; import SettingsDemoMonitors from "./SettingsDemoMonitors.jsx"; import SettingsAbout from "./SettingsAbout.jsx"; import SettingsEmail from "./SettingsEmail.jsx"; import SettingsGlobalThresholds from "./SettingsGlobalThresholds.jsx"; import Button from "@mui/material/Button"; // Utils import { settingsValidation } from "../../../Validation/validation.js"; import { useState } from "react"; import { useTheme } from "@emotion/react"; import { useTranslation } from "react-i18next"; import { useSelector, useDispatch } from "react-redux"; import { setTimezone, setMode, setLanguage, setShowURL, } from "../../../Features/UI/uiSlice.js"; import SettingsStats from "./SettingsStats.jsx"; import { useFetchSettings, useSaveSettings } from "../../../Hooks/v1/settingsHooks.js"; import { useIsAdmin } from "../../../Hooks/v1/useIsAdmin.js"; import { useAddDemoMonitors, useDeleteAllMonitors, useDeleteMonitorStats, } from "../../../Hooks/v1/monitorHooks.js"; // Constants const BREADCRUMBS = [{ name: `Settings`, path: "/settings" }]; const Settings = () => { // Redux state const { mode, language, timezone, showURL } = useSelector((state) => state.ui); // Local state const [settingsData, setSettingsData] = useState({}); const [errors, setErrors] = useState({}); const [isApiKeySet, setIsApiKeySet] = useState(settingsData?.pagespeedKeySet ?? false); const [apiKeyHasBeenReset, setApiKeyHasBeenReset] = useState(false); const [isEmailPasswordSet, setIsEmailPasswordSet] = useState( settingsData?.emailPasswordSet ?? false ); const [emailPasswordHasBeenReset, setEmailPasswordHasBeenReset] = useState(false); // Network const [isSettingsLoading, settingsError] = useFetchSettings({ setSettingsData, setIsApiKeySet, setIsEmailPasswordSet, }); const [addDemoMonitors, isAddingDemoMonitors] = useAddDemoMonitors(); const [isSaving, saveError, saveSettings] = useSaveSettings({ setSettingsData, setIsApiKeySet, setApiKeyHasBeenReset, setIsEmailPasswordSet, setEmailPasswordHasBeenReset, }); const [deleteAllMonitors, isDeletingMonitors] = useDeleteAllMonitors(); const [deleteMonitorStats, isDeletingMonitorStats] = useDeleteMonitorStats(); // Setup const isAdmin = useIsAdmin(); const theme = useTheme(); const HEADING_SX = { mt: theme.spacing(2), mb: theme.spacing(2) }; const { t } = useTranslation(); const dispatch = useDispatch(); // Handlers const handleChange = async (e) => { const { name, value, checked } = e.target; // Special case for showURL until handled properly in the backend if (name === "showURL") { dispatch(setShowURL(value)); return; } let newValue; if ( name === "systemEmailIgnoreTLS" || name === "systemEmailRequireTLS" || name === "systemEmailRejectUnauthorized" || name === "systemEmailSecure" || name === "systemEmailPool" ) { newValue = checked; } // Build next state early const newSettingsData = { ...settingsData, settings: { ...settingsData.settings, [name]: newValue ?? value }, }; if (name === "timezone") { dispatch(setTimezone({ timezone: value })); return; } if (name === "mode") { dispatch(setMode(value)); return; } if (name === "language") { dispatch(setLanguage(value)); return; } if (name === "deleteStats") { await deleteMonitorStats(); return; } if (name === "demo") { await addDemoMonitors(); return; } if (name === "deleteMonitors") { await deleteAllMonitors(); return; } // Validate const { error } = settingsValidation.validate(newSettingsData.settings, { abortEarly: false, }); if (!error || error.details.length === 0) { setErrors({}); } else { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); } setSettingsData(newSettingsData); }; const handleSave = () => { const { error } = settingsValidation.validate(settingsData.settings, { abortEarly: false, }); if (!error || error.details.length === 0) { setErrors({}); } else { const newErrors = {}; error.details.forEach((err) => { newErrors[err.path[0]] = err.message; }); setErrors(newErrors); } saveSettings(settingsData?.settings); }; return ( {t("settingsPage.title")} ); }; export default Settings;