Skip to content

Commit 7f63137

Browse files
committed
feat: add load more for user segment dropdown in targeting
1 parent c05e743 commit 7f63137

7 files changed

Lines changed: 694 additions & 472 deletions

File tree

ui/dashboard/src/@queries/user-segments.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import {
22
userSegmentsFetcher,
33
UserSegmentsFetcherParams
44
} from '@api/user-segment';
5-
import { QueryClient, useQuery, useQueryClient } from '@tanstack/react-query';
5+
import {
6+
QueryClient,
7+
useInfiniteQuery,
8+
useQuery,
9+
useQueryClient
10+
} from '@tanstack/react-query';
611
import type { UserSegmentCollection, QueryOptionsRespond } from '@types';
712

813
type QueryOptions = QueryOptionsRespond<UserSegmentCollection> & {
@@ -48,3 +53,25 @@ export const prefetchUserSegments = (
4853
...queryOptions
4954
});
5055
};
56+
57+
type InfiniteQueryOptions = {
58+
params?: Omit<UserSegmentsFetcherParams, 'cursor'>;
59+
enabled?: boolean;
60+
};
61+
62+
export const useInfiniteQueryUserSegments = ({
63+
params,
64+
enabled = true
65+
}: InfiniteQueryOptions = {}) =>
66+
useInfiniteQuery({
67+
queryKey: [USER_SEGMENTS_QUERY_KEY, 'infinite', params],
68+
queryFn: ({ pageParam = '0' }) =>
69+
userSegmentsFetcher({ ...params, cursor: pageParam as string }),
70+
initialPageParam: '0',
71+
getNextPageParam: (lastPage: UserSegmentCollection) => {
72+
const nextCursor = Number(lastPage.cursor);
73+
const total = Number(lastPage.totalCount);
74+
return nextCursor < total ? String(nextCursor) : undefined;
75+
},
76+
enabled
77+
});

0 commit comments

Comments
 (0)