diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index b1886ff84d..403785858f 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -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: 'alice@foo.bar', - emailConfirmed: false, - }, - }, - ]) - expect(state.accounts.length).toBe(1) - expect(state.accounts[0].email).toBe('alice@foo.bar') - 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": "alice@foo.bar", - "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": "alice@foo.bar", - "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([]) diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index a8bd90ca12..b5a985e67d 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -35,7 +35,6 @@ const ApiContext = React.createContext({ logout: async () => {}, resumeSession: async () => {}, removeAccount: () => {}, - updateCurrentAccount: () => {}, }) export function Provider({children}: React.PropsWithChildren<{}>) { @@ -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 @@ -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 diff --git a/src/state/session/reducer.ts b/src/state/session/reducer.ts index a14cd8e1c4..775a6d0381 100644 --- a/src/state/session/reducer.ts +++ b/src/state/session/reducer.ts @@ -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 @@ -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 { diff --git a/src/state/session/types.ts b/src/state/session/types.ts index b74eeddcb5..d43b57cca9 100644 --- a/src/state/session/types.ts +++ b/src/state/session/types.ts @@ -37,12 +37,4 @@ export type SessionApiContext = { logout: (logContext: LogEvents['account:loggedOut']['logContext']) => void resumeSession: (account: SessionAccount) => Promise removeAccount: (account: SessionAccount) => void - updateCurrentAccount: ( - account: Partial< - Pick< - SessionAccount, - 'handle' | 'email' | 'emailConfirmed' | 'emailAuthFactor' - > - >, - ) => void } diff --git a/src/view/com/modals/ChangeEmail.tsx b/src/view/com/modals/ChangeEmail.tsx index b3b91f72df..b940b2d6dc 100644 --- a/src/view/com/modals/ChangeEmail.tsx +++ b/src/view/com/modals/ChangeEmail.tsx @@ -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' @@ -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.InputEmail) const [email, setEmail] = useState(currentAccount?.email || '') @@ -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) } @@ -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) { diff --git a/src/view/com/modals/ChangeHandle.tsx b/src/view/com/modals/ChangeHandle.tsx index ea49a93f28..52eb51031c 100644 --- a/src/view/com/modals/ChangeHandle.tsx +++ b/src/view/com/modals/ChangeHandle.tsx @@ -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' @@ -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('') @@ -116,9 +111,7 @@ export function Inner({ await updateHandle({ handle: newHandle, }) - updateCurrentAccount({ - handle: newHandle, - }) + await getAgent().resumeSession(getAgent().session!) closeModal() onChanged() } catch (err: any) { @@ -134,9 +127,9 @@ export function Inner({ onChanged, track, closeModal, - updateCurrentAccount, updateHandle, serviceInfo, + getAgent, ]) // rendering diff --git a/src/view/com/modals/VerifyEmail.tsx b/src/view/com/modals/VerifyEmail.tsx index d6a3006cc9..3fdde330d8 100644 --- a/src/view/com/modals/VerifyEmail.tsx +++ b/src/view/com/modals/VerifyEmail.tsx @@ -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' @@ -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( showReminder ? Stages.Reminder : Stages.Email, @@ -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?.() diff --git a/src/view/screens/Settings/DisableEmail2FADialog.tsx b/src/view/screens/Settings/DisableEmail2FADialog.tsx index 83b133f656..b52dcc7a36 100644 --- a/src/view/screens/Settings/DisableEmail2FADialog.tsx +++ b/src/view/screens/Settings/DisableEmail2FADialog.tsx @@ -5,7 +5,7 @@ import {useLingui} from '@lingui/react' import {cleanError} from '#/lib/strings/errors' import {isNative} from '#/platform/detection' -import {useAgent, useSession, useSessionApi} from '#/state/session' +import {useAgent, useSession} from '#/state/session' import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' import * as Toast from '#/view/com/util/Toast' import {atoms as a, useBreakpoints, useTheme} from '#/alf' @@ -30,7 +30,6 @@ export function DisableEmail2FADialog({ const t = useTheme() const {gtMobile} = useBreakpoints() const {currentAccount} = useSession() - const {updateCurrentAccount} = useSessionApi() const {getAgent} = useAgent() const [stage, setStage] = useState(Stages.Email) @@ -61,7 +60,7 @@ export function DisableEmail2FADialog({ token: confirmationCode.trim(), emailAuthFactor: false, }) - updateCurrentAccount({emailAuthFactor: false}) + await getAgent().resumeSession(getAgent().session!) Toast.show(_(msg`Email 2FA disabled`)) } control.close() diff --git a/src/view/screens/Settings/Email2FAToggle.tsx b/src/view/screens/Settings/Email2FAToggle.tsx index 87a56ba5eb..efeb7e4d7c 100644 --- a/src/view/screens/Settings/Email2FAToggle.tsx +++ b/src/view/screens/Settings/Email2FAToggle.tsx @@ -3,7 +3,7 @@ import {msg} 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 {ToggleButton} from 'view/com/util/forms/ToggleButton' import {useDialogControl} from '#/components/Dialog' import {DisableEmail2FADialog} from './DisableEmail2FADialog' @@ -11,7 +11,6 @@ import {DisableEmail2FADialog} from './DisableEmail2FADialog' export function Email2FAToggle() { const {_} = useLingui() const {currentAccount} = useSession() - const {updateCurrentAccount} = useSessionApi() const {openModal} = useModalControls() const disableDialogCtrl = useDialogControl() const {getAgent} = useAgent() @@ -22,11 +21,9 @@ export function Email2FAToggle() { email: currentAccount.email, emailAuthFactor: true, }) - updateCurrentAccount({ - emailAuthFactor: true, - }) + await getAgent().resumeSession(getAgent().session!) } - }, [currentAccount, updateCurrentAccount, getAgent]) + }, [currentAccount, getAgent]) const onToggle = React.useCallback(() => { if (!currentAccount) {