import { Component, type ErrorInfo, type ReactNode } from "react";
import { BarChart, CartesianGrid, ResponsiveContainer, XAxis, YAxis } from "recharts";
// Empty chart placeholder when data fails to render
function EmptyChart() {
return (
);
}
interface ChartErrorBoundaryProps {
children: ReactNode;
resetKey?: string;
}
interface ChartErrorBoundaryState {
hasError: boolean;
prevResetKey?: string;
}
export class ChartErrorBoundary extends Component {
constructor(props: ChartErrorBoundaryProps) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(_: Error) {
return { hasError: true };
}
static getDerivedStateFromProps(props: ChartErrorBoundaryProps, state: ChartErrorBoundaryState) {
// Reset error state when resetKey changes
if (props.resetKey !== state.prevResetKey) {
return { hasError: false, prevResetKey: props.resetKey };
}
return null;
}
componentDidCatch(error: Error, _errorInfo: ErrorInfo) {
console.warn("Chart rendering error:", error.message);
}
render() {
if (this.state.hasError) {
return ;
}
return this.props.children;
}
}