Files
calendar/packages/features/data-table/hooks/useFetchMoreOnBottomReached.ts
T
7eee2728a2 feat: implement standard pagination for org member list (#19802)
* feat: implement standard pagination for org member list

* fix type error

* i18n for pagination

* add paginationMode

* apply the change to PlatformManagedUsersTable

* replace useInfiniteQuery with useQuery

* fix type error

* fix type error

* fix type error

* fix type error

* update comment

* remove optimistic update

* minor changes

* update usage on nuqs

---------

Co-authored-by: Benny Joo <sldisek783@gmail.com>
2025-03-11 09:55:41 +01:00

38 lines
1009 B
TypeScript

"use client";
import { useCallback, useEffect } from "react";
export const useFetchMoreOnBottomReached = ({
tableContainerRef,
hasNextPage,
fetchNextPage,
isFetching,
enabled = true,
}: {
tableContainerRef: React.RefObject<HTMLDivElement>;
hasNextPage: boolean;
fetchNextPage: () => void;
isFetching: boolean;
enabled?: boolean;
}) => {
const fetchMoreOnBottomReached = useCallback(
(containerRefElement?: HTMLDivElement | null) => {
if (!enabled) return;
if (containerRefElement) {
const { scrollHeight, scrollTop, clientHeight } = containerRefElement;
if (scrollHeight - scrollTop - clientHeight < 300 && !isFetching && hasNextPage) {
fetchNextPage();
}
}
},
[fetchNextPage, isFetching, hasNextPage, enabled]
);
useEffect(() => {
if (!enabled) return;
fetchMoreOnBottomReached(tableContainerRef.current);
}, [fetchMoreOnBottomReached, tableContainerRef, enabled]);
return fetchMoreOnBottomReached;
};