Skip to content

Commit d2fda3a

Browse files
authored
feat: memoize SimpleTableLayer and clean up its hooks (#808)
1 parent 4b61d6b commit d2fda3a

File tree

5 files changed

+82
-63
lines changed

5 files changed

+82
-63
lines changed

giraffe/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@influxdata/giraffe",
3-
"version": "2.34.3",
3+
"version": "2.34.4",
44
"main": "dist/index.js",
55
"module": "dist/index.js",
66
"license": "MIT",

giraffe/src/components/SimpleTable/PagedTable.tsx

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
} from 'react'
99
import {DapperScrollbars} from '../DapperScrollbars'
1010
import {FluxDataType} from '../../index'
11-
import {SubsetTable, SimpleTableViewProperties} from './SimpleTableGraph'
11+
import {SubsetTable} from './SimpleTableGraph'
1212
import {FluxResult, Column} from './flows'
1313
import {PaginationContext} from './pagination'
1414
import 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-
3536
const 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

232243
interface Props {
233-
properties: SimpleTableViewProperties
244+
showAll: boolean
234245
result: FluxResult['parsed']
235246
}
236247

237248
const INITIAL_HEADER_HEIGHT = 0
238249
const 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

giraffe/src/components/SimpleTable/SimpleTableGraph.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import {PaginationProvider} from './pagination'
88

99
import styles from './SimpleTableGraph.scss'
1010

11-
export interface SimpleTableViewProperties {
12-
type: 'simple-table'
13-
showAll: boolean
14-
}
15-
1611
interface SubsetTableColumn {
1712
name: string
1813
type: string
@@ -31,15 +26,15 @@ export interface SubsetTable {
3126
}
3227

3328
interface Props {
34-
properties: SimpleTableViewProperties
3529
result: FluxResult['parsed']
30+
showAll: boolean
3631
}
3732

38-
export const SimpleTable: FC<Props> = ({properties, result}) => {
33+
export const SimpleTable: FC<Props> = ({result, showAll}) => {
3934
return (
4035
<div className={`${styles['visualization--simple-table']}`}>
4136
<PaginationProvider totalNumberOfRows={result?.table?.length || 0}>
42-
<PagedTable properties={properties} result={result} />
37+
<PagedTable showAll={showAll} result={result} />
4338
<PageControl />
4439
</PaginationProvider>
4540
</div>

giraffe/src/components/SimpleTable/SimpleTableLayer.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Libraries
2-
import React, {FunctionComponent} from 'react'
2+
import React, {FunctionComponent, useMemo} from 'react'
33

44
// Components
5-
import {SimpleTable, SimpleTableViewProperties} from './SimpleTableGraph'
5+
import {SimpleTable} from './SimpleTableGraph'
66

77
// Utils
88
import {fromFlux, FromFluxResult} from '../../utils/fromFlux'
@@ -21,12 +21,15 @@ export const SimpleTableLayer: FunctionComponent<Props> = ({
2121
fluxResponse,
2222
fromFluxResult,
2323
}: Props) => {
24-
const {showAll} = config
25-
const properties: SimpleTableViewProperties = {
26-
type: 'simple-table',
27-
showAll,
28-
}
29-
const result = fromFluxResult ? fromFluxResult : fromFlux(fluxResponse)
24+
const {showAll = false} = config
25+
26+
const result = useMemo(
27+
() => (fromFluxResult ? fromFluxResult : fromFlux(fluxResponse)),
28+
[fluxResponse, fromFluxResult]
29+
)
3030

31-
return <SimpleTable result={result} properties={properties} />
31+
return useMemo(() => <SimpleTable result={result} showAll={showAll} />, [
32+
result,
33+
showAll,
34+
])
3235
}

stories/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@influxdata/giraffe-stories",
3-
"version": "2.34.3",
3+
"version": "2.34.4",
44
"license": "MIT",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)