mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Account actions working for #638
This commit is contained in:
@ -1,15 +1,42 @@
|
||||
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', { id: Mastodon.Account['id'] }]
|
||||
export type QueryKeyAccount = ['Account', { id?: Mastodon.Account['id']; remoteUrl?: string }]
|
||||
|
||||
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
|
||||
const { id } = queryKey[1]
|
||||
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())
|
||||
}
|
||||
|
||||
const res = await apiInstance<Mastodon.Account>({
|
||||
method: 'get',
|
||||
url: `accounts/${id}`
|
||||
url: `accounts/${matchedId}`
|
||||
})
|
||||
return res.body
|
||||
}
|
||||
@ -21,7 +48,7 @@ const useAccountQuery = ({
|
||||
options?: UseQueryOptions<Mastodon.Account, AxiosError>
|
||||
}) => {
|
||||
const queryKey: QueryKeyAccount = ['Account', { ...queryKeyParams }]
|
||||
return useQuery(queryKey, accountQueryFunction, options)
|
||||
return useQuery(queryKey, accountQueryFunction, { ...options })
|
||||
}
|
||||
|
||||
/* ----- */
|
||||
@ -43,7 +70,7 @@ const accountInListsQueryFunction = async ({
|
||||
const useAccountInListsQuery = ({
|
||||
options,
|
||||
...queryKeyParams
|
||||
}: QueryKeyAccount[1] & {
|
||||
}: QueryKeyAccountInLists[1] & {
|
||||
options?: UseQueryOptions<Mastodon.List[], AxiosError>
|
||||
}) => {
|
||||
const queryKey: QueryKeyAccountInLists = ['AccountInLists', { ...queryKeyParams }]
|
||||
|
Reference in New Issue
Block a user