UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

59 lines (58 loc) • 3.12 kB
import React, { lazy, Suspense, useCallback, useEffect, useState } from 'react'; import { EVENT, EVENT_SUBJECT } from './analytics/types'; import { appearanceForLink, getUrl } from './analytics/utils'; const PreAuthValuePropositionModal = /*#__PURE__*/lazy(() => import( /* webpackChunkName: "@atlaskit-internal_smart-card-pre-auth-value-proposition-modal" */'@atlaskit/smart-card/pre-auth-value-proposition-modal').then(module => ({ default: module.PreAuthValuePropositionModal }))); const 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); } }; const ModalListener = ({ api, cardPluginEvents }) => { const [url, setUrl] = useState(); const [isOpen, setIsOpen] = useState(false); // 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(() => { var _api$userIntent$share, _api$userIntent$share2; if (!isOpen || !(api !== null && api !== void 0 && api.userIntent)) { return; } const 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 () => { var _api$userIntent, _api$userIntent$share3; if (((_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : (_api$userIntent$share3 = _api$userIntent.sharedState.currentState()) === null || _api$userIntent$share3 === void 0 ? void 0 : _api$userIntent$share3.currentUserIntent) === 'overlayOpen') { api.core.actions.execute(api.userIntent.commands.setCurrentUserIntent(previousIntent)); } }; }, [api, isOpen]); useEffect(() => cardPluginEvents.subscribe(event => { const newSmartLinkUrl = getNewSmartLinkUrl(event); if (newSmartLinkUrl) { setUrl(currentUrl => currentUrl !== null && currentUrl !== void 0 ? currentUrl : newSmartLinkUrl); } }), [cardPluginEvents]); const onFinished = useCallback(() => { 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 const PreAuthValuePropositionModalListener = /*#__PURE__*/React.memo(ModalListener);