"use client"; import { useEffect, useRef } from "react"; import type { Chart as ChartInstance, ChartConfiguration, ChartType } from "chart.js"; let chartModulePromise: Promise | null = null; export function preloadChartJs() { if (!chartModulePromise) { chartModulePromise = import("chart.js/auto"); } return chartModulePromise; } 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; let disposed = false; const setupChart = async () => { const { Chart } = await preloadChartJs(); if (disposed) return; const config = createConfig(); const nextType = (config as { type?: ChartType }).type; const currentType = chartRef.current ? (chartRef.current.config as { type?: ChartType }).type : null; if (chartRef.current && currentType === nextType) { chartRef.current.data = config.data as ChartInstance["data"]; chartRef.current.options = (config.options || {}) as ChartInstance["options"]; chartRef.current.update("none"); return; } chartRef.current?.destroy(); chartRef.current = new Chart(canvas, config); }; void setupChart(); return () => { disposed = true; }; }, dependencies); useEffect(() => { return () => { chartRef.current?.destroy(); chartRef.current = null; }; }, []); return canvasRef; }