import useSWRInfinite from 'swr/infinite'; import { smartPopupApi } from 'modules/smart-activation'; export const useInfiniteSmartPopup = (params: { clientAppId: string }) => { const { data, size, setSize, mutate, error } = useSWRInfinite( (index) => ['/smart-activation/popups', index + 1, JSON.stringify(params)], async (_, index) => { return smartPopupApi.getSmartPopups({ page: index, pageSize: 10, clientAppId: params?.clientAppId, }); }, { revalidateOnFocus: false, revalidateFirstPage: false, }, ); const popups = data?.reduce((acc, cur) => [...acc, ...cur.data.popups], []) || []; const hasMore = size < data?.[0].data?.pagination?.totalPages; return { popups, page: size, setPage: setSize, loading: !data && !error, hasMore, mutatePopups: mutate, }; };