import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query' import apiInstance from '@utils/api/instance' import { AxiosError } from 'axios' import { SearchResult } from './search' export type QueryKeyAccount = [ 'Account', Pick | undefined ] const accountQueryFunction = async ({ queryKey }: QueryFunctionContext) => { const key = queryKey[1] if (!key) return Promise.reject() let matchedId = key.id if (key._remote) { await apiInstance({ version: 'v2', method: 'get', url: 'search', params: { q: key.url, type: 'accounts', limit: 1, resolve: true } }) .then(res => { const account = res.body.accounts[0] if (account.url !== key.url) { return Promise.reject() } else { matchedId = account.id } }) .catch(() => Promise.reject()) } const res = await apiInstance({ method: 'get', url: `accounts/${matchedId}` }) return res.body } const useAccountQuery = ({ options, ...queryKeyParams }: { account?: QueryKeyAccount[1] } & { options?: UseQueryOptions }) => { const queryKey: QueryKeyAccount = [ 'Account', queryKeyParams.account ? { id: queryKeyParams.account.id, url: queryKeyParams.account.url, _remote: queryKeyParams.account._remote } : undefined ] return useQuery(queryKey, accountQueryFunction, { ...options, enabled: (queryKeyParams.account?._remote ? !!queryKeyParams.account : true) && options?.enabled }) } /* ----- */ export type QueryKeyAccountInLists = ['AccountInLists', { id: Mastodon.Account['id'] }] const accountInListsQueryFunction = async ({ queryKey }: QueryFunctionContext) => { const { id } = queryKey[1] const res = await apiInstance({ method: 'get', url: `accounts/${id}/lists` }) return res.body } const useAccountInListsQuery = ({ options, ...queryKeyParams }: QueryKeyAccountInLists[1] & { options?: UseQueryOptions }) => { const queryKey: QueryKeyAccountInLists = ['AccountInLists', { ...queryKeyParams }] return useQuery(queryKey, accountInListsQueryFunction, options) } export { useAccountQuery, useAccountInListsQuery }