refacotr notification create page

This commit is contained in:
Alex Holliday committed 2026-08-06 11:54:46 -07:00
1 parent c1e9fd6079
commit bc2115f578
4 files changed
+117 -237

No files matched your search

@@ -0,0 +1,43 @@
import {
Controller,
useFormContext,
type FieldPath,
type FieldValues,
} from "react-hook-form";
import dayjs from "dayjs";
import { DatePicker } from "@/Components/inputs";
interface FormDatePickerFieldProps<T extends FieldValues> {
name: FieldPath<T>;
fieldLabel?: string;
format?: string;
onValueChange?: (value: string) => void; // e.g. () => trigger("startDate")
}
export const FormDatePickerField = <T extends FieldValues>({
name,
fieldLabel,
format = "YYYY-MM-DD",
onValueChange,
}: FormDatePickerFieldProps<T>) => {
const { control } = useFormContext<T>();
return (
<Controller
name={name}
control={control}
render={({ field, fieldState }) => (
<DatePicker
fieldLabel={fieldLabel}
value={field.value ? dayjs(field.value, format) : null}
onChange={(date) => {
const next = date ? date.format(format) : "";
field.onChange(next);
onValueChange?.(next);
}}
error={!!fieldState.error}
helperText={fieldState.error?.message}
/>
)}
/>
);
};
+24 -11
View File
@@ -8,10 +8,23 @@ interface UseNotificationFormOptions {
} }
function buildDefaults(data: Notification | null): NotificationFormData { function buildDefaults(data: Notification | null): NotificationFormData {
// Every channel field defaults to "" (not undefined) so untouched required
// fields fail zod with their custom messages instead of a type error.
const base = {
notificationName: data?.notificationName || "",
address: "",
accessToken: "",
accountSid: "",
phone: "",
twilioPhoneNumber: "",
homeserverUrl: "",
roomId: "",
topic: "",
};
if (data?.type === "matrix") { if (data?.type === "matrix") {
return { return {
...base,
type: "matrix", type: "matrix",
notificationName: data.notificationName || "",
homeserverUrl: data.homeserverUrl || "", homeserverUrl: data.homeserverUrl || "",
roomId: data.roomId || "", roomId: data.roomId || "",
accessToken: data.accessToken || "", accessToken: data.accessToken || "",
@@ -19,51 +32,51 @@ function buildDefaults(data: Notification | null): NotificationFormData {
} }
if (data?.type === "telegram") { if (data?.type === "telegram") {
return { return {
...base,
type: "telegram", type: "telegram",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
accessToken: data.accessToken || "", accessToken: data.accessToken || "",
}; };
} }
if (data?.type === "slack") { if (data?.type === "slack") {
return { return {
...base,
type: "slack", type: "slack",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
}; };
} }
if (data?.type === "discord") { if (data?.type === "discord") {
return { return {
...base,
type: "discord", type: "discord",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
}; };
} }
if (data?.type === "webhook") { if (data?.type === "webhook") {
return { return {
...base,
type: "webhook", type: "webhook",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
}; };
} }
if (data?.type === "pager_duty") { if (data?.type === "pager_duty") {
return { return {
...base,
type: "pager_duty", type: "pager_duty",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
}; };
} }
if (data?.type === "teams") { if (data?.type === "teams") {
return { return {
...base,
type: "teams", type: "teams",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
}; };
} }
if (data?.type === "twilio") { if (data?.type === "twilio") {
return { return {
...base,
type: "twilio", type: "twilio",
notificationName: data.notificationName || "",
accountSid: data.accountSid || "", accountSid: data.accountSid || "",
accessToken: data.accessToken || "", accessToken: data.accessToken || "",
phone: data.phone || "", phone: data.phone || "",
@@ -72,24 +85,24 @@ function buildDefaults(data: Notification | null): NotificationFormData {
} }
if (data?.type === "pushover") { if (data?.type === "pushover") {
return { return {
...base,
type: "pushover", type: "pushover",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
accessToken: data.accessToken || "", accessToken: data.accessToken || "",
}; };
} }
if (data?.type === "ntfy") { if (data?.type === "ntfy") {
return { return {
...base,
type: "ntfy", type: "ntfy",
notificationName: data.notificationName || "",
address: data.address || "", address: data.address || "",
topic: data.topic || "", topic: data.topic || "",
}; };
} }
// Default: email (covers both data === null and data.type === "email") // Default: email (covers both data === null and data.type === "email")
return { return {
...base,
type: "email", type: "email",
notificationName: data?.notificationName || "",
address: data?.address || "", address: data?.address || "",
}; };
} }
+36 -225
View File
@@ -1,14 +1,12 @@
import { BasePage, ConfigBox } from "@/Components/design-elements"; import { BasePage, ConfigBox } from "@/Components/design-elements";
import { TextField, Select, Button } from "@/Components/inputs"; import { Button } from "@/Components/inputs";
import MenuItem from "@mui/material/MenuItem";
import Typography from "@mui/material/Typography";
import Stack from "@mui/material/Stack"; import Stack from "@mui/material/Stack";
import { useTheme } from "@mui/material/styles"; import { useTheme } from "@mui/material/styles";
import { useEffect, useMemo } from "react"; import { useEffect, useMemo } from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { Controller, useForm } from "react-hook-form"; import { FormProvider, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useGet, usePost, usePatch } from "@/Hooks/UseApi"; import { useGet, usePost, usePatch } from "@/Hooks/UseApi";
import { useNotificationForm } from "@/Hooks/useNotificationForm"; import { useNotificationForm } from "@/Hooks/useNotificationForm";
@@ -16,6 +14,8 @@ import type { NotificationFormData } from "@/Validation/notifications";
import type { Notification } from "@/Types/Notification"; import type { Notification } from "@/Types/Notification";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { NotificationChannels } from "@/Types/Notification"; import { NotificationChannels } from "@/Types/Notification";
import { FormTextField } from "@/Components/inputs/forms/FormTextField";
import { FormSelectField } from "@/Components/inputs/forms/FormSelectField";
const NotificationsCreatePage = () => { const NotificationsCreatePage = () => {
const { t } = useTranslation(); const { t } = useTranslation();
@@ -93,6 +93,7 @@ const NotificationsCreatePage = () => {
}; };
return ( return (
<FormProvider {...form}>
<BasePage <BasePage
component="form" component="form"
onSubmit={handleSubmit(onSubmit)} onSubmit={handleSubmit(onSubmit)}
@@ -101,21 +102,10 @@ const NotificationsCreatePage = () => {
title={t("pages.notifications.form.notificationName.title")} title={t("pages.notifications.form.notificationName.title")}
subtitle={t("pages.notifications.form.notificationName.description")} subtitle={t("pages.notifications.form.notificationName.description")}
rightContent={ rightContent={
<Controller <FormTextField
name="notificationName" name="notificationName"
control={control}
defaultValue={defaults.notificationName}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.notificationName.optionName")} fieldLabel={t("pages.notifications.form.notificationName.optionName")}
placeholder={t("pages.notifications.form.notificationName.placeholder")} placeholder={t("pages.notifications.form.notificationName.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
} }
/> />
@@ -123,27 +113,13 @@ const NotificationsCreatePage = () => {
title={t("pages.notifications.form.type.title")} title={t("pages.notifications.form.type.title")}
subtitle={t("pages.notifications.form.type.description")} subtitle={t("pages.notifications.form.type.description")}
rightContent={ rightContent={
<Controller <FormSelectField
name="type" name="type"
control={control}
defaultValue={defaults.type}
render={({ field, fieldState }) => (
<Select
value={field.value}
fieldLabel={t("pages.notifications.form.type.optionType")} fieldLabel={t("pages.notifications.form.type.optionType")}
error={!!fieldState.error} options={NotificationChannels.map((channel) => ({
onChange={field.onChange} value: channel,
> label: t(`pages.notifications.form.type.value.${channel}`),
{NotificationChannels.map((type: string) => ( }))}
<MenuItem
key={type}
value={type}
>
<Typography textTransform="capitalize">{type}</Typography>
</MenuItem>
))}
</Select>
)}
/> />
} }
/> />
@@ -156,21 +132,10 @@ const NotificationsCreatePage = () => {
title={addressConfig.title} title={addressConfig.title}
subtitle={addressConfig.description} subtitle={addressConfig.description}
rightContent={ rightContent={
<Controller <FormTextField
name="address" name="address"
control={control}
defaultValue={"address" in defaults ? defaults.address : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={addressConfig.fieldLabel} fieldLabel={addressConfig.fieldLabel}
placeholder={addressConfig.placeholder} placeholder={addressConfig.placeholder}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
} }
/> />
@@ -181,37 +146,15 @@ const NotificationsCreatePage = () => {
subtitle={t("pages.notifications.form.ntfy.description")} subtitle={t("pages.notifications.form.ntfy.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="address" name="address"
control={control}
defaultValue={"address" in defaults ? defaults.address : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.ntfy.optionServerUrl")} fieldLabel={t("pages.notifications.form.ntfy.optionServerUrl")}
placeholder={t("pages.notifications.form.ntfy.placeholderServerUrl")} placeholder={t("pages.notifications.form.ntfy.placeholderServerUrl")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)} <FormTextField
/>
<Controller
name="topic" name="topic"
control={control}
defaultValue={"topic" in defaults ? defaults.topic : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.ntfy.optionTopic")} fieldLabel={t("pages.notifications.form.ntfy.optionTopic")}
placeholder={t("pages.notifications.form.ntfy.placeholderTopic")} placeholder={t("pages.notifications.form.ntfy.placeholderTopic")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
</Stack> </Stack>
} }
@@ -223,38 +166,15 @@ const NotificationsCreatePage = () => {
subtitle={t("pages.notifications.form.telegram.description")} subtitle={t("pages.notifications.form.telegram.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="accessToken" name="accessToken"
control={control}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.telegram.optionBotToken")} fieldLabel={t("pages.notifications.form.telegram.optionBotToken")}
placeholder={t( placeholder={t("pages.notifications.form.telegram.placeholderBotToken")}
"pages.notifications.form.telegram.placeholderBotToken"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)} <FormTextField
/>
<Controller
name="address" name="address"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.telegram.optionChatId")} fieldLabel={t("pages.notifications.form.telegram.optionChatId")}
placeholder={t("pages.notifications.form.telegram.placeholderChatId")} placeholder={t("pages.notifications.form.telegram.placeholderChatId")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
</Stack> </Stack>
} }
@@ -266,41 +186,16 @@ const NotificationsCreatePage = () => {
subtitle={t("pages.notifications.form.pushover.description")} subtitle={t("pages.notifications.form.pushover.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="accessToken" name="accessToken"
control={control}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.pushover.optionAppToken")} fieldLabel={t("pages.notifications.form.pushover.optionAppToken")}
placeholder={t( placeholder={t("pages.notifications.form.pushover.placeholderAppToken")}
"pages.notifications.form.pushover.placeholderAppToken"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)}
/> <FormTextField
<Controller
name="address" name="address"
control={control}
defaultValue={"address" in defaults ? defaults.address : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.pushover.optionUserKey")} fieldLabel={t("pages.notifications.form.pushover.optionUserKey")}
placeholder={t( placeholder={t("pages.notifications.form.pushover.placeholderUserKey")}
"pages.notifications.form.pushover.placeholderUserKey"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
</Stack> </Stack>
} }
@@ -312,77 +207,27 @@ const NotificationsCreatePage = () => {
subtitle={t("pages.notifications.form.twilio.description")} subtitle={t("pages.notifications.form.twilio.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="accountSid" name="accountSid"
control={control}
defaultValue={"accountSid" in defaults ? defaults.accountSid : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionAccountSid")} fieldLabel={t("pages.notifications.form.twilio.optionAccountSid")}
placeholder={t( placeholder={t("pages.notifications.form.twilio.placeholderAccountSid")}
"pages.notifications.form.twilio.placeholderAccountSid"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)} <FormTextField
/>
<Controller
name="accessToken" name="accessToken"
control={control}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionAuthToken")} fieldLabel={t("pages.notifications.form.twilio.optionAuthToken")}
placeholder={t( placeholder={t("pages.notifications.form.twilio.placeholderAuthToken")}
"pages.notifications.form.twilio.placeholderAuthToken"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)}
/> <FormTextField
<Controller
name="twilioPhoneNumber" name="twilioPhoneNumber"
control={control}
defaultValue={
"twilioPhoneNumber" in defaults ? defaults.twilioPhoneNumber : ""
}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionFromNumber")} fieldLabel={t("pages.notifications.form.twilio.optionFromNumber")}
placeholder={t( placeholder={t("pages.notifications.form.twilio.placeholderFromNumber")}
"pages.notifications.form.twilio.placeholderFromNumber"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)}
/> <FormTextField
<Controller
name="phone" name="phone"
control={control}
defaultValue={"phone" in defaults ? defaults.phone : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionToNumber")} fieldLabel={t("pages.notifications.form.twilio.optionToNumber")}
placeholder={t("pages.notifications.form.twilio.placeholderToNumber")} placeholder={t("pages.notifications.form.twilio.placeholderToNumber")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
</Stack> </Stack>
} }
@@ -394,55 +239,20 @@ const NotificationsCreatePage = () => {
subtitle={t("pages.notifications.form.matrix.description")} subtitle={t("pages.notifications.form.matrix.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="homeserverUrl" name="homeserverUrl"
control={control}
defaultValue={"homeserverUrl" in defaults ? defaults.homeserverUrl : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.homeServer.optionHomeServer")} fieldLabel={t("pages.notifications.form.homeServer.optionHomeServer")}
placeholder={t("pages.notifications.form.homeServer.placeholder")} placeholder={t("pages.notifications.form.homeServer.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)} <FormTextField
/>
<Controller
name="roomId" name="roomId"
control={control}
defaultValue={"roomId" in defaults ? defaults.roomId : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.roomId.optionRoomId")} fieldLabel={t("pages.notifications.form.roomId.optionRoomId")}
placeholder={t("pages.notifications.form.roomId.placeholder")} placeholder={t("pages.notifications.form.roomId.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/> />
)} <FormTextField
/>
<Controller
name="accessToken" name="accessToken"
control={control} fieldLabel={t("pages.notifications.form.accessToken.optionAccessToken")}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t(
"pages.notifications.form.accessToken.optionAccessToken"
)}
placeholder={t("pages.notifications.form.accessToken.placeholder")} placeholder={t("pages.notifications.form.accessToken.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/> />
</Stack> </Stack>
} }
@@ -471,6 +281,7 @@ const NotificationsCreatePage = () => {
</Button> </Button>
</Stack> </Stack>
</BasePage> </BasePage>
</FormProvider>
); );
}; };
+14 -1
View File
@@ -1164,7 +1164,20 @@
"type": { "type": {
"description": "Select the type of notification channel to create.", "description": "Select the type of notification channel to create.",
"optionType": "Type", "optionType": "Type",
"title": "Channel type" "title": "Channel type",
"value": {
"email": "Email",
"slack": "Slack",
"discord": "Discord",
"webhook": "Webhook",
"pager_duty": "PagerDuty",
"matrix": "Matrix",
"teams": "Teams",
"telegram": "Telegram",
"pushover": "Pushover",
"twilio": "Twilio",
"ntfy": "ntfy"
}
}, },
"telegram": { "telegram": {
"title": "Telegram configuration", "title": "Telegram configuration",