tooot/src/utils/queryHooks/search.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
import apiInstance from '@utils/api/instance'
2023-01-03 23:57:23 +01:00
import { queryClient } from '@utils/queryHooks'
import { AxiosError } from 'axios'
2021-01-07 19:13:09 +01:00
2021-12-18 19:59:38 +01:00
export type QueryKeySearch = [
2021-01-07 19:13:09 +01:00
'Search',
{
type?: 'accounts' | 'hashtags' | 'statuses'
term?: string
limit?: number
}
]
2021-03-21 23:06:53 +01:00
export type SearchResult = {
2021-01-07 19:13:09 +01:00
accounts: Mastodon.Account[]
hashtags: Mastodon.Tag[]
statuses: Mastodon.Status[]
}
const queryFunction = async ({ queryKey, meta }: QueryFunctionContext<QueryKeySearch>) => {
const { type, term, limit = 10 } = queryKey[1]
2022-12-15 14:28:36 +01:00
if (!term?.length) {
2023-01-02 23:18:22 +01:00
return Promise.reject('Empty search term')
2022-12-15 14:28:36 +01:00
}
2021-12-18 19:59:38 +01:00
const res = await apiInstance<SearchResult>({
2021-01-07 19:13:09 +01:00
version: 'v2',
method: 'get',
url: 'search',
2021-03-21 23:06:53 +01:00
params: {
2022-12-15 14:28:36 +01:00
q: term,
2021-03-21 23:06:53 +01:00
...(type && { type }),
limit,
resolve: true
},
...(meta && { extras: meta })
2021-12-18 19:59:38 +01:00
})
return res.body
2021-01-07 19:13:09 +01:00
}
2022-12-15 14:28:36 +01:00
const useSearchQuery = <T = SearchResult>({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-12-18 19:59:38 +01:00
}: QueryKeySearch[1] & {
options?: UseQueryOptions<SearchResult, AxiosError, T>
2021-01-07 19:13:09 +01:00
}) => {
2021-12-18 19:59:38 +01:00
const queryKey: QueryKeySearch = ['Search', { ...queryKeyParams }]
2023-01-03 23:57:23 +01:00
return useQuery(queryKey, queryFunction, { ...options, staleTime: 3600, cacheTime: 3600 })
2021-01-07 19:13:09 +01:00
}
2023-01-15 18:00:58 +01:00
export const searchLocalStatus = async (
uri: Mastodon.Status['uri'],
timeout: boolean = false
): Promise<Mastodon.Status> => {
2023-01-03 23:57:23 +01:00
const queryKey: QueryKeySearch = ['Search', { type: 'statuses', term: uri, limit: 1 }]
return await queryClient
.fetchQuery(queryKey, queryFunction, {
staleTime: 3600,
cacheTime: 3600,
retry: false,
2023-01-15 18:00:58 +01:00
...(timeout && { meta: { timeout: 1000 } })
})
2023-01-03 23:57:23 +01:00
.then(res =>
2023-01-11 22:54:24 +01:00
res.statuses[0]?.uri === uri || res.statuses[0]?.url === uri
2023-01-03 23:57:23 +01:00
? res.statuses[0]
: Promise.reject()
)
}
export const searchLocalAccount = async (
2023-01-15 18:00:58 +01:00
url: Mastodon.Account['url'],
timeout: boolean = false
2023-01-03 23:57:23 +01:00
): Promise<Mastodon.Account> => {
const queryKey: QueryKeySearch = ['Search', { type: 'accounts', term: url, limit: 1 }]
return await queryClient
.fetchQuery(queryKey, queryFunction, {
staleTime: 3600,
cacheTime: 3600,
retry: false,
2023-01-15 18:00:58 +01:00
...(timeout && { meta: { timeout: 1000 } })
})
2023-01-03 23:57:23 +01:00
.then(res => (res.accounts[0].url === url ? res.accounts[0] : Promise.reject()))
}
2023-01-01 17:20:35 +01:00
2021-01-11 21:36:57 +01:00
export { useSearchQuery }