Skip to content

Commit

Permalink
Fix desired row updates in row loading for infinite scroll
Browse files Browse the repository at this point in the history
Signed-off-by: Simeon Widdis <sawiddis@amazon.com>
  • Loading branch information
Swiddis committed Sep 6, 2024
1 parent 80e85c8 commit ac69252
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface DefaultDiscoverTableProps {
// ToDo: These would need to be read from an upcoming config panel
const PAGINATED_PAGE_SIZE = 50;
const INFINITE_SCROLLED_PAGE_SIZE = 10;
// How far to queue unrendered rows ahead of time during infinite scrolling
const DESIRED_ROWS_LOOKAHEAD = 5 * INFINITE_SCROLLED_PAGE_SIZE;

const DefaultDiscoverTableUI = ({
columns,
Expand Down Expand Up @@ -86,7 +88,7 @@ const DefaultDiscoverTableUI = ({
*/
const [renderedRowCount, setRenderedRowCount] = useState(INFINITE_SCROLLED_PAGE_SIZE);
const [desiredRowCount, setDesiredRowCount] = useState(
Math.min(rows.length, 5 * INFINITE_SCROLLED_PAGE_SIZE)
Math.min(rows.length, DESIRED_ROWS_LOOKAHEAD)
);
const [displayedRows, setDisplayedRows] = useState(rows.slice(0, PAGINATED_PAGE_SIZE));
const [currentRowCounts, setCurrentRowCounts] = useState({
Expand Down Expand Up @@ -118,7 +120,7 @@ const DefaultDiscoverTableUI = ({
if (entries[0].isIntersecting) {
// Load another batch of rows, some immediately and some lazily
setRenderedRowCount((prevRowCount) => prevRowCount + INFINITE_SCROLLED_PAGE_SIZE);
setDesiredRowCount((prevRowCount) => prevRowCount + 5 * INFINITE_SCROLLED_PAGE_SIZE);
setDesiredRowCount((prevRowCount) => prevRowCount + DESIRED_ROWS_LOOKAHEAD);

Check warning on line 123 in src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx#L123

Added line #L123 was not covered by tests
}
},
{ threshold: 1.0 }
Expand Down Expand Up @@ -170,12 +172,17 @@ const DefaultDiscoverTableUI = ({
lazyLoadLastTimeRef.current = time;
lazyLoadRequestFrameRef.current = requestAnimationFrame(loadMoreRows);
}
// Ensure we have more desired rows in the queue to prevent stalling when we render the
// current desired row count
if (renderedRowCount + DESIRED_ROWS_LOOKAHEAD > desiredRowCount) {
setDesiredRowCount(Math.min(renderedRowCount + DESIRED_ROWS_LOOKAHEAD, rows.length));

Check warning on line 178 in src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx#L178

Added line #L178 was not covered by tests
}
};
lazyLoadRequestFrameRef.current = requestAnimationFrame(loadMoreRows);
}

return () => cancelAnimationFrame(lazyLoadRequestFrameRef.current);
}, [showPagination, renderedRowCount, desiredRowCount]);
}, [showPagination, renderedRowCount, desiredRowCount, rows.length]);

// Allow auto column-sizing using the initially rendered rows and then convert to fixed
const tableLayoutRequestFrameRef = useRef<number>(0);
Expand Down

0 comments on commit ac69252

Please sign in to comment.