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

253 lines
7.5 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 SettingsGlobalping from "./SettingsGlobalping.jsx";
2025-09-25 10:11:41 -07:00
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
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { useSettingsForm } from "@/Hooks/useSettingsForm.js";
2026-02-09 23:28:46 +00:00
import { useState, useEffect } from "react";
2025-05-09 13:28:44 -07:00
import { useTheme } from "@emotion/react";
import { useTranslation } from "react-i18next";
import { useSelector, useDispatch } from "react-redux";
2025-09-25 10:11:41 -07:00
import SettingsStats from "./SettingsStats.jsx";
2025-06-13 09:51:26 +08:00
2026-01-12 23:57:39 +00:00
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
2026-02-12 17:26:17 +00:00
import { useGet, usePost, useDelete, useLazyGet, usePatch } from "@/Hooks/UseApi";
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 = () => {
const { data: fetchedSettings, isLoading: isSettingsLoading } = useGet("/settings");
2025-05-09 13:28:44 -07:00
// Local state
const [isApiKeySet, setIsApiKeySet] = useState(
fetchedSettings?.pagespeedKeySet ?? false
);
2025-06-17 10:58:41 +08:00
const [apiKeyHasBeenReset, setApiKeyHasBeenReset] = useState(false);
const [isGlobalpingKeySet, setIsGlobalpingKeySet] = useState(
fetchedSettings?.globalpingKeySet ?? false
);
const [globalpingKeyHasBeenReset, setGlobalpingKeyHasBeenReset] = useState(false);
2025-06-17 10:58:41 +08:00
const [isEmailPasswordSet, setIsEmailPasswordSet] = useState(
fetchedSettings?.emailPasswordSet ?? false
2025-06-17 10:58:41 +08:00
);
const [emailPasswordHasBeenReset, setEmailPasswordHasBeenReset] = useState(false);
2025-05-09 13:28:44 -07:00
// Network
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
const form = useForm({
resolver: zodResolver(schema),
defaultValues: defaults,
});
const {
control,
watch,
reset,
handleSubmit,
clearErrors,
trigger,
getValues,
setValue,
} = form;
useEffect(() => {
2026-02-18 18:47:58 +00:00
reset(defaults, { keepDirtyValues: false });
}, [defaults, reset]);
2025-07-30 16:39:41 +05:30
2026-02-09 23:28:46 +00:00
useEffect(() => {
if (fetchedSettings) {
setIsApiKeySet(fetchedSettings?.pagespeedKeySet);
setIsGlobalpingKeySet(fetchedSettings?.globalpingKeySet);
2026-02-09 23:28:46 +00:00
setIsEmailPasswordSet(fetchedSettings?.emailPasswordSet);
}
}, [fetchedSettings]);
2026-02-12 17:26:17 +00:00
const { patch: saveSettingsFn, loading: isSaving } = usePatch();
2026-02-09 23:28:46 +00:00
const saveSettings = async (settings) => {
2026-02-12 17:26:17 +00:00
const response = await saveSettingsFn("/settings", settings);
2026-02-09 23:28:46 +00:00
if (response?.data) {
const data = response.data;
setIsApiKeySet(data.pagespeedKeySet);
setIsGlobalpingKeySet(data.globalpingKeySet);
2026-02-09 23:28:46 +00:00
setIsEmailPasswordSet(data.emailPasswordSet);
if (data.pagespeedKeySet === true) {
setApiKeyHasBeenReset(false);
}
if (data.globalpingKeySet === true) {
setGlobalpingKeyHasBeenReset(false);
}
2026-02-09 23:28:46 +00:00
if (data.emailPasswordSet === true) {
setEmailPasswordHasBeenReset(false);
}
}
};
2026-02-09 23:16:14 +00:00
const onSubmit = async (data) => {
2026-02-18 18:47:58 +00:00
// Convert undefined to empty strings for proper unsetting on backend
const toSubmit = Object.fromEntries(
Object.entries(data).map(([key, value]) => {
if (value === undefined) return [key, ""];
if (typeof value === "object" && value !== null) {
// Handle nested objects like globalThresholds
return [
key,
Object.fromEntries(
Object.entries(value).map(([k, v]) => [k, v === undefined ? "" : v])
),
];
}
return [key, value];
})
);
// Don't send empty sensitive fields when the key is already set and
// hasn't been explicitly reset — otherwise the backend $unsets them
if (isApiKeySet && !apiKeyHasBeenReset && !toSubmit.pagespeedApiKey) {
delete toSubmit.pagespeedApiKey;
}
if (isGlobalpingKeySet && !globalpingKeyHasBeenReset && !toSubmit.globalpingApiKey) {
delete toSubmit.globalpingApiKey;
}
if (
isEmailPasswordSet &&
!emailPasswordHasBeenReset &&
!toSubmit.systemEmailPassword
) {
delete toSubmit.systemEmailPassword;
}
saveSettings(toSubmit);
};
2026-02-09 23:16:14 +00:00
// New API hooks to replace monitorHooks
const { loading: isAddingDemoMonitors } = usePost();
const { loading: isDeletingMonitors } = useDelete();
const { loading: isDeletingMonitorStats } = useDelete();
2026-02-09 23:16:14 +00:00
const { get: fetchJson, loading: isFetchingJson } = useLazyGet();
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();
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} />
2026-02-16 00:47:23 -05:00
<Typography variant="h1">{t("pages.settings.title")}</Typography>
<SettingsTimeZone HEADING_SX={HEADING_SX} />
<SettingsUI HEADING_SX={HEADING_SX} />
2025-05-09 13:28:44 -07:00
<SettingsPagespeed
isAdmin={isAdmin}
2025-05-09 13:28:44 -07:00
HEADING_SX={HEADING_SX}
2025-06-17 10:58:41 +08:00
isApiKeySet={isApiKeySet}
apiKeyHasBeenReset={apiKeyHasBeenReset}
setApiKeyHasBeenReset={setApiKeyHasBeenReset}
control={control}
defaults={defaults}
setValue={setValue}
2025-05-09 13:28:44 -07:00
/>
<SettingsGlobalping
isAdmin={isAdmin}
HEADING_SX={HEADING_SX}
isGlobalpingKeySet={isGlobalpingKeySet}
globalpingKeyHasBeenReset={globalpingKeyHasBeenReset}
setGlobalpingKeyHasBeenReset={setGlobalpingKeyHasBeenReset}
control={control}
defaults={defaults}
setValue={setValue}
/>
<SettingsURL
HEADING_SX={HEADING_SX}
control={control}
defaults={defaults}
/>
2025-05-09 13:28:44 -07:00
<SettingsStats
isAdmin={isAdmin}
2025-05-09 13:28:44 -07:00
HEADING_SX={HEADING_SX}
/>
2025-07-26 15:24:25 +05:30
<SettingsGlobalThresholds
isAdmin={isAdmin}
HEADING_SX={HEADING_SX}
control={control}
defaults={defaults}
2025-07-26 15:24:25 +05:30
/>
2025-05-09 13:28:44 -07:00
<SettingsDemoMonitors
isAdmin={isAdmin}
2025-05-09 14:54:28 -07:00
HEADER_SX={HEADING_SX}
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-06-17 10:58:41 +08:00
isEmailPasswordSet={isEmailPasswordSet}
emailPasswordHasBeenReset={emailPasswordHasBeenReset}
setEmailPasswordHasBeenReset={setEmailPasswordHasBeenReset}
control={control}
defaults={defaults}
formValues={watch()}
setValue={setValue}
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}
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
}
2026-02-18 18:47:58 +00:00
disabled={!form.formState.isValid}
2025-05-09 13:28:44 -07:00
variant="contained"
color="accent"
sx={{ px: theme.spacing(12), py: theme.spacing(8) }}
onClick={handleSubmit(onSubmit)}
>
2026-02-16 00:47:23 -05:00
{t("pages.settings.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;