forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {
2 type AppBskyActorDefs,
3 type AppBskyUnspeccedGetSuggestedUsersForExplore,
4} from '@atproto/api'
5import {type QueryClient, useQuery} from '@tanstack/react-query'
6
7import {
8 aggregateUserInterests,
9 createBskyTopicsHeader,
10} from '#/lib/api/feed/utils'
11import {getContentLanguages} from '#/state/preferences/languages'
12import {STALE} from '#/state/queries'
13import {usePreferencesQuery} from '#/state/queries/preferences'
14import {useAgent} from '#/state/session'
15
16export type QueryProps = {
17 category?: string | null
18 limit?: number
19}
20
21export const getSuggestedUsersForExploreQueryKeyRoot =
22 'unspecced-suggested-users-for-explore'
23export const createGetSuggestedUsersForExploreQueryKey = (
24 props: QueryProps,
25) => [getSuggestedUsersForExploreQueryKeyRoot, props.category, props.limit]
26
27export function useGetSuggestedUsersForExploreQuery(props: QueryProps = {}) {
28 const agent = useAgent()
29 const {data: preferences} = usePreferencesQuery()
30
31 return useQuery({
32 staleTime: STALE.MINUTES.THREE,
33 queryKey: createGetSuggestedUsersForExploreQueryKey(props),
34 queryFn: async () => {
35 const contentLangs = getContentLanguages().join(',')
36 const userInterests = aggregateUserInterests(preferences)
37
38 const {data} = await agent.app.bsky.unspecced.getSuggestedUsersForExplore(
39 {
40 category: props.category ?? undefined,
41 limit: props.limit || 10,
42 },
43 {
44 headers: {
45 ...createBskyTopicsHeader(userInterests),
46 'Accept-Language': contentLangs,
47 },
48 },
49 )
50
51 return {...data, recId: data.recIdStr}
52 },
53 })
54}
55
56export function* findAllProfilesInQueryData(
57 queryClient: QueryClient,
58 did: string,
59): Generator<AppBskyActorDefs.ProfileView, void> {
60 const responses =
61 queryClient.getQueriesData<AppBskyUnspeccedGetSuggestedUsersForExplore.OutputSchema>(
62 {
63 queryKey: [getSuggestedUsersForExploreQueryKeyRoot],
64 },
65 )
66 for (const [_key, response] of responses) {
67 if (!response) {
68 continue
69 }
70
71 for (const actor of response.actors) {
72 if (actor.did === did) {
73 yield actor
74 }
75 }
76 }
77}