|
1 | | -import { useQuery } from '@tanstack/react-query' |
| 1 | +import { useInfiniteQuery } from '@tanstack/react-query' |
2 | 2 | import { axios } from 'src/lib/axios' |
3 | | -import { ExtractFnReturnType, QueryConfig } from 'src/lib/react-query' |
4 | | -import { Article } from 'src/types' |
| 3 | +import { InfiniteQueryConfig } from 'src/lib/react-query' |
| 4 | +import { FeedItem } from 'src/types' |
5 | 5 |
|
6 | | -const getAIArticles = async (userTopics: string[]): Promise<Article[]> => { |
7 | | - return axios.get('/engine/feed/get', { |
| 6 | +type Response = { |
| 7 | + data: FeedItem[] |
| 8 | + metadata: { |
| 9 | + next: string | null |
| 10 | + hasNextPage: boolean |
| 11 | + } |
| 12 | +} |
| 13 | +const getAIArticles = async ({ |
| 14 | + tags, |
| 15 | + next, |
| 16 | +}: { |
| 17 | + tags: string[] |
| 18 | + next?: string | null |
| 19 | +}): Promise<Response> => { |
| 20 | + return axios.get('/TO_ADD', { |
| 21 | + auth: { |
| 22 | + username: 'hidden', |
| 23 | + password: 'hidden', |
| 24 | + }, |
8 | 25 | params: { |
9 | | - tags: userTopics.join(','), |
10 | | - limit: 50, |
| 26 | + tags: [...tags].sort().join(','), |
| 27 | + limit: 21, |
| 28 | + next, |
11 | 29 | }, |
12 | 30 | }) |
13 | 31 | } |
14 | 32 |
|
15 | 33 | type QueryFnType = typeof getAIArticles |
16 | 34 |
|
17 | 35 | type UseGetArticlesOptions = { |
18 | | - userTopics: string[] |
19 | | - config?: QueryConfig<QueryFnType> |
| 36 | + tags: string[] |
| 37 | + config?: InfiniteQueryConfig<QueryFnType> |
20 | 38 | } |
21 | 39 |
|
22 | | -export const useGetAIArticles = ({ userTopics, config }: UseGetArticlesOptions) => { |
23 | | - return useQuery<ExtractFnReturnType<QueryFnType>>({ |
| 40 | +export const useGetAIArticles = ({ tags, config }: UseGetArticlesOptions) => { |
| 41 | + return useInfiniteQuery<Response>({ |
24 | 42 | ...config, |
25 | | - queryKey: ['ai', userTopics.join(',')], |
26 | | - queryFn: () => getAIArticles(userTopics), |
| 43 | + queryKey: ['feed', 'v2', tags.join(',')], |
| 44 | + queryFn: ({ pageParam }) => getAIArticles({ tags, next: pageParam }), |
| 45 | + getNextPageParam: (lastPage) => { |
| 46 | + return lastPage.metadata.hasNextPage ? JSON.stringify(lastPage.metadata.next) : undefined |
| 47 | + }, |
27 | 48 | }) |
28 | 49 | } |
0 commit comments