From 515c372ea1a1fc15d0c15bec7f260338c0cb93f6 Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Sun, 27 Oct 2024 12:47:35 +0530 Subject: [PATCH 1/4] Fix: TypeError in notification list --- .../Notifications/NotificationsList.tsx | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index f15d5e8cb2a..a7733b4a330 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -261,7 +261,10 @@ export default function NotificationsList({ const handleSubscribeClick = () => { const status = isSubscribed; - if (status === "NotSubscribed" || status === "SubscribedOnAnotherDevice") { + if (!navigator.serviceWorker) { + return; + } + if (["NotSubscribed", "SubscribedOnAnotherDevice"].includes(status)) { subscribe(); } else { unsubscribe(); @@ -299,36 +302,38 @@ export default function NotificationsList({ const unsubscribe = () => { navigator.serviceWorker.ready .then(function (reg) { - setIsSubscribing(true); - reg.pushManager - .getSubscription() - .then(function (subscription) { - subscription - ?.unsubscribe() - .then(async function (_successful) { - const data = { - pf_endpoint: "", - pf_p256dh: "", - pf_auth: "", - }; + if (reg.pushManager) { + setIsSubscribing(true); + reg.pushManager + .getSubscription() + .then(function (subscription) { + subscription + ?.unsubscribe() + .then(async function (_successful) { + const data = { + pf_endpoint: "", + pf_p256dh: "", + pf_auth: "", + }; - await request(routes.updateUserPnconfig, { - pathParams: { username: username }, - body: data, - }); + await request(routes.updateUserPnconfig, { + pathParams: { username: username }, + body: data, + }); - setIsSubscribed("NotSubscribed"); - setIsSubscribing(false); - }) - .catch(function (_e) { - Error({ - msg: t("unsubscribe_failed"), + setIsSubscribed("NotSubscribed"); + setIsSubscribing(false); + }) + .catch(function (_e) { + Error({ + msg: t("unsubscribe_failed"), + }); }); - }); - }) - .catch(function (_e) { - Error({ msg: t("subscription_error") }); - }); + }) + .catch(function (_e) { + Error({ msg: t("subscription_error") }); + }); + } }) .catch(function (_e) { Sentry.captureException(_e); From 7ae97110208eb01fb0d57fe3ae97a3005a8e229f Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Wed, 6 Nov 2024 11:57:53 +0530 Subject: [PATCH 2/4] Added Suggestions by codeRabbit --- .../Notifications/NotificationsList.tsx | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index a7733b4a330..2b62a043520 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -299,45 +299,44 @@ export default function NotificationsList({ let manageResults: any = null; - const unsubscribe = () => { - navigator.serviceWorker.ready - .then(function (reg) { - if (reg.pushManager) { - setIsSubscribing(true); - reg.pushManager - .getSubscription() - .then(function (subscription) { - subscription - ?.unsubscribe() - .then(async function (_successful) { - const data = { - pf_endpoint: "", - pf_p256dh: "", - pf_auth: "", - }; - - await request(routes.updateUserPnconfig, { - pathParams: { username: username }, - body: data, - }); - - setIsSubscribed("NotSubscribed"); - setIsSubscribing(false); - }) - .catch(function (_e) { - Error({ - msg: t("unsubscribe_failed"), - }); - }); - }) - .catch(function (_e) { - Error({ msg: t("subscription_error") }); - }); + const unsubscribe = async () => { + try { + const reg = await navigator.serviceWorker.ready; + + if (!reg.pushManager) return; + + setIsSubscribing(true); + + const subscription = await reg.pushManager.getSubscription(); + + if (subscription) { + try { + await subscription.unsubscribe(); + + const data = { + pf_endpoint: "", + pf_p256dh: "", + pf_auth: "", + }; + + await request(routes.updateUserPnconfig, { + pathParams: { username }, + body: data, + }); + + setIsSubscribed("NotSubscribed"); + } catch (e) { + Error({ msg: t("unsubscribe_failed") }); + } finally { + setIsSubscribing(false); } - }) - .catch(function (_e) { - Sentry.captureException(_e); - }); + } else { + setIsSubscribing(false); + } + } catch (e) { + Sentry.captureException(e); + Error({ msg: t("subscription_error") }); + } }; async function subscribe() { From 8c1f48925874f8f2e46343ecc46d1a26abb75b39 Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Wed, 6 Nov 2024 12:27:41 +0530 Subject: [PATCH 3/4] Added Suggestions by codeRabbit 2.0 --- .../Notifications/NotificationsList.tsx | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index 2b62a043520..0d06e2c9148 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -303,7 +303,10 @@ export default function NotificationsList({ try { const reg = await navigator.serviceWorker.ready; - if (!reg.pushManager) return; + if (!reg.pushManager) { + Error({ msg: t("unsubscribe_failed") }); + return; + } setIsSubscribing(true); @@ -313,32 +316,27 @@ export default function NotificationsList({ try { await subscription.unsubscribe(); - const data = { - pf_endpoint: "", - pf_p256dh: "", - pf_auth: "", - }; - await request(routes.updateUserPnconfig, { pathParams: { username }, - body: data, + body: { + pf_endpoint: "", + pf_p256dh: "", + pf_auth: "", + }, }); setIsSubscribed("NotSubscribed"); } catch (e) { Error({ msg: t("unsubscribe_failed") }); - } finally { - setIsSubscribing(false); } - } else { - setIsSubscribing(false); } } catch (e) { Sentry.captureException(e); Error({ msg: t("subscription_error") }); + } finally { + setIsSubscribing(false); } }; - async function subscribe() { setIsSubscribing(true); const response = await request(routes.getPublicKey); From 226cce9aeb3f89878284124134f6a2920025f6d5 Mon Sep 17 00:00:00 2001 From: AdityaJ2305 Date: Mon, 18 Nov 2024 19:51:32 +0530 Subject: [PATCH 4/4] notification for unsubscribe revert --- src/components/Notifications/NotificationsList.tsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/components/Notifications/NotificationsList.tsx b/src/components/Notifications/NotificationsList.tsx index 9c5dfce9526..d204e472aab 100644 --- a/src/components/Notifications/NotificationsList.tsx +++ b/src/components/Notifications/NotificationsList.tsx @@ -316,6 +316,9 @@ export default function NotificationsList({ }); setIsSubscribed("NotSubscribed"); + Warn({ + msg: t("unsubscribed_successfully"), + }); } catch (e) { Error({ msg: t("unsubscribe_failed") }); }