Files
iNaturalistReactNative/src/sharedHooks/useInfiniteUserScroll.ts
2025-12-22 18:26:38 -08:00

63 lines
1.3 KiB
TypeScript

import { flatten } from "lodash";
import { useAuthenticatedInfiniteQuery } from "sharedHooks";
const useInfiniteUserScroll = (
queryKey: string,
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
apiCall: Function,
ids: object[],
newInputParams: object,
options: {
enabled: boolean;
},
): object => {
const baseParams = {
...newInputParams,
// TODO: can change this once API pagination is working
per_page: 200,
};
const {
data,
isFetching,
fetchNextPage,
status,
} = useAuthenticatedInfiniteQuery(
[queryKey, baseParams],
async ( { pageParam }, optsWithAuth ) => {
const params = {
...baseParams,
};
if ( pageParam ) {
params.page = pageParam;
} else {
params.page = 1;
}
return apiCall( ids, params, optsWithAuth );
},
{
getNextPageParam: lastPage => lastPage.page + 1,
enabled: options.enabled,
},
);
const pages = data?.pages;
const allResults = pages?.map( page => page?.results );
const flattenedData = flatten( allResults );
return {
data: flattenedData,
isFetching,
fetchNextPage,
status,
totalResults: pages?.[0]
? pages?.[0].total_results
: null,
};
};
export default useInfiniteUserScroll;