Skip to content

Commit

Permalink
refactor: use react useMemo
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <[email protected]>
  • Loading branch information
ruanyl committed Dec 23, 2022
1 parent 97987be commit 670bac5
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions maps_dashboards/public/components/tooltip/tooltipTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
EuiSpacer,
EuiText,
} from '@elastic/eui';
import React, { useState, Fragment, useCallback, useEffect } from 'react';
import React, { useState, Fragment, useCallback, useEffect, useMemo } from 'react';
import { DocumentLayerSpecification } from '../../model/mapLayerType';

export type RowData = {
Expand Down Expand Up @@ -54,7 +54,7 @@ const TooltipTable = ({
}: Props) => {
const [selectedLayers, setSelectedLayers] = useState<EuiComboBoxOptionOption<number>[]>([
{
label: tables[0].layer.name,
label: tables[0]?.layer.name ?? '',
value: 0,
key: '0',
},
Expand All @@ -76,6 +76,7 @@ const TooltipTable = ({
];

useEffect(() => {
// When selected layer changed, reset the active page to the first page
setActivePage(0);
}, [selectedLayers]);

Expand Down Expand Up @@ -108,18 +109,21 @@ const TooltipTable = ({
[tables]
);

const onSelectPage = useCallback((pageIndex) => {
setActivePage(pageIndex);
}, []);

const options = [{ label: 'All layers', value: ALL_LAYERS, key: '-1' }];
tables.forEach(({ layer }, i) => {
options.push({ label: layer.name, value: i, key: `${i}` });
});

const tableItems = mergeTables(
tables,
selectedLayers.map((l) => l.value ?? 0)
const options = useMemo(() => {
const layerOptions = [{ label: 'All layers', value: ALL_LAYERS, key: '-1' }];
tables.forEach(({ layer }, i) => {
layerOptions.push({ label: layer.name, value: i, key: `${i}` });
});
return layerOptions;
}, [tables]);

const tableItems = useMemo(
() =>
mergeTables(
tables,
selectedLayers.map((l) => l.value ?? 0)
),
[tables, selectedLayers]
);
const pageItems = tableItems[activePage];

Expand Down Expand Up @@ -165,7 +169,7 @@ const TooltipTable = ({
aria-label="Compressed pagination"
pageCount={tableItems.length}
activePage={activePage}
onPageClick={onSelectPage}
onPageClick={setActivePage}
compressed
/>
) : (
Expand Down

0 comments on commit 670bac5

Please sign in to comment.