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