about summary refs log tree commit diff
path: root/src/state/queries/messages/get-convo-for-members.ts
blob: 7979e06659fef1288a6d22bc6101818ba5091e10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {ChatBskyConvoGetConvoForMembers} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'

import {logger} from '#/logger'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
import {STALE} from '..'
import {RQKEY as CONVO_KEY} from './conversation'

const RQKEY_ROOT = 'convo-for-user'
export const RQKEY = (did: string) => [RQKEY_ROOT, did]

export function useGetConvoForMembers({
  onSuccess,
  onError,
}: {
  onSuccess?: (data: ChatBskyConvoGetConvoForMembers.OutputSchema) => void
  onError?: (error: Error) => void
}) {
  const queryClient = useQueryClient()
  const agent = useAgent()

  return useMutation({
    mutationFn: async (members: string[]) => {
      const {data} = await agent.api.chat.bsky.convo.getConvoForMembers(
        {members: members},
        {headers: DM_SERVICE_HEADERS},
      )

      return data
    },
    onSuccess: data => {
      queryClient.setQueryData(CONVO_KEY(data.convo.id), data.convo)
      onSuccess?.(data)
    },
    onError: error => {
      logger.error(error)
      onError?.(error)
    },
  })
}

/**
 * Gets the conversation ID for a given DID. Returns null if it's not possible to message them.
 */
export function useMaybeConvoForUser(did: string) {
  const agent = useAgent()

  return useQuery({
    queryKey: RQKEY(did),
    queryFn: async () => {
      const convo = await agent.api.chat.bsky.convo
        .getConvoForMembers({members: [did]}, {headers: DM_SERVICE_HEADERS})
        .catch(() => ({success: null}))

      if (convo.success) {
        return convo.data.convo
      } else {
        return null
      }
    },
    staleTime: STALE.INFINITY,
  })
}