import { useEffect, useMemo } from 'react'; import { useCurrent } from './useCurrent'; import { useItemsPerPage } from './useItemsPerPage'; import { useItemsPerPageLabel } from './useItemsPerPageLabel'; import { useShowingResultsLabel } from './useShowingResultsLabel'; /** * TODO: Move `usePagination` outside from `GenericTable` folder */ export const usePagination = (): { current: ReturnType[0]; setCurrent: ReturnType[1]; itemsPerPage: ReturnType[0]; setItemsPerPage: ReturnType[1]; itemsPerPageLabel: ReturnType; showingResultsLabel: ReturnType; } => { const [itemsPerPage, setItemsPerPage] = useItemsPerPage(); const [current, setCurrent] = useCurrent(); const itemsPerPageLabel = useItemsPerPageLabel(); const showingResultsLabel = useShowingResultsLabel(); // Reset to first page when itemsPerPage changes useEffect(() => { setCurrent(0); }, [itemsPerPage, setCurrent]); return useMemo( () => ({ itemsPerPage, setItemsPerPage, current, setCurrent, itemsPerPageLabel, showingResultsLabel, }), [itemsPerPage, setItemsPerPage, current, setCurrent, itemsPerPageLabel, showingResultsLabel], ); };