import _ from 'lodash'; import { useRef, useLayoutEffect, useEffect } from 'react'; export function useGoToHighlightedRow( isServerSidePagination: boolean, pageSize: number, rows: Array, goToPage: (page: number) => void, highlightedItemId?: string ) { const handlePageChangeRef = useRef(goToPage); useLayoutEffect(() => { handlePageChangeRef.current = goToPage; }); const highlightedItemIdRef = useRef(); useEffect(() => { if ( !isServerSidePagination && highlightedItemId && highlightedItemId !== highlightedItemIdRef.current ) { const page = getRowPage(highlightedItemId, pageSize, rows); if (page) { handlePageChangeRef.current(page); } highlightedItemIdRef.current = highlightedItemId; } }, [highlightedItemId, isServerSidePagination, rows, pageSize]); } function getRowPage( rowID: string, pageSize: number, rows: Array ) { const totalRows = rows.length; if (!rowID || pageSize > totalRows) { return 0; } const paginatedData = _.chunk(rows, pageSize); const itemPage = paginatedData.findIndex((sub) => sub.some((row) => row.id === rowID) ); return itemPage; }