/
AppInitGate.tsx
79 lines (67 loc) · 2.48 KB
/
AppInitGate.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import locales from 'locales'
import React, { useEffect } from 'react'
import { useAsync } from 'react-async-hook'
import { Dimensions } from 'react-native'
import { findBestAvailableLanguage } from 'react-native-localize'
import { AppEvents } from 'src/analytics/Events'
import ValoraAnalytics from 'src/analytics/ValoraAnalytics'
import { appMounted, appUnmounted } from 'src/app/actions'
import { isE2EEnv } from 'src/config'
import i18n from 'src/i18n'
import { currentLanguageSelector } from 'src/i18n/selectors'
import useChangeLanguage from 'src/i18n/useChangeLanguage'
import { navigateToError } from 'src/navigator/NavigationService'
import { useDispatch, useSelector } from 'src/redux/hooks'
import { waitUntilSagasFinishLoading } from 'src/redux/sagas'
import Logger from 'src/utils/Logger'
const TAG = 'AppInitGate'
interface Props {
reactLoadTime: number
appStartedMillis: number
children: React.ReactNode
}
const AppInitGate = ({ appStartedMillis, reactLoadTime, children }: Props) => {
const changelanguage = useChangeLanguage()
const dispatch = useDispatch()
const language = useSelector(currentLanguageSelector)
const bestLanguage = !isE2EEnv
? findBestAvailableLanguage(Object.keys(locales))?.languageTag
: 'en-US'
useEffect(() => {
return () => {
dispatch(appUnmounted())
}
}, [])
const initResult = useAsync(
async () => {
Logger.debug(TAG, 'Starting AppInitGate init')
await waitUntilSagasFinishLoading()
if (!language && bestLanguage) {
await changelanguage(bestLanguage)
}
const reactLoadDuration = (reactLoadTime - appStartedMillis) / 1000
const appLoadDuration = (Date.now() - appStartedMillis) / 1000
Logger.debug('TAG', `reactLoad: ${reactLoadDuration} appLoad: ${appLoadDuration}`)
const { width, height } = Dimensions.get('window')
ValoraAnalytics.startSession(AppEvents.app_launched, {
deviceHeight: height,
deviceWidth: width,
reactLoadDuration,
appLoadDuration,
language: i18n.language || language,
})
Logger.debug(TAG, 'AppInitGate init completed')
dispatch(appMounted())
},
[],
{
onError: (error) => {
Logger.error(TAG, 'Failed init', error)
navigateToError('appInitFailed', error)
},
}
)
// type assertion here because https://github.com/DefinitelyTyped/DefinitelyTyped/issues/44572
return initResult.loading ? null : (children as JSX.Element)
}
export default AppInitGate