mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {type AppBskyActorGetProfile} from '@atproto/api'
2import {useMutation} from '@tanstack/react-query'
3
4import {until} from '#/lib/async/until'
5import {logger} from '#/logger'
6import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
7import {useAgent, useSession} from '#/state/session'
8import type * as bsky from '#/types/bsky'
9
10export function useVerificationCreateMutation() {
11 const agent = useAgent()
12 const {currentAccount} = useSession()
13 const updateProfileVerificationCache = useUpdateProfileVerificationCache()
14
15 return useMutation({
16 async mutationFn({profile}: {profile: bsky.profile.AnyProfileView}) {
17 if (!currentAccount) {
18 throw new Error('User not logged in')
19 }
20
21 const {uri} = await agent.app.bsky.graph.verification.create(
22 {repo: currentAccount.did},
23 {
24 subject: profile.did,
25 createdAt: new Date().toISOString(),
26 handle: profile.handle,
27 displayName: profile.displayName || '',
28 },
29 )
30
31 await until(
32 5,
33 1e3,
34 ({data: profile}: AppBskyActorGetProfile.Response) => {
35 if (
36 profile.verification &&
37 profile.verification.verifications.find(v => v.uri === uri)
38 ) {
39 return true
40 }
41 return false
42 },
43 () => {
44 return agent.getProfile({actor: profile.did ?? ''})
45 },
46 )
47 },
48 async onSuccess(_, {profile}) {
49 logger.metric('verification:create', {}, {statsig: true})
50 await updateProfileVerificationCache({profile})
51 },
52 })
53}