diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/hooks/useNotificationHandler.ts | 15 | ||||
-rw-r--r-- | src/lib/notifications/notifications.ts | 8 | ||||
-rw-r--r-- | src/state/queries/messages/list-converations.ts | 17 | ||||
-rw-r--r-- | src/state/queries/notifications/unread.tsx | 2 |
4 files changed, 18 insertions, 24 deletions
diff --git a/src/lib/hooks/useNotificationHandler.ts b/src/lib/hooks/useNotificationHandler.ts index 3f1cd439d..f3c4da7f6 100644 --- a/src/lib/hooks/useNotificationHandler.ts +++ b/src/lib/hooks/useNotificationHandler.ts @@ -46,8 +46,9 @@ const DEFAULT_HANDLER_OPTIONS = { shouldSetBadge: true, } -// This needs to stay outside the hook to persist between account switches +// These need to stay outside the hook to persist between account switches let storedPayload: NotificationPayload | undefined +let prevDate = 0 export function useNotificationsHandler() { const queryClient = useQueryClient() @@ -58,9 +59,6 @@ export function useNotificationsHandler() { const {setShowLoggedOut} = useLoggedOutViewControls() const closeAllActiveElements = useCloseAllActiveElements() - // Safety to prevent double handling of the same notification - const prevDate = React.useRef(0) - React.useEffect(() => { if (!isAndroid) return @@ -169,11 +167,10 @@ export function useNotificationsHandler() { payload.reason === 'chat-message' && payload.recipientDid === currentAccount?.did ) { - const isCurrentConvo = payload.convoId === currentConvoId return { - shouldShowAlert: !isCurrentConvo, + shouldShowAlert: payload.convoId !== currentConvoId, shouldPlaySound: false, - shouldSetBadge: !isCurrentConvo, + shouldSetBadge: false, } } @@ -185,10 +182,10 @@ export function useNotificationsHandler() { const responseReceivedListener = Notifications.addNotificationResponseReceivedListener(e => { - if (e.notification.date === prevDate.current) { + if (e.notification.date === prevDate) { return } - prevDate.current = e.notification.date + prevDate = e.notification.date logger.debug( 'Notifications: response received', diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 6f28608f4..f9fbdb8bf 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -113,10 +113,16 @@ export function useRequestNotificationsPermission() { ) } -export async function decrementBadgeCount(by = 1) { +export async function decrementBadgeCount(by: number | 'reset' = 1) { if (!isNative) return const currCount = await getBadgeCountAsync() + + if (by === 'reset') { + await setBadgeCountAsync(0) + return + } + let newCount = currCount - by if (newCount < 0) { newCount = 0 diff --git a/src/state/queries/messages/list-converations.ts b/src/state/queries/messages/list-converations.ts index 3939ab8e3..8319fdefe 100644 --- a/src/state/queries/messages/list-converations.ts +++ b/src/state/queries/messages/list-converations.ts @@ -15,7 +15,6 @@ import {useCurrentConvoId} from '#/state/messages/current-convo-id' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {useAgent, useSession} from '#/state/session' -import {decrementBadgeCount} from 'lib/notifications/notifications' export const RQKEY = ['convo-list'] type RQPageParam = string | undefined @@ -135,18 +134,10 @@ export function useOnMarkAsRead() { return useCallback( (chatId: string) => { queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => { - return optimisticUpdate(chatId, old, convo => { - // We only want to decrement the badge by one no matter the unread count, since we only increment once per - // sender regardless of message count - if (convo.unreadCount > 0) { - decrementBadgeCount(1) - } - - return { - ...convo, - unreadCount: 0, - } - }) + return optimisticUpdate(chatId, old, convo => ({ + ...convo, + unreadCount: 0, + })) }) }, [queryClient], diff --git a/src/state/queries/notifications/unread.tsx b/src/state/queries/notifications/unread.tsx index 9d35ee19f..acc68c360 100644 --- a/src/state/queries/notifications/unread.tsx +++ b/src/state/queries/notifications/unread.tsx @@ -119,7 +119,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { // update & broadcast setNumUnread('') broadcast.postMessage({event: ''}) - decrementBadgeCount(Math.min(cacheRef.current.unreadCount, 30)) + decrementBadgeCount('reset') }, async checkUnread({ |