2022-12-02 00:13:59 +01:00
|
|
|
import apiInstance, { InstanceResponse } from '@api/instance'
|
2021-01-07 19:13:09 +01:00
|
|
|
import { AxiosError } from 'axios'
|
2022-12-02 00:13:59 +01:00
|
|
|
import {
|
|
|
|
QueryFunctionContext,
|
|
|
|
useInfiniteQuery,
|
|
|
|
UseInfiniteQueryOptions,
|
|
|
|
useMutation,
|
|
|
|
UseMutationOptions,
|
|
|
|
useQuery,
|
|
|
|
UseQueryOptions
|
|
|
|
} from 'react-query'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-11-30 22:42:42 +01:00
|
|
|
export type QueryKeyLists = ['Lists']
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
const queryFunction = async () => {
|
|
|
|
const res = await apiInstance<Mastodon.List[]>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'get',
|
|
|
|
url: 'lists'
|
2021-12-18 19:59:38 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 22:42:42 +01:00
|
|
|
const useListsQuery = ({ options }: { options?: UseQueryOptions<Mastodon.List[], AxiosError> }) => {
|
|
|
|
const queryKey: QueryKeyLists = ['Lists']
|
2021-01-07 19:13:09 +01:00
|
|
|
return useQuery(queryKey, queryFunction, options)
|
|
|
|
}
|
|
|
|
|
2022-11-30 22:42:42 +01:00
|
|
|
type MutationVarsLists =
|
|
|
|
| {
|
|
|
|
type: 'add'
|
|
|
|
payload: Omit<Mastodon.List, 'id'>
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'edit'
|
|
|
|
payload: Mastodon.List
|
|
|
|
}
|
2022-11-30 23:47:05 +01:00
|
|
|
| {
|
|
|
|
type: 'delete'
|
|
|
|
payload: Pick<Mastodon.List, 'id'>
|
|
|
|
}
|
2022-11-30 22:42:42 +01:00
|
|
|
|
|
|
|
const mutationFunction = async (params: MutationVarsLists) => {
|
|
|
|
const body = new FormData()
|
|
|
|
switch (params.type) {
|
|
|
|
case 'add':
|
|
|
|
body.append('title', params.payload.title)
|
|
|
|
body.append('replies_policy', params.payload.replies_policy)
|
|
|
|
|
|
|
|
return apiInstance<Mastodon.List>({
|
|
|
|
method: 'post',
|
|
|
|
url: 'lists',
|
|
|
|
body
|
|
|
|
}).then(res => res.body)
|
|
|
|
case 'edit':
|
|
|
|
body.append('title', params.payload.title)
|
|
|
|
body.append('replies_policy', params.payload.replies_policy)
|
|
|
|
|
|
|
|
return apiInstance<Mastodon.List>({
|
|
|
|
method: 'put',
|
|
|
|
url: `lists/${params.payload.id}`,
|
|
|
|
body
|
|
|
|
}).then(res => res.body)
|
2022-11-30 23:47:05 +01:00
|
|
|
case 'delete':
|
2022-12-02 00:13:59 +01:00
|
|
|
return apiInstance<{}>({
|
2022-11-30 23:47:05 +01:00
|
|
|
method: 'delete',
|
|
|
|
url: `lists/${params.payload.id}`
|
2022-12-02 00:13:59 +01:00
|
|
|
}).then(res => res.body)
|
2022-11-30 22:42:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const useListsMutation = (
|
|
|
|
options: UseMutationOptions<Mastodon.List, AxiosError, MutationVarsLists>
|
|
|
|
) => {
|
|
|
|
return useMutation(mutationFunction, options)
|
|
|
|
}
|
|
|
|
|
2022-12-02 00:13:59 +01:00
|
|
|
/* ----- */
|
|
|
|
|
|
|
|
export type QueryKeyListAccounts = ['ListAccounts', { id: Mastodon.List['id'] }]
|
|
|
|
|
|
|
|
const accountsQueryFunction = async ({
|
|
|
|
queryKey,
|
|
|
|
pageParam
|
|
|
|
}: QueryFunctionContext<QueryKeyListAccounts>) => {
|
|
|
|
const { id } = queryKey[1]
|
|
|
|
|
|
|
|
return await apiInstance<Mastodon.Account[]>({
|
|
|
|
method: 'get',
|
|
|
|
url: `lists/${id}/accounts`,
|
|
|
|
params: { ...pageParam, limit: 40 }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const useListAccountsQuery = ({
|
|
|
|
options,
|
|
|
|
...queryKeyParams
|
|
|
|
}: QueryKeyListAccounts[1] & {
|
|
|
|
options?: UseInfiniteQueryOptions<InstanceResponse<Mastodon.Account[]>, AxiosError>
|
|
|
|
}) => {
|
|
|
|
const queryKey: QueryKeyListAccounts = ['ListAccounts', queryKeyParams]
|
|
|
|
return useInfiniteQuery(queryKey, accountsQueryFunction, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccountsMutationVarsLists = {
|
|
|
|
type: 'add' | 'delete'
|
|
|
|
payload: Pick<Mastodon.List, 'id'> & { accounts: Mastodon.Account['id'][] }
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountsMutationFunction = async (params: AccountsMutationVarsLists) => {
|
|
|
|
const body = new FormData()
|
|
|
|
for (const account of params.payload.accounts) {
|
|
|
|
body.append('account_ids[]', account)
|
|
|
|
}
|
|
|
|
return apiInstance<{}>({
|
|
|
|
method: params.type === 'add' ? 'post' : 'delete',
|
|
|
|
url: `lists/${params.payload.id}/accounts`,
|
|
|
|
body
|
|
|
|
}).then(res => res.body)
|
|
|
|
}
|
|
|
|
|
|
|
|
const useListAccountsMutation = (
|
|
|
|
options: UseMutationOptions<Mastodon.List, AxiosError, AccountsMutationVarsLists>
|
|
|
|
) => {
|
|
|
|
return useMutation(accountsMutationFunction, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useListsQuery, useListsMutation, useListAccountsQuery, useListAccountsMutation }
|