[Session] Extract resumeSession out (#3811)

This commit is contained in:
dan 2024-05-02 18:17:53 +01:00 committed by GitHub
parent dadf27fd2f
commit 5ec945b762
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 43 additions and 36 deletions

View file

@ -35,7 +35,6 @@ const PUBLIC_BSKY_AGENT = new BskyAgent({service: PUBLIC_BSKY_SERVICE})
configureModerationForGuest()
const StateContext = React.createContext<SessionStateContext>({
isInitialLoad: true,
isSwitchingAccounts: false,
accounts: [],
currentAccount: undefined,
@ -47,7 +46,6 @@ const ApiContext = React.createContext<SessionApiContext>({
login: async () => {},
logout: async () => {},
initSession: async () => {},
resumeSession: async () => {},
removeAccount: () => {},
selectAccount: async () => {},
updateCurrentAccount: () => {},
@ -67,7 +65,6 @@ type State = {
}
export function Provider({children}: React.PropsWithChildren<{}>) {
const [isInitialLoad, setIsInitialLoad] = React.useState(true)
const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false)
const [state, setState] = React.useState<State>({
accounts: persisted.get('session').accounts,
@ -389,21 +386,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[upsertAccount, clearCurrentAccount, createPersistSessionHandler],
)
const resumeSession = React.useCallback<SessionApiContext['resumeSession']>(
async account => {
try {
if (account) {
await initSession(account)
}
} catch (e) {
logger.error(`session: resumeSession failed`, {message: e})
} finally {
setIsInitialLoad(false)
}
},
[initSession],
)
const removeAccount = React.useCallback<SessionApiContext['removeAccount']>(
account => {
setState(s => {
@ -547,11 +529,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
currentAccount: state.accounts.find(
a => a.did === state.currentAccountDid,
),
isInitialLoad,
isSwitchingAccounts,
hasSession: !!state.currentAccountDid,
}),
[state, isInitialLoad, isSwitchingAccounts],
[state, isSwitchingAccounts],
)
const api = React.useMemo(
@ -560,7 +541,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
login,
logout,
initSession,
resumeSession,
removeAccount,
selectAccount,
updateCurrentAccount,
@ -571,7 +551,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
login,
logout,
initSession,
resumeSession,
removeAccount,
selectAccount,
updateCurrentAccount,

View file

@ -6,7 +6,6 @@ export type SessionAccount = PersistedAccount
export type SessionStateContext = {
accounts: SessionAccount[]
currentAccount: SessionAccount | undefined
isInitialLoad: boolean
isSwitchingAccounts: boolean
hasSession: boolean
}
@ -46,7 +45,6 @@ export type SessionApiContext = {
*/
clearCurrentAccount: () => void
initSession: (account: SessionAccount) => Promise<void>
resumeSession: (account?: SessionAccount) => Promise<void>
removeAccount: (account: SessionAccount) => void
selectAccount: (
account: SessionAccount,