{"version":3,"sources":["../../../src/lib/error-boundary.tsx"],"sourcesContent":["// Modified from https://github.com/bvaughn/react-error-boundary\n// Copyright (c) 2020 Brian Vaughn, MIT License\n\nimport * as React from \"react\";\n\ntype ErrorBoundaryState =\n  | { didCatch: true; error: any }\n  | { didCatch: false; error: null };\n\nexport interface ErrorBoundaryContextValue {\n  didCatch: boolean;\n  error: any;\n  resetErrorBoundary: (...args: any[]) => void;\n}\n\nconst ErrorBoundaryContext =\n  React.createContext<ErrorBoundaryContextValue | null>(null);\nErrorBoundaryContext.displayName = \"ErrorBoundaryContext\";\n\nconst initialState: ErrorBoundaryState = {\n  didCatch: false,\n  error: null,\n} satisfies ErrorBoundaryState;\n\nexport class ErrorBoundary extends React.Component<\n  ErrorBoundaryProps,\n  ErrorBoundaryState\n> {\n  constructor(props: ErrorBoundaryProps) {\n    super(props);\n\n    this.resetErrorBoundary = this.resetErrorBoundary.bind(this);\n    this.state = initialState;\n  }\n\n  static getDerivedStateFromError(error: Error) {\n    return { didCatch: true, error };\n  }\n\n  resetErrorBoundary(...args: any[]) {\n    const { error } = this.state;\n\n    if (error !== null) {\n      this.props.onReset?.({\n        args,\n        reason: \"imperative-api\",\n      });\n\n      this.setState(initialState);\n    }\n  }\n\n  componentDidCatch(error: Error, info: React.ErrorInfo) {\n    this.props.onError?.(error, info);\n  }\n\n  componentDidUpdate(\n    prevProps: ErrorBoundaryProps,\n    prevState: ErrorBoundaryState,\n  ) {\n    const { didCatch } = this.state;\n    const { resetKeys } = this.props;\n\n    // There's an edge case where if the thing that triggered the error happens\n    // to *also* be in the resetKeys array, we'd end up resetting the error\n    // boundary immediately.\n    //\n    // This would likely trigger a second error to be thrown. So we make sure\n    // that we don't check the resetKeys on the first call of cDU after the\n    // error is set.\n    if (\n      didCatch &&\n      prevState.error !== null &&\n      hasArrayChanged(prevProps.resetKeys, resetKeys)\n    ) {\n      this.props.onReset?.({\n        next: resetKeys,\n        prev: prevProps.resetKeys,\n        reason: \"keys\",\n      });\n\n      this.setState(initialState);\n    }\n  }\n\n  render() {\n    const { children, fallbackRender, FallbackComponent, fallback } =\n      this.props;\n    const { didCatch, error } = this.state;\n\n    let childToRender = children;\n\n    if (didCatch) {\n      const props: FallbackProps = {\n        error,\n        resetErrorBoundary: this.resetErrorBoundary,\n      };\n\n      if (typeof fallbackRender === \"function\") {\n        childToRender = fallbackRender(props);\n      } else if (FallbackComponent) {\n        childToRender = React.createElement(FallbackComponent, props);\n      } else if (fallback !== undefined) {\n        childToRender = fallback;\n      } else {\n        throw error;\n      }\n    }\n\n    return (\n      <ErrorBoundaryContext.Provider\n        value={{\n          didCatch,\n          error,\n          resetErrorBoundary: this.resetErrorBoundary,\n        }}\n      >\n        {childToRender}\n      </ErrorBoundaryContext.Provider>\n    );\n  }\n}\n\nfunction hasArrayChanged(a: any[] = [], b: any[] = []) {\n  return (\n    a.length !== b.length || a.some((item, index) => !Object.is(item, b[index]))\n  );\n}\n\nexport type FallbackProps = {\n  error: any;\n  resetErrorBoundary: (...args: any[]) => void;\n};\n\ntype ErrorBoundarySharedProps = React.PropsWithChildren<{\n  onError?: (error: Error, info: React.ErrorInfo) => void;\n  onReset?: (\n    details:\n      | { reason: \"imperative-api\"; args: any[] }\n      | { reason: \"keys\"; prev: any[] | undefined; next: any[] | undefined },\n  ) => void;\n  resetKeys?: any[];\n}>;\n\nexport type ErrorBoundaryPropsWithComponent = ErrorBoundarySharedProps & {\n  fallback?: never;\n  FallbackComponent: React.ComponentType<FallbackProps>;\n  fallbackRender?: never;\n};\n\nexport type ErrorBoundaryPropsWithRender = ErrorBoundarySharedProps & {\n  fallback?: never;\n  FallbackComponent?: never;\n  fallbackRender: (props: FallbackProps) => React.ReactNode;\n};\n\nexport type ErrorBoundaryPropsWithFallback = ErrorBoundarySharedProps & {\n  fallback: React.ReactNode;\n  FallbackComponent?: never;\n  fallbackRender?: never;\n};\n\nexport type ErrorBoundaryProps =\n  | ErrorBoundaryPropsWithFallback\n  | ErrorBoundaryPropsWithComponent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