Skip to content

Commit

Permalink
refactor(wallet-mobile): app init
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed May 23, 2024
1 parent 93c3074 commit 15bbb7c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/wallet-mobile/ampli.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Platform": "React Native",
"Language": "TypeScript",
"SDK": "@amplitude/analytics-react-native@^1.0",
"Path": "./src/metrics/ampli",
"Path": "./src/kernel/metrics/ampli",
"InstanceNames": [
"track"
]
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet-mobile/src/InitApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ if (Platform.OS === 'android') {
}

export const InitApp = () => {
const loaded = useInitApp()
if (!loaded) return null
const isLoaded = useInitApp()
if (!isLoaded) return null

return <AppNavigator />
}
Expand Down
49 changes: 29 additions & 20 deletions apps/wallet-mobile/src/YoroiApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {WalletManagerProvider} from './features/WalletManager/context/WalletMana
import {InitApp} from './InitApp'
import {disableLogbox} from './kernel/env'
import {LanguageProvider} from './kernel/i18n'
import {useSetupLogger} from './kernel/logger/hooks/useSetupLogger'
import {makeMetricsManager, MetricsProvider} from './kernel/metrics/metricsManager'
import {useMigrations} from './kernel/storage/migrations/useMigrations'
import {rootStorage} from './kernel/storage/rootStorage'
Expand All @@ -31,15 +32,13 @@ enableFreeze(true)
if (disableLogbox) LogBox.ignoreAllLogs()

const queryClient = new QueryClient()

const metricsManager = makeMetricsManager()

export const YoroiApp = () => {
const migrated = useMigrations(rootStorage)

const Yoroi = () => {
const isMigrated = useMigrations(rootStorage)
const themeStorage = useThemeStorageMaker()

if (!migrated) return null
if (!isMigrated) return null

return (
<AsyncStorageProvider storage={rootStorage}>
Expand All @@ -51,21 +50,17 @@ export const YoroiApp = () => {
<LoadingBoundary style={StyleSheet.absoluteFill}>
<LanguageProvider>
<CurrencyProvider>
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
<RNP.Provider>
<AuthProvider>
<SelectedWalletMetaProvider>
<SelectedWalletProvider>
<LinksProvider>
<SetupWalletProvider>
<InitApp />
</SetupWalletProvider>
</LinksProvider>
</SelectedWalletProvider>
</SelectedWalletMetaProvider>
</AuthProvider>
</RNP.Provider>
</SafeAreaProvider>
<AuthProvider>
<SelectedWalletMetaProvider>
<SelectedWalletProvider>
<LinksProvider>
<SetupWalletProvider>
<InitApp />
</SetupWalletProvider>
</LinksProvider>
</SelectedWalletProvider>
</SelectedWalletMetaProvider>
</AuthProvider>
</CurrencyProvider>
</LanguageProvider>
</LoadingBoundary>
Expand All @@ -77,3 +72,17 @@ export const YoroiApp = () => {
</AsyncStorageProvider>
)
}

export const YoroiApp = () => {
const isReady = useSetupLogger()

if (!isReady) return null

return (
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
<RNP.Provider>
<Yoroi />
</RNP.Provider>
</SafeAreaProvider>
)
}
59 changes: 33 additions & 26 deletions apps/wallet-mobile/src/kernel/logger/hooks/useSetupLogger.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react'

import {getCrashReportsEnabled} from '../../../yoroi-wallets/hooks'
import {appInfo} from '../../appInfo'
import {loggerLevel} from '../../config'
import {isDev, isProduction, sentryDsn} from '../../env'
Expand All @@ -8,36 +9,42 @@ import {Sentry} from '../adapters/sentry'
import {sentryAdapter} from '../adapters/sentry-transporter'
import {logger} from '../logger'

export const useSetupLogger = (initialState: {enabled: boolean}) => {
const ref = React.useRef(initialState.enabled)
ref.current = initialState.enabled
export const useSetupLogger = () => {
const [done, setDone] = React.useState(false)

React.useEffect(() => {
const sampleRate = isProduction ? 0.25 : 1
const initLogger = async () => {
const sampleRate = isProduction ? 0.25 : 1
const isEnabled = await getCrashReportsEnabled()

logger.level = loggerLevel
if (isDev) logger.addTransport(devAdapter().transporter)
logger.level = loggerLevel
if (isDev) logger.addTransport(devAdapter().transporter)

if (ref.current === true) {
logger.enable()
} else {
logger.disable()
}
if (isEnabled) {
logger.enable()
} else {
logger.disable()
}

Sentry.init({
dsn: sentryDsn,
tracesSampleRate: sampleRate,
environment: appInfo.environment,
release: appInfo.release,
dist: appInfo.distribution,
beforeSend(event) {
// https://github.com/getsentry/sentry-javascript/issues/2039
// TODO: this will require to close the app when changing in the settings to take effect
return isEnabled ? event : null
},
})

logger.addTransport(sentryAdapter().transporter)

Sentry.init({
dsn: sentryDsn,
tracesSampleRate: sampleRate,
environment: appInfo.environment,
release: appInfo.release,
dist: appInfo.distribution,
beforeSend(event) {
// https://github.com/getsentry/sentry-javascript/issues/2039
// TODO: this will require to close the app when changing in the settings to take effect
const isEnabled = ref.current
return isEnabled ? event : null
},
})

logger.addTransport(sentryAdapter().transporter)
setDone(true)
}
initLogger()
}, [])

return done
}

0 comments on commit 15bbb7c

Please sign in to comment.