fix(notifications): remove type casts with exhaustive type narrowing in useNotificationForm
Replace the loose NotificationDefaults intermediate type and 'as NotificationFormData' cast with an exhaustive buildDefaults() helper that narrows each branch of the discriminated union explicitly. TypeScript now infers the return type correctly from the discriminated union without any casts, satisfying strong typing for all channels (email, slack, discord, webhook, pager_duty, teams, matrix, telegram).
This commit is contained in:
1 parent
4c94a7c21e
commit
13a7ae82e3
1 file changed
+51
-22
@@ -7,41 +7,70 @@ interface UseNotificationFormOptions {
|
|||||||
data?: Notification | null;
|
data?: Notification | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flat defaults type compatible with all form variants for defaultValue props
|
function buildDefaults(data: Notification | null): NotificationFormData {
|
||||||
type NotificationDefaults = {
|
|
||||||
type: NotificationFormData["type"];
|
|
||||||
notificationName: string;
|
|
||||||
address?: string;
|
|
||||||
homeserverUrl?: string;
|
|
||||||
roomId?: string;
|
|
||||||
accessToken?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
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") {
|
if (data?.type === "matrix") {
|
||||||
defaults = {
|
return {
|
||||||
type: "matrix",
|
type: "matrix",
|
||||||
notificationName: data.notificationName || "",
|
notificationName: data.notificationName || "",
|
||||||
homeserverUrl: data.homeserverUrl || "",
|
homeserverUrl: data.homeserverUrl || "",
|
||||||
roomId: data.roomId || "",
|
roomId: data.roomId || "",
|
||||||
accessToken: data.accessToken || "",
|
accessToken: data.accessToken || "",
|
||||||
};
|
};
|
||||||
} else if (data?.type === "telegram") {
|
}
|
||||||
defaults = {
|
if (data?.type === "telegram") {
|
||||||
|
return {
|
||||||
type: "telegram",
|
type: "telegram",
|
||||||
notificationName: data.notificationName || "",
|
notificationName: data.notificationName || "",
|
||||||
address: data.address || "",
|
address: data.address || "",
|
||||||
accessToken: data.accessToken || "",
|
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 || "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return { schema: notificationSchema, defaults: defaults as NotificationFormData };
|
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
||||||
|
return useMemo(() => {
|
||||||
|
const defaults = buildDefaults(data);
|
||||||
|
return { schema: notificationSchema, defaults };
|
||||||
}, [data]);
|
}, [data]);
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user