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
+62
-33
@@ -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 = {
|
if (data?.type === "matrix") {
|
||||||
type: NotificationFormData["type"];
|
return {
|
||||||
notificationName: string;
|
type: "matrix",
|
||||||
address?: string;
|
notificationName: data.notificationName || "",
|
||||||
homeserverUrl?: string;
|
homeserverUrl: data.homeserverUrl || "",
|
||||||
roomId?: string;
|
roomId: data.roomId || "",
|
||||||
accessToken?: string;
|
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 = {}) => {
|
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
let defaults: NotificationDefaults = {
|
const defaults = buildDefaults(data);
|
||||||
type: (data?.type || "email") as NotificationFormData["type"],
|
return { schema: notificationSchema, defaults };
|
||||||
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 };
|
|
||||||
}, [data]);
|
}, [data]);
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user