/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL */ import { Component, type ErrorInfo, type ReactNode } from 'react'; import { Button } from '@/components/ui/button'; import i18n from '@/i18n'; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('[ErrorBoundary] Caught error:', error, errorInfo); } handleReset = () => { this.setState({ hasError: false, error: null }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
{i18n.t('errorBoundary.title', 'Something went wrong')}

{this.state.error?.message ?? i18n.t('errors.unexpectedError', 'An unexpected error occurred.')}

); } return this.props.children; } }