PromoCursed/node_modules/.cache/babel-loader/0fe11f6d13ba0f464cd291e9a18ed0079d55bd6072811e2ea4d9fe0fcfb1a99b.json

1 line
11 KiB
JSON
Raw Normal View History

2024-08-20 23:25:37 +04:00
{"ast":null,"code":"\"use client\";\n\nimport _toConsumableArray from \"@babel/runtime/helpers/esm/toConsumableArray\";\nimport * as React from 'react';\nimport usePatchElement from '../../_util/hooks/usePatchElement';\nimport { withConfirm, withError, withInfo, withSuccess, withWarn } from '../confirm';\nimport destroyFns from '../destroyFns';\nimport HookModal from './HookModal';\nlet uuid = 0;\nconst ElementsHolder = /*#__PURE__*/React.memo( /*#__PURE__*/React.forwardRef((_props, ref) => {\n const [elements, patchElement] = usePatchElement();\n React.useImperativeHandle(ref, () => ({\n patchElement\n }), []);\n // eslint-disable-next-line react/jsx-no-useless-fragment\n return /*#__PURE__*/React.createElement(React.Fragment, null, elements);\n}));\nfunction useModal() {\n const holderRef = React.useRef(null);\n // ========================== Effect ==========================\n const [actionQueue, setActionQueue] = React.useState([]);\n React.useEffect(() => {\n if (actionQueue.length) {\n const cloneQueue = _toConsumableArray(actionQueue);\n cloneQueue.forEach(action => {\n action();\n });\n setActionQueue([]);\n }\n }, [actionQueue]);\n // =========================== Hook ===========================\n const getConfirmFunc = React.useCallback(withFunc => function hookConfirm(config) {\n var _a;\n uuid += 1;\n const modalRef = /*#__PURE__*/React.createRef();\n // Proxy to promise with `onClose`\n let resolvePromise;\n const promise = new Promise(resolve => {\n resolvePromise = resolve;\n });\n let silent = false;\n let closeFunc;\n const modal = /*#__PURE__*/React.createElement(HookModal, {\n key: `modal-${uuid}`,\n config: withFunc(config),\n ref: modalRef,\n afterClose: () => {\n closeFunc === null || closeFunc === void 0 ? void 0 : closeFunc();\n },\n isSilent: () => silent,\n onConfirm: confirmed => {\n resolvePromise(confirmed);\n }\n });\n closeFunc = (_a = holderRef.current) === null || _a === void 0 ? void 0 : _a.patchElement(modal);\n if (closeFunc) {\n destroyFns.push(closeFunc);\n }\n const instance = {\n destroy: () => {\n function destroyAction() {\n var _a;\n (_a = modalRef.current) === null || _a === void 0 ? void 0 : _a.destroy();\n }\n if (modalRef.current) {\n destroyAction();\n } else {\n setActionQueue(prev => [].concat(_toConsumableArray(prev), [destroyAction]));\n }\n },\n update: newConfig => {\n function updateAction() {\n var _a;\n (_a = modalRef.current) === null || _a === void 0 ? void 0 : _a.update(newConfig);\n }\n if (modalRef.current) {\n updateAction();\n } else {\n setActionQueue(prev => [].concat(_toConsumableArray(prev), [updateAction]));\n }\n },\n then: resolve => {\n silent = true;\n return promise.then(resolve);\n }\n };\n return instance;\n }, []);\n const fns = React.useMemo(() => ({\n info: getConfirmFunc(withInfo),\n success: getConfirmFunc(withSuccess),\n error: getConfirmFunc(withError),\n warning: getConfirmFunc(withWarn),\n confirm: getConfirmFunc(withConfirm)\n }), []);\n return [fns, /*#__PURE__*/React.createElement(ElementsHolder, {\n key: \"modal-holder\",\n ref: holderRef\n })];\n}\nexport default useModal;","map":{"version":3,"names":["_toConsumableArray","React","usePatchElement","withConfirm","withError","withInfo","withSuccess","withWarn","destroyFns","HookModal","uuid","ElementsHolder","memo","forwardRef","_props","ref","elements","patchElement","useImperativeHandle","createElement","Fragment","useModal","holderRef","useRef","actionQueue","setActionQueue","useState","useEffect","length","cloneQueue","forEach","action","getConfirmFunc","useCallback","withFunc","hookConfirm","config","_a","modalRef","createRef","resolvePromise","promise","Promise","resolve","silent","closeFunc","modal","key","afte