import React, { ErrorInfo } from "react" import Button from "../../fundamentals/button" import RefreshIcon from "../../fundamentals/icons/refresh-icon" import WarningCircleIcon from "../../fundamentals/icons/warning-circle" import XCircleIcon from "../../fundamentals/icons/x-circle-icon" type Props = { children: React.ReactNode origin: string } type State = { hasError: boolean hidden?: boolean } class WidgetErrorBoundary extends React.Component { public state: State = { hasError: false, } public static getDerivedStateFromError(_: Error): State { return { hasError: true, hidden: false } } public componentDidCatch(error: Error, errorInfo: ErrorInfo) { if (process.env.NODE_ENV !== "production") { console.group( `%cAn error occurred in a widget from ${this.props.origin}:`, "color: red; font-weight: bold, background-color: #fff;" ) console.error(error) console.error( "%cComponent Stack:", "color: red", errorInfo.componentStack ) console.groupEnd() } } public handleResetError() { this.setState({ hasError: false }) } public hideError() { this.setState({ hidden: true }) } public renderFallback() { if (process.env.NODE_ENV !== "production" && !this.state.hidden) { return ( ) } // Don't render anything in production return null } render() { if (this.state.hasError) { return this.renderFallback() } return this.props.children } } const FallbackWidget = ({ origin, reset, hide, }: { origin: string reset: () => void hide: () => void }) => { return (

Uncaught error

A widget from {origin} crashed. See the console for more info.

What should I do?
If you are the developer of this widget, you should fix the error and reload the page. If you are not the developer, you should contact the maintainer and report the error.

) } export default WidgetErrorBoundary