mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {AppBskyActorDefs, AppBskyFeedGetRepostedBy} 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 = 30
12type RQPageParam = string | undefined
13
14// TODO refactor invalidate on mutate?
15const RQKEY_ROOT = 'post-reposted-by'
16export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri]
17
18export function usePostRepostedByQuery(resolvedUri: string | undefined) {
19 const agent = useAgent()
20 return useInfiniteQuery<
21 AppBskyFeedGetRepostedBy.OutputSchema,
22 Error,
23 InfiniteData<AppBskyFeedGetRepostedBy.OutputSchema>,
24 QueryKey,
25 RQPageParam
26 >({
27 queryKey: RQKEY(resolvedUri || ''),
28 async queryFn({pageParam}: {pageParam: RQPageParam}) {
29 const res = await agent.getRepostedBy({
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<AppBskyFeedGetRepostedBy.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 repostedBy of page.repostedBy) {
57 if (repostedBy.did === did) {
58 yield repostedBy
59 }
60 }
61 }
62 }
63}