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
+268 -388

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 || "",
}; };
} }
+187 -376
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,384 +93,195 @@ const NotificationsCreatePage = () => {
}; };
return ( return (
<BasePage <FormProvider {...form}>
component="form" <BasePage
onSubmit={handleSubmit(onSubmit)} component="form"
> onSubmit={handleSubmit(onSubmit)}
<ConfigBox >
title={t("pages.notifications.form.notificationName.title")} <ConfigBox
subtitle={t("pages.notifications.form.notificationName.description")} title={t("pages.notifications.form.notificationName.title")}
rightContent={ subtitle={t("pages.notifications.form.notificationName.description")}
<Controller rightContent={
name="notificationName" <FormTextField
control={control} name="notificationName"
defaultValue={defaults.notificationName} fieldLabel={t("pages.notifications.form.notificationName.optionName")}
render={({ field, fieldState }) => ( placeholder={t("pages.notifications.form.notificationName.placeholder")}
<TextField />
{...field} }
type="text" />
fieldLabel={t("pages.notifications.form.notificationName.optionName")} <ConfigBox
placeholder={t("pages.notifications.form.notificationName.placeholder")} title={t("pages.notifications.form.type.title")}
fullWidth subtitle={t("pages.notifications.form.type.description")}
error={!!fieldState.error} rightContent={
helperText={fieldState.error?.message ?? ""} <FormSelectField
/> name="type"
)} fieldLabel={t("pages.notifications.form.type.optionType")}
/> options={NotificationChannels.map((channel) => ({
} value: channel,
/> label: t(`pages.notifications.form.type.value.${channel}`),
<ConfigBox }))}
title={t("pages.notifications.form.type.title")} />
subtitle={t("pages.notifications.form.type.description")} }
rightContent={ />
<Controller {watchedType !== "matrix" &&
name="type" watchedType !== "telegram" &&
control={control} watchedType !== "pushover" &&
defaultValue={defaults.type} watchedType !== "twilio" &&
render={({ field, fieldState }) => ( watchedType !== "ntfy" && (
<Select <ConfigBox
value={field.value} title={addressConfig.title}
fieldLabel={t("pages.notifications.form.type.optionType")} subtitle={addressConfig.description}
error={!!fieldState.error} rightContent={
onChange={field.onChange} <FormTextField
> name="address"
{NotificationChannels.map((type: string) => ( fieldLabel={addressConfig.fieldLabel}
<MenuItem placeholder={addressConfig.placeholder}
key={type} />
value={type} }
> />
<Typography textTransform="capitalize">{type}</Typography> )}
</MenuItem> {watchedType === "ntfy" && (
))}
</Select>
)}
/>
}
/>
{watchedType !== "matrix" &&
watchedType !== "telegram" &&
watchedType !== "pushover" &&
watchedType !== "twilio" &&
watchedType !== "ntfy" && (
<ConfigBox <ConfigBox
title={addressConfig.title} title={t("pages.notifications.form.ntfy.title")}
subtitle={addressConfig.description} subtitle={t("pages.notifications.form.ntfy.description")}
rightContent={ rightContent={
<Controller <Stack spacing={theme.spacing(8)}>
name="address" <FormTextField
control={control} name="address"
defaultValue={"address" in defaults ? defaults.address : ""} fieldLabel={t("pages.notifications.form.ntfy.optionServerUrl")}
render={({ field, fieldState }) => ( placeholder={t("pages.notifications.form.ntfy.placeholderServerUrl")}
<TextField />
{...field} <FormTextField
type="text" name="topic"
fieldLabel={addressConfig.fieldLabel} fieldLabel={t("pages.notifications.form.ntfy.optionTopic")}
placeholder={addressConfig.placeholder} placeholder={t("pages.notifications.form.ntfy.placeholderTopic")}
fullWidth />
error={!!fieldState.error} </Stack>
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
} }
/> />
)} )}
{watchedType === "ntfy" && ( {watchedType === "telegram" && (
<ConfigBox <ConfigBox
title={t("pages.notifications.form.ntfy.title")} title={t("pages.notifications.form.telegram.title")}
subtitle={t("pages.notifications.form.ntfy.description")} subtitle={t("pages.notifications.form.telegram.description")}
rightContent={ rightContent={
<Stack spacing={theme.spacing(8)}> <Stack spacing={theme.spacing(8)}>
<Controller <FormTextField
name="address" name="accessToken"
control={control} fieldLabel={t("pages.notifications.form.telegram.optionBotToken")}
defaultValue={"address" in defaults ? defaults.address : ""} placeholder={t("pages.notifications.form.telegram.placeholderBotToken")}
render={({ field, fieldState }) => ( />
<TextField <FormTextField
{...field} name="address"
type="text" fieldLabel={t("pages.notifications.form.telegram.optionChatId")}
fieldLabel={t("pages.notifications.form.ntfy.optionServerUrl")} placeholder={t("pages.notifications.form.telegram.placeholderChatId")}
placeholder={t("pages.notifications.form.ntfy.placeholderServerUrl")} />
fullWidth </Stack>
error={!!fieldState.error} }
helperText={fieldState.error?.message ?? ""} />
/> )}
)} {watchedType === "pushover" && (
/> <ConfigBox
<Controller title={t("pages.notifications.form.pushover.title")}
name="topic" subtitle={t("pages.notifications.form.pushover.description")}
control={control} rightContent={
defaultValue={"topic" in defaults ? defaults.topic : ""} <Stack spacing={theme.spacing(8)}>
render={({ field, fieldState }) => ( <FormTextField
<TextField name="accessToken"
{...field} fieldLabel={t("pages.notifications.form.pushover.optionAppToken")}
type="text" placeholder={t("pages.notifications.form.pushover.placeholderAppToken")}
fieldLabel={t("pages.notifications.form.ntfy.optionTopic")} />
placeholder={t("pages.notifications.form.ntfy.placeholderTopic")}
fullWidth <FormTextField
error={!!fieldState.error} name="address"
helperText={fieldState.error?.message ?? ""} fieldLabel={t("pages.notifications.form.pushover.optionUserKey")}
/> placeholder={t("pages.notifications.form.pushover.placeholderUserKey")}
)} />
/> </Stack>
</Stack> }
} />
/> )}
)} {watchedType === "twilio" && (
{watchedType === "telegram" && ( <ConfigBox
<ConfigBox title={t("pages.notifications.form.twilio.title")}
title={t("pages.notifications.form.telegram.title")} subtitle={t("pages.notifications.form.twilio.description")}
subtitle={t("pages.notifications.form.telegram.description")} rightContent={
rightContent={ <Stack spacing={theme.spacing(8)}>
<Stack spacing={theme.spacing(8)}> <FormTextField
<Controller name="accountSid"
name="accessToken" fieldLabel={t("pages.notifications.form.twilio.optionAccountSid")}
control={control} placeholder={t("pages.notifications.form.twilio.placeholderAccountSid")}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""} />
render={({ field, fieldState }) => ( <FormTextField
<TextField name="accessToken"
{...field} fieldLabel={t("pages.notifications.form.twilio.optionAuthToken")}
type="text" placeholder={t("pages.notifications.form.twilio.placeholderAuthToken")}
fieldLabel={t("pages.notifications.form.telegram.optionBotToken")} />
placeholder={t(
"pages.notifications.form.telegram.placeholderBotToken" <FormTextField
)} name="twilioPhoneNumber"
fullWidth fieldLabel={t("pages.notifications.form.twilio.optionFromNumber")}
error={!!fieldState.error} placeholder={t("pages.notifications.form.twilio.placeholderFromNumber")}
helperText={fieldState.error?.message ?? ""} />
/>
)} <FormTextField
/> name="phone"
<Controller fieldLabel={t("pages.notifications.form.twilio.optionToNumber")}
name="address" placeholder={t("pages.notifications.form.twilio.placeholderToNumber")}
control={control} />
render={({ field, fieldState }) => ( </Stack>
<TextField }
{...field} />
type="text" )}
fieldLabel={t("pages.notifications.form.telegram.optionChatId")} {watchedType === "matrix" && (
placeholder={t("pages.notifications.form.telegram.placeholderChatId")} <ConfigBox
fullWidth title={t("pages.notifications.form.matrix.title")}
error={!!fieldState.error} subtitle={t("pages.notifications.form.matrix.description")}
helperText={fieldState.error?.message ?? ""} rightContent={
/> <Stack spacing={theme.spacing(8)}>
)} <FormTextField
/> name="homeserverUrl"
</Stack> fieldLabel={t("pages.notifications.form.homeServer.optionHomeServer")}
} placeholder={t("pages.notifications.form.homeServer.placeholder")}
/> />
)} <FormTextField
{watchedType === "pushover" && ( name="roomId"
<ConfigBox fieldLabel={t("pages.notifications.form.roomId.optionRoomId")}
title={t("pages.notifications.form.pushover.title")} placeholder={t("pages.notifications.form.roomId.placeholder")}
subtitle={t("pages.notifications.form.pushover.description")} />
rightContent={ <FormTextField
<Stack spacing={theme.spacing(8)}> name="accessToken"
<Controller fieldLabel={t("pages.notifications.form.accessToken.optionAccessToken")}
name="accessToken" placeholder={t("pages.notifications.form.accessToken.placeholder")}
control={control} />
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""} </Stack>
render={({ field, fieldState }) => ( }
<TextField />
{...field} )}
type="text" <Stack
fieldLabel={t("pages.notifications.form.pushover.optionAppToken")} direction="row"
placeholder={t( justifyContent="flex-end"
"pages.notifications.form.pushover.placeholderAppToken" spacing={theme.spacing(2)}
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="address"
control={control}
defaultValue={"address" in defaults ? defaults.address : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.pushover.optionUserKey")}
placeholder={t(
"pages.notifications.form.pushover.placeholderUserKey"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
</Stack>
}
/>
)}
{watchedType === "twilio" && (
<ConfigBox
title={t("pages.notifications.form.twilio.title")}
subtitle={t("pages.notifications.form.twilio.description")}
rightContent={
<Stack spacing={theme.spacing(8)}>
<Controller
name="accountSid"
control={control}
defaultValue={"accountSid" in defaults ? defaults.accountSid : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionAccountSid")}
placeholder={t(
"pages.notifications.form.twilio.placeholderAccountSid"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="accessToken"
control={control}
defaultValue={"accessToken" in defaults ? defaults.accessToken : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionAuthToken")}
placeholder={t(
"pages.notifications.form.twilio.placeholderAuthToken"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="twilioPhoneNumber"
control={control}
defaultValue={
"twilioPhoneNumber" in defaults ? defaults.twilioPhoneNumber : ""
}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionFromNumber")}
placeholder={t(
"pages.notifications.form.twilio.placeholderFromNumber"
)}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="phone"
control={control}
defaultValue={"phone" in defaults ? defaults.phone : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.twilio.optionToNumber")}
placeholder={t("pages.notifications.form.twilio.placeholderToNumber")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
</Stack>
}
/>
)}
{watchedType === "matrix" && (
<ConfigBox
title={t("pages.notifications.form.matrix.title")}
subtitle={t("pages.notifications.form.matrix.description")}
rightContent={
<Stack spacing={theme.spacing(8)}>
<Controller
name="homeserverUrl"
control={control}
defaultValue={"homeserverUrl" in defaults ? defaults.homeserverUrl : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.homeServer.optionHomeServer")}
placeholder={t("pages.notifications.form.homeServer.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="roomId"
control={control}
defaultValue={"roomId" in defaults ? defaults.roomId : ""}
render={({ field, fieldState }) => (
<TextField
{...field}
type="text"
fieldLabel={t("pages.notifications.form.roomId.optionRoomId")}
placeholder={t("pages.notifications.form.roomId.placeholder")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
<Controller
name="accessToken"
control={control}
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")}
fullWidth
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
/>
</Stack>
}
/>
)}
<Stack
direction="row"
justifyContent="flex-end"
spacing={theme.spacing(2)}
>
<Button
variant="contained"
color="primary"
onClick={handleTest}
loading={isTesting}
> >
{t("common.buttons.test")} <Button
</Button> variant="contained"
<Button color="primary"
loading={isSubmitting || isPatching} onClick={handleTest}
type="submit" loading={isTesting}
variant="contained" >
color="primary" {t("common.buttons.test")}
> </Button>
{t("common.buttons.save")} <Button
</Button> loading={isSubmitting || isPatching}
</Stack> type="submit"
</BasePage> variant="contained"
color="primary"
>
{t("common.buttons.save")}
</Button>
</Stack>
</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",