Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix row rendering in infinite scroll #8060

Merged
merged 14 commits into from
Sep 10, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import './_doc_table.scss';

import React, { useEffect, useMemo, useRef, useState, useCallback } from 'react';
import React, { useEffect, useMemo, useRef, useState, useCallback, SetStateAction } from 'react';
import { EuiSmallButtonEmpty, EuiCallOut, EuiProgress } from '@elastic/eui';
import { FormattedMessage } from '@osd/i18n/react';
import { TableHeader } from './table_header';
Expand Down Expand Up @@ -41,6 +41,8 @@
// 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 [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 @@
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 @@ -155,6 +157,10 @@
const lazyLoadRequestFrameRef = useRef<number>(0);
const lazyLoadLastTimeRef = useRef<number>(0);

// When doing infinite scrolling, the `rows` prop gets regularly updated from the outside: we only
// render the additional rows when we know the load isn't too high. To prevent overloading the
// renderer, we throttle by current framerate and only render if the frames are fast enough, then
// we increase the rendered row count and trigger a re-render.
React.useEffect(() => {
if (!showPagination) {
const loadMoreRows = (time: number) => {
Expand All @@ -166,12 +172,17 @@
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]);
Swiddis marked this conversation as resolved.
Show resolved Hide resolved

// Allow auto column-sizing using the initially rendered rows and then convert to fixed
const tableLayoutRequestFrameRef = useRef<number>(0);
Expand Down Expand Up @@ -254,7 +265,7 @@
</table>
{!showPagination && renderedRowCount < rows.length && (
<div ref={sentinelRef}>
<EuiProgress size="xs" color="accent" />
<EuiProgress size="xs" color="accent" data-test-subj="discoverRenderedRowsProgress" />
Swiddis marked this conversation as resolved.
Show resolved Hide resolved
</div>
)}
{!showPagination && rows.length === sampleSize && (
Expand Down
Loading