diff --git a/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx b/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx index 654fb4cd7557..43c5fd17a784 100644 --- a/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx +++ b/src/plugins/discover/public/application/components/default_discover_table/default_discover_table.tsx @@ -5,7 +5,7 @@ import './_doc_table.scss'; -import React from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { EuiDataGridColumn, EuiDataGridSorting } from '@elastic/eui'; import { TableHeader } from './table_header'; import { DocViewFilterFn, OpenSearchSearchHit } from '../../doc_views/doc_views_types'; @@ -42,9 +42,38 @@ export const LegacyDiscoverTable = ({ onFilter, onClose, }: DefaultDiscoverTableProps) => { + const [intersectingRows, setIntersectingRows] = useState([]); + const tableRef = useRef(null); + + useEffect(() => { + const options = { + root: null, // viewport + rootMargin: '0px', + threshold: 0.5, // 50% of the element is visible + }; + + const observer = new IntersectionObserver((entries) => { + const visibleRows = entries + .filter((entry) => entry.isIntersecting) + .map((entry) => Number(entry.target.dataset.index)); + + setIntersectingRows((prevIntersectingRows) => [...prevIntersectingRows, ...visibleRows]); + }, options); + + const tableRows = tableRef.current.querySelectorAll('tbody tr'); + tableRows.forEach((row, index) => { + observer.observe(row); + row.dataset.index = index; // Storing the index for reference + }); + + return () => { + observer.disconnect(); + }; + }, [rows, columns]); // Re-run if the data changes + return ( indexPattern && ( -
|