@suspensive/react
Version:
Suspensive interfaces for react
55 lines (52 loc) • 2.96 kB
JavaScript
'use client';
import { _objectSpread2 } from "./_virtual/_@oxc-project_runtime@0.130.0/helpers/objectSpread2.mjs";
import { Message_useErrorBoundaryGroup_this_hook_should_be_called_in_ErrorBoundary_props_children, SuspensiveError } from "./models/SuspensiveError.mjs";
import { useIsChanged } from "./hooks/useIsChanged.mjs";
import { increase } from "./utils/increase.mjs";
import { createContext, useContext, useEffect, useMemo, useReducer } from "react";
import { Fragment, jsx } from "react/jsx-runtime";
//#region src/ErrorBoundaryGroup.tsx
const ErrorBoundaryGroupContext = createContext(void 0);
if (process.env.NODE_ENV === "development") ErrorBoundaryGroupContext.displayName = "ErrorBoundaryGroupContext";
/**
* ErrorBoundaryGroup is a wrapper component that allows you to manage multiple ErrorBoundaries easily.
* ErrorBoundaries as children of nested ErrorBoundaryGroup will also be reset by parent ErrorBoundaryGroup.Consumer.
* @see {@link https://suspensive.org/docs/react/ErrorBoundaryGroup Suspensive Docs}
*/
const ErrorBoundaryGroup = Object.assign(({ blockOutside = false, children }) => {
const [resetKey, reset] = useReducer(increase, 0);
const parentGroup = useContext(ErrorBoundaryGroupContext);
const isParentGroupResetKeyChanged = useIsChanged(parentGroup === null || parentGroup === void 0 ? void 0 : parentGroup.resetKey);
useEffect(() => {
if (!blockOutside && isParentGroupResetKeyChanged) reset();
}, [isParentGroupResetKeyChanged, blockOutside]);
const value = useMemo(() => ({
reset,
resetKey
}), [resetKey]);
return /* @__PURE__ */ jsx(ErrorBoundaryGroupContext.Provider, {
value,
children
});
}, {
displayName: "ErrorBoundaryGroup",
with: (errorBoundaryGroupProps, Component) => Object.assign((props) => /* @__PURE__ */ jsx(ErrorBoundaryGroup, _objectSpread2(_objectSpread2({}, errorBoundaryGroupProps), {}, { children: /* @__PURE__ */ jsx(Component, _objectSpread2({}, props)) })), { displayName: `${ErrorBoundaryGroup.displayName}.with(${Component.displayName || Component.name || "Component"})` }),
Consumer: ({ children }) => /* @__PURE__ */ jsx(Fragment, { children: children(useErrorBoundaryGroup()) })
});
/**
* This hook provides the reset method for the ErrorBoundaryGroup.
* Must be used within an ErrorBoundaryGroup component.
* @see {@link https://suspensive.org/docs/react/ErrorBoundaryGroup#useerrorboundarygroup Suspensive Docs}
*/
const useErrorBoundaryGroup = () => {
const group = useContext(ErrorBoundaryGroupContext);
SuspensiveError.assert(group != null, Message_useErrorBoundaryGroup_this_hook_should_be_called_in_ErrorBoundary_props_children);
return useMemo(() => ({
/**
* When you want to reset multiple ErrorBoundaries as children of ErrorBoundaryGroup, You can use this reset
*/
reset: group.reset }), [group.reset]);
};
//#endregion
export { ErrorBoundaryGroup, ErrorBoundaryGroupContext, useErrorBoundaryGroup };
//# sourceMappingURL=ErrorBoundaryGroup.mjs.map