stock-bot/apps/stock/web-app/src/features/charts/components/ChartErrorBoundary.tsx
2025-07-02 19:58:43 -04:00

51 lines
No EOL
1.5 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Component, ReactNode } from 'react';
interface Props {
children: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class ChartErrorBoundary extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
console.error('Chart error:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<div className="flex items-center justify-center h-full bg-surface-secondary rounded-lg border border-border p-8">
<div className="text-center">
<div className="text-error text-6xl mb-4"></div>
<h2 className="text-lg font-semibold text-text-primary mb-2">
Chart Loading Error
</h2>
<p className="text-sm text-text-secondary mb-4">
{this.state.error?.message || 'Unable to load the chart'}
</p>
<button
onClick={() => this.setState({ hasError: false, error: null })}
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
>
Try Again
</button>
</div>
</div>
);
}
return this.props.children;
}
}