RocketChat/Rocket.Chat

View on GitHub
apps/meteor/client/components/GenericTable/hooks/usePagination.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { useEffect, useMemo } from 'react';

import { useCurrent } from './useCurrent';
import { useItemsPerPage } from './useItemsPerPage';
import { useItemsPerPageLabel } from './useItemsPerPageLabel';
import { useShowingResultsLabel } from './useShowingResultsLabel';

export const usePagination = (): {
    current: ReturnType<typeof useCurrent>[0];
    setCurrent: ReturnType<typeof useCurrent>[1];
    itemsPerPage: ReturnType<typeof useItemsPerPage>[0];
    setItemsPerPage: ReturnType<typeof useItemsPerPage>[1];
    itemsPerPageLabel: ReturnType<typeof useItemsPerPageLabel>;
    showingResultsLabel: ReturnType<typeof useShowingResultsLabel>;
} => {
    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],
    );
};