import React from 'react'; import Icon from '@/components/elements/Icon'; import { TriangleAlert } from 'lucide-react'; type ResetKey = string | number | boolean | null | undefined; const retryButtonClass = [ 'ml-3 shrink-0 rounded-sm border border-border bg-popover px-2 py-1 text-xs uppercase tracking-wide', 'text-foreground transition-colors duration-150 hover:bg-secondary', ].join(' '); interface Props { children?: React.ReactNode; resetKeys?: readonly ResetKey[]; /** Called by Retry before the children render again. */ onReset?: () => void; } interface State { hasError: boolean; } const keysChanged = (previous: readonly ResetKey[] = [], next: readonly ResetKey[] = []): boolean => previous.length !== next.length || previous.some((key, index) => !Object.is(key, next[index])); class ErrorBoundary extends React.Component { state: State = { hasError: false, }; static getDerivedStateFromError() { return { hasError: true }; } componentDidCatch(error: Error) { console.error(error); } componentDidUpdate(prevProps: Props, prevState: State) { if (prevState.hasError && this.state.hasError && keysChanged(prevProps.resetKeys, this.props.resetKeys)) { this.reset(); } } reset = () => { this.setState({ hasError: false }); }; retry = () => { this.props.onReset?.(); this.reset(); }; render() { return this.state.hasError ? (

An error was encountered by the application while rendering this view. Try refreshing the page.

) : ( this.props.children ); } } export default ErrorBoundary;