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