Some comments

This commit is contained in:
Eric Bailey
2024-04-04 14:06:32 -05:00
parent cbac0a91cf
commit fd001836ed
+22 -12
View File
@@ -44,8 +44,12 @@ export type StateContext = {
isSwitchingAccounts: boolean isSwitchingAccounts: boolean
hasSession: boolean hasSession: boolean
accounts: SessionAccount[] accounts: SessionAccount[]
/**
* This value is derived from `BskyAgent.session`
*/
currentAccount: SessionAccount | undefined currentAccount: SessionAccount | undefined
} }
export type ApiContext = { export type ApiContext = {
createAccount: (props: { createAccount: (props: {
service: string service: string
@@ -160,7 +164,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[], [],
) )
const upsertAccount = React.useCallback( const upsertAndPersistAccount = React.useCallback(
(account: SessionAccount) => { (account: SessionAccount) => {
persistNextUpdate() persistNextUpdate()
setAccounts(accounts => [ setAccounts(accounts => [
@@ -173,7 +177,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
const clearCurrentAccount = React.useCallback(() => { const clearCurrentAccount = React.useCallback(() => {
logger.warn(`session: clear current account`) logger.warn(`session: clear current account`)
persistNextUpdate() persistNextUpdate()
setAgent(PUBLIC_BSKY_AGENT) setAgent(PUBLIC_BSKY_AGENT)
BskyAgent.configure({appLabelers: [BSKY_LABELER_DID]}) 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 * to persist this data and wipe their tokens, effectively logging them
* out. * out.
*/ */
upsertAccount(refreshedAccount) upsertAndPersistAccount(refreshedAccount)
} }
}, },
[clearCurrentAccount, upsertAccount], [clearCurrentAccount, upsertAndPersistAccount],
) )
const createAccount = React.useCallback<ApiContext['createAccount']>( const createAccount = React.useCallback<ApiContext['createAccount']>(
@@ -286,13 +289,13 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
agent.setPersistSessionHandler(persistSession(agent)) agent.setPersistSessionHandler(persistSession(agent))
setAgent(agent) setAgent(agent)
upsertAccount(account) upsertAndPersistAccount(account)
logger.debug(`session: created account`, {}, logger.DebugContext.session) logger.debug(`session: created account`, {}, logger.DebugContext.session)
track('Create Account') track('Create Account')
logEvent('account:create:success', {}) logEvent('account:create:success', {})
}, },
[upsertAccount, persistSession], [upsertAndPersistAccount, persistSession],
) )
const login = React.useCallback<ApiContext['login']>( const login = React.useCallback<ApiContext['login']>(
@@ -312,14 +315,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
agent.setPersistSessionHandler(persistSession(agent)) agent.setPersistSessionHandler(persistSession(agent))
setAgent(agent) setAgent(agent)
upsertAccount(account) upsertAndPersistAccount(account)
logger.debug(`session: logged in`, {}, logger.DebugContext.session) logger.debug(`session: logged in`, {}, logger.DebugContext.session)
track('Sign In', {resumedSession: false}) track('Sign In', {resumedSession: false})
logEvent('account:loggedIn', {logContext, withPassword: true}) logEvent('account:loggedIn', {logContext, withPassword: true})
}, },
[upsertAccount, persistSession], [upsertAndPersistAccount, persistSession],
) )
const logout = React.useCallback<ApiContext['logout']>( const logout = React.useCallback<ApiContext['logout']>(
@@ -382,7 +385,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
) )
agent.session = prevSession agent.session = prevSession
setAgent(agent) setAgent(agent)
upsertAccount(account) upsertAndPersistAccount(account)
} else { } else {
logger.debug( logger.debug(
`session: attempting to resumeSession using previous session`, `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<ApiContext['resumeSession']>( const resumeSession = React.useCallback<ApiContext['resumeSession']>(
@@ -431,9 +434,15 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
if (!currentAccount) return if (!currentAccount) return
await agent.resumeSession(sessionAccountToAgentSession(currentAccount)!) await agent.resumeSession(sessionAccountToAgentSession(currentAccount)!)
persistNextUpdate() persistNextUpdate()
upsertAccount(agentToSessionAccount(agent)!) upsertAndPersistAccount(agentToSessionAccount(agent)!)
setAgent(agent.clone()) setAgent(agent.clone())
}, [currentAccount, agent, setAgent, persistNextUpdate, upsertAccount]) }, [
currentAccount,
agent,
setAgent,
persistNextUpdate,
upsertAndPersistAccount,
])
const selectAccount = React.useCallback<ApiContext['selectAccount']>( const selectAccount = React.useCallback<ApiContext['selectAccount']>(
async (account, logContext) => { async (account, logContext) => {
@@ -472,6 +481,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logger.DebugContext.session, logger.DebugContext.session,
) )
// already persisted on other side of broadcast
setAccounts(persistedSession.accounts) setAccounts(persistedSession.accounts)
if ( if (