import * as React from 'react' import { useId } from 'react' import * as RechartsChart from 'recharts' import { cn } from '@/lib/utils' export interface ChartConfig { [key: string]: { label?: string icon?: React.ComponentType color?: string } } type ChartContextProps = { config: ChartConfig } const THEMES = { light: '', dark: '.dark' } as const const CHART_INITIAL_DIMENSION = { width: 1, height: 1 } const ChartContext = React.createContext(null) export function useChart() { const context = React.useContext(ChartContext) if (!context) { throw new Error('useChart must be used within a ') } return context } export function ChartContainer({ id, config, className, children, ...props }: React.ComponentProps<'div'> & { config: ChartConfig children: React.ComponentProps< typeof RechartsChart.ResponsiveContainer >['children'] }) { const uniqueId = useId() const chartId = `chart-${id || uniqueId.replace(/:/g, '')}` const containerRef = React.useRef(null) const [isReady, setIsReady] = React.useState(false) React.useLayoutEffect(() => { const node = containerRef.current if (!node) return const updateSize = () => { const rect = node.getBoundingClientRect() const hasSize = rect.width > 0 && rect.height > 0 setIsReady((current) => (current === hasSize ? current : hasSize)) } updateSize() if (typeof ResizeObserver === 'undefined') return const observer = new ResizeObserver(updateSize) observer.observe(node) return () => observer.disconnect() }, []) return (
{isReady ? ( {children} ) : (
)}
) } export function ChartStyle({ id, config, }: { id: string config: ChartConfig }) { const colorConfig = Object.entries(config).filter( ([, itemConfig]) => itemConfig.color ) if (!colorConfig.length) return null return (