"use client"; import * as React from 'react'; import { render } from "rc-util/es/React/render"; import ConfigProvider, { globalConfig, warnContext } from '../config-provider'; import PurePanel from './PurePanel'; import useNotification, { useInternalNotification } from './useNotification'; let notification = null; let act = callback => callback(); let taskQueue = []; let defaultGlobalConfig = {}; function getGlobalContext() { const { prefixCls: globalPrefixCls, getContainer: globalGetContainer, rtl, maxCount, top, bottom } = defaultGlobalConfig; const mergedPrefixCls = globalPrefixCls !== null && globalPrefixCls !== void 0 ? globalPrefixCls : globalConfig().getPrefixCls('notification'); const mergedContainer = (globalGetContainer === null || globalGetContainer === void 0 ? void 0 : globalGetContainer()) || document.body; return { prefixCls: mergedPrefixCls, getContainer: () => mergedContainer, rtl, maxCount, top, bottom }; } const GlobalHolder = /*#__PURE__*/React.forwardRef((_, ref) => { const [notificationConfig, setNotificationConfig] = React.useState(getGlobalContext); const [api, holder] = useInternalNotification(notificationConfig); const global = globalConfig(); const rootPrefixCls = global.getRootPrefixCls(); const rootIconPrefixCls = global.getIconPrefixCls(); const theme = global.getTheme(); const sync = () => { setNotificationConfig(getGlobalContext); }; React.useEffect(sync, []); React.useImperativeHandle(ref, () => { const instance = Object.assign({}, api); Object.keys(instance).forEach(method => { instance[method] = function () { sync(); return api[method].apply(api, arguments); }; }); return { instance, sync }; }); return /*#__PURE__*/React.createElement(ConfigProvider, { prefixCls: rootPrefixCls, iconPrefixCls: rootIconPrefixCls, theme: theme }, holder); }); function flushNotice() { if (!notification) { const holderFragment = document.createDocumentFragment(); const newNotification = { fragment: holderFragment }; notification = newNotification; // Delay render to avoid sync issue act(() => { render( /*#__PURE__*/React.createElement(GlobalHolder, { ref: node => { const { instance, sync } = node || {}; Promise.resolve().then(() => { if (!newNotification.instance && instance) { newNotification.instance = instance; newNotification.sync = sync; flushNotice(); } }); } }), holderFragment); }); return; } // Notification not ready if (!notification.instance) { return; } // >>> Execute task taskQueue.forEach(task => { // eslint-disable-next-line default-case switch (task.type) { case 'open': { act(() => { notification.instance.open(Object.assign(Object.assign({}, defaultGlobalConfig), task.config)); }); break; } case 'destroy': act(() => { notification === null || notification === void 0 ? void 0 : notification.instance.destroy(task.key); }); break; } }); // Clean up taskQueue = []; } // ============================================================================== // == Export == // ============================================================================== function setNotificationGlobalConfig(config) { defaultGlobalConfig = Object.assign(Object.assign({}, defaultGlobalConfig), config); // Trigger sync for it act(() => { var _a; (_a = notification === null || notification === void 0 ? void 0 : notification.sync) === null || _a === void 0 ? void 0 : _a.call(notification); }); } function open(config) { // Warning if exist theme if (process.env.NODE_ENV !== 'production') { warnContext('notification'); } taskQueue.push({ type: 'open', config }); flushNotice(); } function destroy(key) { taskQueue.push({ type: 'destroy', key }); flushNotice(); } const methods = ['success', 'info', 'warning', 'error']; const baseStaticMethods = { open, destroy, config: setNotificationGlobalConfig, useNotification, _InternalPanelDoNotUseOrYouWillBeFired: PurePanel }; const staticMethods = baseStaticMethods; methods.forEach(type => { staticMethods[type] = config => open(Object.assign(Object.assign({}, config), { type })); }); // ============================================================================== // == Test == // ============================================================================== const noop = () => {}; /** @internal Only Work in test env */ // eslint-disable-next-line import/no-mutable-exports export let actWrapper = noop; if (process.env.NODE_ENV === 'test') { actWrapper = wrapper => { act = wrapper; }; } export default staticMethods;