import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query' import apiInstance from '@utils/api/instance' 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 }: QueryFunctionContext) => { const { type, term, limit = 20 } = queryKey[1] if (!term?.length) { return Promise.reject() } const res = await apiInstance({ version: 'v2', method: 'get', url: 'search', params: { q: term, ...(type && { type }), limit, resolve: true } }) return res.body } const useSearchQuery = ({ options, ...queryKeyParams }: QueryKeySearch[1] & { options?: UseQueryOptions }) => { const queryKey: QueryKeySearch = ['Search', { ...queryKeyParams }] return useQuery(queryKey, queryFunction, options) } export { useSearchQuery }