2022-12-10 14:47:19 +01:00
|
|
|
import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
2022-12-28 23:41:36 +01:00
|
|
|
import apiInstance from '@utils/api/instance'
|
|
|
|
import { AxiosError } from 'axios'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2021-05-09 21:59:03 +02:00
|
|
|
export type QueryKeyAccount = ['Account', { id: Mastodon.Account['id'] }]
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
|
2021-01-07 19:13:09 +01:00
|
|
|
const { id } = queryKey[1]
|
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const res = await apiInstance<Mastodon.Account>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'get',
|
|
|
|
url: `accounts/${id}`
|
2022-12-10 14:47:19 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
const useAccountQuery = ({
|
2021-01-07 19:13:09 +01:00
|
|
|
options,
|
|
|
|
...queryKeyParams
|
2021-05-09 21:59:03 +02:00
|
|
|
}: QueryKeyAccount[1] & {
|
2021-12-18 19:59:38 +01:00
|
|
|
options?: UseQueryOptions<Mastodon.Account, AxiosError>
|
2021-01-07 19:13:09 +01:00
|
|
|
}) => {
|
2021-05-09 21:59:03 +02:00
|
|
|
const queryKey: QueryKeyAccount = ['Account', { ...queryKeyParams }]
|
2022-12-03 15:50:15 +01:00
|
|
|
return useQuery(queryKey, accountQueryFunction, options)
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:50:15 +01:00
|
|
|
/* ----- */
|
|
|
|
|
|
|
|
export type QueryKeyAccountInLists = ['AccountInLists', { id: Mastodon.Account['id'] }]
|
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const accountInListsQueryFunction = async ({
|
2022-12-04 16:16:53 +01:00
|
|
|
queryKey
|
|
|
|
}: QueryFunctionContext<QueryKeyAccountInLists>) => {
|
2022-12-03 15:50:15 +01:00
|
|
|
const { id } = queryKey[1]
|
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const res = await apiInstance<Mastodon.List[]>({
|
2022-12-03 15:50:15 +01:00
|
|
|
method: 'get',
|
|
|
|
url: `accounts/${id}/lists`
|
2022-12-10 14:47:19 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2022-12-03 15:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const useAccountInListsQuery = ({
|
|
|
|
options,
|
|
|
|
...queryKeyParams
|
|
|
|
}: QueryKeyAccount[1] & {
|
|
|
|
options?: UseQueryOptions<Mastodon.List[], AxiosError>
|
|
|
|
}) => {
|
2022-12-04 16:16:53 +01:00
|
|
|
const queryKey: QueryKeyAccountInLists = ['AccountInLists', { ...queryKeyParams }]
|
2022-12-03 15:50:15 +01:00
|
|
|
return useQuery(queryKey, accountInListsQueryFunction, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useAccountQuery, useAccountInListsQuery }
|