refactor(notifications): hoist default values, remove else block in useNotificationForm
Per @ajhollid review: define defaults up-front with a NotificationDefaults type and eliminate the trailing else branch entirely. The if/else-if blocks only override when the type is matrix or telegram.
This commit is contained in:
1 parent
a864bc0c05
commit
40968c370b
1 file changed
+16
-12
@@ -6,13 +6,26 @@ interface UseNotificationFormOptions {
|
|||||||
data?: Notification | null;
|
data?: Notification | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NotificationDefaults = {
|
||||||
|
type: Notification["type"];
|
||||||
|
notificationName: string;
|
||||||
|
address?: string;
|
||||||
|
homeserverUrl?: string;
|
||||||
|
roomId?: string;
|
||||||
|
accessToken?: string;
|
||||||
|
};
|
||||||
|
|
||||||
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
export const useNotificationForm = ({ data = null }: UseNotificationFormOptions = {}) => {
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
let defaults;
|
let defaults: NotificationDefaults = {
|
||||||
|
type: (data?.type || "email") as Notification["type"],
|
||||||
|
notificationName: data?.notificationName || "",
|
||||||
|
address: data?.address || "",
|
||||||
|
};
|
||||||
|
|
||||||
if (data?.type === "matrix") {
|
if (data?.type === "matrix") {
|
||||||
defaults = {
|
defaults = {
|
||||||
type: "matrix" as const,
|
type: "matrix",
|
||||||
notificationName: data.notificationName || "",
|
notificationName: data.notificationName || "",
|
||||||
homeserverUrl: data.homeserverUrl || "",
|
homeserverUrl: data.homeserverUrl || "",
|
||||||
roomId: data.roomId || "",
|
roomId: data.roomId || "",
|
||||||
@@ -20,20 +33,11 @@ export const useNotificationForm = ({ data = null }: UseNotificationFormOptions
|
|||||||
};
|
};
|
||||||
} else if (data?.type === "telegram") {
|
} else if (data?.type === "telegram") {
|
||||||
defaults = {
|
defaults = {
|
||||||
type: "telegram" as const,
|
type: "telegram",
|
||||||
notificationName: data.notificationName || "",
|
notificationName: data.notificationName || "",
|
||||||
address: data.address || "",
|
address: data.address || "",
|
||||||
accessToken: data.accessToken || "",
|
accessToken: data.accessToken || "",
|
||||||
};
|
};
|
||||||
} else {
|
|
||||||
defaults = {
|
|
||||||
type: (data?.type || "email") as Exclude<
|
|
||||||
Notification["type"],
|
|
||||||
"matrix" | "telegram"
|
|
||||||
>,
|
|
||||||
notificationName: data?.notificationName || "",
|
|
||||||
address: data?.address || "",
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { schema: notificationSchema, defaults };
|
return { schema: notificationSchema, defaults };
|
||||||
|
|||||||
Reference in new issue
Block a user