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 { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice'
import { disableAllPushes, getInstances } 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'
import { useEffect } from 'react'
import { useTranslation } from 'react-i18next'
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
const pushUseConnect = () => {
const { t } = useTranslation('screen')
const { theme } = useTheme()
2021-03-04 01:03:53 +01:00
2022-04-30 23:47:52 +02:00
const dispatch = useAppDispatch()
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 instances = useSelector(getInstances, (prev, next) => prev.length === next.length)
const pushEnabled = instances.filter(instance => instance.push.global.value)
2022-06-03 21:25:20 +02:00
const connect = () => {
apiTooot({
method: 'get',
url: `/push/connect/${expoToken}`
2022-11-22 22:00:33 +01:00
})
.then(() => Notifications.setBadgeCountAsync(0))
.catch(error => {
Sentry.setExtras({
API: 'tooot',
expoToken,
...(error?.response && { response: error.response })
})
Sentry.captureMessage('Push connect error', {
contexts: { errorObject: error }
2022-06-03 21:25:20 +02:00
})
2022-11-22 22:00:33 +01:00
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
useEffect(() => {
Sentry.setExtras({
expoToken,
pushEnabledCount: pushEnabled
})
if (expoToken && pushEnabled.length) {
connect()
}
2022-06-03 21:25:20 +02:00
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
}
export default pushUseConnect