diff --git a/client/src/Components/v2/design-elements/SplitBox.tsx b/client/src/Components/v2/design-elements/SplitBox.tsx new file mode 100644 index 000000000..3a778dbcf --- /dev/null +++ b/client/src/Components/v2/design-elements/SplitBox.tsx @@ -0,0 +1,78 @@ +import Stack from "@mui/material/Stack"; +import Box from "@mui/material/Box"; +import Typography from "@mui/material/Typography"; +import { useTheme } from "@mui/material/styles"; +import useMediaQuery from "@mui/material/useMediaQuery"; +export const SplitBox = ({ + left, + right, +}: { + left: React.ReactNode; + right: React.ReactNode; +}) => { + const theme = useTheme(); + const isSmall = useMediaQuery(theme.breakpoints.down("md")); + return ( + + + {left} + + + {right} + + + ); +}; + +export const ConfigBox = ({ + title, + subtitle, + leftContent, + rightContent, +}: { + title: string; + subtitle: string; + leftContent?: React.ReactNode; + rightContent: React.ReactNode; +}) => { + const theme = useTheme(); + return ( + + + {title} + + {subtitle} + {leftContent} + + } + right={rightContent} + /> + ); +}; diff --git a/client/src/Components/v2/design-elements/index.tsx b/client/src/Components/v2/design-elements/index.tsx index 33248a345..a96fea052 100644 --- a/client/src/Components/v2/design-elements/index.tsx +++ b/client/src/Components/v2/design-elements/index.tsx @@ -17,3 +17,4 @@ export * from "./StatBox"; export * from "./BaseChart"; export * from "./Gauge"; export * from "./Tabs"; +export * from "./SplitBox"; diff --git a/client/src/Hooks/useNotificationForm.ts b/client/src/Hooks/useNotificationForm.ts new file mode 100644 index 000000000..dd882db67 --- /dev/null +++ b/client/src/Hooks/useNotificationForm.ts @@ -0,0 +1,28 @@ +import { useMemo } from "react"; +import { notificationSchema } from "@/Validation/notifications"; +import type { Notification } from "@/Types/Notification"; + +interface UseNotificationFormOptions { + data?: Notification | null; +} + +export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => { + return useMemo(() => { + const defaults = + data?.type === "matrix" + ? { + type: "matrix" as const, + notificationName: data.notificationName || "", + homeserverUrl: data.homeserverUrl || "", + roomId: data.roomId || "", + accessToken: data.accessToken || "", + } + : { + type: (data?.type || "email") as Exclude, + notificationName: data?.notificationName || "", + address: data?.address || "", + }; + + return { schema: notificationSchema, defaults }; + }, [data]); +}; diff --git a/client/src/Pages/Notifications/create/index.tsx b/client/src/Pages/Notifications/create/index.tsx index 82cee4861..58b14a426 100644 --- a/client/src/Pages/Notifications/create/index.tsx +++ b/client/src/Pages/Notifications/create/index.tsx @@ -1,5 +1,78 @@ +import { BasePage, ConfigBox } from "@/Components/v2/design-elements"; +import { TextField } from "@/Components/v2/inputs"; + +import { useEffect } from "react"; +import { useParams } from "react-router-dom"; +import { Controller, useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useGet } from "@/Hooks/UseApi"; +import { useNotificationForm } from "@/Hooks/useNotificationForm"; +import type { NotificationFormData } from "@/Validation/notifications"; +import type { Notification } from "@/Types/Notification"; +import { useTranslation } from "react-i18next"; + const NotificationsCreatePage = () => { - return
Create Notifications Page
; + const { t } = useTranslation(); + const { notificationId } = useParams<{ notificationId: string }>(); + const isEditMode = Boolean(notificationId); + + const { data: existingNotification, isLoading: notificationIsLoading } = + useGet(isEditMode ? `/notifications/${notificationId}` : null); + + const { schema, defaults } = useNotificationForm({ data: existingNotification }); + + const form = useForm({ + resolver: zodResolver(schema), + defaultValues: defaults, + }); + + const { + register, + control, + watch, + reset, + formState: { errors }, + } = form; + + // Reset form when defaults change (i.e., when data is fetched) + useEffect(() => { + reset(defaults); + }, [defaults, reset]); + + const watchedType = watch("type"); + + // Suppress unused variable warnings - these will be used when UI is built + void register; + void control; + + return ( + + ( + + )} + /> + } + /> + + ); }; export default NotificationsCreatePage; diff --git a/client/src/Validation/notifications.ts b/client/src/Validation/notifications.ts new file mode 100644 index 000000000..c78cf1ef9 --- /dev/null +++ b/client/src/Validation/notifications.ts @@ -0,0 +1,51 @@ +import { z } from "zod"; + +const baseSchema = z.object({ + notificationName: z + .string() + .min(1, "Notification name is required") + .max(100, "Notification name must be at most 100 characters"), +}); + +const emailSchema = baseSchema.extend({ + type: z.literal("email"), + address: z.string().min(1, "Email is required").email("Please enter a valid email address"), +}); + +const slackSchema = baseSchema.extend({ + type: z.literal("slack"), + address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"), +}); + +const discordSchema = baseSchema.extend({ + type: z.literal("discord"), + address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"), +}); + +const webhookSchema = baseSchema.extend({ + type: z.literal("webhook"), + address: z.string().min(1, "Webhook URL is required").url("Please enter a valid URL"), +}); + +const pagerDutySchema = baseSchema.extend({ + type: z.literal("pager_duty"), + address: z.string().min(1, "Integration key is required"), +}); + +const matrixSchema = baseSchema.extend({ + type: z.literal("matrix"), + homeserverUrl: z.string().min(1, "Homeserver URL is required").url("Please enter a valid URL"), + roomId: z.string().min(1, "Room ID is required"), + accessToken: z.string().min(1, "Access token is required"), +}); + +export const notificationSchema = z.discriminatedUnion("type", [ + emailSchema, + slackSchema, + discordSchema, + webhookSchema, + pagerDutySchema, + matrixSchema, +]); + +export type NotificationFormData = z.infer; diff --git a/client/src/locales/en.json b/client/src/locales/en.json index 960397021..b98b43b7d 100644 --- a/client/src/locales/en.json +++ b/client/src/locales/en.json @@ -342,6 +342,14 @@ "headers": { "destination": "Destination" } + }, + "form": { + "notificationName": { + "description": "A descriptive name for the notification channel", + "optionName": "Channel name", + "placeholder": "e.g. Production Alerts", + "title": "Channel name" + } } } },