refactor settings page
This commit is contained in:
1 parent
05f265743b
commit
08bd2d56f3
3 files changed
+86
-247
No files matched your search
@@ -18,7 +18,10 @@ interface FormAutocompleteProps<T extends FieldValues, O extends AutocompleteOpt
|
||||
placeholder?: string;
|
||||
}
|
||||
|
||||
export const FormAutocomplete = <T extends FieldValues, O extends AutocompleteOption>({
|
||||
export const FormAutocompleteField = <
|
||||
T extends FieldValues,
|
||||
O extends AutocompleteOption,
|
||||
>({
|
||||
name,
|
||||
options,
|
||||
fieldLabel,
|
||||
|
||||
@@ -13,11 +13,13 @@ import { SPACING } from "@/Utils/Theme/constants";
|
||||
interface FormSwitchProps<T extends FieldValues> {
|
||||
name: FieldPath<T>;
|
||||
label: string;
|
||||
labelPlacement?: "start" | "end"; // left | right
|
||||
}
|
||||
|
||||
export const FormSwitchField = <T extends FieldValues>({
|
||||
name,
|
||||
label,
|
||||
labelPlacement = "end",
|
||||
}: FormSwitchProps<T>) => {
|
||||
const { control } = useFormContext<T>();
|
||||
const theme = useTheme();
|
||||
@@ -29,14 +31,27 @@ export const FormSwitchField = <T extends FieldValues>({
|
||||
render={({ field }) => (
|
||||
<Stack
|
||||
direction="row"
|
||||
alignItems={"center"}
|
||||
spacing={theme.spacing(SPACING.LG)}
|
||||
alignItems="center"
|
||||
spacing={labelPlacement === "start" ? undefined : theme.spacing(SPACING.LG)}
|
||||
justifyContent={labelPlacement === "start" ? "space-between" : undefined}
|
||||
>
|
||||
{labelPlacement === "start" ? (
|
||||
<>
|
||||
<Typography>{label}</Typography>
|
||||
<SwitchComponent
|
||||
checked={field.value ?? false}
|
||||
onChange={(e) => field.onChange(e.target.checked)}
|
||||
/>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<SwitchComponent
|
||||
checked={field.value ?? false}
|
||||
onChange={(e) => field.onChange(e.target.checked)}
|
||||
/>
|
||||
<Typography>{label}</Typography>
|
||||
</>
|
||||
)}
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { BasePage, ConfigBox, TextLink } from "@/Components/design-elements";
|
||||
import { Autocomplete, Select, Dialog, SwitchComponent } from "@/Components/inputs";
|
||||
import { Autocomplete, Select, Dialog } from "@/Components/inputs";
|
||||
import { logger } from "@/Utils/logger";
|
||||
import { LAYOUT } from "@/Utils/Theme/constants";
|
||||
import {
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
type SelectChangeEvent,
|
||||
} from "@mui/material";
|
||||
import { useEffect } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { FormProvider, useForm, type FieldPath } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useSelector, useDispatch } from "react-redux";
|
||||
@@ -22,8 +22,7 @@ import { useSettingsForm } from "@/Hooks/useSettingsForm";
|
||||
import { useIsAdmin } from "@/Hooks/useIsAdmin.js";
|
||||
import type { SettingsFormData, SettingsFormInput } from "@/Validation/settings";
|
||||
import { useState } from "react";
|
||||
import { Controller } from "react-hook-form";
|
||||
import { TextField, Button, FieldLabel, SliderWithLabel } from "@/Components/inputs";
|
||||
import { Button, FieldLabel } from "@/Components/inputs";
|
||||
import { languageNames } from "@/Components/inputs/LanguageSelector";
|
||||
import { Box, Typography } from "@mui/material";
|
||||
import { useDelete } from "@/Hooks/UseApi";
|
||||
@@ -39,6 +38,9 @@ import {
|
||||
import timezones from "@/Utils/timezones.json";
|
||||
import type { RootState } from "@/Types/state";
|
||||
import { CHECK_TTL_SENTINEL } from "@/Types/Check";
|
||||
import { FormTextField } from "@/Components/inputs/forms/FormTextField";
|
||||
import { FormSliderField } from "@/Components/inputs/forms/FormSliderField";
|
||||
import { FormSwitchField } from "@/Components/inputs/forms/FormSwitchField";
|
||||
|
||||
interface Timezone {
|
||||
id: string;
|
||||
@@ -90,6 +92,26 @@ export const SettingsPage = () => {
|
||||
// Initialize form with schema and defaults
|
||||
const { schema, defaults } = useSettingsForm({ data: fetchedSettings?.settings });
|
||||
|
||||
const emailSwitches: { name: FieldPath<SettingsFormInput>; label: string }[] = [
|
||||
{
|
||||
name: "systemEmailSecure",
|
||||
label: t("pages.settings.form.email.option.secure.label"),
|
||||
},
|
||||
{ name: "systemEmailPool", label: t("pages.settings.form.email.option.pool.label") },
|
||||
{
|
||||
name: "systemEmailIgnoreTLS",
|
||||
label: t("pages.settings.form.email.option.ignoreTLS.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailRequireTLS",
|
||||
label: t("pages.settings.form.email.option.requireTLS.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailRejectUnauthorized",
|
||||
label: t("pages.settings.form.email.option.rejectUnauthorized.label"),
|
||||
},
|
||||
];
|
||||
|
||||
const form = useForm<SettingsFormInput, unknown, SettingsFormData>({
|
||||
resolver: zodResolver(schema),
|
||||
defaultValues: defaults,
|
||||
@@ -280,6 +302,7 @@ export const SettingsPage = () => {
|
||||
const languages = Object.keys(i18n.options.resources || {});
|
||||
|
||||
return (
|
||||
<FormProvider {...form}>
|
||||
<BasePage
|
||||
headerKey="settings"
|
||||
component="form"
|
||||
@@ -358,24 +381,13 @@ export const SettingsPage = () => {
|
||||
rightContent={
|
||||
<>
|
||||
{(isApiKeySet === false || apiKeyHasBeenReset === true) && (
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="pagespeedApiKey"
|
||||
control={form.control}
|
||||
defaultValue={defaults.pagespeedApiKey}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
fieldLabel={t(
|
||||
"pages.settings.form.pagespeed.option.apiKey.label"
|
||||
)}
|
||||
type="password"
|
||||
fieldLabel={t("pages.settings.form.pagespeed.option.apiKey.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.pagespeed.option.apiKey.placeholder"
|
||||
)}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
|
||||
@@ -403,29 +415,10 @@ export const SettingsPage = () => {
|
||||
title={t("pages.settings.form.url.title")}
|
||||
subtitle={t("pages.settings.form.url.description")}
|
||||
rightContent={
|
||||
<Controller
|
||||
<FormSwitchField
|
||||
name="showURL"
|
||||
control={form.control}
|
||||
defaultValue={defaults.showURL}
|
||||
render={({ field, fieldState }) => (
|
||||
<Select
|
||||
{...field}
|
||||
value={field.value === undefined ? "false" : field.value.toString()}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value === "true";
|
||||
field.onChange(value);
|
||||
}}
|
||||
fieldLabel={t("pages.settings.form.url.option.showURL.label")}
|
||||
error={!!fieldState.error}
|
||||
>
|
||||
<MenuItem value="true">
|
||||
{t("pages.settings.form.url.option.showURL.enabled")}
|
||||
</MenuItem>
|
||||
<MenuItem value="false">
|
||||
{t("pages.settings.form.url.option.showURL.disabled")}
|
||||
</MenuItem>
|
||||
</Select>
|
||||
)}
|
||||
label={t("pages.settings.form.url.option.showURL.label")}
|
||||
labelPlacement="start"
|
||||
/>
|
||||
}
|
||||
/>
|
||||
@@ -453,19 +446,11 @@ export const SettingsPage = () => {
|
||||
title={t("pages.settings.form.retention.title")}
|
||||
subtitle={t("pages.settings.form.retention.description")}
|
||||
rightContent={
|
||||
<Controller
|
||||
<FormSliderField
|
||||
name="checkTTL"
|
||||
control={form.control}
|
||||
defaultValue={defaults.checkTTL}
|
||||
render={({ field }) => (
|
||||
<SliderWithLabel
|
||||
{...field}
|
||||
fieldLabel={t("pages.settings.form.retention.option.days.label")}
|
||||
min={1}
|
||||
max={CHECK_TTL_SENTINEL}
|
||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||
value={field.value || 30}
|
||||
onChange={(_, value) => field.onChange(value)}
|
||||
valueLabelDisplay="auto"
|
||||
valueLabelFormat={(value: number) =>
|
||||
value >= CHECK_TTL_SENTINEL
|
||||
@@ -478,8 +463,6 @@ export const SettingsPage = () => {
|
||||
: `${value}`
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
@@ -491,75 +474,36 @@ export const SettingsPage = () => {
|
||||
subtitle={t("pages.settings.form.thresholds.description")}
|
||||
rightContent={
|
||||
<Stack spacing={2}>
|
||||
<Controller
|
||||
<FormSliderField
|
||||
name="globalThresholds.cpu"
|
||||
control={form.control}
|
||||
defaultValue={defaults.globalThresholds?.cpu}
|
||||
render={({ field }) => (
|
||||
<SliderWithLabel
|
||||
{...field}
|
||||
fieldLabel={t("pages.settings.form.thresholds.option.cpu.label")}
|
||||
valueLabelDisplay="auto"
|
||||
min={1}
|
||||
max={100}
|
||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||
value={field.value || 1}
|
||||
onChange={(_, value) => field.onChange(value)}
|
||||
valueLabelDisplay="auto"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
<FormSliderField
|
||||
name="globalThresholds.memory"
|
||||
control={form.control}
|
||||
defaultValue={defaults.globalThresholds?.memory}
|
||||
render={({ field }) => (
|
||||
<SliderWithLabel
|
||||
{...field}
|
||||
fieldLabel={t("pages.settings.form.thresholds.option.memory.label")}
|
||||
valueLabelDisplay="auto"
|
||||
min={1}
|
||||
max={100}
|
||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||
value={field.value || 1}
|
||||
onChange={(_, value) => field.onChange(value)}
|
||||
valueLabelDisplay="auto"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
<FormSliderField
|
||||
name="globalThresholds.disk"
|
||||
control={form.control}
|
||||
defaultValue={defaults.globalThresholds?.disk}
|
||||
render={({ field }) => (
|
||||
<SliderWithLabel
|
||||
{...field}
|
||||
fieldLabel={t("pages.settings.form.thresholds.option.disk.label")}
|
||||
valueLabelDisplay="auto"
|
||||
min={1}
|
||||
max={100}
|
||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||
value={field.value || 1}
|
||||
onChange={(_, value) => field.onChange(value)}
|
||||
valueLabelDisplay="auto"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
|
||||
<FormSliderField
|
||||
name="globalThresholds.temperature"
|
||||
control={form.control}
|
||||
defaultValue={defaults.globalThresholds?.temperature}
|
||||
render={({ field }) => (
|
||||
<SliderWithLabel
|
||||
{...field}
|
||||
fieldLabel={t(
|
||||
"pages.settings.form.thresholds.option.temperature.label"
|
||||
)}
|
||||
valueLabelDisplay="auto"
|
||||
min={1}
|
||||
max={150}
|
||||
sliderMaxWidth={{ xs: "100%", md: "50%" }}
|
||||
value={field.value || 1}
|
||||
onChange={(_, value) => field.onChange(value)}
|
||||
valueLabelDisplay="auto"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</Stack>
|
||||
}
|
||||
@@ -605,7 +549,8 @@ export const SettingsPage = () => {
|
||||
user: form.watch("systemEmailUser") || address,
|
||||
pass: "<your_password>",
|
||||
},
|
||||
name: form.watch("systemEmailConnectionHost") || "localhost",
|
||||
name:
|
||||
form.watch("systemEmailConnectionHost") || "localhost",
|
||||
pool: form.watch("systemEmailPool") ?? false,
|
||||
tls: {
|
||||
rejectUnauthorized:
|
||||
@@ -644,98 +589,41 @@ export const SettingsPage = () => {
|
||||
rightContent={
|
||||
<Stack gap={theme.spacing(LAYOUT.MD)}>
|
||||
{/* Email Host */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailHost"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.host.label")}
|
||||
placeholder={t("pages.settings.form.email.option.host.placeholder")}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Email Port */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailPort"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
name={field.name}
|
||||
ref={field.ref}
|
||||
onBlur={field.onBlur}
|
||||
value={
|
||||
field.value === undefined || field.value === 0 ? "" : field.value
|
||||
}
|
||||
onChange={(e) => {
|
||||
const val = e.target.value;
|
||||
field.onChange(val === "" ? 0 : Number(val));
|
||||
}}
|
||||
fieldLabel={t("pages.settings.form.email.option.port.label")}
|
||||
type="number"
|
||||
inputProps={{ min: 0 }}
|
||||
placeholder={t("pages.settings.form.email.option.port.placeholder")}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Email Address */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailAddress"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.address.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.email.option.address.placeholder"
|
||||
)}
|
||||
type="email"
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
fieldLabel={t("pages.settings.form.email.option.address.label")}
|
||||
placeholder={t("pages.settings.form.email.option.address.placeholder")}
|
||||
/>
|
||||
|
||||
{/* Email Display Name (Optional) */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailDisplayName"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.displayName.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.email.option.displayName.placeholder"
|
||||
)}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Email User (Optional) */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailUser"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.user.label")}
|
||||
placeholder={t("pages.settings.form.email.option.user.placeholder")}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Email Password with Reset Pattern */}
|
||||
@@ -760,114 +648,46 @@ export const SettingsPage = () => {
|
||||
</Stack>
|
||||
</Box>
|
||||
) : (
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailPassword"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.password.label")}
|
||||
type="password"
|
||||
fieldLabel={t("pages.settings.form.email.option.password.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.email.option.password.placeholder"
|
||||
)}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* TLS Servername (Optional) */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailTLSServername"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t("pages.settings.form.email.option.tlsServername.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.email.option.tlsServername.placeholder"
|
||||
)}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Connection Host (Optional) */}
|
||||
<Controller
|
||||
<FormTextField
|
||||
name="systemEmailConnectionHost"
|
||||
control={form.control}
|
||||
render={({ field, fieldState }) => (
|
||||
<TextField
|
||||
{...field}
|
||||
value={field.value ?? ""}
|
||||
fieldLabel={t(
|
||||
"pages.settings.form.email.option.connectionHost.label"
|
||||
)}
|
||||
fieldLabel={t("pages.settings.form.email.option.connectionHost.label")}
|
||||
placeholder={t(
|
||||
"pages.settings.form.email.option.connectionHost.placeholder"
|
||||
)}
|
||||
error={!!fieldState.error}
|
||||
helperText={fieldState.error?.message}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
|
||||
{/* Boolean Switches */}
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
gap: theme.spacing(LAYOUT.XS),
|
||||
}}
|
||||
display={"flex"}
|
||||
flexDirection={"column"}
|
||||
gap={theme.spacing(LAYOUT.XS)}
|
||||
>
|
||||
{[
|
||||
{
|
||||
name: "systemEmailSecure",
|
||||
label: t("pages.settings.form.email.option.secure.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailPool",
|
||||
label: t("pages.settings.form.email.option.pool.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailIgnoreTLS",
|
||||
label: t("pages.settings.form.email.option.ignoreTLS.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailRequireTLS",
|
||||
label: t("pages.settings.form.email.option.requireTLS.label"),
|
||||
},
|
||||
{
|
||||
name: "systemEmailRejectUnauthorized",
|
||||
label: t("pages.settings.form.email.option.rejectUnauthorized.label"),
|
||||
},
|
||||
].map(({ name, label }) => (
|
||||
<Controller
|
||||
{emailSwitches.map(({ name, label }) => (
|
||||
<FormSwitchField
|
||||
key={name}
|
||||
name={name as any}
|
||||
control={form.control}
|
||||
render={({ field }) => (
|
||||
<Box
|
||||
sx={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "space-between",
|
||||
}}
|
||||
>
|
||||
<Typography>{label}</Typography>
|
||||
<SwitchComponent
|
||||
checked={field.value ?? false}
|
||||
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
|
||||
field.onChange(e.target.checked)
|
||||
}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
name={name}
|
||||
label={label}
|
||||
labelPlacement="start"
|
||||
/>
|
||||
))}
|
||||
</Box>
|
||||
@@ -1078,6 +898,7 @@ export const SettingsPage = () => {
|
||||
</Button>
|
||||
</Stack>
|
||||
</BasePage>
|
||||
</FormProvider>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user