forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetErrorBoundary.tsx
More file actions
54 lines (46 loc) · 1.42 KB
/
WidgetErrorBoundary.tsx
File metadata and controls
54 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use client';
import { Component, type ReactNode } from 'react';
import { useTranslation } from '@/hooks/useTranslation';
interface Props {
children: ReactNode;
fallback?: ReactNode;
}
interface State {
hasError: boolean;
error: Error | null;
}
class WidgetErrorBoundaryInner extends Component<Props & { errorLabel: string; showCodeLabel: string }, State> {
state: State = { hasError: false, error: null };
static getDerivedStateFromError(error: Error): State {
return { hasError: true, error };
}
componentDidCatch(error: Error) {
console.warn('[WidgetErrorBoundary]', error);
}
render() {
if (this.state.hasError) {
if (this.props.fallback) return this.props.fallback;
return (
<div className="rounded-lg border border-status-error-border bg-status-error-muted p-3 text-sm">
<p className="font-medium text-status-error-foreground">{this.props.errorLabel}</p>
{this.state.error && (
<p className="mt-1 text-xs text-muted-foreground">{this.state.error.message}</p>
)}
</div>
);
}
return this.props.children;
}
}
export function WidgetErrorBoundary({ children, fallback }: Props) {
const { t } = useTranslation();
return (
<WidgetErrorBoundaryInner
errorLabel={t('widget.error')}
showCodeLabel={t('widget.showCode')}
fallback={fallback}
>
{children}
</WidgetErrorBoundaryInner>
);
}