Pare down session and prefs changes to minimum

This commit is contained in:
Eric Bailey
2024-03-18 15:25:24 -05:00
parent df7f9856bb
commit 78adc68559
2 changed files with 27 additions and 30 deletions
-9
View File
@@ -1,6 +1,5 @@
import {useCallback} from 'react' import {useCallback} from 'react'
import {isWeb} from '#/platform/detection'
import {useAnalytics} from '#/lib/analytics/analytics' import {useAnalytics} from '#/lib/analytics/analytics'
import {useSessionApi, SessionAccount} from '#/state/session' import {useSessionApi, SessionAccount} from '#/state/session'
import * as Toast from '#/view/com/util/Toast' import * as Toast from '#/view/com/util/Toast'
@@ -24,14 +23,6 @@ export function useAccountSwitcher() {
try { try {
if (account.accessJwt) { if (account.accessJwt) {
closeAllActiveElements() closeAllActiveElements()
if (isWeb) {
// We're switching accounts, which remounts the entire app.
// On mobile, this gets us Home, but on the web we also need reset the URL.
// We can't change the URL via a navigate() call because the navigator
// itself is about to unmount, and it calls pushState() too late.
// So we change the URL ourselves. The navigator will pick it up on remount.
history.pushState(null, '', '/')
}
await selectAccount(account, logContext) await selectAccount(account, logContext)
setTimeout(() => { setTimeout(() => {
Toast.show(`Signed in as @${account.handle}`) Toast.show(`Signed in as @${account.handle}`)
+19 -13
View File
@@ -409,6 +409,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
if (canReusePrevSession) { if (canReusePrevSession) {
logger.debug(`session: attempting to reuse previous session`) logger.debug(`session: attempting to reuse previous session`)
/**
* If session isn't expired, immediately update global agent and session
* state so that the app can rerender with new data.
*/
agent.session = prevSession agent.session = prevSession
__globalAgent = agent __globalAgent = agent
queryClient.clear() queryClient.clear()
@@ -422,16 +426,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
} }
// Intentionally not awaited to unblock the UI: // Intentionally not awaited to unblock the UI:
resumeSessionWithFreshAccount() resumeSessionWithFreshAccount().catch(e => {
.then(freshAccount => {
if (JSON.stringify(account) !== JSON.stringify(freshAccount)) {
logger.info(
`session: reuse of previous session returned a fresh account, upserting`,
)
upsertAccount(freshAccount)
}
})
.catch(e => {
/* /*
* Note: `agent.persistSession` is also called when this fails, and * Note: `agent.persistSession` is also called when this fails, and
* we handle that failure via `createPersistSessionHandler` * we handle that failure via `createPersistSessionHandler`
@@ -441,15 +436,16 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}) })
__globalAgent = PUBLIC_BSKY_AGENT __globalAgent = PUBLIC_BSKY_AGENT
queryClient.clear()
}) })
} else { } else {
logger.debug(`session: attempting to resume using previous session`) logger.debug(`session: attempting to resume using previous session`)
try { try {
const freshAccount = await resumeSessionWithFreshAccount() // this calls `upsertAccount` via `agent.resumeSession` and persistor
await resumeSessionWithFreshAccount()
// only update global agen if resumeSessionWithFreshAccount succeeded
__globalAgent = agent __globalAgent = agent
queryClient.clear()
upsertAccount(freshAccount)
} catch (e) { } catch (e) {
/* /*
* Note: `agent.persistSession` is also called when this fails, and * Note: `agent.persistSession` is also called when this fails, and
@@ -460,6 +456,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}) })
__globalAgent = PUBLIC_BSKY_AGENT __globalAgent = PUBLIC_BSKY_AGENT
} finally {
queryClient.clear()
} }
} }
@@ -560,6 +558,14 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
setState(s => ({...s, isSwitchingAccounts: true})) setState(s => ({...s, isSwitchingAccounts: true}))
try { try {
await initSession(account) await initSession(account)
if (isWeb) {
// We're switching accounts, which remounts the entire app.
// On mobile, this gets us Home, but on the web we also need reset the URL.
// We can't change the URL via a navigate() call because the navigator
// itself is about to unmount, and it calls pushState() too late.
// So we change the URL ourselves. The navigator will pick it up on remount.
history.pushState(null, '', '/')
}
setState(s => ({...s, isSwitchingAccounts: false})) setState(s => ({...s, isSwitchingAccounts: false}))
logEvent('account:loggedIn', {logContext, withPassword: false}) logEvent('account:loggedIn', {logContext, withPassword: false})
} catch (e) { } catch (e) {