import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query' import apiInstance from '@utils/api/instance' import { queryClient } from '@utils/queryHooks' import { AxiosError } from 'axios' export type QueryKeySearch = [ 'Search', { type?: 'accounts' | 'hashtags' | 'statuses' term?: string limit?: number } ] export type SearchResult = { accounts: Mastodon.Account[] hashtags: Mastodon.Tag[] statuses: Mastodon.Status[] } const queryFunction = async ({ queryKey, meta }: QueryFunctionContext) => { const { type, term, limit = 10 } = queryKey[1] if (!term?.length) { return Promise.reject('Empty search term') } const res = await apiInstance({ version: 'v2', method: 'get', url: 'search', params: { q: term, ...(type && { type }), limit, resolve: true }, ...(meta && { extras: meta }) }) return res.body } const useSearchQuery = ({ options, ...queryKeyParams }: QueryKeySearch[1] & { options?: UseQueryOptions }) => { const queryKey: QueryKeySearch = ['Search', { ...queryKeyParams }] return useQuery(queryKey, queryFunction, { ...options, staleTime: 3600, cacheTime: 3600 }) } export const searchLocalStatus = async ( uri: Mastodon.Status['uri'], timeout: boolean = false ): Promise => { const queryKey: QueryKeySearch = ['Search', { type: 'statuses', term: uri, limit: 1 }] return await queryClient .fetchQuery(queryKey, queryFunction, { staleTime: 3600, cacheTime: 3600, retry: false, ...(timeout && { meta: { timeout: 1000 } }) }) .then(res => res.statuses[0]?.uri === uri || res.statuses[0]?.url === uri ? res.statuses[0] : Promise.reject() ) } export const searchLocalAccount = async ( url: Mastodon.Account['url'], timeout: boolean = false ): Promise => { const queryKey: QueryKeySearch = ['Search', { type: 'accounts', term: url, limit: 1 }] return await queryClient .fetchQuery(queryKey, queryFunction, { staleTime: 3600, cacheTime: 3600, retry: false, ...(timeout && { meta: { timeout: 1000 } }) }) .then(res => (res.accounts[0].url === url ? res.accounts[0] : Promise.reject())) } export { useSearchQuery }