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:14:35 +00:00
|
|
|
import { useGet } from "@/Hooks/UseApi";
|
|
|
|
|
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";
|
|
|
|
|
import { TextField, Button } from "@/Components/v2/inputs";
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
|
|
// 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 19:59:03 +00:00
|
|
|
const languages = Object.keys(i18n.options.resources || {});
|
|
|
|
|
|
|
|
|
|
return (
|
2026-02-18 20:14:35 +00:00
|
|
|
<BasePage component="form">
|
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>
|
|
|
|
|
<Typography sx={{ mb: theme.spacing(4) }}>
|
|
|
|
|
{t("pages.settings.pageSpeedSettings.labelApiKeySet")}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleResetApiKey}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="error"
|
|
|
|
|
>
|
|
|
|
|
{t("common.buttons.reset")}
|
|
|
|
|
</Button>
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
justifyContent="flex-end"
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
loading={false}
|
|
|
|
|
type="submit"
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
>
|
|
|
|
|
{t("common.buttons.save")}
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
2026-02-18 19:59:03 +00:00
|
|
|
</Stack>
|
|
|
|
|
</BasePage>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SettingsPage;
|