{"version":3,"file":"CatchBoundary.cjs","names":[],"sources":["../../src/CatchBoundary.tsx"],"sourcesContent":["'use client'\n\nimport * as React from 'react'\nimport type { ErrorRouteComponent } from './route'\nimport type { ErrorInfo } from 'react'\n\nexport function CatchBoundary(props: {\n  getResetKey: () => number | string\n  children: React.ReactNode\n  errorComponent?: ErrorRouteComponent\n  onCatch?: (error: Error, errorInfo: ErrorInfo) => void\n}) {\n  const errorComponent = props.errorComponent ?? ErrorComponent\n\n  return (\n    <CatchBoundaryImpl\n      getResetKey={props.getResetKey}\n      onCatch={props.onCatch}\n      children={({ error, reset }) => {\n        if (error) {\n          return React.createElement(errorComponent, {\n            error,\n            reset,\n          })\n        }\n\n        return props.children\n      }}\n    />\n  )\n}\n\nclass CatchBoundaryImpl extends React.Component<{\n  getResetKey: () => number | string\n  children: (props: {\n    error: Error | null\n    reset: () => void\n  }) => React.ReactNode\n  onCatch?: (error: Error, errorInfo: ErrorInfo) => void\n}> {\n  state = { error: null } as { error: Error | null; resetKey?: string | number }\n\n  static getDerivedStateFromProps(\n    props: { getResetKey: () => string | number },\n    state: { resetKey?: string | number; error: Error | null },\n  ) {\n    const resetKey = props.getResetKey()\n\n    if (state.error && state.resetKey !== resetKey) {\n      return { resetKey, error: null }\n    }\n\n    return { resetKey }\n  }\n  static getDerivedStateFromError(error: Error) {\n    return { error }\n  }\n  reset() {\n    this.setState({ error: null })\n  }\n  componentDidCatch(error: Error, errorInfo: ErrorInfo) {\n    if (this.props.onCatch) {\n      this.props.onCatch(error, errorInfo)\n    }\n  }\n  render() {\n    return this.props.children({\n      error: this.state.error,\n      reset: () => {\n        this.reset()\n      },\n    })\n  }\n}\n\nexport function ErrorComponent({ error }: { error: any }) {\n  const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production')\n\n  return (\n    <div style={{ padding: '.5rem', maxWidth: '100%' }}>\n      <div style={{ display: 'flex', alignItems: 'center', gap: '.5rem' }}>\n        <strong style={{ fontSize: '1rem' }}>Something went wrong!</strong>\n        <button\n          style={{\n            appearance: 'none',\n            fontSize: '.6em',\n            border: '1px solid currentColor',\n            padding: '.1rem .2rem',\n            fontWeight: 'bold',\n            borderRadius: '.25rem',\n          }}\n          onClick={() => setShow((d) => !d)}\n        >\n          {show ? 'Hide Error' : 'Show Error'}\n        </button>\n      </div>\n      <div style={{ height: '.25rem' }} />\n      {show ? (\n        <div>\n          <pre\n            style={{\n              fontSize: '.7em',\n              border: '1px solid red',\n              borderRadius: '.25rem',\n              padding: '.3rem',\n              color: 'red',\n              overflow: 'auto',\n            }}\n          >\n            {error.message ? <code>{error.message}</code> : null}\n          </pre>\n        </div>\n      ) : null}\n    </div>\n  )\n}\n"],"mappings":";;;;;;AAMA,SAAgB,cAAc,OAK3B;CACD,MAAM,iBAAiB,MAAM,kBAAkB;CAE/C,OACE,iBAAA,GAAA,kBAAA,KAAC,mBAAD;EACE,aAAa,MAAM;EACnB,SAAS,MAAM;EACf,WAAW,EAAE,OAAO,YAAY;GAC9B,IAAI,OACF,OAAO,MAAM,cAAc,gBAAgB;IACzC;IACA;GACF,CAAC;GAGH,OAAO,MAAM;EACf;CACD,CAAA;AAEL;AAEA,IAAM,oBAAN,cAAgC,MAAM,UAOnC;;;eACO,EAAE,OAAO,KAAK;;CAEtB,OAAO,yBACL,OACA,OACA;EACA,MAAM,WAAW,MAAM,YAAY;EAEnC,IAAI,MAAM,SAAS,MAAM,aAAa,UACpC,OAAO;GAAE;GAAU,OAAO;EAAK;EAGjC,OAAO,EAAE,SAAS;CACpB;CACA,OAAO,yBAAyB,OAAc;EAC5C,OAAO,EAAE,MAAM;CACjB;CACA,QAAQ;EACN,KAAK,SAAS,EAAE,OAAO,KAAK,CAAC;CAC/B;CACA,kBAAkB,OAAc,WAAsB;EACpD,IAAI,KAAK,MAAM,SACb,KAAK,MAAM,QAAQ,OAAO,SAAS;CAEvC;CACA,SAAS;EACP,OAAO,KAAK,MAAM,SAAS;GACzB,OAAO,KAAK,MAAM;GAClB,aAAa;IACX,KAAK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