2026-01-29 19:29:36 +00:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import { notificationSchema } from "@/Validation/notifications";
|
2026-03-25 19:13:37 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:07:50 -04:00
|
|
|
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: "",
|
|
|
|
|
};
|
2026-03-26 14:07:50 -04:00
|
|
|
if (data?.type === "matrix") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
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,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "telegram",
|
|
|
|
|
address: data.address || "",
|
|
|
|
|
accessToken: data.accessToken || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (data?.type === "slack") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "slack",
|
|
|
|
|
address: data.address || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (data?.type === "discord") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "discord",
|
|
|
|
|
address: data.address || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (data?.type === "webhook") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "webhook",
|
|
|
|
|
address: data.address || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (data?.type === "pager_duty") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "pager_duty",
|
|
|
|
|
address: data.address || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (data?.type === "teams") {
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
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",
|
2026-04-17 23:11:11 +03:00
|
|
|
accountSid: data.accountSid || "",
|
2026-04-16 21:46:40 +03:00
|
|
|
accessToken: data.accessToken || "",
|
|
|
|
|
phone: data.phone || "",
|
2026-04-17 23:11:11 +03:00
|
|
|
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 || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-03-26 14:07:50 -04:00
|
|
|
// Default: email (covers both data === null and data.type === "email")
|
|
|
|
|
return {
|
2026-08-06 11:54:46 -07:00
|
|
|
...base,
|
2026-03-26 14:07:50 -04:00
|
|
|
type: "email",
|
|
|
|
|
address: data?.address || "",
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-03-25 18:06:26 -04:00
|
|
|
|
2026-01-29 19:29:36 +00:00
|
|
|
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
|
|
|
|
return useMemo(() => {
|
2026-03-26 14:07:50 -04:00
|
|
|
const defaults = buildDefaults(data);
|
|
|
|
|
return { schema: notificationSchema, defaults };
|
2026-01-29 19:29:36 +00:00
|
|
|
}, [data]);
|
|
|
|
|
};
|