'use client'
import { useMemo, useState, useEffect } from 'react'
import {
BarChart, Bar, LineChart, Line, AreaChart, Area,
PieChart, Pie, Cell, RadarChart, Radar, PolarGrid, PolarAngleAxis,
XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer,
FunnelChart, Funnel, LabelList,
} from 'recharts'
const CHART_COLORS = ['#6366f1', '#22d3ee', '#f59e0b', '#ef4444', '#10b981', '#a78bfa', '#fb923c', '#14b8a6']
interface ChartData {
label: string
value: number
}
interface NoteChartProps {
type: 'bar' | 'horizontal-bar' | 'line' | 'area' | 'pie' | 'radar' | 'funnel' | 'gauge'
title?: string
data: ChartData[]
colors?: string[]
showLegend?: boolean
height?: number
}
// Hook to detect dark mode without re-render loops
function useDarkMode() {
const [isDark, setIsDark] = useState(() => typeof document !== 'undefined' && document.documentElement.classList.contains('dark'))
useEffect(() => {
const checkDarkMode = () => {
setIsDark(document.documentElement.classList.contains('dark'))
}
// Initial check
checkDarkMode()
// Listen for class changes on html element
const observer = new MutationObserver(checkDarkMode)
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['class'],
})
return () => observer.disconnect()
}, [])
return isDark
}
export function NoteChart({ type, title, data, colors, showLegend, height = 250 }: NoteChartProps) {
const chartColors = colors ?? CHART_COLORS
const dark = useDarkMode()
const ttStyle = {
background: dark ? '#1C1C1C' : '#fff',
border: dark ? '1px solid rgba(255,255,255,0.08)' : '1px solid rgba(0,0,0,0.08)',
borderRadius: 8,
fontSize: 12,
color: dark ? '#fff' : '#000',
}
const tickStyle = { fill: dark ? 'rgba(255,255,255,0.55)' : 'rgba(0,0,0,0.55)', fontSize: 11 }
const gridStroke = dark ? 'rgba(255,255,255,0.07)' : 'rgba(0,0,0,0.07)'
const legendStyle = { fontSize: 12, color: dark ? 'rgba(255,255,255,0.6)' : 'rgba(0,0,0,0.6)' }
const xKey = 'label'
const yKeys = ['value']
const renderChart = () => {
switch (type) {
case 'bar':
return (