Files

63 lines
1.5 KiB
TypeScript
Raw Permalink Normal View History

'use client';
import { Component, type ReactNode } from 'react';
import { Button } from '@/components/ui/button';
interface Props {
children: ReactNode;
fallback?: ReactNode;
widgetName?: string;
}
interface State {
hasError: boolean;
error: Error | null;
}
export class WidgetErrorBoundary 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(
`Widget error (${this.props.widgetName || 'unknown'}):`,
error,
errorInfo
);
}
render() {
if (this.state.hasError) {
if (this.props.fallback) {
return this.props.fallback;
}
return (
<div className="flex h-full min-h-[200px] flex-col items-center justify-center gap-3 rounded-lg border border-destructive/20 bg-destructive/5 p-4">
<p className="text-sm font-medium text-destructive">
{this.props.widgetName || 'Widget'} failed to load
</p>
<p className="text-xs text-muted-foreground text-center">
{this.state.error?.message || 'An unexpected error occurred'}
</p>
<Button
variant="outline"
size="sm"
onClick={() => this.setState({ hasError: false, error: null })}
>
Retry
</Button>
</div>
);
}
return this.props.children;
}
}