2022-08-09 08:00:22 -07:00
|
|
|
import { useInfiniteQuery } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
import { fetchRelationships } from 'soapbox/actions/accounts';
|
|
|
|
import { importFetchedAccounts } from 'soapbox/actions/importer';
|
|
|
|
import { getLinks } from 'soapbox/api';
|
2022-08-10 05:46:00 -07:00
|
|
|
import { useApi, useAppDispatch } from 'soapbox/hooks';
|
2022-08-09 08:00:22 -07:00
|
|
|
|
|
|
|
type Account = {
|
|
|
|
acct: string
|
|
|
|
avatar: string
|
|
|
|
avatar_static: string
|
|
|
|
bot: boolean
|
|
|
|
created_at: string
|
|
|
|
discoverable: boolean
|
|
|
|
display_name: string
|
|
|
|
followers_count: number
|
|
|
|
following_count: number
|
|
|
|
group: boolean
|
|
|
|
header: string
|
|
|
|
header_static: string
|
|
|
|
id: string
|
|
|
|
last_status_at: string
|
|
|
|
location: string
|
|
|
|
locked: boolean
|
|
|
|
note: string
|
|
|
|
statuses_count: number
|
|
|
|
url: string
|
|
|
|
username: string
|
|
|
|
verified: boolean
|
|
|
|
website: string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Suggestion = {
|
|
|
|
source: 'staff'
|
|
|
|
account: Account
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:46:00 -07:00
|
|
|
|
|
|
|
export default function useOnboardingSuggestions() {
|
|
|
|
const api = useApi();
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
|
|
const getV2Suggestions = async(pageParam: any): Promise<{ data: Suggestion[], link: string | undefined, hasMore: boolean }> => {
|
2022-08-09 08:00:22 -07:00
|
|
|
const link = pageParam?.link || '/api/v2/suggestions';
|
2022-08-10 05:46:00 -07:00
|
|
|
const response = await api.get<Suggestion[]>(link);
|
2022-08-09 08:00:22 -07:00
|
|
|
const hasMore = !!response.headers.link;
|
|
|
|
const nextLink = getLinks(response).refs.find(link => link.rel === 'next')?.uri;
|
|
|
|
|
|
|
|
const accounts = response.data.map(({ account }) => account);
|
|
|
|
const accountIds = accounts.map((account) => account.id);
|
|
|
|
dispatch(importFetchedAccounts(accounts));
|
|
|
|
dispatch(fetchRelationships(accountIds));
|
|
|
|
|
|
|
|
return {
|
|
|
|
data: response.data,
|
|
|
|
link: nextLink,
|
|
|
|
hasMore,
|
|
|
|
};
|
2022-08-10 05:46:00 -07:00
|
|
|
};
|
2022-08-09 08:00:22 -07:00
|
|
|
|
2022-08-10 05:46:00 -07:00
|
|
|
const result = useInfiniteQuery(['suggestions', 'v2'], ({ pageParam }) => getV2Suggestions(pageParam), {
|
2022-08-09 08:00:22 -07:00
|
|
|
keepPreviousData: true,
|
|
|
|
getNextPageParam: (config) => {
|
|
|
|
if (config.hasMore) {
|
|
|
|
return { link: config.link };
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = result.data?.pages.reduce<Suggestion[]>(
|
|
|
|
(prev: Suggestion[], curr) => [...prev, ...curr.data],
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
...result,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
}
|