2023-01-26 00:57:48 +01:00
|
|
|
import { useMutation, UseMutationOptions, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
2022-12-28 23:41:36 +01:00
|
|
|
import apiGeneral from '@utils/api/general'
|
|
|
|
import apiInstance from '@utils/api/instance'
|
|
|
|
import { AxiosError } from 'axios'
|
|
|
|
import * as AuthSession from 'expo-auth-session'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-10 01:59:26 +01:00
|
|
|
export type QueryKeyApps = ['Apps']
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2023-01-26 00:57:48 +01:00
|
|
|
const queryFunctionApps = async () => {
|
2022-12-10 01:59:26 +01:00
|
|
|
const res = await apiInstance<Mastodon.Apps>({
|
|
|
|
method: 'get',
|
|
|
|
url: 'apps/verify_credentials'
|
2021-01-28 00:41:53 +01:00
|
|
|
})
|
2022-12-10 01:59:26 +01:00
|
|
|
return res.body
|
|
|
|
}
|
|
|
|
|
|
|
|
const useAppsQuery = (
|
|
|
|
params: {
|
|
|
|
options?: UseQueryOptions<Mastodon.Apps, AxiosError>
|
|
|
|
} | void
|
|
|
|
) => {
|
|
|
|
const queryKey: QueryKeyApps = ['Apps']
|
|
|
|
return useQuery(queryKey, queryFunctionApps, params?.options)
|
|
|
|
}
|
2021-01-16 23:30:25 +01:00
|
|
|
|
2023-01-09 16:44:11 +01:00
|
|
|
type MutationVarsApps = { domain: string; scopes: string[] }
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2023-07-12 22:34:50 +02:00
|
|
|
export const redirectUri = AuthSession.makeRedirectUri({ native: 'tooot://instance-auth' })
|
2023-01-09 16:44:11 +01:00
|
|
|
const mutationFunctionApps = async ({ domain, scopes }: MutationVarsApps) => {
|
2021-02-20 19:12:44 +01:00
|
|
|
return apiGeneral<Mastodon.Apps>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'post',
|
2022-12-10 01:59:26 +01:00
|
|
|
domain: domain,
|
2023-01-09 16:44:11 +01:00
|
|
|
url: 'api/v1/apps',
|
|
|
|
body: {
|
|
|
|
client_name: 'tooot',
|
|
|
|
website: 'https://tooot.app',
|
|
|
|
redirect_uris: redirectUri,
|
|
|
|
scopes: scopes.join(' ')
|
|
|
|
}
|
2021-02-11 01:33:31 +01:00
|
|
|
}).then(res => res.body)
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2022-12-10 01:59:26 +01:00
|
|
|
const useAppsMutation = (
|
|
|
|
options: UseMutationOptions<Mastodon.Apps, AxiosError, MutationVarsApps>
|
|
|
|
) => {
|
|
|
|
return useMutation(mutationFunctionApps, options)
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 22:34:50 +02:00
|
|
|
export { useAppsMutation, useAppsQuery }
|
|
|
|
|