mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Ready for push feature
This commit is contained in:
54
src/utils/push/useRespond.ts
Normal file
54
src/utils/push/useRespond.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { NavigationContainerRef } from '@react-navigation/native'
|
||||
import { Dispatch } from '@reduxjs/toolkit'
|
||||
import { QueryKeyTimeline } from '@utils/queryHooks/timeline'
|
||||
import { Instance, updateInstanceActive } from '@utils/slices/instancesSlice'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { findIndex } from 'lodash'
|
||||
import { useEffect } from 'react'
|
||||
import { QueryClient } from 'react-query'
|
||||
import pushUseNavigate from './useNavigate'
|
||||
|
||||
export interface Params {
|
||||
navigationRef: React.RefObject<NavigationContainerRef>
|
||||
queryClient: QueryClient
|
||||
instances: Instance[]
|
||||
dispatch: Dispatch<any>
|
||||
}
|
||||
|
||||
const pushUseRespond = ({
|
||||
navigationRef,
|
||||
queryClient,
|
||||
instances,
|
||||
dispatch
|
||||
}: Params) => {
|
||||
return useEffect(() => {
|
||||
const subscription = Notifications.addNotificationResponseReceivedListener(
|
||||
({ notification }) => {
|
||||
const queryKey: QueryKeyTimeline = [
|
||||
'Timeline',
|
||||
{ page: 'Notifications' }
|
||||
]
|
||||
queryClient.invalidateQueries(queryKey)
|
||||
const payloadData = notification.request.content.data as {
|
||||
notification_id?: string
|
||||
instanceUrl: string
|
||||
accountId: string
|
||||
}
|
||||
|
||||
const notificationIndex = findIndex(
|
||||
instances,
|
||||
instance =>
|
||||
instance.url === payloadData.instanceUrl &&
|
||||
instance.account.id === payloadData.accountId
|
||||
)
|
||||
if (notificationIndex !== -1) {
|
||||
dispatch(updateInstanceActive(instances[notificationIndex]))
|
||||
}
|
||||
pushUseNavigate(navigationRef, payloadData.notification_id)
|
||||
}
|
||||
)
|
||||
return () => subscription.remove()
|
||||
}, [instances])
|
||||
}
|
||||
|
||||
export default pushUseRespond
|
Reference in New Issue
Block a user