Files
Checkmate/client/src/Hooks/useNotificationForm.ts
T

115 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-01-29 19:29:36 +00:00
import { useMemo } from "react";
import { notificationSchema } from "@/Validation/notifications";
import type { NotificationFormData } from "@/Validation/notifications";
2026-01-29 19:29:36 +00:00
import type { Notification } from "@/Types/Notification";
interface UseNotificationFormOptions {
data?: Notification | null;
}
function buildDefaults(data: Notification | null): NotificationFormData {
2026-08-06 11:54:46 -07:00
// 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") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "matrix",
homeserverUrl: data.homeserverUrl || "",
roomId: data.roomId || "",
accessToken: data.accessToken || "",
};
}
if (data?.type === "telegram") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "telegram",
address: data.address || "",
accessToken: data.accessToken || "",
};
}
if (data?.type === "slack") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "slack",
address: data.address || "",
};
}
if (data?.type === "discord") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "discord",
address: data.address || "",
};
}
if (data?.type === "webhook") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "webhook",
address: data.address || "",
};
}
if (data?.type === "pager_duty") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "pager_duty",
address: data.address || "",
};
}
if (data?.type === "teams") {
return {
2026-08-06 11:54:46 -07:00
...base,
type: "teams",
address: data.address || "",
};
}
2026-04-16 21:46:40 +03:00
if (data?.type === "twilio") {
return {
2026-08-06 11:54:46 -07:00
...base,
2026-04-16 21:46:40 +03:00
type: "twilio",
accountSid: data.accountSid || "",
2026-04-16 21:46:40 +03:00
accessToken: data.accessToken || "",
phone: data.phone || "",
twilioPhoneNumber: data.twilioPhoneNumber || "",
2026-04-16 21:46:40 +03:00
};
}
2026-04-16 16:25:24 +03:00
if (data?.type === "pushover") {
return {
2026-08-06 11:54:46 -07:00
...base,
2026-04-16 16:25:24 +03:00
type: "pushover",
address: data.address || "",
accessToken: data.accessToken || "",
};
}
2026-05-27 16:57:11 +01:00
if (data?.type === "ntfy") {
return {
2026-08-06 11:54:46 -07:00
...base,
2026-05-27 16:57:11 +01:00
type: "ntfy",
address: data.address || "",
topic: data.topic || "",
};
}
// Default: email (covers both data === null and data.type === "email")
return {
2026-08-06 11:54:46 -07:00
...base,
type: "email",
address: data?.address || "",
};
}
2026-01-29 19:29:36 +00:00
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
return useMemo(() => {
const defaults = buildDefaults(data);
return { schema: notificationSchema, defaults };
2026-01-29 19:29:36 +00:00
}, [data]);
};