enlighten-qalb / src /components /UI /ErrorBoundary.jsx
eli02's picture
feat: Implement MainLayout component for consistent layout structure
3299552
raw
history blame
1.44 kB
import React from 'react';
import { Box, Typography, Button } from '@mui/material';
import ErrorIcon from '@mui/icons-material/Error';
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, errorInfo) {
console.error('ErrorBoundary caught an error:', error, errorInfo);
}
render() {
if (this.state.hasError) {
return (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
padding: 4,
minHeight: '100vh',
textAlign: 'center',
}}
>
<ErrorIcon color="error" sx={{ fontSize: 60, mb: 2 }} />
<Typography variant="h4" gutterBottom>
Something went wrong
</Typography>
<Typography variant="body1" color="textSecondary" sx={{ mb: 3 }}>
{this.state.error && this.state.error.toString()}
</Typography>
<Button
variant="contained"
color="primary"
onClick={() => window.location.reload()}
>
Reload page
</Button>
</Box>
);
}
return this.props.children;
}
}
export default ErrorBoundary;