import type { ChartV2LegendProps } from './chartV2.types' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import { ChartV2TokensType } from './chartV2.tokens' import Block from '../Primitives/Block/Block' import PrimitiveButton from '../Primitives/PrimitiveButton/PrimitiveButton' import Text from '../Text/Text' import Seperator from '../common/Seperator' import { useChartLegend } from './useChartLegend' const ChartV2Legend = ({ chartRef, chartRefs, customLegendItems, renderItem, layout = 'horizontal', }: ChartV2LegendProps) => { const { legends } = useResponsiveTokens('CHARTSV2') const { chart, allItems, hoveredItem, setHoveredItem, handleClick } = useChartLegend(chartRef, chartRefs) if (!chart || !allItems.length) return null const isVertical = layout === 'vertical' return ( {allItems.map((item, i) => { const key = item.name ?? '' const custom = customLegendItems?.find((c) => c.key === key) const name = custom?.name ?? key const value = custom?.value const hasValue = value != null && value !== '' const color = custom?.color ?? (item as { color?: string }).color ?? '#888' const visible = Boolean(item.visible) const isDimmed = hoveredItem !== null && hoveredItem !== item const opacity = !visible ? 0.5 : isDimmed ? 0.3 : 1 const handlers = { onClick: () => handleClick(item, i), onMouseEnter: () => setHoveredItem(item), onMouseLeave: () => setHoveredItem(null), } if (renderItem) { return ( {renderItem({ item, name, visible, color, value, onClick: handlers.onClick, })} ) } return ( {name} {hasValue && ( <> {value} )} ) })} ) } ChartV2Legend.displayName = 'ChartV2Legend' export default ChartV2Legend