Bluesky app fork with some witchin' additions 💫
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[Session] Extract resumeSession out (#3811)

authored by

dan and committed by
GitHub
5ec945b7 dadf27fd

+43 -36
+24 -8
src/App.native.tsx
··· 16 16 import {useQueryClient} from '@tanstack/react-query' 17 17 18 18 import {Provider as StatsigProvider} from '#/lib/statsig/statsig' 19 + import {logger} from '#/logger' 19 20 import {init as initPersistedState} from '#/state/persisted' 20 21 import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs' 21 22 import {Provider as ModerationOptsProvider} from '#/state/preferences/moderation-opts' ··· 34 35 import {Provider as UnreadNotifsProvider} from 'state/queries/notifications/unread' 35 36 import { 36 37 Provider as SessionProvider, 38 + SessionAccount, 37 39 useSession, 38 40 useSessionApi, 39 41 } from 'state/session' ··· 53 55 SplashScreen.preventAutoHideAsync() 54 56 55 57 function InnerApp() { 56 - const {isInitialLoad, currentAccount} = useSession() 57 - const {resumeSession} = useSessionApi() 58 + const [isReady, setIsReady] = React.useState(false) 59 + const {currentAccount} = useSession() 60 + const {initSession} = useSessionApi() 58 61 const theme = useColorModeTheme() 59 62 const {_} = useLingui() 60 63 ··· 62 65 63 66 // init 64 67 useEffect(() => { 65 - listenSessionDropped(() => { 66 - Toast.show(_(msg`Sorry! Your session expired. Please log in again.`)) 67 - }) 68 - 68 + async function resumeSession(account?: SessionAccount) { 69 + try { 70 + if (account) { 71 + await initSession(account) 72 + } 73 + } catch (e) { 74 + logger.error(`session: resumeSession failed`, {message: e}) 75 + } finally { 76 + setIsReady(true) 77 + } 78 + } 69 79 const account = readLastActiveAccount() 70 80 resumeSession(account) 71 - }, [resumeSession, _]) 81 + }, [initSession]) 82 + 83 + useEffect(() => { 84 + return listenSessionDropped(() => { 85 + Toast.show(_(msg`Sorry! Your session expired. Please log in again.`)) 86 + }) 87 + }, [_]) 72 88 73 89 return ( 74 90 <SafeAreaProvider initialMetrics={initialWindowMetrics}> 75 91 <Alf theme={theme}> 76 - <Splash isReady={!isInitialLoad}> 92 + <Splash isReady={isReady}> 77 93 <React.Fragment 78 94 // Resets the entire tree below when it changes: 79 95 key={currentAccount?.did}>
+18 -4
src/App.web.tsx
··· 6 6 import {SafeAreaProvider} from 'react-native-safe-area-context' 7 7 8 8 import {Provider as StatsigProvider} from '#/lib/statsig/statsig' 9 + import {logger} from '#/logger' 9 10 import {init as initPersistedState} from '#/state/persisted' 10 11 import {Provider as LabelDefsProvider} from '#/state/preferences/label-defs' 11 12 import {Provider as ModerationOptsProvider} from '#/state/preferences/moderation-opts' ··· 22 23 import {Provider as UnreadNotifsProvider} from 'state/queries/notifications/unread' 23 24 import { 24 25 Provider as SessionProvider, 26 + SessionAccount, 25 27 useSession, 26 28 useSessionApi, 27 29 } from 'state/session' ··· 36 38 import I18nProvider from './locale/i18nProvider' 37 39 38 40 function InnerApp() { 39 - const {isInitialLoad, currentAccount} = useSession() 40 - const {resumeSession} = useSessionApi() 41 + const [isReady, setIsReady] = React.useState(false) 42 + const {currentAccount} = useSession() 43 + const {initSession} = useSessionApi() 41 44 const theme = useColorModeTheme() 42 45 useIntentHandler() 43 46 44 47 // init 45 48 useEffect(() => { 49 + async function resumeSession(account?: SessionAccount) { 50 + try { 51 + if (account) { 52 + await initSession(account) 53 + } 54 + } catch (e) { 55 + logger.error(`session: resumeSession failed`, {message: e}) 56 + } finally { 57 + setIsReady(true) 58 + } 59 + } 46 60 const account = readLastActiveAccount() 47 61 resumeSession(account) 48 - }, [resumeSession]) 62 + }, [initSession]) 49 63 50 64 // wait for session to resume 51 - if (isInitialLoad) return null 65 + if (!isReady) return null 52 66 53 67 return ( 54 68 <Alf theme={theme}>
+1 -22
src/state/session/index.tsx
··· 35 35 configureModerationForGuest() 36 36 37 37 const StateContext = React.createContext<SessionStateContext>({ 38 - isInitialLoad: true, 39 38 isSwitchingAccounts: false, 40 39 accounts: [], 41 40 currentAccount: undefined, ··· 47 46 login: async () => {}, 48 47 logout: async () => {}, 49 48 initSession: async () => {}, 50 - resumeSession: async () => {}, 51 49 removeAccount: () => {}, 52 50 selectAccount: async () => {}, 53 51 updateCurrentAccount: () => {}, ··· 67 65 } 68 66 69 67 export function Provider({children}: React.PropsWithChildren<{}>) { 70 - const [isInitialLoad, setIsInitialLoad] = React.useState(true) 71 68 const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false) 72 69 const [state, setState] = React.useState<State>({ 73 70 accounts: persisted.get('session').accounts, ··· 389 386 [upsertAccount, clearCurrentAccount, createPersistSessionHandler], 390 387 ) 391 388 392 - const resumeSession = React.useCallback<SessionApiContext['resumeSession']>( 393 - async account => { 394 - try { 395 - if (account) { 396 - await initSession(account) 397 - } 398 - } catch (e) { 399 - logger.error(`session: resumeSession failed`, {message: e}) 400 - } finally { 401 - setIsInitialLoad(false) 402 - } 403 - }, 404 - [initSession], 405 - ) 406 - 407 389 const removeAccount = React.useCallback<SessionApiContext['removeAccount']>( 408 390 account => { 409 391 setState(s => { ··· 547 529 currentAccount: state.accounts.find( 548 530 a => a.did === state.currentAccountDid, 549 531 ), 550 - isInitialLoad, 551 532 isSwitchingAccounts, 552 533 hasSession: !!state.currentAccountDid, 553 534 }), 554 - [state, isInitialLoad, isSwitchingAccounts], 535 + [state, isSwitchingAccounts], 555 536 ) 556 537 557 538 const api = React.useMemo( ··· 560 541 login, 561 542 logout, 562 543 initSession, 563 - resumeSession, 564 544 removeAccount, 565 545 selectAccount, 566 546 updateCurrentAccount, ··· 571 551 login, 572 552 logout, 573 553 initSession, 574 - resumeSession, 575 554 removeAccount, 576 555 selectAccount, 577 556 updateCurrentAccount,
-2
src/state/session/types.ts
··· 6 6 export type SessionStateContext = { 7 7 accounts: SessionAccount[] 8 8 currentAccount: SessionAccount | undefined 9 - isInitialLoad: boolean 10 9 isSwitchingAccounts: boolean 11 10 hasSession: boolean 12 11 } ··· 46 45 */ 47 46 clearCurrentAccount: () => void 48 47 initSession: (account: SessionAccount) => Promise<void> 49 - resumeSession: (account?: SessionAccount) => Promise<void> 50 48 removeAccount: (account: SessionAccount) => void 51 49 selectAccount: ( 52 50 account: SessionAccount,