diff --git a/client/src/Hooks/useNotificationForm.ts b/client/src/Hooks/useNotificationForm.ts index deb1db62b..c45773db7 100644 --- a/client/src/Hooks/useNotificationForm.ts +++ b/client/src/Hooks/useNotificationForm.ts @@ -7,41 +7,70 @@ interface UseNotificationFormOptions { data?: Notification | null; } -// Flat defaults type compatible with all form variants for defaultValue props -type NotificationDefaults = { - type: NotificationFormData["type"]; - notificationName: string; - address?: string; - homeserverUrl?: string; - roomId?: string; - accessToken?: string; -}; +function buildDefaults(data: Notification | null): NotificationFormData { + if (data?.type === "matrix") { + return { + type: "matrix", + notificationName: data.notificationName || "", + homeserverUrl: data.homeserverUrl || "", + roomId: data.roomId || "", + accessToken: data.accessToken || "", + }; + } + if (data?.type === "telegram") { + return { + type: "telegram", + notificationName: data.notificationName || "", + address: data.address || "", + accessToken: data.accessToken || "", + }; + } + if (data?.type === "slack") { + return { + type: "slack", + notificationName: data.notificationName || "", + address: data.address || "", + }; + } + if (data?.type === "discord") { + return { + type: "discord", + notificationName: data.notificationName || "", + address: data.address || "", + }; + } + if (data?.type === "webhook") { + return { + type: "webhook", + notificationName: data.notificationName || "", + address: data.address || "", + }; + } + if (data?.type === "pager_duty") { + return { + type: "pager_duty", + notificationName: data.notificationName || "", + address: data.address || "", + }; + } + if (data?.type === "teams") { + return { + type: "teams", + notificationName: data.notificationName || "", + address: data.address || "", + }; + } + // Default: email (covers both data === null and data.type === "email") + return { + type: "email", + notificationName: data?.notificationName || "", + address: data?.address || "", + }; +} export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => { return useMemo(() => { - let defaults: NotificationDefaults = { - type: (data?.type || "email") as NotificationFormData["type"], - notificationName: data?.notificationName || "", - address: data?.address || "", - }; - - if (data?.type === "matrix") { - defaults = { - type: "matrix", - notificationName: data.notificationName || "", - homeserverUrl: data.homeserverUrl || "", - roomId: data.roomId || "", - accessToken: data.accessToken || "", - }; - } else if (data?.type === "telegram") { - defaults = { - type: "telegram", - notificationName: data.notificationName || "", - address: data.address || "", - accessToken: data.accessToken || "", - }; - } - - return { schema: notificationSchema, defaults: defaults as NotificationFormData }; + const defaults = buildDefaults(data); + return { schema: notificationSchema, defaults }; }, [data]); };