tooot/src/utils/queryHooks/account.ts

118 lines
3.2 KiB
TypeScript
Raw Normal View History

import { QueryFunctionContext, useQuery, UseQueryOptions } from '@tanstack/react-query'
2023-01-03 23:57:23 +01:00
import apiGeneral from '@utils/api/general'
import apiInstance from '@utils/api/instance'
import { appendRemote } from '@utils/helpers/appendRemote'
2023-01-03 23:57:23 +01:00
import { urlMatcher } from '@utils/helpers/urlMatcher'
import { AxiosError } from 'axios'
2023-01-03 23:57:23 +01:00
import { searchLocalAccount } from './search'
2021-01-07 19:13:09 +01:00
2023-01-02 23:18:22 +01:00
export type QueryKeyAccount = [
'Account',
2023-01-03 23:57:23 +01:00
(
| (Partial<Pick<Mastodon.Account, 'id' | 'acct' | 'username' | '_remote'>> &
Pick<Mastodon.Account, 'url'> & { _local?: boolean })
| undefined
)
2023-01-02 23:18:22 +01:00
]
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-03 23:57:23 +01:00
let matchedAccount: Mastodon.Account | undefined = undefined
2023-01-02 02:08:12 +01:00
2023-01-02 23:18:22 +01:00
if (key._remote) {
2023-01-03 23:57:23 +01:00
const match = urlMatcher(key.url)
const domain = match?.domain
2023-02-06 14:00:40 +01:00
const id = key.id
2023-01-03 23:57:23 +01:00
const acct = key.acct || key.username || match?.account?.acct
if (!key._local && domain) {
try {
if (id) {
matchedAccount = await apiGeneral<Mastodon.Account>({
method: 'get',
domain: domain,
url: `api/v1/accounts/${id}`
}).then(res => appendRemote.account(res.body))
2023-01-03 23:57:23 +01:00
} else if (acct) {
matchedAccount = await apiGeneral<Mastodon.Account>({
method: 'get',
domain: domain,
url: 'api/v1/accounts/lookup',
params: { acct }
}).then(res => appendRemote.account(res.body))
2023-01-02 02:08:12 +01:00
}
2023-01-03 23:57:23 +01:00
} catch {}
}
2021-01-07 19:13:09 +01:00
2023-01-03 23:57:23 +01:00
if (!matchedAccount) {
matchedAccount = await searchLocalAccount(key.url)
}
} else {
if (!matchedAccount) {
matchedAccount = await apiInstance<Mastodon.Account>({
method: 'get',
url: `accounts/${key.id}`
}).then(res => res.body)
}
}
return matchedAccount
2021-01-07 19:13:09 +01:00
}
2021-12-18 19:59:38 +01:00
const useAccountQuery = ({
2023-01-03 23:57:23 +01:00
account,
_local,
options
}: {
account?: QueryKeyAccount[1]
_local?: boolean
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',
2023-01-03 23:57:23 +01:00
account
2023-01-02 23:18:22 +01:00
? {
2023-01-03 23:57:23 +01:00
id: account.id,
username: account.username,
url: account.url,
_remote: account._remote,
...(_local && { _local })
2023-01-02 23:18:22 +01:00
}
: undefined
]
return useQuery(queryKey, accountQueryFunction, {
...options,
2023-01-03 23:57:23 +01:00
enabled: (account?._remote ? !!account : true) && options?.enabled
2023-01-02 23:18:22 +01:00
})
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 }