import React, { Component, ErrorInfo, ReactNode } from 'react'; import { __ } from '@wordpress/i18n'; import { Button } from '@wordpress/components'; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } /** * Error Boundary component for SliderBerg blocks * Catches JavaScript errors in child components and displays a fallback UI */ export class ErrorBoundary extends Component< ErrorBoundaryProps, ErrorBoundaryState > { constructor( props: ErrorBoundaryProps ) { super( props ); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError( error: Error ): Partial< ErrorBoundaryState > { // Update state so the next render will show the fallback UI return { hasError: true, error }; } componentDidCatch( error: Error, errorInfo: ErrorInfo ): void { // Log error details for debugging this.setState( { error, errorInfo, } ); // Log to console in development if ( process.env.NODE_ENV === 'development' ) { // eslint-disable-next-line no-console console.error( 'SliderBerg Error Boundary caught an error:', error, errorInfo ); } } handleReset = (): void => { this.setState( { hasError: false, error: null, errorInfo: null, } ); }; render(): ReactNode { if ( this.state.hasError ) { // Custom fallback UI provided if ( this.props.fallback ) { return this.props.fallback; } // Default fallback UI return (

{ __( 'Something went wrong', 'sliderberg' ) }

{ __( 'An error occurred while rendering this slider block.', 'sliderberg' ) }

{ process.env.NODE_ENV === 'development' && this.state.error && (
{ __( 'Error Details', 'sliderberg' ) }
										{ this.state.error.toString() }
										{ '\n\n' }
										{ this.state.errorInfo?.componentStack }
									
) }
); } return this.props.children; } }