tooot/src/utils/queryHooks/users.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
2021-08-29 15:25:38 +02:00
import { TabSharedStackParamList } from '@utils/navigation/navigators'
2021-01-07 19:13:09 +01:00
import { AxiosError } from 'axios'
import { useInfiniteQuery, UseInfiniteQueryOptions } from 'react-query'
2021-03-15 00:18:44 +01:00
export type QueryKeyUsers = [
'Users',
2021-08-29 15:25:38 +02:00
TabSharedStackParamList['Tab-Shared-Users']
2021-01-07 19:13:09 +01:00
]
const queryFunction = ({
queryKey,
pageParam
}: {
2021-03-15 00:18:44 +01:00
queryKey: QueryKeyUsers
2021-02-11 01:33:31 +01:00
pageParam?: { [key: string]: string }
2021-01-07 19:13:09 +01:00
}) => {
2021-03-15 00:18:44 +01:00
const { reference, id, type } = 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',
2021-03-15 00:18:44 +01:00
url: `${reference}/${id}/${type}`,
2021-01-07 19:13:09 +01:00
params
})
}
2021-03-15 00:18:44 +01:00
const useUsersQuery = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-03-15 00:18:44 +01:00
}: QueryKeyUsers[1] & {
2021-02-11 01:33:31 +01:00
options?: UseInfiniteQueryOptions<
{
body: Mastodon.Account[]
links?: { prev?: string; next?: string }
},
AxiosError,
2021-03-15 00:18:44 +01:00
{
body: Mastodon.Account[]
links?: { prev?: string; next?: string }
}
2021-02-11 01:33:31 +01:00
>
2021-01-07 19:13:09 +01:00
}) => {
2021-03-15 00:18:44 +01:00
const queryKey: QueryKeyUsers = ['Users', { ...queryKeyParams }]
2021-01-07 19:13:09 +01:00
return useInfiniteQuery(queryKey, queryFunction, options)
}
2021-03-15 00:18:44 +01:00
export { useUsersQuery }