tooot/App.tsx

141 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-28 16:54:19 +01:00
import NetInfo from '@react-native-community/netinfo'
2020-12-27 18:43:49 +01:00
import client from '@root/api/client'
import { Index } from '@root/Index'
import { persistor, store } from '@root/store'
import { resetLocal } from '@root/utils/slices/instancesSlice'
import ThemeManager from '@utils/styles/ThemeManager'
import * as SplashScreen from 'expo-splash-screen'
import React, { useCallback, useEffect, useState } from 'react'
2020-12-25 18:20:09 +01:00
import { enableScreens } from 'react-native-screens'
2020-12-28 16:54:19 +01:00
import { onlineManager, QueryClient, QueryClientProvider } from 'react-query'
2020-11-21 13:19:05 +01:00
import { Provider } from 'react-redux'
import { PersistGate } from 'redux-persist/integration/react'
2020-12-27 18:43:49 +01:00
import * as Sentry from 'sentry-expo'
2020-12-26 12:59:16 +01:00
if (__DEV__) {
const whyDidYouRender = require('@welldone-software/why-did-you-render')
whyDidYouRender(React, {
trackHooks: true,
hotReloadBufferMs: 1000
})
}
2020-11-17 23:57:23 +01:00
2020-12-28 16:54:19 +01:00
// onlineManager.setEventListener(setOnline => {
// return NetInfo.addEventListener(state => {
// setOnline(state.isConnected)
// })
// })
2020-12-27 18:43:49 +01:00
Sentry.init({
dsn:
'https://c9e29aa05f774aca8f36def98244ce04@o389581.ingest.sentry.io/5571975',
enableInExpoDevelopment: false,
debug: __DEV__
})
2020-12-25 18:20:09 +01:00
const queryClient = new QueryClient()
enableScreens()
2020-12-28 16:54:19 +01:00
NetInfo.fetch().then(state => {
console.log('Connection type', state.type)
console.log('Is connected?', state.isConnected)
console.log('Is internet', state.isInternetReachable)
console.log('Details', state.details)
})
2020-11-23 00:07:32 +01:00
const App: React.FC = () => {
2020-12-28 16:54:19 +01:00
useEffect(() => onlineManager.setOnline(false), [])
const [appLoaded, setAppLoaded] = useState(false)
const [startVerification, setStartVerification] = useState(false)
const [localCorrupt, setLocalCorrupt] = useState(false)
useEffect(() => {
const delaySplash = async () => {
try {
await SplashScreen.preventAutoHideAsync()
} catch (e) {
console.warn(e)
}
}
delaySplash()
}, [])
useEffect(() => {
const hideSplash = async () => {
try {
await SplashScreen.hideAsync()
} catch (e) {
console.warn(e)
}
}
if (appLoaded) {
hideSplash()
}
}, [appLoaded])
const onBeforeLift = useCallback(() => setStartVerification(true), [])
useEffect(() => {
const verifyCredentials = async () => {
const localUrl = store.getState().instances.local.url
const localToken = store.getState().instances.local.token
if (localUrl && localToken) {
client({
method: 'get',
instance: 'remote',
instanceDomain: localUrl,
url: `accounts/verify_credentials`,
headers: { Authorization: `Bearer ${localToken}` }
})
.then(res => {
if (res.body.id !== store.getState().instances.local.account.id) {
store.dispatch(resetLocal())
setLocalCorrupt(true)
}
setAppLoaded(true)
})
.catch(() => {
store.dispatch(resetLocal())
setLocalCorrupt(true)
setAppLoaded(true)
})
} else {
setAppLoaded(true)
}
}
if (startVerification) {
verifyCredentials()
}
}, [startVerification])
const main = useCallback(
bootstrapped => {
if (bootstrapped && appLoaded) {
require('@root/i18n/i18n')
return (
<ThemeManager>
<Index localCorrupt={localCorrupt} />
</ThemeManager>
)
} else {
return null
}
},
[appLoaded]
)
2020-11-23 00:07:32 +01:00
return (
2020-12-21 23:30:26 +01:00
<QueryClientProvider client={queryClient}>
<Provider store={store}>
<PersistGate
persistor={persistor}
onBeforeLift={onBeforeLift}
children={main}
/>
2020-12-21 23:30:26 +01:00
</Provider>
</QueryClientProvider>
2020-11-23 00:07:32 +01:00
)
}
2020-10-31 21:04:46 +01:00
export default App