mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-03-25 10:02:27 -04:00
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import {Component, ErrorInfo, ReactNode} from 'react'
|
|
import {Button} from 'web/components/buttons/button'
|
|
|
|
interface Props {
|
|
children?: ReactNode
|
|
fallback?: ReactNode
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean
|
|
error?: Error
|
|
}
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props)
|
|
this.state = {hasError: false}
|
|
}
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
return {hasError: true, error}
|
|
}
|
|
|
|
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error('ErrorBoundary caught an error:', error, errorInfo)
|
|
}
|
|
|
|
handleReset = () => {
|
|
this.setState({hasError: false, error: undefined})
|
|
window.location.reload()
|
|
}
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) {
|
|
return this.props.fallback
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen flex-col items-center justify-center p-4">
|
|
<div className="max-w-md text-center">
|
|
<h1 className="mb-4 text-2xl font-bold text-gray-900">Something went wrong</h1>
|
|
<p className="mb-6 text-gray-600">
|
|
We apologize for the inconvenience. An unexpected error occurred.
|
|
</p>
|
|
{this.state.error && (
|
|
<details className="mb-6 text-left">
|
|
<summary className="cursor-pointer text-sm text-gray-500 hover:text-gray-700">
|
|
Error details
|
|
</summary>
|
|
<pre className="mt-2 overflow-auto rounded p-2 text-xs">
|
|
{this.state.error.message}
|
|
</pre>
|
|
</details>
|
|
)}
|
|
<div className="flex gap-3 justify-center">
|
|
<Button color="gray" onClick={this.handleReset}>
|
|
Reload page
|
|
</Button>
|
|
<Button color="gray" onClick={() => window.location.replace('/')}>
|
|
Go home
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return this.props.children
|
|
}
|
|
}
|