tooot/src/utils/queryHooks/account.ts

96 lines
2.4 KiB
TypeScript
Raw Normal View History

import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
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 23:18:22 +01:00
export type QueryKeyAccount = [
'Account',
Pick<Mastodon.Account, 'id' | 'url' | '_remote'> | undefined
]
2021-01-07 19:13:09 +01:00
const accountQueryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
2023-01-02 23:18:22 +01:00
const key = queryKey[1]
if (!key) return Promise.reject()
2023-01-02 02:08:12 +01:00
2023-01-02 23:18:22 +01:00
let matchedId = key.id
2023-01-02 02:08:12 +01:00
2023-01-02 23:18:22 +01:00
if (key._remote) {
2023-01-02 02:08:12 +01:00
await apiInstance<SearchResult>({
version: 'v2',
method: 'get',
url: 'search',
params: {
2023-01-02 23:18:22 +01:00
q: key.url,
2023-01-02 02:08:12 +01:00
type: 'accounts',
limit: 1,
resolve: true
}
})
.then(res => {
const account = res.body.accounts[0]
2023-01-02 23:18:22 +01:00
if (account.url !== key.url) {
2023-01-02 02:08:12 +01:00
return Promise.reject()
} else {
matchedId = account.id
}
})
.catch(() => Promise.reject())
}
2021-01-07 19:13:09 +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}`
})
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
2023-01-02 23:18:22 +01:00
}: { account?: QueryKeyAccount[1] } & {
2021-12-18 19:59:38 +01:00
options?: UseQueryOptions<Mastodon.Account, AxiosError>
2021-01-07 19:13:09 +01:00
}) => {
2023-01-02 23:18:22 +01:00
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
})
2021-01-07 19:13:09 +01:00
}
2022-12-03 15:50:15 +01:00
/* ----- */
export type QueryKeyAccountInLists = ['AccountInLists', { id: Mastodon.Account['id'] }]
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]
const res = await apiInstance<Mastodon.List[]>({
2022-12-03 15:50:15 +01:00
method: 'get',
url: `accounts/${id}/lists`
})
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 }