|
| 1 | +import 'react-native-get-random-values' |
| 2 | + |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 4 | +import { SplashScreen, Stack } from 'expo-router' |
| 5 | +import { StatusBar } from 'expo-status-bar' |
| 6 | +import { useEffect } from 'react' |
| 7 | +import { ActivityIndicator, View } from 'react-native' |
| 8 | +import { useAccount, useReconnect, WagmiProvider } from 'wagmi' |
| 9 | +import { wagmiConfig } from '@/wagmi/config' |
| 10 | + |
| 11 | +SplashScreen.preventAutoHideAsync() |
| 12 | + |
| 13 | +const queryClient = new QueryClient() |
| 14 | + |
| 15 | +export default function RootLayout() { |
| 16 | + return ( |
| 17 | + // `reconnectOnMount={false}` because wagmi's built-in reconnect runs |
| 18 | + // synchronously inside `<Hydrate>`'s render and mutates the store, |
| 19 | + // which surfaces as "Cannot update a component while rendering a |
| 20 | + // different component" warnings whenever this provider re-renders |
| 21 | + // (e.g. on expo-router navigation transitions). We trigger reconnect |
| 22 | + // ourselves from a mount effect below, after commit. |
| 23 | + <WagmiProvider config={wagmiConfig} reconnectOnMount={false}> |
| 24 | + <QueryClientProvider client={queryClient}> |
| 25 | + <App /> |
| 26 | + <StatusBar style="auto" /> |
| 27 | + </QueryClientProvider> |
| 28 | + </WagmiProvider> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +function App() { |
| 33 | + const { address } = useAccount() |
| 34 | + const { reconnect, isSuccess, isError } = useReconnect() |
| 35 | + |
| 36 | + // Manual replacement for `reconnectOnMount` (see WagmiProvider above). |
| 37 | + useEffect(() => { |
| 38 | + reconnect() |
| 39 | + }, [reconnect]) |
| 40 | + |
| 41 | + // Gate the UI on the reconnect mutation settling rather than on |
| 42 | + // `useConnection().status`. With `reconnectOnMount={false}` the store |
| 43 | + // initially reports `disconnected`, which would otherwise flash the |
| 44 | + // login screen for a moment before the saved session is restored. |
| 45 | + const isReady = isSuccess || isError |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (isReady) SplashScreen.hideAsync() |
| 49 | + }, [isReady]) |
| 50 | + |
| 51 | + if (!isReady) { |
| 52 | + return ( |
| 53 | + <View |
| 54 | + style={{ |
| 55 | + flex: 1, |
| 56 | + backgroundColor: '#E6F4FE', |
| 57 | + justifyContent: 'center', |
| 58 | + alignItems: 'center', |
| 59 | + }} |
| 60 | + > |
| 61 | + <ActivityIndicator size="large" color="#4285F4" /> |
| 62 | + </View> |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <Stack screenOptions={{ headerShown: false }}> |
| 68 | + <Stack.Protected guard={address !== undefined}> |
| 69 | + <Stack.Screen name="index" /> |
| 70 | + </Stack.Protected> |
| 71 | + <Stack.Protected guard={address === undefined}> |
| 72 | + <Stack.Screen name="login" /> |
| 73 | + <Stack.Screen name="oauth-callback" options={{ animation: 'none' }} /> |
| 74 | + </Stack.Protected> |
| 75 | + </Stack> |
| 76 | + ) |
| 77 | +} |
0 commit comments