Skip to content

Commit

Permalink
Replace updateCurrentAccount() with refreshSession() (#3910)
Browse files Browse the repository at this point in the history
Replace updateCurrentAccount() with resumeSession()
  • Loading branch information
gaearon authored May 8, 2024
1 parent f62b045 commit 0c6bf27
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 257 deletions.
171 changes: 0 additions & 171 deletions src/state/session/__tests__/session-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1302,177 +1302,6 @@ describe('session', () => {
`)
})

it('updates current account', () => {
let state = getInitialState([])

const agent1 = new BskyAgent({service: 'https://alice.com'})
agent1.session = {
did: 'alice-did',
handle: 'alice.test',
accessJwt: 'alice-access-jwt-1',
refreshJwt: 'alice-refresh-jwt-1',
}
state = run(state, [
{
type: 'switched-to-account',
newAgent: agent1,
newAccount: agentToSessionAccountOrThrow(agent1),
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].accessJwt).toBe('alice-access-jwt-1')
expect(state.currentAgentState.did).toBe('alice-did')

state = run(state, [
{
type: 'updated-current-account',
updatedFields: {
email: '[email protected]',
emailConfirmed: false,
},
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].email).toBe('[email protected]')
expect(state.accounts[0].emailConfirmed).toBe(false)
expect(state.currentAgentState.did).toBe('alice-did')
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": "[email protected]",
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)

state = run(state, [
{
type: 'updated-current-account',
updatedFields: {
handle: 'alice-updated.test',
},
},
])
expect(state.accounts.length).toBe(1)
expect(state.accounts[0].handle).toBe('alice-updated.test')
expect(state.currentAgentState.did).toBe('alice-did')
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": "[email protected]",
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice-updated.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)

const agent2 = new BskyAgent({service: 'https://bob.com'})
agent2.session = {
did: 'bob-did',
handle: 'bob.test',
accessJwt: 'bob-access-jwt-1',
refreshJwt: 'bob-refresh-jwt-1',
}
state = run(state, [
{
// Switch to Bob.
type: 'switched-to-account',
newAgent: agent2,
newAccount: agentToSessionAccountOrThrow(agent2),
},
{
// Update Bob.
type: 'updated-current-account',
updatedFields: {
handle: 'bob-updated.test',
},
},
{
// Switch back to Alice.
type: 'switched-to-account',
newAgent: agent1,
newAccount: agentToSessionAccountOrThrow(agent1),
},
{
// Update Alice.
type: 'updated-current-account',
updatedFields: {
handle: 'alice-updated-2.test',
},
},
])
expect(printState(state)).toMatchInlineSnapshot(`
{
"accounts": [
{
"accessJwt": "alice-access-jwt-1",
"deactivated": false,
"did": "alice-did",
"email": undefined,
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "alice-updated-2.test",
"pdsUrl": undefined,
"refreshJwt": "alice-refresh-jwt-1",
"service": "https://alice.com/",
},
{
"accessJwt": "bob-access-jwt-1",
"deactivated": false,
"did": "bob-did",
"email": undefined,
"emailAuthFactor": false,
"emailConfirmed": false,
"handle": "bob-updated.test",
"pdsUrl": undefined,
"refreshJwt": "bob-refresh-jwt-1",
"service": "https://bob.com/",
},
],
"currentAgentState": {
"agent": {
"service": "https://alice.com/",
},
"did": "alice-did",
},
"needsPersist": true,
}
`)
})

it('replaces local accounts with synced accounts', () => {
let state = getInitialState([])

Expand Down
20 changes: 1 addition & 19 deletions src/state/session/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const ApiContext = React.createContext<SessionApiContext>({
logout: async () => {},
resumeSession: async () => {},
removeAccount: () => {},
updateCurrentAccount: () => {},
})

export function Provider({children}: React.PropsWithChildren<{}>) {
Expand Down Expand Up @@ -149,15 +148,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
[cancelPendingTask],
)

const updateCurrentAccount = React.useCallback<
SessionApiContext['updateCurrentAccount']
>(account => {
dispatch({
type: 'updated-current-account',
updatedFields: account,
})
}, [])

React.useEffect(() => {
if (state.needsPersist) {
state.needsPersist = false
Expand Down Expand Up @@ -210,16 +200,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
logout,
resumeSession,
removeAccount,
updateCurrentAccount,
}),
[
createAccount,
login,
logout,
resumeSession,
removeAccount,
updateCurrentAccount,
],
[createAccount, login, logout, resumeSession, removeAccount],
)

// @ts-ignore
Expand Down
26 changes: 0 additions & 26 deletions src/state/session/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ export type Action =
newAgent: OpaqueBskyAgent
newAccount: SessionAccount
}
| {
type: 'updated-current-account'
updatedFields: Partial<
Pick<
SessionAccount,
'handle' | 'email' | 'emailConfirmed' | 'emailAuthFactor'
>
>
}
| {
type: 'removed-account'
accountDid: string
Expand Down Expand Up @@ -134,23 +125,6 @@ export function reducer(state: State, action: Action): State {
needsPersist: true,
}
}
case 'updated-current-account': {
const {updatedFields} = action
return {
accounts: state.accounts.map(a => {
if (a.did === state.currentAgentState.did) {
return {
...a,
...updatedFields,
}
} else {
return a
}
}),
currentAgentState: state.currentAgentState,
needsPersist: true,
}
}
case 'removed-account': {
const {accountDid} = action
return {
Expand Down
8 changes: 0 additions & 8 deletions src/state/session/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,4 @@ export type SessionApiContext = {
logout: (logContext: LogEvents['account:loggedOut']['logContext']) => void
resumeSession: (account: SessionAccount) => Promise<void>
removeAccount: (account: SessionAccount) => void
updateCurrentAccount: (
account: Partial<
Pick<
SessionAccount,
'handle' | 'email' | 'emailConfirmed' | 'emailAuthFactor'
>
>,
) => void
}
13 changes: 3 additions & 10 deletions src/view/com/modals/ChangeEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useModalControls} from '#/state/modals'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {usePalette} from 'lib/hooks/usePalette'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {cleanError} from 'lib/strings/errors'
Expand All @@ -28,7 +28,6 @@ export function Component() {
const pal = usePalette('default')
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const {updateCurrentAccount} = useSessionApi()
const {_} = useLingui()
const [stage, setStage] = useState<Stages>(Stages.InputEmail)
const [email, setEmail] = useState<string>(currentAccount?.email || '')
Expand All @@ -51,10 +50,7 @@ export function Component() {
setStage(Stages.ConfirmCode)
} else {
await getAgent().com.atproto.server.updateEmail({email: email.trim()})
updateCurrentAccount({
email: email.trim(),
emailConfirmed: false,
})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email updated`))
setStage(Stages.Done)
}
Expand Down Expand Up @@ -83,10 +79,7 @@ export function Component() {
email: email.trim(),
token: confirmationCode.trim(),
})
updateCurrentAccount({
email: email.trim(),
emailConfirmed: false,
})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email updated`))
setStage(Stages.Done)
} catch (e) {
Expand Down
15 changes: 4 additions & 11 deletions src/view/com/modals/ChangeHandle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ import {logger} from '#/logger'
import {useModalControls} from '#/state/modals'
import {useFetchDid, useUpdateHandleMutation} from '#/state/queries/handle'
import {useServiceQuery} from '#/state/queries/service'
import {
SessionAccount,
useAgent,
useSession,
useSessionApi,
} from '#/state/session'
import {SessionAccount, useAgent, useSession} from '#/state/session'
import {useAnalytics} from 'lib/analytics/analytics'
import {usePalette} from 'lib/hooks/usePalette'
import {cleanError} from 'lib/strings/errors'
Expand Down Expand Up @@ -73,10 +68,10 @@ export function Inner({
const {_} = useLingui()
const pal = usePalette('default')
const {track} = useAnalytics()
const {updateCurrentAccount} = useSessionApi()
const {closeModal} = useModalControls()
const {mutateAsync: updateHandle, isPending: isUpdateHandlePending} =
useUpdateHandleMutation()
const {getAgent} = useAgent()

const [error, setError] = useState<string>('')

Expand Down Expand Up @@ -116,9 +111,7 @@ export function Inner({
await updateHandle({
handle: newHandle,
})
updateCurrentAccount({
handle: newHandle,
})
await getAgent().resumeSession(getAgent().session!)
closeModal()
onChanged()
} catch (err: any) {
Expand All @@ -134,9 +127,9 @@ export function Inner({
onChanged,
track,
closeModal,
updateCurrentAccount,
updateHandle,
serviceInfo,
getAgent,
])

// rendering
Expand Down
5 changes: 2 additions & 3 deletions src/view/com/modals/VerifyEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {useLingui} from '@lingui/react'

import {logger} from '#/logger'
import {useModalControls} from '#/state/modals'
import {useAgent, useSession, useSessionApi} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {usePalette} from 'lib/hooks/usePalette'
import {useWebMediaQueries} from 'lib/hooks/useWebMediaQueries'
import {cleanError} from 'lib/strings/errors'
Expand Down Expand Up @@ -43,7 +43,6 @@ export function Component({
const pal = usePalette('default')
const {getAgent} = useAgent()
const {currentAccount} = useSession()
const {updateCurrentAccount} = useSessionApi()
const {_} = useLingui()
const [stage, setStage] = useState<Stages>(
showReminder ? Stages.Reminder : Stages.Email,
Expand Down Expand Up @@ -82,7 +81,7 @@ export function Component({
email: (currentAccount?.email || '').trim(),
token: confirmationCode.trim(),
})
updateCurrentAccount({emailConfirmed: true})
await getAgent().resumeSession(getAgent().session!)
Toast.show(_(msg`Email verified`))
closeModal()
onSuccess?.()
Expand Down
Loading

0 comments on commit 0c6bf27

Please sign in to comment.