refactor legend handler to hook

This commit is contained in:
tom
2022-10-26 17:33:40 +03:00
parent 4756cda903
commit 79b7d8a458
2 changed files with 18 additions and 7 deletions
+2 -7
View File
@@ -1,5 +1,4 @@
import { useToken, Button, Box } from '@chakra-ui/react';
import _range from 'lodash/range';
import React from 'react';
import type { TimeChartData } from 'ui/shared/chart/types';
@@ -15,6 +14,7 @@ import ChartOverlay from 'ui/shared/chart/ChartOverlay';
import ChartSelectionX from 'ui/shared/chart/ChartSelectionX';
import ChartTooltip from 'ui/shared/chart/ChartTooltip';
// import useBrushX from 'ui/shared/chart/useBrushX';
import useChartLegend from 'ui/shared/chart/useChartLegend';
import useChartSize from 'ui/shared/chart/useChartSize';
import useTimeChartController from 'ui/shared/chart/useTimeChartController';
@@ -40,7 +40,7 @@ const EthereumChart = () => {
},
];
const [ selectedLines, setSelectedLines ] = React.useState<Array<number>>(_range(data.length));
const { selectedLines, handleLegendItemClick } = useChartLegend(data.length);
const filteredData = data.filter((_, index) => selectedLines.includes(index));
const { yTickFormat, xScale, yScale } = useTimeChartController({
@@ -57,11 +57,6 @@ const EthereumChart = () => {
setRange([ 0, Infinity ]);
}, [ ]);
const handleLegendItemClick = React.useCallback((index: number) => {
const nextSelectedLines = selectedLines.includes(index) ? selectedLines.filter((item) => item !== index) : [ ...selectedLines, index ];
setSelectedLines(nextSelectedLines);
}, [ selectedLines ]);
// uncomment if we need brush the chart
// const brushLimits = React.useMemo(() => (
// [ [ 0, innerHeight ], [ innerWidth, height ] ] as [[number, number], [number, number]]
+16
View File
@@ -0,0 +1,16 @@
import _range from 'lodash/range';
import React from 'react';
export default function useChartLegend(dataLength: number) {
const [ selectedLines, setSelectedLines ] = React.useState<Array<number>>(_range(dataLength));
const handleLegendItemClick = React.useCallback((index: number) => {
const nextSelectedLines = selectedLines.includes(index) ? selectedLines.filter((item) => item !== index) : [ ...selectedLines, index ];
setSelectedLines(nextSelectedLines);
}, [ selectedLines ]);
return {
selectedLines,
handleLegendItemClick,
};
}