Make notifications init reactive to queryClient (#3329)

zio/stable
dan 2024-04-03 23:42:28 +01:00 committed by GitHub
parent fc1e30afd6
commit 6c728f79de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 51 deletions

View File

@ -19,8 +19,8 @@ import {init as initPersistedState} from '#/state/persisted'
import * as persisted from '#/state/persisted' import * as persisted from '#/state/persisted'
import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs' import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs'
import {useIntentHandler} from 'lib/hooks/useIntentHandler' import {useIntentHandler} from 'lib/hooks/useIntentHandler'
import {useNotificationsListener} from 'lib/notifications/notifications'
import {useOTAUpdates} from 'lib/hooks/useOTAUpdates' import {useOTAUpdates} from 'lib/hooks/useOTAUpdates'
import * as notifications from 'lib/notifications/notifications'
import { import {
asyncStoragePersister, asyncStoragePersister,
dehydrateOptions, dehydrateOptions,
@ -61,11 +61,11 @@ function InnerApp() {
const theme = useColorModeTheme() const theme = useColorModeTheme()
const {_} = useLingui() const {_} = useLingui()
useIntentHandler() useIntentHandler()
useNotificationsListener(queryClient)
useOTAUpdates() useOTAUpdates()
// init // init
useEffect(() => { useEffect(() => {
notifications.init(queryClient)
listenSessionDropped(() => { listenSessionDropped(() => {
Toast.show(_(msg`Sorry! Your session expired. Please log in again.`)) Toast.show(_(msg`Sorry! Your session expired. Please log in again.`))
}) })

View File

@ -1,12 +1,14 @@
import {useEffect} from 'react'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import {QueryClient} from '@tanstack/react-query' import {QueryClient} from '@tanstack/react-query'
import {resetToTab} from '../../Navigation'
import {devicePlatform, isIOS} from 'platform/detection'
import {track} from 'lib/analytics/analytics'
import {logger} from '#/logger' import {logger} from '#/logger'
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed' import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed'
import {truncateAndInvalidate} from '#/state/queries/util' import {truncateAndInvalidate} from '#/state/queries/util'
import {SessionAccount, getAgent} from '#/state/session' import {getAgent, SessionAccount} from '#/state/session'
import {track} from 'lib/analytics/analytics'
import {devicePlatform, isIOS} from 'platform/detection'
import {resetToTab} from '../../Navigation'
import {logEvent} from '../statsig/statsig' import {logEvent} from '../statsig/statsig'
const SERVICE_DID = (serviceUrl?: string) => const SERVICE_DID = (serviceUrl?: string) =>
@ -80,10 +82,11 @@ export function registerTokenChangeHandler(
} }
} }
export function init(queryClient: QueryClient) { export function useNotificationsListener(queryClient: QueryClient) {
useEffect(() => {
// handle notifications that are received, both in the foreground or background // handle notifications that are received, both in the foreground or background
// NOTE: currently just here for debug logging // NOTE: currently just here for debug logging
Notifications.addNotificationReceivedListener(event => { const sub1 = Notifications.addNotificationReceivedListener(event => {
logger.debug( logger.debug(
'Notifications: received', 'Notifications: received',
{event}, {event},
@ -109,7 +112,8 @@ export function init(queryClient: QueryClient) {
}) })
// handle notifications that are tapped on // handle notifications that are tapped on
Notifications.addNotificationResponseReceivedListener(response => { const sub2 = Notifications.addNotificationResponseReceivedListener(
response => {
logger.debug( logger.debug(
'Notifications: response received', 'Notifications: response received',
{ {
@ -117,7 +121,9 @@ export function init(queryClient: QueryClient) {
}, },
logger.DebugContext.notifications, logger.DebugContext.notifications,
) )
if (response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER) { if (
response.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
) {
logger.debug( logger.debug(
'User pressed a notification, opening notifications tab', 'User pressed a notification, opening notifications tab',
{}, {},
@ -128,5 +134,11 @@ export function init(queryClient: QueryClient) {
truncateAndInvalidate(queryClient, RQKEY_NOTIFS()) truncateAndInvalidate(queryClient, RQKEY_NOTIFS())
resetToTab('NotificationsTab') // open notifications tab resetToTab('NotificationsTab') // open notifications tab
} }
}) },
)
return () => {
sub1.remove()
sub2.remove()
}
}, [queryClient])
} }