2023-12-08 02:20:17 +01:00
|
|
|
import {AppBskyActorDefs} from '@atproto/api'
|
|
|
|
import {QueryClient, useQuery} from '@tanstack/react-query'
|
|
|
|
|
|
|
|
import {STALE} from '#/state/queries'
|
2024-04-25 23:29:05 +02:00
|
|
|
import {useAgent} from '#/state/session'
|
2023-12-08 02:20:17 +01:00
|
|
|
|
2024-04-04 19:57:38 +02:00
|
|
|
const RQKEY_ROOT = 'actor-search'
|
2024-04-11 00:32:31 +02:00
|
|
|
export const RQKEY = (query: string) => [RQKEY_ROOT, query]
|
2023-12-08 02:20:17 +01:00
|
|
|
|
2024-04-11 00:32:31 +02:00
|
|
|
export function useActorSearch({
|
|
|
|
query,
|
|
|
|
enabled,
|
|
|
|
}: {
|
|
|
|
query: string
|
|
|
|
enabled?: boolean
|
|
|
|
}) {
|
2024-04-25 23:29:05 +02:00
|
|
|
const {getAgent} = useAgent()
|
2023-12-08 02:20:17 +01:00
|
|
|
return useQuery<AppBskyActorDefs.ProfileView[]>({
|
|
|
|
staleTime: STALE.MINUTES.ONE,
|
2024-04-11 00:32:31 +02:00
|
|
|
queryKey: RQKEY(query || ''),
|
2023-12-08 02:20:17 +01:00
|
|
|
async queryFn() {
|
|
|
|
const res = await getAgent().searchActors({
|
2024-04-11 00:32:31 +02:00
|
|
|
q: query,
|
2023-12-08 02:20:17 +01:00
|
|
|
})
|
|
|
|
return res.data.actors
|
|
|
|
},
|
2024-04-11 00:32:31 +02:00
|
|
|
enabled: enabled && !!query,
|
2023-12-08 02:20:17 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* findAllProfilesInQueryData(
|
|
|
|
queryClient: QueryClient,
|
|
|
|
did: string,
|
|
|
|
) {
|
|
|
|
const queryDatas = queryClient.getQueriesData<AppBskyActorDefs.ProfileView[]>(
|
|
|
|
{
|
2024-04-04 19:57:38 +02:00
|
|
|
queryKey: [RQKEY_ROOT],
|
2023-12-08 02:20:17 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
for (const [_queryKey, queryData] of queryDatas) {
|
|
|
|
if (!queryData) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for (const actor of queryData) {
|
|
|
|
if (actor.did === did) {
|
|
|
|
yield actor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|