"use client"; import { useEffect, useRef } from "react"; import { Chart, ChartConfiguration, ChartType } from "chart.js/auto"; export function useChart( createConfig: () => ChartConfiguration, dependencies: React.DependencyList, ) { const canvasRef = useRef(null); const chartRef = useRef | null>(null); useEffect(() => { const canvas = canvasRef.current; if (!canvas) return; const config = createConfig(); if (chartRef.current) { chartRef.current.destroy(); chartRef.current = null; } chartRef.current = new Chart(canvas, config); return () => { chartRef.current?.destroy(); chartRef.current = null; }; }, dependencies); useEffect(() => { return () => { chartRef.current?.destroy(); chartRef.current = null; }; }, []); return canvasRef; }