mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at rm-proxy 92 lines 2.0 kB view raw
1import {AppBskyActorDefs, AppBskyActorSearchActors} from '@atproto/api' 2import { 3 InfiniteData, 4 QueryClient, 5 QueryKey, 6 useInfiniteQuery, 7 useQuery, 8} from '@tanstack/react-query' 9 10import {STALE} from '#/state/queries' 11import {useAgent} from '#/state/session' 12 13const RQKEY_ROOT = 'actor-search' 14export const RQKEY = (query: string) => [RQKEY_ROOT, query] 15 16export const RQKEY_PAGINATED = (query: string) => [ 17 `${RQKEY_ROOT}_paginated`, 18 query, 19] 20 21export function useActorSearch({ 22 query, 23 enabled, 24}: { 25 query: string 26 enabled?: boolean 27}) { 28 const agent = useAgent() 29 return useQuery<AppBskyActorDefs.ProfileView[]>({ 30 staleTime: STALE.MINUTES.ONE, 31 queryKey: RQKEY(query || ''), 32 async queryFn() { 33 const res = await agent.searchActors({ 34 q: query, 35 }) 36 return res.data.actors 37 }, 38 enabled: enabled && !!query, 39 }) 40} 41 42export function useActorSearchPaginated({ 43 query, 44 enabled, 45}: { 46 query: string 47 enabled?: boolean 48}) { 49 const agent = useAgent() 50 return useInfiniteQuery< 51 AppBskyActorSearchActors.OutputSchema, 52 Error, 53 InfiniteData<AppBskyActorSearchActors.OutputSchema>, 54 QueryKey, 55 string | undefined 56 >({ 57 staleTime: STALE.MINUTES.FIVE, 58 queryKey: RQKEY_PAGINATED(query), 59 queryFn: async ({pageParam}) => { 60 const res = await agent.searchActors({ 61 q: query, 62 limit: 25, 63 cursor: pageParam, 64 }) 65 return res.data 66 }, 67 enabled: enabled && !!query, 68 initialPageParam: undefined, 69 getNextPageParam: lastPage => lastPage.cursor, 70 }) 71} 72 73export function* findAllProfilesInQueryData( 74 queryClient: QueryClient, 75 did: string, 76) { 77 const queryDatas = queryClient.getQueriesData<AppBskyActorDefs.ProfileView[]>( 78 { 79 queryKey: [RQKEY_ROOT], 80 }, 81 ) 82 for (const [_queryKey, queryData] of queryDatas) { 83 if (!queryData) { 84 continue 85 } 86 for (const actor of queryData) { 87 if (actor.did === did) { 88 yield actor 89 } 90 } 91 } 92}