|
| 1 | +import React from 'react'; |
| 2 | +import { ReactComponent as ImageOops } from 'images/img-oops.svg'; |
| 3 | +import './PlayErrorBoundary.css'; |
| 4 | + |
| 5 | +class PlayErrorBoundary extends React.Component { |
| 6 | + constructor(props) { |
| 7 | + super(props); |
| 8 | + this.state = { hasError: false, error: null, isChunkError: false }; |
| 9 | + } |
| 10 | + |
| 11 | + static getDerivedStateFromError(error) { |
| 12 | + // Detect chunk load failures (network errors loading lazy chunks) |
| 13 | + const isChunkError = |
| 14 | + error?.name === 'ChunkLoadError' || |
| 15 | + /loading chunk/i.test(error?.message) || |
| 16 | + /failed to fetch dynamically imported module/i.test(error?.message); |
| 17 | + |
| 18 | + return { hasError: true, error, isChunkError }; |
| 19 | + } |
| 20 | + |
| 21 | + componentDidCatch(error, errorInfo) { |
| 22 | + console.error(`Error loading play "${this.props.playName}":`, error, errorInfo); |
| 23 | + } |
| 24 | + |
| 25 | + handleRetry = () => { |
| 26 | + this.setState({ hasError: false, error: null, isChunkError: false }); |
| 27 | + }; |
| 28 | + |
| 29 | + handleGoBack = () => { |
| 30 | + window.location.href = '/plays'; |
| 31 | + }; |
| 32 | + |
| 33 | + render() { |
| 34 | + if (this.state.hasError) { |
| 35 | + return ( |
| 36 | + <div className="play-error-boundary play-error-container"> |
| 37 | + <ImageOops className="play-error-image" /> |
| 38 | + <h2 className="play-error-title"> |
| 39 | + {this.state.isChunkError ? 'Failed to load this play' : 'Something went wrong'} |
| 40 | + </h2> |
| 41 | + <p className="play-error-message"> |
| 42 | + {this.state.isChunkError |
| 43 | + ? 'There was a network error loading this play. Please check your connection and try again.' |
| 44 | + : `An error occurred while rendering "${this.props.playName || 'this play'}".`} |
| 45 | + </p> |
| 46 | + <div className="play-error-actions"> |
| 47 | + {this.state.isChunkError && ( |
| 48 | + <button className="play-error-retry-button" onClick={this.handleRetry}> |
| 49 | + Retry |
| 50 | + </button> |
| 51 | + )} |
| 52 | + <button className="play-error-back-button" onClick={this.handleGoBack}> |
| 53 | + Back to Plays |
| 54 | + </button> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return this.props.children; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export default PlayErrorBoundary; |
0 commit comments