UNPKG

@suspensive/react

Version:

Suspensive interfaces for react

1 lines 4.57 kB
{"version":3,"file":"ErrorBoundaryGroup.mjs","names":[],"sources":["../src/ErrorBoundaryGroup.tsx"],"sourcesContent":["'use client'\nimport {\n type ComponentProps,\n type ComponentType,\n type PropsWithChildren,\n type ReactNode,\n createContext,\n useContext,\n useEffect,\n useMemo,\n useReducer,\n} from 'react'\nimport { useIsChanged } from './hooks/useIsChanged'\nimport {\n Message_useErrorBoundaryGroup_this_hook_should_be_called_in_ErrorBoundary_props_children,\n SuspensiveError,\n} from './models/SuspensiveError'\nimport type { PropsWithoutChildren } from './utility-types/PropsWithoutChildren'\nimport { increase } from './utils/increase'\n\nexport const ErrorBoundaryGroupContext = createContext<{ reset: () => void; resetKey: number } | undefined>(undefined)\nif (process.env.NODE_ENV === 'development') {\n ErrorBoundaryGroupContext.displayName = 'ErrorBoundaryGroupContext'\n}\n\nexport interface ErrorBoundaryGroupProps extends PropsWithChildren {\n /**\n * If you use blockOutside as true, ErrorBoundaryGroup will protect multiple ErrorBoundaries as its children from external ErrorBoundaryGroup's resetKey\n * @default false\n */\n blockOutside?: boolean\n}\n\n/**\n * ErrorBoundaryGroup is a wrapper component that allows you to manage multiple ErrorBoundaries easily.\n * ErrorBoundaries as children of nested ErrorBoundaryGroup will also be reset by parent ErrorBoundaryGroup.Consumer.\n * @see {@link https://suspensive.org/docs/react/ErrorBoundaryGroup Suspensive Docs}\n */\nexport const ErrorBoundaryGroup = Object.assign(\n ({ blockOutside = false, children }: ErrorBoundaryGroupProps) => {\n const [resetKey, reset] = useReducer(increase, 0)\n const parentGroup = useContext(ErrorBoundaryGroupContext)\n const isParentGroupResetKeyChanged = useIsChanged(parentGroup?.resetKey)\n\n useEffect(() => {\n if (!blockOutside && isParentGroupResetKeyChanged) {\n reset()\n }\n }, [isParentGroupResetKeyChanged, blockOutside])\n\n const value = useMemo(() => ({ reset, resetKey }), [resetKey])\n\n return <ErrorBoundaryGroupContext.Provider value={value}>{children}</ErrorBoundaryGroupContext.Provider>\n },\n {\n displayName: 'ErrorBoundaryGroup',\n with: <TProps extends ComponentProps<ComponentType> = Record<string, never>>(\n errorBoundaryGroupProps: PropsWithoutChildren<ErrorBoundaryGroupProps>,\n Component: ComponentType<TProps>\n ) =>\n Object.assign(\n (props: TProps) => (\n <ErrorBoundaryGroup {...errorBoundaryGroupProps}>\n <Component {...props} />\n </ErrorBoundaryGroup>\n ),\n {\n displayName: `${ErrorBoundaryGroup.displayName}.with(${Component.displayName || Component.name || 'Component'})`,\n }\n ),\n Consumer: ({\n children,\n }: {\n children: (errorBoundaryGroup: ReturnType<typeof useErrorBoundaryGroup>) => ReactNode\n }) => <>{children(useErrorBoundaryGroup())}</>,\n }\n)\n\n/**\n * This hook provides the reset method for the ErrorBoundaryGroup.\n * Must be used within an ErrorBoundaryGroup component.\n * @see {@link https://suspensive.org/docs/react/ErrorBoundaryGroup#useerrorboundarygroup Suspensive Docs}\n */\nexport const useErrorBoundaryGroup = (): { reset: () => void } => {\n const group = useContext(ErrorBoundaryGroupContext)\n SuspensiveError.assert(\n group != null,\n Message_useErrorBoundaryGroup_this_hook_should_be_called_in_ErrorBoundary_props_children\n )\n return useMemo(\n () => ({\n /**\n * When you want to reset multiple ErrorBoundaries as children of ErrorBoundaryGroup, You can use this reset\n */\n reset: group.reset,\n }),\n [group.reset]\n )\n}\n"],"mappings":";;;;;;;;;;AAoBA,MAAa,4BAA4B,cAAmE,MAAS;AACrH,IAAI,QAAQ,IAAI,aAAa,eAC3B,0BAA0B,cAAc;;;;;;AAgB1C,MAAa,qBAAqB,OAAO,QACtC,EAAE,eAAe,OAAO,eAAwC;CAC/D,MAAM,CAAC,UAAU,SAAS,WAAW,UAAU,CAAC;CAChD,MAAM,cAAc,WAAW,yBAAyB;CACxD,MAAM,+BAA+B,uEAAa,YAAa,QAAQ;CAEvE,gBAAgB;EACd,IAAI,CAAC,gBAAgB,8BACnB,MAAM;CAEV,GAAG,CAAC,8BAA8B,YAAY,CAAC;CAE/C,MAAM,QAAQ,eAAe;EAAE;EAAO;CAAS,IAAI,CAAC,QAAQ,CAAC;CAE7D,OAAO,oBAAC,0BAA0B,UAA3B;EAA2C;EAAQ;CAA6C;AACzG,GACA;CACE,aAAa;CACb,OACE,yBACA,cAEA,OAAO,QACJ,UACC,oBAAC,sDAAuB,0CACtB,oBAAC,8BAAc,KAAQ,IACL,IAEtB,EACE,aAAa,GAAG,mBAAmB,YAAY,QAAQ,UAAU,eAAe,UAAU,QAAQ,YAAY,GAChH,CACF;CACF,WAAW,EACT,eAGI,0CAAG,SAAS,sBAAsB,CAAC,EAAI;AAC/C,CACF;;;;;;AAOA,MAAa,8BAAqD;CAChE,MAAM,QAAQ,WAAW,yBAAyB;CAClD,gBAAgB,OACd,SAAS,MACT,wFACF;CACA,OAAO,eACE;;;;AAIL,OAAO,MAAM,MACf,IACA,CAAC,MAAM,KAAK,CACd;AACF"}