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