about summary refs log tree commit diff
path: root/src/state/queries/verification/useVerificationCreateMutation.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/verification/useVerificationCreateMutation.tsx')
-rw-r--r--src/state/queries/verification/useVerificationCreateMutation.tsx53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/state/queries/verification/useVerificationCreateMutation.tsx b/src/state/queries/verification/useVerificationCreateMutation.tsx
new file mode 100644
index 000000000..1048eb9d2
--- /dev/null
+++ b/src/state/queries/verification/useVerificationCreateMutation.tsx
@@ -0,0 +1,53 @@
+import {type AppBskyActorGetProfile} from '@atproto/api'
+import {useMutation} from '@tanstack/react-query'
+
+import {until} from '#/lib/async/until'
+import {logger} from '#/logger'
+import {useUpdateProfileVerificationCache} from '#/state/queries/verification/useUpdateProfileVerificationCache'
+import {useAgent, useSession} from '#/state/session'
+import type * as bsky from '#/types/bsky'
+
+export function useVerificationCreateMutation() {
+  const agent = useAgent()
+  const {currentAccount} = useSession()
+  const updateProfileVerificationCache = useUpdateProfileVerificationCache()
+
+  return useMutation({
+    async mutationFn({profile}: {profile: bsky.profile.AnyProfileView}) {
+      if (!currentAccount) {
+        throw new Error('User not logged in')
+      }
+
+      const {uri} = await agent.app.bsky.graph.verification.create(
+        {repo: currentAccount.did},
+        {
+          subject: profile.did,
+          createdAt: new Date().toISOString(),
+          handle: profile.handle,
+          displayName: profile.displayName || '',
+        },
+      )
+
+      await until(
+        5,
+        1e3,
+        ({data: profile}: AppBskyActorGetProfile.Response) => {
+          if (
+            profile.verification &&
+            profile.verification.verifications.find(v => v.uri === uri)
+          ) {
+            return true
+          }
+          return false
+        },
+        () => {
+          return agent.getProfile({actor: profile.did ?? ''})
+        },
+      )
+    },
+    async onSuccess(_, {profile}) {
+      logger.metric('verification:create', {})
+      await updateProfileVerificationCache({profile})
+    },
+  })
+}