Files
Checkmate/client/src/Pages/v1/Settings/index.jsx
T

302 lines
8.0 KiB
React
Raw Normal View History

2025-05-09 13:28:44 -07:00
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
2025-09-27 14:54:55 -07:00
import Breadcrumbs from "@/Components/v1/Breadcrumbs/index.jsx";
2025-09-25 10:11:41 -07:00
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";
2025-11-21 10:43:29 -08:00
import SettingsExport from "./SettingsExport.jsx";
2025-05-09 13:28:44 -07:00
import Button from "@mui/material/Button";
// Utils
2025-09-25 10:11:41 -07:00
import { settingsValidation } from "../../../Validation/validation.js";
2025-05-09 13:28:44 -07:00
import { useState } from "react";
import { useTheme } from "@emotion/react";
import { useTranslation } from "react-i18next";
import { useSelector, useDispatch } from "react-redux";
2025-09-25 10:19:08 -07:00
import {
setTimezone,
setMode,
setLanguage,
setShowURL,
} from "../../../Features/UI/uiSlice.js";
2025-09-25 10:11:41 -07:00
import SettingsStats from "./SettingsStats.jsx";
2025-06-13 09:51:26 +08:00
2025-09-25 10:11:41 -07:00
import { useFetchSettings, useSaveSettings } from "../../../Hooks/v1/settingsHooks.js";
import { useIsAdmin } from "../../../Hooks/v1/useIsAdmin.js";
2025-06-13 09:51:26 +08:00
import {
useAddDemoMonitors,
useDeleteAllMonitors,
2025-06-13 14:04:08 +08:00
useDeleteMonitorStats,
2025-11-21 10:43:29 -08:00
useFetchJson,
2025-09-25 10:11:41 -07:00
} from "../../../Hooks/v1/monitorHooks.js";
2025-02-06 12:07:39 -08:00
// Constants
2025-05-09 13:28:44 -07:00
const BREADCRUMBS = [{ name: `Settings`, path: "/settings" }];
const Settings = () => {
2025-05-09 13:28:44 -07:00
// Redux state
const { mode, language, timezone, showURL } = useSelector((state) => state.ui);
2025-05-09 13:28:44 -07:00
// Local state
2025-05-09 14:54:28 -07:00
const [settingsData, setSettingsData] = useState({});
const [errors, setErrors] = useState({});
2025-06-17 10:58:41 +08:00
const [isApiKeySet, setIsApiKeySet] = useState(settingsData?.pagespeedKeySet ?? false);
const [apiKeyHasBeenReset, setApiKeyHasBeenReset] = useState(false);
const [isEmailPasswordSet, setIsEmailPasswordSet] = useState(
settingsData?.emailPasswordSet ?? false
);
const [emailPasswordHasBeenReset, setEmailPasswordHasBeenReset] = useState(false);
2025-05-09 13:28:44 -07:00
// Network
const [isSettingsLoading, settingsError] = useFetchSettings({
2025-05-09 14:54:28 -07:00
setSettingsData,
2025-06-17 10:58:41 +08:00
setIsApiKeySet,
setIsEmailPasswordSet,
2025-05-09 13:28:44 -07:00
});
2025-07-30 16:39:41 +05:30
2025-06-13 09:51:26 +08:00
const [addDemoMonitors, isAddingDemoMonitors] = useAddDemoMonitors();
2025-06-17 10:58:41 +08:00
const [isSaving, saveError, saveSettings] = useSaveSettings({
setSettingsData,
setIsApiKeySet,
setApiKeyHasBeenReset,
setIsEmailPasswordSet,
setEmailPasswordHasBeenReset,
});
2025-06-13 09:51:26 +08:00
const [deleteAllMonitors, isDeletingMonitors] = useDeleteAllMonitors();
2025-06-13 14:04:08 +08:00
const [deleteMonitorStats, isDeletingMonitorStats] = useDeleteMonitorStats();
2025-11-21 10:43:29 -08:00
const [fetchJson, isFetchingJson] = useFetchJson();
2025-02-06 12:07:39 -08:00
2025-05-09 13:28:44 -07:00
// Setup
const isAdmin = useIsAdmin();
2025-05-09 13:28:44 -07:00
const theme = useTheme();
const HEADING_SX = { mt: theme.spacing(2), mb: theme.spacing(2) };
2025-06-13 09:51:26 +08:00
const { t } = useTranslation();
2025-05-09 13:28:44 -07:00
const dispatch = useDispatch();
// Handlers
const handleChange = async (e) => {
2025-06-11 15:11:28 +05:30
const { name, value, checked } = e.target;
2026-01-10 21:49:10 +00:00
// eslint-disable-next-line no-console
console.log("=== SETTINGS HANDLER CHANGE ===", { name, value, checked, type: typeof value });
2026-01-10 22:02:36 +00:00
let newValue;
2025-06-11 15:29:25 +05:30
if (
name === "systemEmailIgnoreTLS" ||
name === "systemEmailRequireTLS" ||
2025-06-12 00:39:47 +05:30
name === "systemEmailRejectUnauthorized" ||
name === "systemEmailSecure" ||
name === "systemEmailPool"
2025-06-11 15:29:25 +05:30
) {
2025-06-11 15:11:28 +05:30
newValue = checked;
}
2025-05-09 13:28:44 -07:00
// Build next state early
2025-05-09 14:54:28 -07:00
const newSettingsData = {
...settingsData,
2025-06-11 15:11:28 +05:30
settings: { ...settingsData.settings, [name]: newValue ?? value },
2025-05-09 14:54:28 -07:00
};
2025-05-09 13:28:44 -07:00
if (name === "timezone") {
dispatch(setTimezone({ timezone: value }));
2025-06-18 09:43:41 +08:00
return;
2025-05-09 13:28:44 -07:00
}
2025-02-06 12:07:39 -08:00
2025-05-09 13:28:44 -07:00
if (name === "mode") {
dispatch(setMode(value));
2025-06-18 09:43:41 +08:00
return;
}
2025-05-09 13:28:44 -07:00
if (name === "language") {
dispatch(setLanguage(value));
2025-06-18 09:43:41 +08:00
return;
2025-05-09 13:28:44 -07:00
}
2024-09-03 10:37:26 -07:00
2025-05-09 13:28:44 -07:00
if (name === "deleteStats") {
2025-06-13 12:16:52 +08:00
await deleteMonitorStats();
2025-05-09 13:28:44 -07:00
return;
}
2024-08-30 10:04:47 -07:00
2025-05-09 13:28:44 -07:00
if (name === "demo") {
2025-06-13 09:51:26 +08:00
await addDemoMonitors();
2025-05-09 13:28:44 -07:00
return;
}
2024-07-20 20:48:59 -04:00
2025-05-09 13:28:44 -07:00
if (name === "deleteMonitors") {
2025-06-13 09:51:26 +08:00
await deleteAllMonitors();
2025-05-09 13:28:44 -07:00
return;
}
2025-11-21 10:43:29 -08:00
if (name === "export") {
const json = await fetchJson();
if (!json || json.length === 0) {
return;
}
const blob = new Blob([JSON.stringify(json, null, 2)], {
type: "application/json",
});
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "monitors.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
return;
}
2025-06-18 09:43:41 +08:00
// 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);
}
2025-05-09 14:54:28 -07:00
setSettingsData(newSettingsData);
2026-01-10 21:29:15 +00:00
// Update Redux immediately for UI feedback
if (name === "showURL") {
2026-01-10 21:58:13 +00:00
dispatch(setShowURL(value));
2026-01-10 21:29:15 +00:00
}
};
2025-05-07 13:53:45 -04:00
2025-05-09 13:28:44 -07:00
const handleSave = () => {
2026-01-10 21:49:10 +00:00
// eslint-disable-next-line no-console
console.log("=== HANDLE SAVE ===", { settingsDataSettings: settingsData?.settings });
2025-05-09 14:54:28 -07:00
const { error } = settingsValidation.validate(settingsData.settings, {
abortEarly: false,
});
2025-05-09 13:28:44 -07:00
if (!error || error.details.length === 0) {
setErrors({});
} else {
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
2025-07-30 16:39:41 +05:30
2025-05-09 13:28:44 -07:00
setErrors(newErrors);
}
2025-05-09 14:54:28 -07:00
saveSettings(settingsData?.settings);
2025-05-09 13:28:44 -07:00
};
2025-03-02 02:21:24 +03:00
return (
2025-05-09 13:28:44 -07:00
<Stack gap={theme.spacing(10)}>
<Breadcrumbs list={BREADCRUMBS} />
2025-06-24 14:30:14 +08:00
<Typography variant="h1">{t("settingsPage.title")}</Typography>
2025-05-09 13:28:44 -07:00
<SettingsTimeZone
HEADING_SX={HEADING_SX}
handleChange={handleChange}
timezone={timezone}
/>
<SettingsUI
HEADING_SX={HEADING_SX}
handleChange={handleChange}
mode={mode}
language={language}
/>
<SettingsPagespeed
isAdmin={isAdmin}
2025-05-09 13:28:44 -07:00
HEADING_SX={HEADING_SX}
2025-05-09 14:54:28 -07:00
settingsData={settingsData}
setSettingsData={setSettingsData}
2025-06-17 10:58:41 +08:00
isApiKeySet={isApiKeySet}
apiKeyHasBeenReset={apiKeyHasBeenReset}
setApiKeyHasBeenReset={setApiKeyHasBeenReset}
2025-05-09 13:28:44 -07:00
/>
<SettingsURL
HEADING_SX={HEADING_SX}
handleChange={handleChange}
showURL={showURL}
/>
2025-05-09 13:28:44 -07:00
<SettingsStats
isAdmin={isAdmin}
2025-05-09 13:28:44 -07:00
HEADING_SX={HEADING_SX}
2025-05-09 14:54:28 -07:00
settingsData={settingsData}
2025-05-09 13:28:44 -07:00
handleChange={handleChange}
errors={errors}
/>
2025-07-26 15:24:25 +05:30
<SettingsGlobalThresholds
isAdmin={isAdmin}
HEADING_SX={HEADING_SX}
settingsData={settingsData}
setSettingsData={setSettingsData}
/>
2025-05-09 13:28:44 -07:00
<SettingsDemoMonitors
isAdmin={isAdmin}
2025-05-09 14:54:28 -07:00
HEADER_SX={HEADING_SX}
handleChange={handleChange}
2025-06-13 09:51:26 +08:00
isLoading={
isSettingsLoading || isSaving || isDeletingMonitorStats || isAddingDemoMonitors
}
2025-05-09 14:54:28 -07:00
/>
<SettingsEmail
isAdmin={isAdmin}
2025-05-09 14:54:28 -07:00
HEADER_SX={HEADING_SX}
2025-05-09 13:28:44 -07:00
handleChange={handleChange}
2025-05-09 14:54:28 -07:00
settingsData={settingsData}
setSettingsData={setSettingsData}
2025-06-17 10:58:41 +08:00
isEmailPasswordSet={isEmailPasswordSet}
emailPasswordHasBeenReset={emailPasswordHasBeenReset}
setEmailPasswordHasBeenReset={setEmailPasswordHasBeenReset}
2025-05-09 13:28:44 -07:00
/>
2025-06-23 11:07:12 +08:00
2025-11-21 10:43:29 -08:00
<SettingsExport
isAdmin={isAdmin}
HEADER_SX={HEADING_SX}
handleChange={handleChange}
isLoading={isSettingsLoading || isSaving || isFetchingJson}
/>
2025-05-09 13:28:44 -07:00
<SettingsAbout />
<Stack
2025-05-09 13:28:44 -07:00
direction="row"
justifyContent="flex-end"
sx={{
position: "sticky",
bottom: 0,
boxShadow: theme.shape.boxShadow,
zIndex: 1000,
2025-06-08 08:14:17 +08:00
mt: 3,
backgroundColor: theme.palette.primary.main,
display: "flex",
justifyContent: "flex-end",
pb: theme.spacing(4),
pr: theme.spacing(15),
pl: theme.spacing(5),
pt: theme.spacing(4),
border: 1,
borderStyle: "solid",
borderColor: theme.palette.primary.lowContrast,
2025-06-08 08:14:17 +08:00
borderRadius: theme.spacing(2),
}}
>
2025-05-09 13:28:44 -07:00
<Button
2025-06-13 09:51:26 +08:00
loading={
isSaving || isDeletingMonitorStats || isSettingsLoading || isDeletingMonitors
}
2025-05-09 13:28:44 -07:00
disabled={Object.keys(errors).length > 0}
variant="contained"
color="accent"
sx={{ px: theme.spacing(12), py: theme.spacing(8) }}
2025-05-09 13:28:44 -07:00
onClick={handleSave}
>
2025-06-24 14:30:14 +08:00
{t("settingsPage.saveButtonLabel")}
2025-05-09 13:28:44 -07:00
</Button>
</Stack>
2025-05-09 13:28:44 -07:00
</Stack>
);
2024-06-18 21:01:41 -04:00
};
export default Settings;