2026-02-18 19:59:03 +00:00
|
|
|
import { BasePage, ConfigBox } from "@/Components/v2/design-elements";
|
|
|
|
|
import { Autocomplete, Select } from "@/Components/v2/inputs";
|
|
|
|
|
import { Stack, useTheme, MenuItem, type SelectChangeEvent } from "@mui/material";
|
2026-02-18 20:14:35 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
2026-02-18 19:59:03 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { useSelector, useDispatch } from "react-redux";
|
|
|
|
|
import DummyChart from "@/Pages/Settings/DummyChart";
|
2026-02-18 20:35:46 +00:00
|
|
|
import { useGet, usePatch } from "@/Hooks/UseApi";
|
2026-02-18 20:14:35 +00:00
|
|
|
import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
2026-02-18 20:20:45 +00:00
|
|
|
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
2026-02-18 20:14:35 +00:00
|
|
|
import type { SettingsFormData } from "@/Validation/settings";
|
2026-02-18 20:20:45 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { Controller } from "react-hook-form";
|
2026-02-18 20:35:46 +00:00
|
|
|
import { TextField, FieldLabel, Button } from "@/Components/v2/inputs";
|
2026-02-18 20:20:45 +00:00
|
|
|
import { Typography, Box } from "@mui/material";
|
2026-02-18 19:59:03 +00:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
setTimezone,
|
|
|
|
|
setMode,
|
|
|
|
|
setLanguage,
|
|
|
|
|
setChartType,
|
|
|
|
|
type ThemeMode,
|
|
|
|
|
type ChartType,
|
|
|
|
|
} from "@/Features/UI/uiSlice.js";
|
|
|
|
|
import timezones from "@/Utils/timezones.json";
|
|
|
|
|
import type { RootState } from "@/Types/state";
|
|
|
|
|
|
|
|
|
|
interface Timezone {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 20:20:45 +00:00
|
|
|
interface SettingsResponse {
|
|
|
|
|
settings: any;
|
|
|
|
|
pagespeedKeySet: boolean;
|
|
|
|
|
emailPasswordSet: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 19:59:03 +00:00
|
|
|
export const SettingsPage = () => {
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const { t, i18n } = useTranslation();
|
|
|
|
|
const dispatch = useDispatch();
|
2026-02-18 20:20:45 +00:00
|
|
|
const isAdmin = useIsAdmin();
|
2026-02-18 20:14:35 +00:00
|
|
|
|
|
|
|
|
// Fetch settings data from API
|
2026-02-18 20:20:45 +00:00
|
|
|
const { data: fetchedSettings } = useGet<SettingsResponse>("/settings");
|
2026-02-18 20:35:46 +00:00
|
|
|
// Form submission
|
|
|
|
|
const { patch, loading: isSaving } = usePatch<SettingsFormData, SettingsResponse>();
|
2026-02-18 20:20:45 +00:00
|
|
|
|
|
|
|
|
// Local state for API key reset
|
|
|
|
|
const [isApiKeySet, setIsApiKeySet] = useState(
|
|
|
|
|
fetchedSettings?.pagespeedKeySet ?? false
|
|
|
|
|
);
|
|
|
|
|
const [apiKeyHasBeenReset, setApiKeyHasBeenReset] = useState(false);
|
2026-02-18 20:14:35 +00:00
|
|
|
|
|
|
|
|
// Initialize form with schema and defaults
|
|
|
|
|
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
|
|
|
|
|
|
|
|
|
const form = useForm<SettingsFormData>({
|
|
|
|
|
resolver: zodResolver(schema),
|
|
|
|
|
defaultValues: defaults,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Reset form when defaults change
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
form.reset(defaults);
|
|
|
|
|
}, [defaults, form]);
|
|
|
|
|
|
2026-02-18 20:20:45 +00:00
|
|
|
// Update isApiKeySet when fetchedSettings changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (fetchedSettings) {
|
|
|
|
|
setIsApiKeySet(fetchedSettings.pagespeedKeySet);
|
|
|
|
|
}
|
|
|
|
|
}, [fetchedSettings]);
|
|
|
|
|
|
2026-02-18 19:59:03 +00:00
|
|
|
const {
|
|
|
|
|
timezone: selectedTimezoneId,
|
|
|
|
|
mode,
|
|
|
|
|
language = "en",
|
|
|
|
|
chartType = "histogram",
|
|
|
|
|
} = useSelector((state: RootState) => state.ui);
|
|
|
|
|
|
|
|
|
|
// Convert timezones to match AutoComplete format (id instead of _id)
|
|
|
|
|
const timezoneOptions: Timezone[] = timezones.map((tz) => ({
|
|
|
|
|
id: tz._id,
|
|
|
|
|
name: tz.name,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const selectedTimezone =
|
|
|
|
|
timezoneOptions.find((tz) => tz.id === selectedTimezoneId) ?? null;
|
|
|
|
|
|
|
|
|
|
const handleTimezoneChange = (newValue: Timezone | null) => {
|
|
|
|
|
const newId = newValue?.id ?? "";
|
|
|
|
|
dispatch(setTimezone({ timezone: newId }));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleModeChange = (e: SelectChangeEvent<ThemeMode>) => {
|
|
|
|
|
dispatch(setMode(e.target.value));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleLanguageChange = (e: SelectChangeEvent<string>) => {
|
|
|
|
|
dispatch(setLanguage(e.target.value));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChartTypeChange = (e: SelectChangeEvent<ChartType>) => {
|
|
|
|
|
dispatch(setChartType(e.target.value));
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-18 20:20:45 +00:00
|
|
|
const handleResetApiKey = () => {
|
|
|
|
|
form.setValue("pagespeedApiKey", "");
|
|
|
|
|
setApiKeyHasBeenReset(true);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-18 20:35:46 +00:00
|
|
|
const onSubmit = async (data: SettingsFormData) => {
|
|
|
|
|
// Convert undefined to empty string for backend unsetting
|
|
|
|
|
const processedData = Object.entries(data).reduce((acc, [key, value]) => {
|
|
|
|
|
const typedKey = key as keyof SettingsFormData;
|
|
|
|
|
if (value === undefined) {
|
|
|
|
|
(acc as any)[typedKey] = "";
|
|
|
|
|
} else if (typeof value === "object" && value !== null) {
|
|
|
|
|
// Handle nested objects like globalThresholds
|
|
|
|
|
(acc as any)[typedKey] = Object.entries(value).reduce(
|
|
|
|
|
(nested, [nestedKey, nestedValue]) => ({
|
|
|
|
|
...nested,
|
|
|
|
|
[nestedKey]: nestedValue === undefined ? "" : nestedValue,
|
|
|
|
|
}),
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
(acc as any)[typedKey] = value;
|
|
|
|
|
}
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as Partial<SettingsFormData>);
|
|
|
|
|
|
|
|
|
|
const result = await patch("/settings", processedData as SettingsFormData);
|
|
|
|
|
|
|
|
|
|
if (result?.success) {
|
|
|
|
|
// Update API key state from response
|
|
|
|
|
if (result.data) {
|
|
|
|
|
setIsApiKeySet(result.data.pagespeedKeySet);
|
|
|
|
|
setApiKeyHasBeenReset(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onError = (errors: unknown) => {
|
|
|
|
|
console.log("Form validation errors:", errors);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-18 19:59:03 +00:00
|
|
|
const languages = Object.keys(i18n.options.resources || {});
|
|
|
|
|
|
|
|
|
|
return (
|
2026-02-18 20:35:46 +00:00
|
|
|
<BasePage
|
|
|
|
|
component="form"
|
|
|
|
|
onSubmit={form.handleSubmit(onSubmit, onError)}
|
|
|
|
|
>
|
2026-02-18 19:59:03 +00:00
|
|
|
<Stack gap={theme.spacing(8)}>
|
|
|
|
|
<ConfigBox
|
|
|
|
|
title={t("pages.settings.form.timezone.title")}
|
|
|
|
|
subtitle={t("pages.settings.form.timezone.description")}
|
|
|
|
|
rightContent={
|
|
|
|
|
<Autocomplete
|
|
|
|
|
value={selectedTimezone}
|
|
|
|
|
options={timezoneOptions}
|
|
|
|
|
getOptionLabel={(option: Timezone) => option.name}
|
|
|
|
|
isOptionEqualToValue={(option: Timezone, value: Timezone) =>
|
|
|
|
|
option.id === value.id
|
|
|
|
|
}
|
|
|
|
|
onChange={(_, newValue) => {
|
|
|
|
|
handleTimezoneChange(newValue as Timezone | null);
|
|
|
|
|
}}
|
|
|
|
|
fieldLabel={t("pages.settings.form.timezone.option.timezone.label")}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<ConfigBox
|
|
|
|
|
title={t("pages.settings.form.ui.title")}
|
|
|
|
|
subtitle={t("pages.settings.form.ui.description")}
|
|
|
|
|
rightContent={
|
|
|
|
|
<Stack gap={theme.spacing(8)}>
|
|
|
|
|
<Select
|
|
|
|
|
value={mode}
|
|
|
|
|
onChange={handleModeChange}
|
|
|
|
|
fieldLabel={t("pages.settings.form.ui.option.theme.label")}
|
|
|
|
|
>
|
|
|
|
|
<MenuItem value="light">
|
|
|
|
|
{t("pages.settings.form.ui.option.theme.light")}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
<MenuItem value="dark">
|
|
|
|
|
{t("pages.settings.form.ui.option.theme.dark")}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
</Select>
|
|
|
|
|
<Select
|
|
|
|
|
value={language}
|
|
|
|
|
onChange={handleLanguageChange}
|
|
|
|
|
fieldLabel={t("pages.settings.form.ui.option.language.label")}
|
|
|
|
|
>
|
|
|
|
|
{languages.map((lang) => (
|
|
|
|
|
<MenuItem
|
|
|
|
|
key={lang}
|
|
|
|
|
value={lang}
|
|
|
|
|
>
|
|
|
|
|
{lang.toUpperCase()}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
))}
|
|
|
|
|
</Select>
|
|
|
|
|
<Select
|
|
|
|
|
value={chartType}
|
|
|
|
|
onChange={handleChartTypeChange}
|
|
|
|
|
fieldLabel={t("pages.settings.form.ui.option.chartType.label")}
|
|
|
|
|
>
|
|
|
|
|
<MenuItem value="histogram">
|
|
|
|
|
{t("pages.settings.form.ui.option.chartType.histogram")}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
<MenuItem value="heatmap">
|
|
|
|
|
{t("pages.settings.form.ui.option.chartType.heatmap")}
|
|
|
|
|
</MenuItem>
|
|
|
|
|
</Select>
|
|
|
|
|
<DummyChart chartType={chartType} />
|
|
|
|
|
</Stack>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2026-02-18 20:20:45 +00:00
|
|
|
{isAdmin && (
|
|
|
|
|
<ConfigBox
|
|
|
|
|
title={t("pages.settings.form.pagespeed.title")}
|
|
|
|
|
subtitle={t("pages.settings.form.pagespeed.description")}
|
|
|
|
|
rightContent={
|
|
|
|
|
<>
|
|
|
|
|
{(isApiKeySet === false || apiKeyHasBeenReset === true) && (
|
|
|
|
|
<Controller
|
|
|
|
|
name="pagespeedApiKey"
|
|
|
|
|
control={form.control}
|
|
|
|
|
defaultValue={defaults.pagespeedApiKey}
|
|
|
|
|
render={({ field, fieldState }) => (
|
|
|
|
|
<TextField
|
|
|
|
|
{...field}
|
|
|
|
|
fieldLabel={t(
|
|
|
|
|
"pages.settings.form.pagespeed.option.apiKey.label"
|
|
|
|
|
)}
|
|
|
|
|
type="password"
|
|
|
|
|
placeholder={t(
|
|
|
|
|
"pages.settings.form.pagespeed.option.apiKey.placeholder"
|
|
|
|
|
)}
|
|
|
|
|
error={!!fieldState.error}
|
|
|
|
|
helperText={fieldState.error?.message}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{isApiKeySet === true && apiKeyHasBeenReset === false && (
|
|
|
|
|
<Box>
|
2026-02-18 20:35:46 +00:00
|
|
|
<FieldLabel>
|
|
|
|
|
{t("pages.settings.form.pagespeed.option.apiKey.labelSet")}
|
|
|
|
|
</FieldLabel>
|
2026-02-18 20:20:45 +00:00
|
|
|
<Button
|
|
|
|
|
onClick={handleResetApiKey}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="error"
|
|
|
|
|
>
|
|
|
|
|
{t("common.buttons.reset")}
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2026-02-18 20:35:46 +00:00
|
|
|
</Stack>
|
|
|
|
|
|
|
|
|
|
{/* Sticky Save Button */}
|
|
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
justifyContent="flex-end"
|
|
|
|
|
sx={{
|
|
|
|
|
position: "sticky",
|
|
|
|
|
bottom: 0,
|
|
|
|
|
backgroundColor: theme.palette.background.paper,
|
|
|
|
|
borderTop: `1px solid ${theme.palette.divider}`,
|
|
|
|
|
padding: theme.spacing(8),
|
|
|
|
|
marginLeft: theme.spacing(-8),
|
|
|
|
|
marginRight: theme.spacing(-8),
|
|
|
|
|
marginBottom: theme.spacing(-8),
|
|
|
|
|
zIndex: 1000,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
loading={isSaving}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
disabled={!form.formState.isValid}
|
2026-02-18 20:20:45 +00:00
|
|
|
>
|
2026-02-18 20:35:46 +00:00
|
|
|
{t("common.buttons.save")}
|
|
|
|
|
</Button>
|
2026-02-18 19:59:03 +00:00
|
|
|
</Stack>
|
|
|
|
|
</BasePage>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SettingsPage;
|