diff --git a/src/components/NetworkChart.tsx b/src/components/NetworkChart.tsx index 0af6b21..a48420e 100644 --- a/src/components/NetworkChart.tsx +++ b/src/components/NetworkChart.tsx @@ -111,27 +111,14 @@ export const NetworkChartClient = React.memo(function NetworkChart({ const defaultChart = "All"; - const [activeCharts, setActiveCharts] = React.useState([ - defaultChart, - ]); + const [activeChart, setActiveChart] = React.useState(defaultChart); - const handleButtonClick = useCallback((chart: string) => { - setActiveCharts((prev) => { - if (chart === defaultChart) { - return [defaultChart]; - } - - const newCharts = prev.filter((c) => c !== defaultChart); - const chartIndex = newCharts.indexOf(chart); - - if (chartIndex === -1) { - return newCharts.length === 0 ? [chart] : [...newCharts, chart]; - } else { - const result = newCharts.filter((c) => c !== chart); - return result.length === 0 ? [defaultChart] : result; - } - }); - }, []); + const handleButtonClick = useCallback( + (chart: string) => { + setActiveChart((prev) => (prev === chart ? defaultChart : chart)); + }, + [defaultChart], + ); const getColorByIndex = useCallback( (chart: string) => { @@ -146,7 +133,7 @@ export const NetworkChartClient = React.memo(function NetworkChart({ chartDataKey.map((key) => ( )), - [chartDataKey, activeCharts, chartData, handleButtonClick], + [chartDataKey, activeChart, chartData, handleButtonClick], ); const chartLines = useMemo(() => { - if (activeCharts.includes(defaultChart)) { - return chartDataKey.map((key) => ( + if (activeChart !== defaultChart) { + return ( - )); + ); } - - return activeCharts.map((chart) => ( + return chartDataKey.map((key) => ( )); - }, [activeCharts, chartDataKey, getColorByIndex]); + }, [activeChart, defaultChart, chartDataKey, getColorByIndex]); return ( @@ -212,9 +196,9 @@ export const NetworkChartClient = React.memo(function NetworkChart({ @@ -247,7 +231,7 @@ export const NetworkChartClient = React.memo(function NetworkChart({ /> } /> - {activeCharts.includes(defaultChart) && ( + {activeChart === defaultChart && ( } /> )} {chartLines}