From 6d3db3d596498c008593b044398ffd11455c21cc Mon Sep 17 00:00:00 2001 From: 0xsysr3ll <0xsysr3ll@pm.me> Date: Mon, 8 Dec 2025 21:55:43 +0100 Subject: [PATCH] fix(webpush): ensure the local storage reflects the correct notification status Signed-off-by: 0xsysr3ll <0xsysr3ll@pm.me> --- .../UserNotificationsWebPush/index.tsx | 46 +++++++++---------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx b/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx index 8041e79de..14bf9c928 100644 --- a/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx +++ b/src/components/UserProfile/UserSettings/UserNotificationSettings/UserNotificationsWebPush/index.tsx @@ -114,33 +114,27 @@ const UserWebPushSettings = () => { endpoint ); - // Delete from backend if endpoint is available + localStorage.setItem('pushNotificationsEnabled', 'false'); + setWebPushEnabled(false); + const endpointToDelete = unsubscribedEndpoint || subEndpoint || endpoint; + const endpointsToDelete: string[] = endpointToDelete + ? [endpointToDelete] + : dataDevices?.map((device: { endpoint: string }) => device.endpoint) ?? + []; - if (endpointToDelete) { - await deletePushSubscriptionFromBackend(endpointToDelete); - } else if (dataDevices && dataDevices.length > 0) { - let hasFailures = false; - - for (const device of dataDevices) { - try { - await deletePushSubscriptionFromBackend(device.endpoint); - } catch (error) { - hasFailures = true; - } - } - - if (hasFailures) { - addToast(intl.formatMessage(messages.disablingwebpusherror), { - autoDismiss: true, - appearance: 'error', - }); - return; + for (const ep of endpointsToDelete) { + try { + await axios.delete( + `/api/v1/user/${user?.id}/pushSubscription/${encodeURIComponent( + ep + )}` + ); + } catch { + // Ignore individual deletion failures - backend cleanup is best effort } } - localStorage.setItem('pushNotificationsEnabled', 'false'); - setWebPushEnabled(false); addToast(intl.formatMessage(messages.webpushhasbeendisabled), { autoDismiss: true, appearance: 'success', @@ -181,17 +175,19 @@ const UserWebPushSettings = () => { useEffect(() => { const verifyWebPush = async () => { const enabled = await verifyPushSubscription(user?.id, currentSettings); - setWebPushEnabled(enabled); + const hasBackendSubscriptions = dataDevices && dataDevices.length > 0; + const isEnabled = enabled || hasBackendSubscriptions; + setWebPushEnabled(isEnabled); localStorage.setItem( 'pushNotificationsEnabled', - enabled ? 'true' : 'false' + isEnabled ? 'true' : 'false' ); }; if (user?.id) { verifyWebPush(); } - }, [user?.id, currentSettings]); + }, [user?.id, currentSettings, dataDevices]); useEffect(() => { const getSubscriptionEndpoint = async () => {