tooot/src/utils/queryHooks/users.ts

43 lines
1020 B
TypeScript
Raw Normal View History

2021-12-18 19:59:38 +01:00
import apiInstance, { InstanceResponse } 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'
2021-12-18 19:59:38 +01:00
import {
QueryFunctionContext,
useInfiniteQuery,
UseInfiniteQueryOptions
} from 'react-query'
2021-01-07 19:13:09 +01:00
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-12-18 19:59:38 +01:00
}: QueryFunctionContext<QueryKeyUsers>) => {
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<
2021-12-18 19:59:38 +01:00
InstanceResponse<Mastodon.Account[]>,
AxiosError
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 }