fix(chart): prevent infinite re-render loop by using useState/useEffect for dark mode detection
Some checks failed
CI / Lint, Test & Build (push) Failing after 16s
CI / Deploy production (on server) (push) Has been skipped

This commit is contained in:
Antigravity
2026-05-22 18:20:42 +00:00
parent beca2c52c3
commit 5bd3f7f5ec

View File

@@ -1,6 +1,6 @@
'use client'
import { useMemo } from 'react'
import { useMemo, useState, useEffect } from 'react'
import {
BarChart, Bar, LineChart, Line, AreaChart, Area,
PieChart, Pie, Cell, RadarChart, Radar, PolarGrid, PolarAngleAxis,
@@ -24,11 +24,34 @@ interface NoteChartProps {
height?: number
}
const isDark = () => document.documentElement.classList.contains('dark')
// 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
}
function NoteChart({ type, title, data, colors, showLegend, height = 250 }: NoteChartProps) {
const chartColors = colors ?? CHART_COLORS
const dark = isDark()
const dark = useDarkMode()
const ttStyle = {
background: dark ? '#1C1C1C' : '#fff',