Use resetQueries, revert history push
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import {useCallback} from 'react'
|
import {useCallback} from 'react'
|
||||||
|
|
||||||
import {useAnalytics} from '#/lib/analytics/analytics'
|
import {useAnalytics} from '#/lib/analytics/analytics'
|
||||||
import {useSessionApi, SessionAccount} from '#/state/session'
|
import {isWeb} from '#/platform/detection'
|
||||||
import * as Toast from '#/view/com/util/Toast'
|
import {SessionAccount, useSessionApi} from '#/state/session'
|
||||||
import {useCloseAllActiveElements} from '#/state/util'
|
|
||||||
import {useLoggedOutViewControls} from '#/state/shell/logged-out'
|
import {useLoggedOutViewControls} from '#/state/shell/logged-out'
|
||||||
|
import {useCloseAllActiveElements} from '#/state/util'
|
||||||
|
import * as Toast from '#/view/com/util/Toast'
|
||||||
import {LogEvents} from '../statsig/statsig'
|
import {LogEvents} from '../statsig/statsig'
|
||||||
|
|
||||||
export function useAccountSwitcher() {
|
export function useAccountSwitcher() {
|
||||||
@@ -23,6 +24,14 @@ 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}`)
|
||||||
|
|||||||
@@ -211,7 +211,7 @@ 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`)
|
||||||
__globalAgent = PUBLIC_BSKY_AGENT
|
__globalAgent = PUBLIC_BSKY_AGENT
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
setStateAndPersist(s => ({
|
setStateAndPersist(s => ({
|
||||||
...s,
|
...s,
|
||||||
currentAccount: undefined,
|
currentAccount: undefined,
|
||||||
@@ -286,7 +286,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
__globalAgent = agent
|
__globalAgent = agent
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
upsertAccount(account)
|
upsertAccount(account)
|
||||||
|
|
||||||
logger.debug(`session: created account`, {}, logger.DebugContext.session)
|
logger.debug(`session: created account`, {}, logger.DebugContext.session)
|
||||||
@@ -334,7 +334,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
__globalAgent = agent
|
__globalAgent = agent
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
if (IS_DEV && isWeb) window.agent = agent
|
if (IS_DEV && isWeb) window.agent = agent
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
upsertAccount(account)
|
upsertAccount(account)
|
||||||
|
|
||||||
logger.debug(`session: logged in`, {}, logger.DebugContext.session)
|
logger.debug(`session: logged in`, {}, logger.DebugContext.session)
|
||||||
@@ -415,7 +415,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
*/
|
*/
|
||||||
agent.session = prevSession
|
agent.session = prevSession
|
||||||
__globalAgent = agent
|
__globalAgent = agent
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
upsertAccount(account)
|
upsertAccount(account)
|
||||||
|
|
||||||
if (prevSession.deactivated) {
|
if (prevSession.deactivated) {
|
||||||
@@ -436,7 +436,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
__globalAgent = PUBLIC_BSKY_AGENT
|
__globalAgent = PUBLIC_BSKY_AGENT
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
logger.debug(`session: attempting to resume using previous session`)
|
logger.debug(`session: attempting to resume using previous session`)
|
||||||
@@ -457,7 +457,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
|||||||
|
|
||||||
__globalAgent = PUBLIC_BSKY_AGENT
|
__globalAgent = PUBLIC_BSKY_AGENT
|
||||||
} finally {
|
} finally {
|
||||||
queryClient.clear()
|
queryClient.resetQueries()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,14 +558,6 @@ 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user