tooot/src/utils/queryHooks/apps.ts

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import client from '@api/client'
import { AxiosError } from 'axios'
2021-01-16 23:30:25 +01:00
import Constants from 'expo-constants'
2021-01-07 19:13:09 +01:00
import { useQuery, UseQueryOptions } from 'react-query'
export type QueryKey = ['Apps', { instanceDomain?: string }]
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
2021-01-16 23:30:25 +01:00
let redirectUri: string
switch (Constants.manifest.releaseChannel) {
case 'production':
case 'staging':
case 'testing':
redirectUri = 'tooot://expo-auth-session'
break
default:
redirectUri = 'exp://127.0.0.1:19000'
break
}
2021-01-07 19:13:09 +01:00
const { instanceDomain } = queryKey[1]
const formData = new FormData()
2021-01-22 01:34:20 +01:00
formData.append('client_name', 'tooot')
2021-01-16 23:30:25 +01:00
formData.append('website', 'https://tooot.app')
formData.append('redirect_uris', redirectUri)
2021-01-07 19:13:09 +01:00
formData.append('scopes', 'read write follow push')
return client<Mastodon.Apps>({
method: 'post',
instance: 'remote',
instanceDomain,
url: `apps`,
body: formData
})
}
2021-01-11 21:36:57 +01:00
const useAppsQuery = <TData = Mastodon.Apps>({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
}: QueryKey[1] & {
options?: UseQueryOptions<Mastodon.Apps, AxiosError, TData>
}) => {
const queryKey: QueryKey = ['Apps', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useAppsQuery }