2022-12-15 14:28:36 +01:00
|
|
|
import apiInstance from '@api/instance'
|
2021-08-29 15:25:38 +02:00
|
|
|
import { TabSharedStackParamList } from '@utils/navigation/navigators'
|
2021-01-07 19:13:09 +01:00
|
|
|
import { AxiosError } from 'axios'
|
2021-12-18 19:59:38 +01:00
|
|
|
import {
|
|
|
|
QueryFunctionContext,
|
|
|
|
useInfiniteQuery,
|
|
|
|
UseInfiniteQueryOptions
|
2022-12-10 14:47:19 +01:00
|
|
|
} from '@tanstack/react-query'
|
2022-12-15 01:41:34 +01:00
|
|
|
import apiGeneral from '@api/general'
|
2022-12-15 14:28:36 +01:00
|
|
|
import { PagedResponse } from '@api/helpers'
|
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
|
|
|
|
2022-12-15 14:28:36 +01:00
|
|
|
const queryFunction = async ({
|
|
|
|
queryKey,
|
|
|
|
pageParam,
|
|
|
|
meta
|
|
|
|
}: 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',
|
|
|
|
url: `${page.reference}/${page.status.id}/${page.type}`,
|
|
|
|
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',
|
|
|
|
url: `${page.reference}/${page.account.id}/${page.type}`,
|
|
|
|
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 {
|
|
|
|
const domain = page.account.url.match(
|
|
|
|
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/i
|
|
|
|
)?.[1]
|
|
|
|
if (!domain?.length) {
|
|
|
|
throw new Error()
|
2022-12-15 01:41:34 +01:00
|
|
|
}
|
2022-12-15 14:28:36 +01:00
|
|
|
|
2022-12-18 01:29:33 +01:00
|
|
|
const resLookup = await apiGeneral<Mastodon.Account>({
|
2022-12-15 14:28:36 +01:00
|
|
|
method: 'get',
|
|
|
|
domain,
|
2022-12-18 01:29:33 +01:00
|
|
|
url: 'api/v1/accounts/lookup',
|
|
|
|
params: { acct: page.account.acct }
|
2022-12-15 01:41:34 +01:00
|
|
|
})
|
2022-12-18 01:29:33 +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,
|
2022-12-18 01:29:33 +01:00
|
|
|
url: `api/v1/${page.reference}/${resLookup.body.id}/${page.type}`,
|
2022-12-15 01:41:34 +01:00
|
|
|
params
|
2022-12-15 14:28:36 +01:00
|
|
|
})
|
|
|
|
return { ...res, remoteData: true }
|
|
|
|
} 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] & {
|
2021-02-11 01:33:31 +01:00
|
|
|
options?: UseInfiniteQueryOptions<
|
2022-12-15 14:28:36 +01:00
|
|
|
PagedResponse<Mastodon.Account[]> & { warnIncomplete: boolean; remoteData: boolean },
|
2021-12-18 19:59:38 +01:00
|
|
|
AxiosError
|
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 }]
|
2021-01-07 19:13:09 +01:00
|
|
|
return useInfiniteQuery(queryKey, queryFunction, options)
|
|
|
|
}
|
|
|
|
|
2021-03-15 00:18:44 +01:00
|
|
|
export { useUsersQuery }
|