mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {AppBskyActorDefs, AppBskyGraphGetKnownFollowers} from '@atproto/api'
2import {
3 InfiniteData,
4 QueryClient,
5 QueryKey,
6 useInfiniteQuery,
7} from '@tanstack/react-query'
8
9import {useAgent} from '#/state/session'
10
11const PAGE_SIZE = 50
12type RQPageParam = string | undefined
13
14const RQKEY_ROOT = 'profile-known-followers'
15export const RQKEY = (did: string) => [RQKEY_ROOT, did]
16
17export function useProfileKnownFollowersQuery(did: string | undefined) {
18 const agent = useAgent()
19 return useInfiniteQuery<
20 AppBskyGraphGetKnownFollowers.OutputSchema,
21 Error,
22 InfiniteData<AppBskyGraphGetKnownFollowers.OutputSchema>,
23 QueryKey,
24 RQPageParam
25 >({
26 queryKey: RQKEY(did || ''),
27 async queryFn({pageParam}: {pageParam: RQPageParam}) {
28 const res = await agent.app.bsky.graph.getKnownFollowers({
29 actor: did!,
30 limit: PAGE_SIZE,
31 cursor: pageParam,
32 })
33 return res.data
34 },
35 initialPageParam: undefined,
36 getNextPageParam: lastPage => lastPage.cursor,
37 enabled: !!did,
38 })
39}
40
41export function* findAllProfilesInQueryData(
42 queryClient: QueryClient,
43 did: string,
44): Generator<AppBskyActorDefs.ProfileView, void> {
45 const queryDatas = queryClient.getQueriesData<
46 InfiniteData<AppBskyGraphGetKnownFollowers.OutputSchema>
47 >({
48 queryKey: [RQKEY_ROOT],
49 })
50 for (const [_queryKey, queryData] of queryDatas) {
51 if (!queryData?.pages) {
52 continue
53 }
54 for (const page of queryData?.pages) {
55 for (const follow of page.followers) {
56 if (follow.did === did) {
57 yield follow
58 }
59 }
60 }
61 }
62}