tooot/src/utils/queryHooks/translate.ts

43 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-05-19 20:40:16 +02:00
import apiGeneral from '@api/general'
2021-05-19 23:28:01 +02:00
import { AxiosError } from 'axios'
import { Constants } from 'react-native-unimodules'
import { useQuery, UseQueryOptions } from 'react-query'
2021-05-19 20:40:16 +02:00
export type QueryKeyTranslate = [
'Translate',
{ toot: string; source: string; target: string }
]
2021-05-19 23:28:01 +02:00
const queryFunction = async ({ queryKey }: { queryKey: QueryKeyTranslate }) => {
2021-05-19 20:40:16 +02:00
const { toot, source, target } = queryKey[1]
2021-05-19 23:28:01 +02:00
const res = await apiGeneral<Translate.Translate>({
2021-05-19 20:40:16 +02:00
domain: 'translate.tooot.app',
method: 'post',
url: 'translate',
params: {
2021-05-19 23:28:01 +02:00
api_key: Constants.manifest?.extra?.translateKey,
2021-05-19 20:40:16 +02:00
q: toot,
source,
target
}
2021-05-19 23:28:01 +02:00
})
return res.body.translatedText
2021-05-19 20:40:16 +02:00
}
2021-05-19 23:28:01 +02:00
const useTranslateQuery = ({
options,
...queryKeyParams
}: QueryKeyTranslate[1] & {
options?: UseQueryOptions<
Translate.Translate['translatedText'],
AxiosError,
Translate.Translate['translatedText']
>
}) => {
2021-05-19 20:40:16 +02:00
const queryKey: QueryKeyTranslate = ['Translate', { ...queryKeyParams }]
2021-05-19 23:28:01 +02:00
return useQuery(queryKey, queryFunction, options)
2021-05-19 20:40:16 +02:00
}
export { useTranslateQuery }