@@ -8,7 +8,7 @@ import React, {
88} from 'react'
99import { DapperScrollbars } from '../DapperScrollbars'
1010import { FluxDataType } from '../../index'
11- import { SubsetTable , SimpleTableViewProperties } from './SimpleTableGraph'
11+ import { SubsetTable } from './SimpleTableGraph'
1212import { FluxResult , Column } from './flows'
1313import { PaginationContext } from './pagination'
1414import InnerTable from './InnerTable'
@@ -23,6 +23,8 @@ interface ExtendedColumn {
2323 data : any [ ]
2424}
2525
26+ const MAXIMUM_ESTIMATED_ROW_HEIGHT = 42
27+
2628/*
2729 * @param result - the result of the query
2830 * @param paginationOffset - the start index of the first row of the current page
@@ -31,7 +33,6 @@ interface ExtendedColumn {
3133 * @param rowHeight - the height of each row
3234 * @returns the number of rows that are on the given page (defined by the paginationOffset)
3335 * */
34-
3536const getNumberOfRowsOnCurrentPage = (
3637 result : FluxResult [ 'parsed' ] ,
3738 paginationOffset : number ,
@@ -43,13 +44,20 @@ const getNumberOfRowsOnCurrentPage = (
4344 return 0
4445 }
4546
47+ const minimumLength = result ?. table ?. length ?? 1
48+ const estimatedRowHeight = Math . min (
49+ Math . ceil ( totalAvailableHeight / minimumLength ) ,
50+ MAXIMUM_ESTIMATED_ROW_HEIGHT
51+ )
52+ const visibleRowHeight = Math . max ( rowHeight , estimatedRowHeight )
53+
4654 let runningHeight = 14
4755 let rowIdx = paginationOffset
4856 let currentTable
4957 let lastSignature
5058 let signature
5159
52- const lastVisibleRowMinimumHeight = 0.2 * rowHeight
60+ const lastVisibleRowMinimumHeight = 0.2 * visibleRowHeight
5361
5462 while ( rowIdx < result . table . length ) {
5563 if ( result . table . columns ?. table ?. data ?. [ rowIdx ] !== currentTable ) {
@@ -69,7 +77,10 @@ const getNumberOfRowsOnCurrentPage = (
6977 runningHeight += 10
7078 }
7179
72- if ( runningHeight + 0.25 * rowHeight >= totalAvailableHeight ) {
80+ if (
81+ runningHeight + lastVisibleRowMinimumHeight >=
82+ totalAvailableHeight
83+ ) {
7384 break
7485 }
7586
@@ -81,7 +92,7 @@ const getNumberOfRowsOnCurrentPage = (
8192 continue
8293 }
8394
84- runningHeight += rowHeight
95+ runningHeight += visibleRowHeight
8596
8697 if ( runningHeight + lastVisibleRowMinimumHeight >= totalAvailableHeight ) {
8798 break
@@ -112,13 +123,13 @@ const subsetResult = (
112123 } )
113124 )
114125 . filter ( column => ! ! column . data . filter ( _c => _c !== undefined ) . length )
115- . reduce ( ( arr , curr ) => {
116- if ( arr [ curr . name ] ) {
117- arr [ curr . name ] . push ( curr )
118- return arr
126+ . reduce ( ( acc , curr ) => {
127+ if ( acc [ curr . name ] ) {
128+ acc [ curr . name ] . push ( curr )
129+ return acc
119130 }
120- arr [ curr . name ] = [ curr ]
121- return arr
131+ acc [ curr . name ] = [ curr ]
132+ return acc
122133 } , { } )
123134
124135 const tables : SubsetTable [ ] = [ ]
@@ -230,18 +241,19 @@ const subsetResult = (
230241}
231242
232243interface Props {
233- properties : SimpleTableViewProperties
244+ showAll : boolean
234245 result : FluxResult [ 'parsed' ]
235246}
236247
237248const INITIAL_HEADER_HEIGHT = 0
238249const INITIAL_HEIGHT = 0
239- const INITIAL_ROW_HEIGHT = 10 // must be greater than 0
240- const PagedTable : FC < Props > = ( { result, properties } ) => {
250+ const INITIAL_ROW_HEIGHT = 0
251+ const PagedTable : FC < Props > = ( { result, showAll } ) => {
241252 const {
242253 paginationOffset,
243254 setNumberOfRowsOnCurrentPage,
244255 maxNumberOfRowsOnAnyPage,
256+ numberOfRowsOnCurrentPage,
245257 setMaxNumberOfRowsOnAnyPage,
246258 setCurrentPage,
247259 setTotalPages,
@@ -264,11 +276,10 @@ const PagedTable: FC<Props> = ({result, properties}) => {
264276 // eslint-disable-next-line react-hooks/exhaustive-deps
265277 useEffect ( ( ) => {
266278 if (
267- tableHeaderHeight === INITIAL_HEADER_HEIGHT &&
268- pagedTableHeaderRef ?. current
279+ pagedTableHeaderRef ?. current ?. clientHeight > 0 &&
280+ tableHeaderHeight === INITIAL_HEADER_HEIGHT
269281 ) {
270- const calculatedHeaderHeight =
271- pagedTableHeaderRef . current . clientHeight ?? 0
282+ const calculatedHeaderHeight = pagedTableHeaderRef . current . clientHeight
272283
273284 if ( calculatedHeaderHeight !== tableHeaderHeight ) {
274285 setTableHeaderHeight ( calculatedHeaderHeight )
@@ -278,9 +289,12 @@ const PagedTable: FC<Props> = ({result, properties}) => {
278289
279290 // eslint-disable-next-line react-hooks/exhaustive-deps
280291 useEffect ( ( ) => {
281- if ( tableRowHeight === INITIAL_ROW_HEIGHT && pagedTableBodyRef ?. current ) {
292+ if (
293+ pagedTableBodyRef ?. current ?. children ?. [ 0 ] ?. clientHeight > 0 &&
294+ tableRowHeight === INITIAL_ROW_HEIGHT
295+ ) {
282296 const calculatedRowHeight =
283- pagedTableBodyRef . current . children ?. [ 0 ] ? .clientHeight ?? 0
297+ pagedTableBodyRef . current . children [ 0 ] . clientHeight
284298
285299 if ( calculatedRowHeight !== tableRowHeight ) {
286300 setTableRowHeight ( calculatedRowHeight )
@@ -331,7 +345,7 @@ const PagedTable: FC<Props> = ({result, properties}) => {
331345 }
332346 } , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps
333347
334- const numberOfRowsOnCurrentPage = useMemo ( ( ) => {
348+ const updatedNumberOfRowsOnCurrentPage = useMemo ( ( ) => {
335349 return getNumberOfRowsOnCurrentPage (
336350 result ,
337351 paginationOffset ,
@@ -340,26 +354,19 @@ const PagedTable: FC<Props> = ({result, properties}) => {
340354 tableRowHeight
341355 )
342356 } , [
343- result ,
344- paginationOffset ,
345357 availableHeightForTable ,
358+ paginationOffset ,
359+ result ,
346360 tableHeaderHeight ,
347361 tableRowHeight ,
348362 ] )
349363
350- const tables = useMemo ( ( ) => {
351- return subsetResult (
352- result ,
353- paginationOffset ,
354- numberOfRowsOnCurrentPage ,
355- properties . showAll
356- )
357- } , [ result , paginationOffset , numberOfRowsOnCurrentPage ] ) // eslint-disable-line react-hooks/exhaustive-deps
358-
359364 // Pagination stuff
360365 useEffect ( ( ) => {
361- setNumberOfRowsOnCurrentPage ( numberOfRowsOnCurrentPage )
362- } , [ numberOfRowsOnCurrentPage ] ) // eslint-disable-line react-hooks/exhaustive-deps
366+ if ( updatedNumberOfRowsOnCurrentPage !== numberOfRowsOnCurrentPage ) {
367+ setNumberOfRowsOnCurrentPage ( updatedNumberOfRowsOnCurrentPage )
368+ }
369+ } , [ updatedNumberOfRowsOnCurrentPage ] ) // eslint-disable-line react-hooks/exhaustive-deps
363370
364371 useEffect ( ( ) => {
365372 // The max number of rows that will ever be on a page will be the number of rows on the first page
@@ -373,7 +380,7 @@ const PagedTable: FC<Props> = ({result, properties}) => {
373380 tableRowHeight
374381 )
375382 setMaxNumberOfRowsOnAnyPage ( maxNumberOfRowsOnPage )
376- } , [ result , availableHeightForTable , tableHeaderHeight , tableRowHeight ] ) // eslint-disable-line react-hooks/exhaustive-deps
383+ } , [ availableHeightForTable , result , tableHeaderHeight , tableRowHeight ] ) // eslint-disable-line react-hooks/exhaustive-deps
377384
378385 useEffect ( ( ) => {
379386 setCurrentPage ( 1 )
@@ -387,15 +394,29 @@ const PagedTable: FC<Props> = ({result, properties}) => {
387394 }
388395 } , [ maxNumberOfRowsOnAnyPage , result ] ) // eslint-disable-line react-hooks/exhaustive-deps
389396
390- const inner =
391- ! ! numberOfRowsOnCurrentPage &&
392- tables . map ( ( table , index ) => (
393- < InnerTable
394- table = { table }
395- key = { `table${ index } ` }
396- pagedTableRefs = { { pagedTableHeaderRef, pagedTableBodyRef} }
397- />
398- ) )
397+ const tables = useMemo (
398+ ( ) =>
399+ subsetResult (
400+ result ,
401+ paginationOffset ,
402+ numberOfRowsOnCurrentPage ,
403+ showAll
404+ ) ,
405+ [ numberOfRowsOnCurrentPage , paginationOffset , result ] // eslint-disable-line react-hooks/exhaustive-deps
406+ )
407+
408+ const inner = useMemo ( ( ) => {
409+ if ( numberOfRowsOnCurrentPage > 0 ) {
410+ return tables . map ( ( table , index ) => (
411+ < InnerTable
412+ table = { table }
413+ key = { `table${ index } ` }
414+ pagedTableRefs = { { pagedTableHeaderRef, pagedTableBodyRef} }
415+ />
416+ ) )
417+ }
418+ return null
419+ } , [ numberOfRowsOnCurrentPage , tables ] )
399420
400421 return (
401422 < div
0 commit comments