From 29148a8c49d3ea4b06f69bf7a72146a405e40979 Mon Sep 17 00:00:00 2001 From: Aaron Cook Date: Wed, 4 Oct 2023 09:40:33 +0200 Subject: [PATCH] fix: only request new notification signatures (#2575) --- .../PushNotificationsBanner.test.ts | 84 +++++++++++++++++++ .../PushNotificationsBanner/index.tsx | 16 ++-- 2 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/components/settings/PushNotifications/PushNotificationsBanner/PushNotificationsBanner.test.ts diff --git a/src/components/settings/PushNotifications/PushNotificationsBanner/PushNotificationsBanner.test.ts b/src/components/settings/PushNotifications/PushNotificationsBanner/PushNotificationsBanner.test.ts new file mode 100644 index 0000000000..ca17984eb3 --- /dev/null +++ b/src/components/settings/PushNotifications/PushNotificationsBanner/PushNotificationsBanner.test.ts @@ -0,0 +1,84 @@ +import { _getSafesToRegister } from '.' +import type { AddedSafesState } from '@/store/addedSafesSlice' +import type { PushNotificationPreferences } from '@/services/push-notifications/preferences' + +describe('PushNotificationsBanner', () => { + describe('getSafesToRegister', () => { + it('should return all added safes if no preferences exist', () => { + const addedSafes = { + '1': { + '0x123': {}, + '0x456': {}, + }, + '4': { + '0x789': {}, + }, + } as unknown as AddedSafesState + const allPreferences = undefined + + const result = _getSafesToRegister(addedSafes, allPreferences) + + expect(result).toEqual({ + '1': ['0x123', '0x456'], + '4': ['0x789'], + }) + }) + + it('should return only newly added safes if preferences exist', () => { + const addedSafes = { + '1': { + '0x123': {}, + '0x456': {}, + }, + '4': { + '0x789': {}, + }, + } as unknown as AddedSafesState + const allPreferences = { + '1:0x123': { + safeAddress: '0x123', + chainId: '1', + }, + '4:0x789': { + safeAddress: '0x789', + chainId: '4', + }, + } as unknown as PushNotificationPreferences + + const result = _getSafesToRegister(addedSafes, allPreferences) + + expect(result).toEqual({ + '1': ['0x456'], + }) + }) + + it('should return all added safes if no preferences match', () => { + const addedSafes = { + '1': { + '0x123': {}, + '0x456': {}, + }, + '4': { + '0x789': {}, + }, + } as unknown as AddedSafesState + const allPreferences = { + '1:0x111': { + safeAddress: '0x111', + chainId: '1', + }, + '4:0x222': { + safeAddress: '0x222', + chainId: '4', + }, + } as unknown as PushNotificationPreferences + + const result = _getSafesToRegister(addedSafes, allPreferences) + + expect(result).toEqual({ + '1': ['0x123', '0x456'], + '4': ['0x789'], + }) + }) + }) +}) diff --git a/src/components/settings/PushNotifications/PushNotificationsBanner/index.tsx b/src/components/settings/PushNotifications/PushNotificationsBanner/index.tsx index 8a6211b4c3..f1fbfb8082 100644 --- a/src/components/settings/PushNotifications/PushNotificationsBanner/index.tsx +++ b/src/components/settings/PushNotifications/PushNotificationsBanner/index.tsx @@ -65,7 +65,10 @@ export const useDismissPushNotificationsBanner = () => { } } -const getSafesToRegister = (addedSafes: AddedSafesState, allPreferences: PushNotificationPreferences | undefined) => { +export const _getSafesToRegister = ( + addedSafes: AddedSafesState, + allPreferences: PushNotificationPreferences | undefined, +) => { // Regiser all added Safes if (!allPreferences) { return transformAddedSafes(addedSafes) @@ -77,13 +80,14 @@ const getSafesToRegister = (addedSafes: AddedSafesState, allPreferences: PushNot const notificationRegistrations = Object.values(allPreferences) const newlyAddedSafes = addedSafeAddressesOnChain.filter((safeAddress) => { - return ( - notificationRegistrations.length === 0 || - notificationRegistrations.some((registration) => !sameAddress(registration.safeAddress, safeAddress)) + return !notificationRegistrations.some( + (registration) => chainId === registration.chainId && sameAddress(registration.safeAddress, safeAddress), ) }) - acc[chainId] = newlyAddedSafes + if (newlyAddedSafes.length > 0) { + acc[chainId] = newlyAddedSafes + } return acc }, {}) @@ -124,7 +128,7 @@ export const PushNotificationsBanner = ({ children }: { children: ReactElement } trackEvent(PUSH_NOTIFICATION_EVENTS.ENABLE_ALL) const allPreferences = getAllPreferences() - const safesToRegister = getSafesToRegister(addedSafes, allPreferences) + const safesToRegister = _getSafesToRegister(addedSafes, allPreferences) try { await assertWalletChain(onboard, safe.chainId)