2022-12-10 14:47:19 +01:00
|
|
|
import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
|
2022-12-28 23:41:36 +01:00
|
|
|
import apiInstance from '@utils/api/instance'
|
|
|
|
import { AxiosError } from 'axios'
|
2023-01-02 02:08:12 +01:00
|
|
|
import { SearchResult } from './search'
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2023-01-02 02:08:12 +01:00
|
|
|
export type QueryKeyAccount = ['Account', { id?: Mastodon.Account['id']; remoteUrl?: string }]
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
|
2023-01-02 02:08:12 +01:00
|
|
|
const { id, remoteUrl } = queryKey[1]
|
|
|
|
if (!id && !remoteUrl) return Promise.reject()
|
|
|
|
|
|
|
|
let matchedId = id
|
|
|
|
|
|
|
|
if (remoteUrl) {
|
|
|
|
await apiInstance<SearchResult>({
|
|
|
|
version: 'v2',
|
|
|
|
method: 'get',
|
|
|
|
url: 'search',
|
|
|
|
params: {
|
|
|
|
q: remoteUrl,
|
|
|
|
type: 'accounts',
|
|
|
|
limit: 1,
|
|
|
|
resolve: true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
const account = res.body.accounts[0]
|
|
|
|
if (account.url !== remoteUrl) {
|
|
|
|
return Promise.reject()
|
|
|
|
} else {
|
|
|
|
matchedId = account.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => Promise.reject())
|
|
|
|
}
|
2021-01-07 19:13:09 +01:00
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const res = await apiInstance<Mastodon.Account>({
|
2021-01-07 19:13:09 +01:00
|
|
|
method: 'get',
|
2023-01-02 02:08:12 +01:00
|
|
|
url: `accounts/${matchedId}`
|
2022-12-10 14:47:19 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-18 19:59:38 +01:00
|
|
|
const useAccountQuery = ({
|
2021-01-07 19:13:09 +01:00
|
|
|
options,
|
|
|
|
...queryKeyParams
|
2021-05-09 21:59:03 +02:00
|
|
|
}: QueryKeyAccount[1] & {
|
2021-12-18 19:59:38 +01:00
|
|
|
options?: UseQueryOptions<Mastodon.Account, AxiosError>
|
2021-01-07 19:13:09 +01:00
|
|
|
}) => {
|
2021-05-09 21:59:03 +02:00
|
|
|
const queryKey: QueryKeyAccount = ['Account', { ...queryKeyParams }]
|
2023-01-02 02:08:12 +01:00
|
|
|
return useQuery(queryKey, accountQueryFunction, { ...options })
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2022-12-03 15:50:15 +01:00
|
|
|
/* ----- */
|
|
|
|
|
|
|
|
export type QueryKeyAccountInLists = ['AccountInLists', { id: Mastodon.Account['id'] }]
|
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const accountInListsQueryFunction = async ({
|
2022-12-04 16:16:53 +01:00
|
|
|
queryKey
|
|
|
|
}: QueryFunctionContext<QueryKeyAccountInLists>) => {
|
2022-12-03 15:50:15 +01:00
|
|
|
const { id } = queryKey[1]
|
|
|
|
|
2022-12-10 14:47:19 +01:00
|
|
|
const res = await apiInstance<Mastodon.List[]>({
|
2022-12-03 15:50:15 +01:00
|
|
|
method: 'get',
|
|
|
|
url: `accounts/${id}/lists`
|
2022-12-10 14:47:19 +01:00
|
|
|
})
|
|
|
|
return res.body
|
2022-12-03 15:50:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const useAccountInListsQuery = ({
|
|
|
|
options,
|
|
|
|
...queryKeyParams
|
2023-01-02 02:08:12 +01:00
|
|
|
}: QueryKeyAccountInLists[1] & {
|
2022-12-03 15:50:15 +01:00
|
|
|
options?: UseQueryOptions<Mastodon.List[], AxiosError>
|
|
|
|
}) => {
|
2022-12-04 16:16:53 +01:00
|
|
|
const queryKey: QueryKeyAccountInLists = ['AccountInLists', { ...queryKeyParams }]
|
2022-12-03 15:50:15 +01:00
|
|
|
return useQuery(queryKey, accountInListsQueryFunction, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
export { useAccountQuery, useAccountInListsQuery }
|