mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Lots of updates
This commit is contained in:
46
src/utils/queryHooks/relationships.ts
Normal file
46
src/utils/queryHooks/relationships.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import client from '@api/client'
|
||||
import { AxiosError } from 'axios'
|
||||
import { useInfiniteQuery, UseInfiniteQueryOptions } from 'react-query'
|
||||
|
||||
export type QueryKey = [
|
||||
'Relationships',
|
||||
{ type: 'following' | 'followers'; id: Mastodon.Account['id'] }
|
||||
]
|
||||
|
||||
const queryFunction = ({
|
||||
queryKey,
|
||||
pageParam
|
||||
}: {
|
||||
queryKey: QueryKey
|
||||
pageParam?: { direction: 'next'; id: Mastodon.Status['id'] }
|
||||
}) => {
|
||||
const { type, id } = queryKey[1]
|
||||
let params: { [key: string]: string } = {}
|
||||
|
||||
if (pageParam) {
|
||||
switch (pageParam.direction) {
|
||||
case 'next':
|
||||
params.max_id = pageParam.id
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return client<Mastodon.Account[]>({
|
||||
method: 'get',
|
||||
instance: 'local',
|
||||
url: `accounts/${id}/${type}`,
|
||||
params
|
||||
})
|
||||
}
|
||||
|
||||
const hookRelationships = <TData = Mastodon.Account[]>({
|
||||
options,
|
||||
...queryKeyParams
|
||||
}: QueryKey[1] & {
|
||||
options?: UseInfiniteQueryOptions<Mastodon.Account[], AxiosError, TData>
|
||||
}) => {
|
||||
const queryKey: QueryKey = ['Relationships', { ...queryKeyParams }]
|
||||
return useInfiniteQuery(queryKey, queryFunction, options)
|
||||
}
|
||||
|
||||
export default hookRelationships
|
Reference in New Issue
Block a user