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