revert: chart select

This commit is contained in:
hamster1963 2024-12-02 14:37:30 +08:00
parent 30bf0e93f1
commit e780521b0b

View File

@ -111,27 +111,14 @@ export const NetworkChartClient = React.memo(function NetworkChart({
const defaultChart = "All"; const defaultChart = "All";
const [activeCharts, setActiveCharts] = React.useState<string[]>([ const [activeChart, setActiveChart] = React.useState(defaultChart);
defaultChart,
]);
const handleButtonClick = useCallback((chart: string) => { const handleButtonClick = useCallback(
setActiveCharts((prev) => { (chart: string) => {
if (chart === defaultChart) { setActiveChart((prev) => (prev === chart ? defaultChart : chart));
return [defaultChart]; },
} [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 getColorByIndex = useCallback( const getColorByIndex = useCallback(
(chart: string) => { (chart: string) => {
@ -146,7 +133,7 @@ export const NetworkChartClient = React.memo(function NetworkChart({
chartDataKey.map((key) => ( chartDataKey.map((key) => (
<button <button
key={key} key={key}
data-active={activeCharts.includes(key)} data-active={activeChart === key}
className={`relative z-30 flex cursor-pointer flex-1 flex-col justify-center gap-1 border-b border-neutral-200 dark:border-neutral-800 px-6 py-4 text-left data-[active=true]:bg-muted/50 sm:border-l sm:border-t-0 sm:px-6`} className={`relative z-30 flex cursor-pointer flex-1 flex-col justify-center gap-1 border-b border-neutral-200 dark:border-neutral-800 px-6 py-4 text-left data-[active=true]:bg-muted/50 sm:border-l sm:border-t-0 sm:px-6`}
onClick={() => handleButtonClick(key)} onClick={() => handleButtonClick(key)}
> >
@ -158,38 +145,35 @@ export const NetworkChartClient = React.memo(function NetworkChart({
</span> </span>
</button> </button>
)), )),
[chartDataKey, activeCharts, chartData, handleButtonClick], [chartDataKey, activeChart, chartData, handleButtonClick],
); );
const chartLines = useMemo(() => { const chartLines = useMemo(() => {
if (activeCharts.includes(defaultChart)) { if (activeChart !== defaultChart) {
return chartDataKey.map((key) => ( return (
<Line <Line
key={key}
isAnimationActive={false} isAnimationActive={false}
strokeWidth={1} strokeWidth={1}
type="linear" type="linear"
dot={false} dot={false}
dataKey={key} dataKey="avg_delay"
stroke={getColorByIndex(key)} stroke={getColorByIndex(activeChart)}
connectNulls={true}
/> />
)); );
} }
return chartDataKey.map((key) => (
return activeCharts.map((chart) => (
<Line <Line
key={chart} key={key}
isAnimationActive={false} isAnimationActive={false}
strokeWidth={1} strokeWidth={1}
type="linear" type="linear"
dot={false} dot={false}
dataKey={chart} dataKey={key}
stroke={getColorByIndex(chart)} stroke={getColorByIndex(key)}
connectNulls={true} connectNulls={true}
/> />
)); ));
}, [activeCharts, chartDataKey, getColorByIndex]); }, [activeChart, defaultChart, chartDataKey, getColorByIndex]);
return ( return (
<Card> <Card>
@ -212,9 +196,9 @@ export const NetworkChartClient = React.memo(function NetworkChart({
<LineChart <LineChart
accessibilityLayer accessibilityLayer
data={ data={
activeCharts.includes(defaultChart) activeChart === defaultChart
? formattedData ? formattedData
: chartData[activeCharts[0]] : chartData[activeChart]
} }
margin={{ left: 12, right: 12 }} margin={{ left: 12, right: 12 }}
> >
@ -247,7 +231,7 @@ export const NetworkChartClient = React.memo(function NetworkChart({
/> />
} }
/> />
{activeCharts.includes(defaultChart) && ( {activeChart === defaultChart && (
<ChartLegend content={<ChartLegendContent />} /> <ChartLegend content={<ChartLegendContent />} />
)} )}
{chartLines} {chartLines}