tooot/src/utils/queryHooks/translate.ts

44 lines
1011 B
TypeScript

import apiTooot from '@api/tooot'
import haptics from '@components/haptics'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
type Translations = {
provider: string
sourceLanguage: string
text: string[]
}
export type QueryKeyTranslate = [
'Translate',
{
source: string
target: string
text: string[]
}
]
const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
const { source, target, text } = queryKey[1]
const res = await apiTooot<Translations>({
method: 'post',
url: 'translate',
body: { source, target, text }
})
haptics('Light')
return res.body
}
const useTranslateQuery = ({
options,
...queryKeyParams
}: QueryKeyTranslate[1] & {
options?: UseQueryOptions<Translations, AxiosError, Translations>
}) => {
const queryKey: QueryKeyTranslate = ['Translate', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, { ...options, retry: false })
}
export { useTranslateQuery }