From c1f8c54df7420509bd588f36abd5011242dcc2ed Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Sun, 14 Jan 2024 19:23:09 +0800 Subject: [PATCH] fix: notification premission check (#263) * fix: notification premission check * fix: rm useless condition --- src/hooks/use-notification.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/hooks/use-notification.ts b/src/hooks/use-notification.ts index e985391b16..841a8b3852 100644 --- a/src/hooks/use-notification.ts +++ b/src/hooks/use-notification.ts @@ -10,13 +10,12 @@ let permissionGranted: boolean | null = null; const checkPermission = async () => { if (permissionGranted == null) { permissionGranted = await isPermissionGranted(); - } else if (permissionGranted == false) { + } + if (!permissionGranted) { const permission = await requestPermission(); permissionGranted = permission === "granted"; - return permissionGranted; - } else { - return permissionGranted; } + return permissionGranted; }; export type NotificationOptions = { @@ -39,15 +38,17 @@ export const useNotification = async ({ }: NotificationOptions) => { if (!title) { throw new Error("missing message argument!"); - } else if (!checkPermission()) { + } + const permissionGranted = await checkPermission(); + if (!permissionGranted) { // fallback to mui notification Notice[type](`${title} ${body ? `: ${body}` : ""}}`); // throw new Error("notification permission not granted!"); - } else { - const options: Options = { - title: title, - }; - if (body) options.body = body; - sendNotification(options); + return; } + const options: Options = { + title: title, + }; + if (body) options.body = body; + sendNotification(options); };