| 1 |
/** |
| 2 |
* App-level error boundary. |
| 3 |
* |
| 4 |
* Catches render/commit-phase exceptions (including DOM reconciliation errors |
| 5 |
* such as "Failed to execute 'removeChild' on 'Node'", which browser |
| 6 |
* auto-translation can trigger by mutating React-managed text nodes) so a |
| 7 |
* single subtree failure degrades to a friendly reload prompt instead of an |
| 8 |
* uncaught exception that white-screens the admin. |
| 9 |
*/ |
| 10 |
|
| 11 |
import React from "react"; |
| 12 |
import { __ } from "../lib/i18n"; |
| 13 |
|
| 14 |
interface ErrorBoundaryProps { |
| 15 |
children: React.ReactNode; |
| 16 |
} |
| 17 |
|
| 18 |
interface ErrorBoundaryState { |
| 19 |
hasError: boolean; |
| 20 |
} |
| 21 |
|
| 22 |
export class ErrorBoundary extends React.Component< |
| 23 |
ErrorBoundaryProps, |
| 24 |
ErrorBoundaryState |
| 25 |
> { |
| 26 |
constructor(props: ErrorBoundaryProps) { |
| 27 |
super(props); |
| 28 |
this.state = { hasError: false }; |
| 29 |
} |
| 30 |
|
| 31 |
static getDerivedStateFromError(): ErrorBoundaryState { |
| 32 |
return { hasError: true }; |
| 33 |
} |
| 34 |
|
| 35 |
componentDidCatch(error: unknown, info: unknown): void { |
| 36 |
// Keep a console record for support/debugging; not shown to the user. |
| 37 |
console.error("Yatra admin caught a rendering error:", error, info); |
| 38 |
} |
| 39 |
|
| 40 |
private handleReload = (): void => { |
| 41 |
window.location.reload(); |
| 42 |
}; |
| 43 |
|
| 44 |
render(): React.ReactNode { |
| 45 |
if (!this.state.hasError) { |
| 46 |
return this.props.children; |
| 47 |
} |
| 48 |
|
| 49 |
return ( |
| 50 |
<div className="flex items-center justify-center min-h-[50vh] p-6"> |
| 51 |
<div className="max-w-md text-center"> |
| 52 |
<h2 className="text-xl font-semibold text-gray-900 dark:text-white mb-2"> |
| 53 |
{__("Something went wrong", "yatra")} |
| 54 |
</h2> |
| 55 |
<p className="text-sm text-gray-600 dark:text-gray-400 mb-5"> |
| 56 |
{__( |
| 57 |
"The page hit an unexpected error and needs to reload. Your data is safe.", |
| 58 |
"yatra", |
| 59 |
)} |
| 60 |
</p> |
| 61 |
<button |
| 62 |
type="button" |
| 63 |
onClick={this.handleReload} |
| 64 |
className="inline-flex items-center justify-center px-4 py-2 rounded-md bg-blue-600 text-white text-sm font-medium hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500" |
| 65 |
> |
| 66 |
{__("Reload page", "yatra")} |
| 67 |
</button> |
| 68 |
</div> |
| 69 |
</div> |
| 70 |
); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
export default ErrorBoundary; |
| 75 |
|