tooot/src/utils/queryHooks/account.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
2021-01-07 19:13:09 +01:00
import { AxiosError } from 'axios'
import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
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
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
2021-01-07 19:13:09 +01:00
const { id } = queryKey[1]
const res = await apiInstance<Mastodon.Account>({
2021-01-07 19:13:09 +01:00
method: 'get',
url: `accounts/${id}`
})
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'] }]
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]
const res = await apiInstance<Mastodon.List[]>({
2022-12-03 15:50:15 +01:00
method: 'get',
url: `accounts/${id}/lists`
})
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 }