UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

73 lines (72 loc) • 3.55 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import React, { lazy, Suspense, useCallback, useEffect, useState } from 'react'; import { EVENT, EVENT_SUBJECT } from './analytics/types'; import { appearanceForLink, getUrl } from './analytics/utils'; var PreAuthValuePropositionModal = /*#__PURE__*/lazy(function () { return import( /* webpackChunkName: "@atlaskit-internal_smart-card-pre-auth-value-proposition-modal" */'@atlaskit/smart-card/pre-auth-value-proposition-modal').then(function (module) { return { default: module.PreAuthValuePropositionModal }; }); }); var getNewSmartLinkUrl = function getNewSmartLinkUrl(event) { if (event.subject !== EVENT_SUBJECT.LINK || event.data.isUndo || event.data.isRedo || appearanceForLink(event.data.node) === 'url') { return; } if (event.event === EVENT.CREATED || event.event === EVENT.UPDATED && event.data.previousDisplay === 'url') { return getUrl(event.data.node); } }; var ModalListener = function ModalListener(_ref) { var api = _ref.api, cardPluginEvents = _ref.cardPluginEvents; var _useState = useState(), _useState2 = _slicedToArray(_useState, 2), url = _useState2[0], setUrl = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isOpen = _useState4[0], setIsOpen = _useState4[1]; // While the pre-auth modal is visible, use 'overlayOpen' to suppress the floating toolbar. // Jira portals editor popups above its issue dialog, so the toolbar would otherwise appear // above the modal blanket. Keep it available while link eligibility is still being checked. // On close or unmount, restore the previous intent unless another interaction has changed it. useEffect(function () { var _api$userIntent$share, _api$userIntent$share2; if (!isOpen || !(api !== null && api !== void 0 && api.userIntent)) { return; } var previousIntent = (_api$userIntent$share = (_api$userIntent$share2 = api.userIntent.sharedState.currentState()) === null || _api$userIntent$share2 === void 0 ? void 0 : _api$userIntent$share2.currentUserIntent) !== null && _api$userIntent$share !== void 0 ? _api$userIntent$share : 'default'; api.core.actions.execute(api.userIntent.commands.setCurrentUserIntent('overlayOpen')); return function () { var _api$userIntent; if (((_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 || (_api$userIntent = _api$userIntent.sharedState.currentState()) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.currentUserIntent) === 'overlayOpen') { api.core.actions.execute(api.userIntent.commands.setCurrentUserIntent(previousIntent)); } }; }, [api, isOpen]); useEffect(function () { return cardPluginEvents.subscribe(function (event) { var newSmartLinkUrl = getNewSmartLinkUrl(event); if (newSmartLinkUrl) { setUrl(function (currentUrl) { return currentUrl !== null && currentUrl !== void 0 ? currentUrl : newSmartLinkUrl; }); } }); }, [cardPluginEvents]); var onFinished = useCallback(function () { setIsOpen(false); setUrl(undefined); }, []); return url ? /*#__PURE__*/React.createElement(Suspense, { fallback: null }, /*#__PURE__*/React.createElement(PreAuthValuePropositionModal, { key: url, url: url, onFinished: onFinished, onOpenChange: setIsOpen })) : null; }; export var PreAuthValuePropositionModalListener = /*#__PURE__*/React.memo(ModalListener);