From fd001836edcd75a508400e40adaf6cae436b76c1 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 4 Apr 2024 14:06:32 -0500 Subject: [PATCH] Some comments --- src/state/session/index.tsx | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 155aba5714..60cea6b831 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -44,8 +44,12 @@ export type StateContext = { isSwitchingAccounts: boolean hasSession: boolean accounts: SessionAccount[] + /** + * This value is derived from `BskyAgent.session` + */ currentAccount: SessionAccount | undefined } + export type ApiContext = { createAccount: (props: { service: string @@ -160,7 +164,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { [], ) - const upsertAccount = React.useCallback( + const upsertAndPersistAccount = React.useCallback( (account: SessionAccount) => { persistNextUpdate() setAccounts(accounts => [ @@ -173,7 +177,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const clearCurrentAccount = React.useCallback(() => { logger.warn(`session: clear current account`) - persistNextUpdate() setAgent(PUBLIC_BSKY_AGENT) BskyAgent.configure({appLabelers: [BSKY_LABELER_DID]}) @@ -229,10 +232,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) { * to persist this data and wipe their tokens, effectively logging them * out. */ - upsertAccount(refreshedAccount) + upsertAndPersistAccount(refreshedAccount) } }, - [clearCurrentAccount, upsertAccount], + [clearCurrentAccount, upsertAndPersistAccount], ) const createAccount = React.useCallback( @@ -286,13 +289,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) { agent.setPersistSessionHandler(persistSession(agent)) setAgent(agent) - upsertAccount(account) + upsertAndPersistAccount(account) logger.debug(`session: created account`, {}, logger.DebugContext.session) track('Create Account') logEvent('account:create:success', {}) }, - [upsertAccount, persistSession], + [upsertAndPersistAccount, persistSession], ) const login = React.useCallback( @@ -312,14 +315,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) { agent.setPersistSessionHandler(persistSession(agent)) setAgent(agent) - upsertAccount(account) + upsertAndPersistAccount(account) logger.debug(`session: logged in`, {}, logger.DebugContext.session) track('Sign In', {resumedSession: false}) logEvent('account:loggedIn', {logContext, withPassword: true}) }, - [upsertAccount, persistSession], + [upsertAndPersistAccount, persistSession], ) const logout = React.useCallback( @@ -382,7 +385,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { ) agent.session = prevSession setAgent(agent) - upsertAccount(account) + upsertAndPersistAccount(account) } else { logger.debug( `session: attempting to resumeSession using previous session`, @@ -399,7 +402,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } } }, - [upsertAccount, clearCurrentAccount, persistSession], + [upsertAndPersistAccount, clearCurrentAccount, persistSession], ) const resumeSession = React.useCallback( @@ -431,9 +434,15 @@ export function Provider({children}: React.PropsWithChildren<{}>) { if (!currentAccount) return await agent.resumeSession(sessionAccountToAgentSession(currentAccount)!) persistNextUpdate() - upsertAccount(agentToSessionAccount(agent)!) + upsertAndPersistAccount(agentToSessionAccount(agent)!) setAgent(agent.clone()) - }, [currentAccount, agent, setAgent, persistNextUpdate, upsertAccount]) + }, [ + currentAccount, + agent, + setAgent, + persistNextUpdate, + upsertAndPersistAccount, + ]) const selectAccount = React.useCallback( async (account, logContext) => { @@ -472,6 +481,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { logger.DebugContext.session, ) + // already persisted on other side of broadcast setAccounts(persistedSession.accounts) if (