diff options
Diffstat (limited to 'src/state/queries/post-liked-by.ts')
-rw-r--r-- | src/state/queries/post-liked-by.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/state/queries/post-liked-by.ts b/src/state/queries/post-liked-by.ts new file mode 100644 index 000000000..2cde07f28 --- /dev/null +++ b/src/state/queries/post-liked-by.ts @@ -0,0 +1,61 @@ +import {AppBskyActorDefs, AppBskyFeedGetLikes} from '@atproto/api' +import { + useInfiniteQuery, + InfiniteData, + QueryClient, + QueryKey, +} from '@tanstack/react-query' + +import {getAgent} from '#/state/session' + +const PAGE_SIZE = 30 +type RQPageParam = string | undefined + +// TODO refactor invalidate on mutate? +export const RQKEY = (resolvedUri: string) => ['post-liked-by', resolvedUri] + +export function usePostLikedByQuery(resolvedUri: string | undefined) { + return useInfiniteQuery< + AppBskyFeedGetLikes.OutputSchema, + Error, + InfiniteData<AppBskyFeedGetLikes.OutputSchema>, + QueryKey, + RQPageParam + >({ + queryKey: RQKEY(resolvedUri || ''), + async queryFn({pageParam}: {pageParam: RQPageParam}) { + const res = await getAgent().getLikes({ + uri: resolvedUri || '', + limit: PAGE_SIZE, + cursor: pageParam, + }) + return res.data + }, + initialPageParam: undefined, + getNextPageParam: lastPage => lastPage.cursor, + enabled: !!resolvedUri, + }) +} + +export function* findAllProfilesInQueryData( + queryClient: QueryClient, + did: string, +): Generator<AppBskyActorDefs.ProfileView, void> { + const queryDatas = queryClient.getQueriesData< + InfiniteData<AppBskyFeedGetLikes.OutputSchema> + >({ + queryKey: ['post-liked-by'], + }) + for (const [_queryKey, queryData] of queryDatas) { + if (!queryData?.pages) { + continue + } + for (const page of queryData?.pages) { + for (const like of page.likes) { + if (like.actor.did === did) { + yield like.actor + } + } + } + } +} |