|
| 1 | +/* |
| 2 | + * Copyright © 2026, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { Component, type ErrorInfo, type ReactNode } from 'react'; |
| 9 | +import { Alert, Box, Button, Typography } from '@mui/material'; |
| 10 | +import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline'; |
| 11 | +import RefreshIcon from '@mui/icons-material/Refresh'; |
| 12 | +import ContentCopyIcon from '@mui/icons-material/ContentCopy'; |
| 13 | +import { FormattedMessage } from 'react-intl'; |
| 14 | + |
| 15 | +const styles = { |
| 16 | + container: { |
| 17 | + display: 'flex', |
| 18 | + flexDirection: 'column', |
| 19 | + height: '100%', |
| 20 | + }, |
| 21 | + centeredContent: (theme: any) => ({ |
| 22 | + display: 'flex', |
| 23 | + flexDirection: 'column', |
| 24 | + alignItems: 'center', |
| 25 | + justifyContent: 'center', |
| 26 | + flex: 1, |
| 27 | + gap: theme.spacing(2), |
| 28 | + }), |
| 29 | + icon: (theme: any) => ({ |
| 30 | + fontSize: 48, |
| 31 | + color: theme.palette.error.main, |
| 32 | + }), |
| 33 | + alertMessage: { |
| 34 | + width: '100%', |
| 35 | + '& .MuiAlert-message': { |
| 36 | + overflow: 'hidden', |
| 37 | + textOverflow: 'ellipsis', |
| 38 | + whiteSpace: 'nowrap', |
| 39 | + }, |
| 40 | + '& .MuiAlert-action': { |
| 41 | + flexShrink: 0, |
| 42 | + }, |
| 43 | + }, |
| 44 | +}; |
| 45 | + |
| 46 | +interface PanelErrorBoundaryProps { |
| 47 | + children: ReactNode; |
| 48 | + onReset?: () => void; |
| 49 | +} |
| 50 | + |
| 51 | +interface PanelErrorBoundaryState { |
| 52 | + hasError: boolean; |
| 53 | + error: Error | null; |
| 54 | + errorInfo: ErrorInfo | null; |
| 55 | + copied: boolean; |
| 56 | +} |
| 57 | + |
| 58 | +export default class PanelErrorBoundary extends Component<PanelErrorBoundaryProps, PanelErrorBoundaryState> { |
| 59 | + constructor(props: PanelErrorBoundaryProps) { |
| 60 | + super(props); |
| 61 | + this.state = { hasError: false, error: null, errorInfo: null, copied: false }; |
| 62 | + } |
| 63 | + |
| 64 | + static getDerivedStateFromError(error: Error): PanelErrorBoundaryState { |
| 65 | + return { hasError: true, error, errorInfo: null, copied: false }; |
| 66 | + } |
| 67 | + |
| 68 | + componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
| 69 | + console.error('Panel error caught:', error, errorInfo); |
| 70 | + this.setState({ errorInfo }); |
| 71 | + } |
| 72 | + |
| 73 | + handleReset = () => { |
| 74 | + this.setState({ hasError: false, error: null, errorInfo: null, copied: false }); |
| 75 | + this.props.onReset?.(); |
| 76 | + }; |
| 77 | + |
| 78 | + getErrorText = (): string => { |
| 79 | + if (!this.state.error) return ''; |
| 80 | + |
| 81 | + let errorText = `Error: ${this.state.error.toString()}`; |
| 82 | + if (this.state.errorInfo?.componentStack) { |
| 83 | + errorText += `\nComponent Stack: ${this.state.errorInfo.componentStack}`; |
| 84 | + } |
| 85 | + if (this.state.error.stack) { |
| 86 | + errorText += `\nStack Trace: ${this.state.error.stack}`; |
| 87 | + } |
| 88 | + return errorText; |
| 89 | + }; |
| 90 | + |
| 91 | + handleCopy = async () => { |
| 92 | + const errorText = this.getErrorText(); |
| 93 | + if (errorText) { |
| 94 | + await navigator.clipboard.writeText(errorText); |
| 95 | + this.setState({ copied: true }); |
| 96 | + setTimeout(() => this.setState({ copied: false }), 2000); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + render() { |
| 101 | + if (this.state.hasError) { |
| 102 | + return ( |
| 103 | + <Box sx={styles.container}> |
| 104 | + <Box sx={styles.centeredContent}> |
| 105 | + <ErrorOutlineIcon sx={styles.icon} /> |
| 106 | + <Typography> |
| 107 | + <FormattedMessage id="PanelError" /> |
| 108 | + </Typography> |
| 109 | + <Button variant="outlined" size="small" startIcon={<RefreshIcon />} onClick={this.handleReset}> |
| 110 | + <FormattedMessage id="Reload" /> |
| 111 | + </Button> |
| 112 | + </Box> |
| 113 | + {this.state.error && ( |
| 114 | + <Alert |
| 115 | + severity="warning" |
| 116 | + sx={styles.alertMessage} |
| 117 | + action={ |
| 118 | + <Button |
| 119 | + color="inherit" |
| 120 | + size="small" |
| 121 | + startIcon={<ContentCopyIcon />} |
| 122 | + onClick={this.handleCopy} |
| 123 | + > |
| 124 | + <FormattedMessage id={this.state.copied ? 'Copied' : 'CopyError'} /> |
| 125 | + </Button> |
| 126 | + } |
| 127 | + > |
| 128 | + {this.state.error.toString()} |
| 129 | + </Alert> |
| 130 | + )} |
| 131 | + </Box> |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + return this.props.children; |
| 136 | + } |
| 137 | +} |
0 commit comments