| 1 | 'use client'; |
| 2 | |
| 3 | import React from 'react'; |
| 4 | |
| 5 | interface Props { |
| 6 | children: React.ReactNode; |
| 7 | name?: string; |
| 8 | } |
| 9 | |
| 10 | interface State { |
| 11 | hasError: boolean; |
| 12 | error?: Error; |
| 13 | } |
| 14 | |
| 15 | export default class ErrorBoundary extends React.Component<Props, State> { |
| 16 | constructor(props: Props) { |
| 17 | super(props); |
| 18 | this.state = { hasError: false }; |
| 19 | } |
| 20 | |
| 21 | static getDerivedStateFromError(error: Error): State { |
| 22 | return { hasError: true, error }; |
| 23 | } |
| 24 | |
| 25 | componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { |
| 26 | console.error(`[OSIRIS] ${this.props.name || 'Component'} Error:`, error, errorInfo); |
| 27 | } |
| 28 | |
| 29 | render() { |
| 30 | if (this.state.hasError) { |
| 31 | return ( |
| 32 | <div className="flex items-center justify-center w-full h-full bg-[var(--bg-secondary)] rounded-lg border border-red-900/30 p-4"> |
| 33 | <div className="text-center"> |
| 34 | <div className="text-xs font-mono text-red-400 tracking-widest mb-2"> |
| 35 | ⚠ {this.props.name?.toUpperCase() || 'COMPONENT'} ERROR |
| 36 | </div> |
| 37 | <div className="text-[10px] font-mono text-[var(--text-muted)] max-w-[300px] truncate"> |
| 38 | {this.state.error?.message} |
| 39 | </div> |
| 40 | <button |
| 41 | onClick={() => this.setState({ hasError: false })} |
| 42 | className="mt-3 px-3 py-1 text-[9px] font-mono tracking-widest text-[var(--gold-primary)] border border-[var(--border-primary)] rounded hover:bg-[var(--hover-accent)] transition-colors" |
| 43 | > |
| 44 | RETRY |
| 45 | </button> |
| 46 | </div> |
| 47 | </div> |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | return this.props.children; |
| 52 | } |
| 53 | } |