tooot/src/utils/queryHooks/users.ts

98 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-12-18 19:59:38 +01:00
import {
2023-01-03 23:57:23 +01:00
QueryFunctionContext,
useInfiniteQuery,
UseInfiniteQueryOptions
} from '@tanstack/react-query'
import apiGeneral from '@utils/api/general'
import { PagedResponse } from '@utils/api/helpers'
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 { TabSharedStackParamList } from '@utils/navigation/navigators'
import { AxiosError } from 'axios'
2023-01-07 18:01:08 +01:00
import { infinitePageParams } from './utils'
2021-01-07 19:13:09 +01:00
2022-12-15 01:41:34 +01:00
export type QueryKeyUsers = ['Users', TabSharedStackParamList['Tab-Shared-Users']]
2021-01-07 19:13:09 +01:00
2023-01-03 23:57:23 +01:00
const queryFunction = async ({ queryKey, pageParam }: QueryFunctionContext<QueryKeyUsers>) => {
2022-12-15 01:41:34 +01:00
const page = queryKey[1]
2021-02-11 01:33:31 +01:00
let params: { [key: string]: string } = { ...pageParam }
2021-01-07 19:13:09 +01:00
2022-12-15 01:41:34 +01:00
switch (page.reference) {
case 'statuses':
return apiInstance<Mastodon.Account[]>({
method: 'get',
2023-01-03 23:57:23 +01:00
url: `statuses/${page.status.id}/${page.type}`,
2022-12-15 01:41:34 +01:00
params
2022-12-15 14:28:36 +01:00
})
2022-12-15 01:41:34 +01:00
case 'accounts':
const localInstance = page.account.username === page.account.acct
if (localInstance) {
return apiInstance<Mastodon.Account[]>({
method: 'get',
2023-01-03 23:57:23 +01:00
url: `accounts/${page.account.id}/${page.type}`,
2022-12-15 01:41:34 +01:00
params
2022-12-15 14:28:36 +01:00
})
2022-12-15 01:41:34 +01:00
} else {
2022-12-15 14:28:36 +01:00
let res: PagedResponse<Mastodon.Account[]>
try {
2023-01-03 23:57:23 +01:00
const domain = urlMatcher(page.account.url)?.domain
2022-12-15 14:28:36 +01:00
if (!domain?.length) {
throw new Error()
2022-12-15 01:41:34 +01:00
}
2022-12-15 14:28:36 +01:00
const resLookup = await apiGeneral<Mastodon.Account>({
2022-12-15 14:28:36 +01:00
method: 'get',
domain,
url: 'api/v1/accounts/lookup',
params: { acct: page.account.acct }
2022-12-15 01:41:34 +01:00
})
if (resLookup?.body) {
2022-12-15 14:28:36 +01:00
res = await apiGeneral<Mastodon.Account[]>({
2022-12-15 01:41:34 +01:00
method: 'get',
2022-12-15 14:28:36 +01:00
domain,
2023-01-03 23:57:23 +01:00
url: `api/v1/accounts/${resLookup.body.id}/${page.type}`,
2022-12-15 01:41:34 +01:00
params
2022-12-15 14:28:36 +01:00
})
return {
...res,
2023-02-25 23:42:04 +01:00
body: res.body.map(account => appendRemote.account(account, domain)),
remoteData: true
}
2022-12-15 14:28:36 +01:00
} else {
throw new Error()
}
} catch {
res = await apiInstance<Mastodon.Account[]>({
method: 'get',
url: `${page.reference}/${page.account.id}/${page.type}`,
params
2022-12-15 01:41:34 +01:00
})
2022-12-15 14:28:36 +01:00
return { ...res, warnIncomplete: true }
}
2022-12-15 01:41:34 +01:00
}
}
2021-01-07 19:13:09 +01:00
}
2021-03-15 00:18:44 +01:00
const useUsersQuery = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-03-15 00:18:44 +01:00
}: QueryKeyUsers[1] & {
2023-01-07 18:01:08 +01:00
options?: Omit<
UseInfiniteQueryOptions<
PagedResponse<Mastodon.Account[]> & { warnIncomplete: boolean; remoteData: boolean },
AxiosError
>,
'getPreviousPageParam' | 'getNextPageParam'
2021-02-11 01:33:31 +01:00
>
2021-01-07 19:13:09 +01:00
}) => {
2021-03-15 00:18:44 +01:00
const queryKey: QueryKeyUsers = ['Users', { ...queryKeyParams }]
2023-01-07 18:01:08 +01:00
return useInfiniteQuery(queryKey, queryFunction, {
...options,
...infinitePageParams
})
2021-01-07 19:13:09 +01:00
}
2021-03-15 00:18:44 +01:00
export { useUsersQuery }