about summary refs log tree commit diff
path: root/src/state/queries/actor-starter-packs.ts
blob: bde71974371824751deb1d28e3e1fc88789850d9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
  type AppBskyGraphGetActorStarterPacks,
  type AppBskyGraphGetStarterPacksWithMembership,
} from '@atproto/api'
import {
  type InfiniteData,
  type QueryClient,
  type QueryKey,
  useInfiniteQuery,
} from '@tanstack/react-query'

import {useAgent} from '#/state/session'

export const RQKEY_ROOT = 'actor-starter-packs'
export const RQKEY_WITH_MEMBERSHIP_ROOT = 'actor-starter-packs-with-membership'
export const RQKEY = (did?: string) => [RQKEY_ROOT, did]
export const RQKEY_WITH_MEMBERSHIP = (did?: string) => [
  RQKEY_WITH_MEMBERSHIP_ROOT,
  did,
]

export function useActorStarterPacksQuery({
  did,
  enabled = true,
}: {
  did?: string
  enabled?: boolean
}) {
  const agent = useAgent()

  return useInfiniteQuery<
    AppBskyGraphGetActorStarterPacks.OutputSchema,
    Error,
    InfiniteData<AppBskyGraphGetActorStarterPacks.OutputSchema>,
    QueryKey,
    string | undefined
  >({
    queryKey: RQKEY(did),
    queryFn: async ({pageParam}: {pageParam?: string}) => {
      const res = await agent.app.bsky.graph.getActorStarterPacks({
        actor: did!,
        limit: 10,
        cursor: pageParam,
      })
      return res.data
    },
    enabled: Boolean(did) && enabled,
    initialPageParam: undefined,
    getNextPageParam: lastPage => lastPage.cursor,
  })
}

export function useActorStarterPacksWithMembershipsQuery({
  did,
  enabled = true,
}: {
  did?: string
  enabled?: boolean
}) {
  const agent = useAgent()

  return useInfiniteQuery<
    AppBskyGraphGetStarterPacksWithMembership.OutputSchema,
    Error,
    InfiniteData<AppBskyGraphGetStarterPacksWithMembership.OutputSchema>,
    QueryKey,
    string | undefined
  >({
    queryKey: RQKEY_WITH_MEMBERSHIP(did),
    queryFn: async ({pageParam}: {pageParam?: string}) => {
      const res = await agent.app.bsky.graph.getStarterPacksWithMembership({
        actor: did!,
        limit: 10,
        cursor: pageParam,
      })
      return res.data
    },
    enabled: Boolean(did) && enabled,
    initialPageParam: undefined,
    getNextPageParam: lastPage => lastPage.cursor,
  })
}

export async function invalidateActorStarterPacksQuery({
  queryClient,
  did,
}: {
  queryClient: QueryClient
  did: string
}) {
  await queryClient.invalidateQueries({queryKey: RQKEY(did)})
}