import { cn } from '@affine/admin/utils'; import { createContext, forwardRef, useContext, useId, useMemo } from 'react'; import type { TooltipProps } from 'recharts'; import { ResponsiveContainer, Tooltip as RechartsTooltip } from 'recharts'; const THEMES = { light: '', dark: '.dark' } as const; export type ChartConfig = Record< string, { label?: React.ReactNode; color?: string; theme?: Partial>; } >; type ChartContextValue = { config: ChartConfig; }; const ChartContext = createContext(null); function useChart() { const value = useContext(ChartContext); if (!value) { throw new Error('useChart must be used within '); } return value; } function ChartStyle({ chartId, config, }: { chartId: string; config: ChartConfig; }) { const colorEntries = Object.entries(config).filter( ([, item]) => item.color || item.theme ); if (!colorEntries.length) { return null; } const css = Object.entries(THEMES) .map(([themeKey, prefix]) => { const declarations = colorEntries .map(([key, item]) => { const color = item.theme?.[themeKey as keyof typeof THEMES] ?? item.color; return color ? ` --color-${key}: ${color};` : ''; }) .filter(Boolean) .join('\n'); if (!declarations) { return ''; } return `${prefix} [data-chart="${chartId}"] {\n${declarations}\n}`; }) .filter(Boolean) .join('\n'); if (!css) { return null; } return