tooot/src/utils/queryHooks/relationship.ts

41 lines
901 B
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
2021-01-10 02:12:14 +01:00
export type QueryKeyRelationship = [
'Relationship',
{ id: Mastodon.Account['id'] }
]
2021-01-07 19:13:09 +01:00
2021-01-10 02:12:14 +01:00
const queryFunction = ({ queryKey }: { queryKey: QueryKeyRelationship }) => {
2021-01-07 19:13:09 +01:00
const { id } = queryKey[1]
2021-01-10 02:12:14 +01:00
return client<Mastodon.Relationship[]>({
2021-01-07 19:13:09 +01:00
method: 'get',
instance: 'local',
url: `accounts/relationships`,
params: {
'id[]': id
}
})
}
2021-01-10 02:12:14 +01:00
const hookRelationship = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-01-10 02:12:14 +01:00
}: QueryKeyRelationship[1] & {
options?: UseQueryOptions<
Mastodon.Relationship[],
AxiosError,
Mastodon.Relationship
>
2021-01-07 19:13:09 +01:00
}) => {
2021-01-10 02:12:14 +01:00
const queryKey: QueryKeyRelationship = ['Relationship', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, {
...options,
select: data => data[0]
})
2021-01-07 19:13:09 +01:00
}
export default hookRelationship