483e79497b
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import {type Client} from '@atproto/lex'
|
|
import {useMutation} from '@tanstack/react-query'
|
|
|
|
import {usePdsClient, useSessionApi} from '#/state/session'
|
|
import {useRequestEmailUpdate} from '#/components/dialogs/EmailDialog/data/useRequestEmailUpdate'
|
|
import {com} from '#/lexicons'
|
|
|
|
async function updateEmailAndRefreshSession(
|
|
pdsClient: Client,
|
|
refreshSession: () => Promise<unknown>,
|
|
email: string,
|
|
token?: string,
|
|
) {
|
|
await pdsClient.call(com.atproto.server.updateEmail, {
|
|
email: email.trim(),
|
|
token,
|
|
})
|
|
await refreshSession()
|
|
}
|
|
|
|
export function useUpdateEmail() {
|
|
const pdsClient = usePdsClient()
|
|
const {refreshSession} = useSessionApi()
|
|
const {mutateAsync: requestEmailUpdate} = useRequestEmailUpdate()
|
|
|
|
return useMutation<
|
|
{status: 'tokenRequired' | 'success'},
|
|
Error,
|
|
{email: string; token?: string}
|
|
>({
|
|
mutationFn: async ({email, token}: {email: string; token?: string}) => {
|
|
if (token) {
|
|
await updateEmailAndRefreshSession(
|
|
pdsClient,
|
|
refreshSession,
|
|
email,
|
|
token,
|
|
)
|
|
return {
|
|
status: 'success',
|
|
}
|
|
} else {
|
|
const {tokenRequired} = await requestEmailUpdate()
|
|
if (tokenRequired) {
|
|
return {
|
|
status: 'tokenRequired',
|
|
}
|
|
} else {
|
|
await updateEmailAndRefreshSession(
|
|
pdsClient,
|
|
refreshSession,
|
|
email,
|
|
token,
|
|
)
|
|
return {
|
|
status: 'success',
|
|
}
|
|
}
|
|
}
|
|
},
|
|
})
|
|
}
|