From 85b34418ef31247f8d88bdb08248a149192c5b46 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 3 May 2024 16:36:58 +0100 Subject: [PATCH] Don't clear toasts when changing users (#3843) * Move ThemeProvider to the top * Move RootSiblingParent above the remounted part * Move ToastContainer outside the remounted part * Remove setTimeout around toasts * Consistently handle dropped session --- src/App.native.tsx | 55 +++++++++++------------ src/App.web.tsx | 60 ++++++++++++++----------- src/lib/hooks/useAccountSwitcher.ts | 7 +-- src/screens/Login/ChooseAccountForm.tsx | 4 +- 4 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/App.native.tsx b/src/App.native.tsx index dc1ccda6..b60d024d 100644 --- a/src/App.native.tsx +++ b/src/App.native.tsx @@ -89,38 +89,37 @@ function InnerApp() { return ( - - - - - - {/* LabelDefsProvider MUST come before ModerationOptsProvider */} - - - - - - - {/* All components should be within this provider */} - + + + + + + + + {/* LabelDefsProvider MUST come before ModerationOptsProvider */} + + + + + - - - - - - - - - - - - + + + + + + + + + + + + ) diff --git a/src/App.web.tsx b/src/App.web.tsx index cf28421c..0fed089c 100644 --- a/src/App.web.tsx +++ b/src/App.web.tsx @@ -4,6 +4,8 @@ import 'view/icons' import React, {useEffect, useState} from 'react' import {RootSiblingParent} from 'react-native-root-siblings' import {SafeAreaProvider} from 'react-native-safe-area-context' +import {msg} from '@lingui/macro' +import {useLingui} from '@lingui/react' import {Provider as StatsigProvider} from '#/lib/statsig/statsig' import {logger} from '#/logger' @@ -30,18 +32,21 @@ import { import {Provider as ShellStateProvider} from 'state/shell' import {Provider as LoggedOutViewProvider} from 'state/shell/logged-out' import {Provider as SelectedFeedProvider} from 'state/shell/selected-feed' +import * as Toast from 'view/com/util/Toast' import {ToastContainer} from 'view/com/util/Toast.web' import {Shell} from 'view/shell/index' import {ThemeProvider as Alf} from '#/alf' import {useColorModeTheme} from '#/alf/util/useColorModeTheme' import {Provider as PortalProvider} from '#/components/Portal' import I18nProvider from './locale/i18nProvider' +import {listenSessionDropped} from './state/events' function InnerApp() { const [isReady, setIsReady] = React.useState(false) const {currentAccount} = useSession() const {initSession} = useSessionApi() const theme = useColorModeTheme() + const {_} = useLingui() useIntentHandler() // init @@ -61,39 +66,44 @@ function InnerApp() { resumeSession(account) }, [initSession]) + useEffect(() => { + return listenSessionDropped(() => { + Toast.show(_(msg`Sorry! Your session expired. Please log in again.`)) + }) + }, [_]) + // wait for session to resume if (!isReady) return null return ( - - - - {/* LabelDefsProvider MUST come before ModerationOptsProvider */} - - - - - - - {/* All components should be within this provider */} - + + + + + + {/* LabelDefsProvider MUST come before ModerationOptsProvider */} + + + + + - - - - - - - - - - - + + + + + + + + + + + ) } diff --git a/src/lib/hooks/useAccountSwitcher.ts b/src/lib/hooks/useAccountSwitcher.ts index de50e533..558fcf74 100644 --- a/src/lib/hooks/useAccountSwitcher.ts +++ b/src/lib/hooks/useAccountSwitcher.ts @@ -41,9 +41,7 @@ export function useAccountSwitcher() { } await initSession(account) logEvent('account:loggedIn', {logContext, withPassword: false}) - setTimeout(() => { - Toast.show(_(msg`Signed in as @${account.handle}`)) - }, 100) + Toast.show(_(msg`Signed in as @${account.handle}`)) } else { requestSwitchToAccount({requestedAccount: account.did}) Toast.show( @@ -56,9 +54,6 @@ export function useAccountSwitcher() { message: e.message, }) clearCurrentAccount() // back user out to login - setTimeout(() => { - Toast.show(_(msg`Sorry! We need you to enter your password.`)) - }, 100) } finally { setPendingDid(null) } diff --git a/src/screens/Login/ChooseAccountForm.tsx b/src/screens/Login/ChooseAccountForm.tsx index e097e0db..098ddeb1 100644 --- a/src/screens/Login/ChooseAccountForm.tsx +++ b/src/screens/Login/ChooseAccountForm.tsx @@ -52,9 +52,7 @@ export const ChooseAccountForm = ({ withPassword: false, }) track('Sign In', {resumedSession: true}) - setTimeout(() => { - Toast.show(_(msg`Signed in as @${account.handle}`)) - }, 100) + Toast.show(_(msg`Signed in as @${account.handle}`)) } catch (e: any) { logger.error('choose account: initSession failed', { message: e.message,