From 031f938428dbb08f438402a87463a350cae57122 Mon Sep 17 00:00:00 2001 From: abbyhu2000 Date: Wed, 31 Jan 2024 01:04:15 +0000 Subject: [PATCH] Table optimization with lazy loading Signed-off-by: abbyhu2000 --- .../default_discover_table.tsx | 37 +++++++++++++++++-- .../default_discover_table/table_header.tsx | 2 - .../default_discover_table/table_rows.tsx | 5 ++- 3 files changed, 36 insertions(+), 8 deletions(-) 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 && ( - +
- {rows.map((row: OpenSearchSearchHit) => { + {rows.map((row: OpenSearchSearchHit, index: number) => { return ( column.id)} columns={columns} diff --git a/src/plugins/discover/public/application/components/default_discover_table/table_header.tsx b/src/plugins/discover/public/application/components/default_discover_table/table_header.tsx index 60b765f9a9d3..4d714228f3bc 100644 --- a/src/plugins/discover/public/application/components/default_discover_table/table_header.tsx +++ b/src/plugins/discover/public/application/components/default_discover_table/table_header.tsx @@ -14,7 +14,6 @@ import './_table_header.scss'; import React from 'react'; import { EuiDataGridColumn, EuiDataGridSorting } from '@elastic/eui'; import { IndexPattern } from '../../../opensearch_dashboards_services'; -import { SortOrder, getDefaultSort } from '../../view_components/utils/get_default_sort'; import { TableHeaderColumn } from './table_header_column'; interface Props { @@ -65,7 +64,6 @@ export function TableHeader({ return ( +