tooot/src/utils/queryHooks/users.ts

102 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-12-18 19:59:38 +01:00
import apiInstance, { InstanceResponse } 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
} from '@tanstack/react-query'
2022-12-15 01:41:34 +01:00
import apiGeneral from '@api/general'
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 01:41:34 +01:00
const queryFunction = ({ queryKey, pageParam }: QueryFunctionContext<QueryKeyUsers>) => {
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
}).then(res => ({ ...res, warnIncomplete: false }))
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
}).then(res => ({ ...res, warnIncomplete: false }))
} else {
const domain = page.account.url.match(
/^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)/i
)?.[1]
if (!domain) {
return apiInstance<Mastodon.Account[]>({
method: 'get',
url: `${page.reference}/${page.account.id}/${page.type}`,
params
}).then(res => ({ ...res, warnIncomplete: true }))
}
return apiGeneral<{ accounts: Mastodon.Account[] }>({
method: 'get',
domain,
url: 'api/v2/search',
params: {
q: `@${page.account.acct}`,
type: 'accounts',
limit: '1'
}
})
.then(res => {
if (res?.body?.accounts?.length === 1) {
return apiGeneral<Mastodon.Account[]>({
method: 'get',
domain,
url: `api/v1/${page.reference}/${res.body.accounts[0].id}/${page.type}`,
params
})
.catch(() => {
return apiInstance<Mastodon.Account[]>({
method: 'get',
url: `${page.reference}/${page.account.id}/${page.type}`,
params
}).then(res => ({ ...res, warnIncomplete: true }))
})
.then(res => ({ ...res, warnIncomplete: false }))
} else {
return apiInstance<Mastodon.Account[]>({
method: 'get',
url: `${page.reference}/${page.account.id}/${page.type}`,
params
}).then(res => ({ ...res, warnIncomplete: true }))
}
})
.catch(() => {
return apiInstance<Mastodon.Account[]>({
method: 'get',
url: `${page.reference}/${page.account.id}/${page.type}`,
params
}).then(res => ({ ...res, warnIncomplete: true }))
})
}
}
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 01:41:34 +01:00
InstanceResponse<Mastodon.Account[]> & { warnIncomplete: 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 }