mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
619 restructure local storage (#628)
* To MMKV migration working * POC migrated font size settings * Moved settings to mmkv * Fix typos * Migrated contexts slice * Migrated app slice * POC instance emoji update * Migrated drafts * Migrated simple instance properties * All migrated! * Re-structure files * Tolerant of undefined settings * Can properly logging in and out including empty state
This commit is contained in:
@ -1,30 +1,33 @@
|
||||
import apiGeneral from '@api/general'
|
||||
import apiTooot from '@api/tooot'
|
||||
import { displayMessage } from '@components/Message'
|
||||
import navigationRef from '@helpers/navigationRef'
|
||||
import { useAppDispatch } from '@root/store'
|
||||
import * as Sentry from '@sentry/react-native'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { getExpoToken, retrieveExpoToken } from '@utils/slices/appSlice'
|
||||
import { disableAllPushes, getInstances } from '@utils/slices/instancesSlice'
|
||||
import apiGeneral from '@utils/api/general'
|
||||
import apiTooot from '@utils/api/tooot'
|
||||
import navigationRef from '@utils/navigation/navigationRef'
|
||||
import {
|
||||
getAccountDetails,
|
||||
getGlobalStorage,
|
||||
setAccountStorage,
|
||||
useGlobalStorage
|
||||
} from '@utils/storage/actions'
|
||||
import { AxiosError } from 'axios'
|
||||
import * as Notifications from 'expo-notifications'
|
||||
import { useEffect } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { AppState } from 'react-native'
|
||||
import { useSelector } from 'react-redux'
|
||||
import { updateExpoToken } from './updateExpoToken'
|
||||
|
||||
const pushUseConnect = () => {
|
||||
const { t } = useTranslation('screens')
|
||||
|
||||
const dispatch = useAppDispatch()
|
||||
useEffect(() => {
|
||||
dispatch(retrieveExpoToken())
|
||||
updateExpoToken()
|
||||
}, [])
|
||||
|
||||
const expoToken = useSelector(getExpoToken)
|
||||
const instances = useSelector(getInstances, (prev, next) => prev.length === next.length)
|
||||
const pushEnabled = instances.filter(instance => instance.push.global)
|
||||
const [expoToken] = useGlobalStorage.string('app.expo_token')
|
||||
const pushEnabledCount = getGlobalStorage.object('accounts')?.filter(account => {
|
||||
return getAccountDetails(['push'], account)?.push?.global
|
||||
}).length
|
||||
|
||||
const connectQuery = useQuery<any, AxiosError>(
|
||||
['tooot', { endpoint: 'push/connect' }],
|
||||
@ -68,19 +71,24 @@ const pushUseConnect = () => {
|
||||
}
|
||||
})
|
||||
|
||||
dispatch(disableAllPushes())
|
||||
getGlobalStorage.object('accounts')?.forEach(account => {
|
||||
const accountDetails = getAccountDetails(['push', 'auth.domain', 'auth.token'], account)
|
||||
if (!accountDetails) return
|
||||
|
||||
instances.forEach(instance => {
|
||||
if (instance.push.global) {
|
||||
if (accountDetails.push.global) {
|
||||
apiGeneral<{}>({
|
||||
method: 'delete',
|
||||
domain: instance.url,
|
||||
domain: accountDetails['auth.domain'],
|
||||
url: 'api/v1/push/subscription',
|
||||
headers: {
|
||||
Authorization: `Bearer ${instance.token}`
|
||||
Authorization: `Bearer ${accountDetails['auth.token']}`
|
||||
}
|
||||
}).catch(() => console.log('error!!!'))
|
||||
}
|
||||
setAccountStorage(
|
||||
[{ key: 'push', value: { ...accountDetails?.push, global: false } }],
|
||||
account
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -88,14 +96,14 @@ const pushUseConnect = () => {
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
Sentry.setContext('Push', { expoToken, pushEnabledCount: pushEnabled.length })
|
||||
Sentry.setContext('Push', { expoToken, pushEnabledCount })
|
||||
|
||||
if (expoToken && pushEnabled.length) {
|
||||
if (expoToken && pushEnabledCount) {
|
||||
connectQuery.refetch()
|
||||
}
|
||||
|
||||
const appStateListener = AppState.addEventListener('change', state => {
|
||||
if (expoToken && pushEnabled.length && state === 'active') {
|
||||
if (expoToken && pushEnabledCount && state === 'active') {
|
||||
Notifications.getBadgeCountAsync().then(count => {
|
||||
if (count > 0) {
|
||||
connectQuery.refetch()
|
||||
@ -107,7 +115,7 @@ const pushUseConnect = () => {
|
||||
return () => {
|
||||
appStateListener.remove()
|
||||
}
|
||||
}, [expoToken, pushEnabled.length])
|
||||
}, [expoToken, pushEnabledCount])
|
||||
}
|
||||
|
||||
export default pushUseConnect
|
||||
|
Reference in New Issue
Block a user