about summary refs log tree commit diff
path: root/src/App.native.tsx
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-05-02 18:17:53 +0100
committerGitHub <noreply@github.com>2024-05-02 18:17:53 +0100
commit5ec945b7625a8daff3baaf76826c2f9e3c12e279 (patch)
tree0de1fcb4a7350410ef7f82760707ab4422d92021 /src/App.native.tsx
parentdadf27fd2f417584093fb344cdacd9dbe332dade (diff)
downloadvoidsky-5ec945b7625a8daff3baaf76826c2f9e3c12e279.tar.zst
[Session] Extract resumeSession out (#3811)
Diffstat (limited to 'src/App.native.tsx')
-rw-r--r--src/App.native.tsx32
1 files changed, 24 insertions, 8 deletions
diff --git a/src/App.native.tsx b/src/App.native.tsx
index ac6c2e83d..dc1ccda6d 100644
--- a/src/App.native.tsx
+++ b/src/App.native.tsx
@@ -16,6 +16,7 @@ import {useLingui} from '@lingui/react'
 import {useQueryClient} from '@tanstack/react-query'
 
 import {Provider as StatsigProvider} from '#/lib/statsig/statsig'
+import {logger} from '#/logger'
 import {init as initPersistedState} from '#/state/persisted'
 import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs'
 import {Provider as ModerationOptsProvider} from '#/state/preferences/moderation-opts'
@@ -34,6 +35,7 @@ import {Provider as PrefsStateProvider} from 'state/preferences'
 import {Provider as UnreadNotifsProvider} from 'state/queries/notifications/unread'
 import {
   Provider as SessionProvider,
+  SessionAccount,
   useSession,
   useSessionApi,
 } from 'state/session'
@@ -53,8 +55,9 @@ import {listenSessionDropped} from './state/events'
 SplashScreen.preventAutoHideAsync()
 
 function InnerApp() {
-  const {isInitialLoad, currentAccount} = useSession()
-  const {resumeSession} = useSessionApi()
+  const [isReady, setIsReady] = React.useState(false)
+  const {currentAccount} = useSession()
+  const {initSession} = useSessionApi()
   const theme = useColorModeTheme()
   const {_} = useLingui()
 
@@ -62,18 +65,31 @@ function InnerApp() {
 
   // init
   useEffect(() => {
-    listenSessionDropped(() => {
-      Toast.show(_(msg`Sorry! Your session expired. Please log in again.`))
-    })
-
+    async function resumeSession(account?: SessionAccount) {
+      try {
+        if (account) {
+          await initSession(account)
+        }
+      } catch (e) {
+        logger.error(`session: resumeSession failed`, {message: e})
+      } finally {
+        setIsReady(true)
+      }
+    }
     const account = readLastActiveAccount()
     resumeSession(account)
-  }, [resumeSession, _])
+  }, [initSession])
+
+  useEffect(() => {
+    return listenSessionDropped(() => {
+      Toast.show(_(msg`Sorry! Your session expired. Please log in again.`))
+    })
+  }, [_])
 
   return (
     <SafeAreaProvider initialMetrics={initialWindowMetrics}>
       <Alf theme={theme}>
-        <Splash isReady={!isInitialLoad}>
+        <Splash isReady={isReady}>
           <React.Fragment
             // Resets the entire tree below when it changes:
             key={currentAccount?.did}>