about summary refs log tree commit diff
path: root/src/state/queries/handle.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/handle.ts')
-rw-r--r--src/state/queries/handle.ts35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/state/queries/handle.ts b/src/state/queries/handle.ts
index 97e9b2107..4c3296587 100644
--- a/src/state/queries/handle.ts
+++ b/src/state/queries/handle.ts
@@ -1,9 +1,10 @@
 import React from 'react'
-import {useQueryClient} from '@tanstack/react-query'
+import {useQueryClient, useMutation} from '@tanstack/react-query'
 
 import {useSession} from '#/state/session'
 
 const fetchHandleQueryKey = (handleOrDid: string) => ['handle', handleOrDid]
+const fetchDidQueryKey = (handleOrDid: string) => ['did', handleOrDid]
 
 export function useFetchHandle() {
   const {agent} = useSession()
@@ -23,3 +24,35 @@ export function useFetchHandle() {
     [agent, queryClient],
   )
 }
+
+export function useUpdateHandleMutation() {
+  const {agent} = useSession()
+
+  return useMutation({
+    mutationFn: async ({handle}: {handle: string}) => {
+      await agent.updateHandle({handle})
+    },
+  })
+}
+
+export function useFetchDid() {
+  const {agent} = useSession()
+  const queryClient = useQueryClient()
+
+  return React.useCallback(
+    async (handleOrDid: string) => {
+      return queryClient.fetchQuery({
+        queryKey: fetchDidQueryKey(handleOrDid),
+        queryFn: async () => {
+          let identifier = handleOrDid
+          if (!identifier.startsWith('did:')) {
+            const res = await agent.resolveHandle({handle: identifier})
+            identifier = res.data.did
+          }
+          return identifier
+        },
+      })
+    },
+    [agent, queryClient],
+  )
+}