tooot/src/utils/queryHooks/relationships.ts

48 lines
1.1 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 { useInfiniteQuery, UseInfiniteQueryOptions } from 'react-query'
2021-02-27 16:33:54 +01:00
export type QueryKeyRelationships = [
2021-01-07 19:13:09 +01:00
'Relationships',
{ type: 'following' | 'followers'; id: Mastodon.Account['id'] }
]
const queryFunction = ({
queryKey,
pageParam
}: {
2021-02-27 16:33:54 +01:00
queryKey: QueryKeyRelationships
2021-02-11 01:33:31 +01:00
pageParam?: { [key: string]: string }
2021-01-07 19:13:09 +01:00
}) => {
const { type, id } = queryKey[1]
2021-02-11 01:33:31 +01:00
let params: { [key: string]: string } = { ...pageParam }
2021-01-07 19:13:09 +01:00
2021-02-20 19:12:44 +01:00
return apiInstance<Mastodon.Account[]>({
2021-01-07 19:13:09 +01:00
method: 'get',
url: `accounts/${id}/${type}`,
params
})
}
2021-01-11 21:36:57 +01:00
const useRelationshipsQuery = <TData = Mastodon.Account[]>({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-02-27 16:33:54 +01:00
}: QueryKeyRelationships[1] & {
2021-02-11 01:33:31 +01:00
options?: UseInfiniteQueryOptions<
{
body: Mastodon.Account[]
links?: { prev?: string; next?: string }
},
AxiosError,
TData
>
2021-01-07 19:13:09 +01:00
}) => {
2021-02-27 16:33:54 +01:00
const queryKey: QueryKeyRelationships = [
'Relationships',
{ ...queryKeyParams }
]
2021-01-07 19:13:09 +01:00
return useInfiniteQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useRelationshipsQuery }