about summary refs log tree commit diff
path: root/src/state/queries/my-follows.ts
blob: f95c3f5a7c365995385eec0e5d407cbe1c5ee307 (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
import {AppBskyActorDefs} from '@atproto/api'
import {useQuery} from '@tanstack/react-query'
import {useSession, getAgent} from '../session'
import {STALE} from '#/state/queries'

// sanity limit is SANITY_PAGE_LIMIT*PAGE_SIZE total records
const SANITY_PAGE_LIMIT = 1000
const PAGE_SIZE = 100
// ...which comes 10,000k follows

export const RQKEY = () => ['my-follows']

export function useMyFollowsQuery() {
  const {currentAccount} = useSession()
  return useQuery<AppBskyActorDefs.ProfileViewBasic[]>({
    staleTime: STALE.MINUTES.ONE,
    queryKey: RQKEY(),
    async queryFn() {
      if (!currentAccount) {
        return []
      }
      let cursor
      let arr: AppBskyActorDefs.ProfileViewBasic[] = []
      for (let i = 0; i < SANITY_PAGE_LIMIT; i++) {
        const res = await getAgent().getFollows({
          actor: currentAccount.did,
          cursor,
          limit: PAGE_SIZE,
        })
        // TODO
        // res.data.follows = res.data.follows.filter(
        //   profile =>
        //     !moderateProfile(profile, this.rootStore.preferences.moderationOpts)
        //       .account.filter,
        // )
        arr = arr.concat(res.data.follows)
        if (!res.data.cursor) {
          break
        }
        cursor = res.data.cursor
      }
      return arr
    },
  })
}