about summary refs log tree commit diff
path: root/src/state/queries/profile-follows.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/profile-follows.ts')
-rw-r--r--src/state/queries/profile-follows.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/state/queries/profile-follows.ts b/src/state/queries/profile-follows.ts
new file mode 100644
index 000000000..428c8aebd
--- /dev/null
+++ b/src/state/queries/profile-follows.ts
@@ -0,0 +1,63 @@
+import {AppBskyActorDefs, AppBskyGraphGetFollows} from '@atproto/api'
+import {
+  useInfiniteQuery,
+  InfiniteData,
+  QueryClient,
+  QueryKey,
+} from '@tanstack/react-query'
+
+import {getAgent} from '#/state/session'
+import {STALE} from '#/state/queries'
+
+const PAGE_SIZE = 30
+type RQPageParam = string | undefined
+
+// TODO refactor invalidate on mutate?
+export const RQKEY = (did: string) => ['profile-follows', did]
+
+export function useProfileFollowsQuery(did: string | undefined) {
+  return useInfiniteQuery<
+    AppBskyGraphGetFollows.OutputSchema,
+    Error,
+    InfiniteData<AppBskyGraphGetFollows.OutputSchema>,
+    QueryKey,
+    RQPageParam
+  >({
+    staleTime: STALE.MINUTES.ONE,
+    queryKey: RQKEY(did || ''),
+    async queryFn({pageParam}: {pageParam: RQPageParam}) {
+      const res = await getAgent().app.bsky.graph.getFollows({
+        actor: did || '',
+        limit: PAGE_SIZE,
+        cursor: pageParam,
+      })
+      return res.data
+    },
+    initialPageParam: undefined,
+    getNextPageParam: lastPage => lastPage.cursor,
+    enabled: !!did,
+  })
+}
+
+export function* findAllProfilesInQueryData(
+  queryClient: QueryClient,
+  did: string,
+): Generator<AppBskyActorDefs.ProfileView, void> {
+  const queryDatas = queryClient.getQueriesData<
+    InfiniteData<AppBskyGraphGetFollows.OutputSchema>
+  >({
+    queryKey: ['profile-follows'],
+  })
+  for (const [_queryKey, queryData] of queryDatas) {
+    if (!queryData?.pages) {
+      continue
+    }
+    for (const page of queryData?.pages) {
+      for (const follow of page.follows) {
+        if (follow.did === did) {
+          yield follow
+        }
+      }
+    }
+  }
+}