UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

47 lines 3.14 kB
import { useEffect, useMemo, useState } from 'react'; import { isLocalStorageKeyDiscovered, LOCAL_STORAGE_DISCOVERY_KEY_TOOLBAR } from '../local-storage'; const useLinkUpgradeDiscoverability = ({ url, linkPosition, cardContext, pluginInjectionApi, isPulseEnabled, isOverlayEnabled }) => { var _cardContext$store, _pluginInjectionApi$c, _pluginInjectionApi$c2; const [urlState, setUrlState] = useState(cardContext === null || cardContext === void 0 ? void 0 : (_cardContext$store = cardContext.store) === null || _cardContext$store === void 0 ? void 0 : _cardContext$store.getState()[url]); const { overlayCandidatePosition, inlineCardAwarenessCandidatePosition } = (pluginInjectionApi === null || pluginInjectionApi === void 0 ? void 0 : (_pluginInjectionApi$c = pluginInjectionApi.card) === null || _pluginInjectionApi$c === void 0 ? void 0 : (_pluginInjectionApi$c2 = _pluginInjectionApi$c.sharedState) === null || _pluginInjectionApi$c2 === void 0 ? void 0 : _pluginInjectionApi$c2.currentState()) || {}; useEffect(() => { var _cardContext$store2; const unsubscribe = cardContext === null || cardContext === void 0 ? void 0 : (_cardContext$store2 = cardContext.store) === null || _cardContext$store2 === void 0 ? void 0 : _cardContext$store2.subscribe(() => { var _cardContext$store3; setUrlState(cardContext === null || cardContext === void 0 ? void 0 : (_cardContext$store3 = cardContext.store) === null || _cardContext$store3 === void 0 ? void 0 : _cardContext$store3.getState()[url]); }); return () => { if (unsubscribe) { unsubscribe(); } }; }, [cardContext === null || cardContext === void 0 ? void 0 : cardContext.store, url]); const canBeUpgradedToEmbed = useMemo(() => { var _cardContext$extracto; const isResolved = (urlState === null || urlState === void 0 ? void 0 : urlState.status) === 'resolved'; return isResolved && !!(cardContext !== null && cardContext !== void 0 && (_cardContext$extracto = cardContext.extractors) !== null && _cardContext$extracto !== void 0 && _cardContext$extracto.getPreview(url, 'web')); }, [cardContext === null || cardContext === void 0 ? void 0 : cardContext.extractors, url, urlState === null || urlState === void 0 ? void 0 : urlState.status]); const shouldShowLinkPulse = useMemo(() => { return isPulseEnabled && linkPosition === inlineCardAwarenessCandidatePosition && canBeUpgradedToEmbed; }, [canBeUpgradedToEmbed, isPulseEnabled, linkPosition, inlineCardAwarenessCandidatePosition]); const shouldShowLinkOverlay = (urlState === null || urlState === void 0 ? void 0 : urlState.status) === 'resolved' && isOverlayEnabled; const isLinkMostRecentlyInserted = overlayCandidatePosition === linkPosition; const shouldShowToolbarPulse = useMemo(() => isPulseEnabled && !isLocalStorageKeyDiscovered(LOCAL_STORAGE_DISCOVERY_KEY_TOOLBAR) && canBeUpgradedToEmbed, [canBeUpgradedToEmbed, isPulseEnabled]); return { shouldShowToolbarPulse, shouldShowLinkPulse, shouldShowLinkOverlay, isLinkMostRecentlyInserted }; }; export default useLinkUpgradeDiscoverability;