tooot/src/utils/push/useConnect.ts

113 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-03-04 01:03:53 +01:00
import apiGeneral from '@api/general'
2021-06-21 11:59:29 +02:00
import apiTooot from '@api/tooot'
2021-03-04 01:03:53 +01:00
import { displayMessage } from '@components/Message'
2021-08-29 15:25:38 +02:00
import navigationRef from '@helpers/navigationRef'
2022-04-30 23:47:52 +02:00
import { useAppDispatch } from '@root/store'
2022-11-14 16:04:15 +01:00
import * as Sentry from '@sentry/react-native'
import { InstanceLatest } from '@utils/migrations/instances/migration'
import { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice'
import { disableAllPushes } from '@utils/slices/instancesSlice'
2022-01-30 22:51:03 +01:00
import { useTheme } from '@utils/styles/ThemeManager'
2021-03-04 01:03:53 +01:00
import * as Notifications from 'expo-notifications'
2022-10-27 22:51:02 +02:00
import { TFunction } from 'i18next'
2021-03-04 01:03:53 +01:00
import { useEffect } from 'react'
2022-06-03 21:25:20 +02:00
import { AppState } from 'react-native'
import { useSelector } from 'react-redux'
2021-03-04 01:03:53 +01:00
export interface Params {
2021-03-28 23:31:10 +02:00
t: TFunction<'screens'>
instances: InstanceLatest[]
2021-03-04 01:03:53 +01:00
}
2022-01-30 22:51:03 +01:00
const pushUseConnect = ({ t, instances }: Params) => {
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
2022-02-12 14:51:01 +01:00
const { theme } = useTheme()
2022-06-03 21:25:20 +02:00
useEffect(() => {
dispatch(retrieveExpoToken())
2022-06-03 21:25:20 +02:00
}, [])
2022-01-30 22:51:03 +01:00
2022-06-03 21:25:20 +02:00
const expoToken = useSelector(getExpoToken)
const connect = () => {
apiTooot({
method: 'get',
url: `push/connect/${expoToken}`,
sentry: true
2022-11-22 22:00:33 +01:00
})
.then(() => Notifications.setBadgeCountAsync(0))
.catch(error => {
Sentry.setExtras({
API: 'tooot',
...(error?.response && { response: error.response }),
...(error?.request && { request: error.request })
2022-06-03 21:25:20 +02:00
})
2022-11-22 22:00:33 +01:00
Sentry.captureException(error)
Notifications.setBadgeCountAsync(0)
if (error?.status == 404) {
displayMessage({
theme,
type: 'error',
duration: 'long',
message: t('pushError.message'),
description: t('pushError.description'),
onPress: () => {
navigationRef.navigate('Screen-Tabs', {
screen: 'Tab-Me',
params: {
screen: 'Tab-Me-Root'
}
})
navigationRef.navigate('Screen-Tabs', {
screen: 'Tab-Me',
params: {
screen: 'Tab-Me-Settings'
}
})
}
})
2021-03-04 01:03:53 +01:00
2022-11-22 22:00:33 +01:00
dispatch(disableAllPushes())
2021-03-04 01:03:53 +01:00
2022-11-22 22:00:33 +01:00
instances.forEach(instance => {
if (instance.push.global.value) {
apiGeneral<{}>({
method: 'delete',
domain: instance.url,
url: 'api/v1/push/subscription',
headers: {
Authorization: `Bearer ${instance.token}`
}
}).catch(() => console.log('error!!!'))
}
})
}
})
2022-06-03 21:25:20 +02:00
}
2021-03-04 01:03:53 +01:00
2022-06-03 21:25:20 +02:00
const pushEnabled = instances.filter(instance => instance.push.global.value)
useEffect(() => {
const appStateListener = AppState.addEventListener('change', state => {
if (expoToken && pushEnabled.length && state === 'active') {
Notifications.getBadgeCountAsync().then(count => {
if (count > 0) {
connect()
}
})
}
})
return () => {
appStateListener.remove()
2021-03-04 01:03:53 +01:00
}
2022-06-03 21:25:20 +02:00
}, [expoToken, pushEnabled.length])
2021-03-04 01:03:53 +01:00
2022-06-03 21:25:20 +02:00
return useEffect(() => {
if (expoToken && pushEnabled.length) {
2021-12-12 22:09:18 +01:00
connect()
2021-03-04 01:03:53 +01:00
}
2022-06-03 21:25:20 +02:00
}, [expoToken, pushEnabled.length])
2021-03-04 01:03:53 +01:00
}
export default pushUseConnect