@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
51 lines • 3.62 kB
JavaScript
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import { useEffect, useMemo, useState } from 'react';
import { isLocalStorageKeyDiscovered, LOCAL_STORAGE_DISCOVERY_KEY_TOOLBAR } from '../local-storage';
var useLinkUpgradeDiscoverability = function useLinkUpgradeDiscoverability(_ref) {
var _cardContext$store, _pluginInjectionApi$c;
var url = _ref.url,
linkPosition = _ref.linkPosition,
cardContext = _ref.cardContext,
pluginInjectionApi = _ref.pluginInjectionApi,
isPulseEnabled = _ref.isPulseEnabled,
isOverlayEnabled = _ref.isOverlayEnabled;
var _useState = useState(cardContext === null || cardContext === void 0 || (_cardContext$store = cardContext.store) === null || _cardContext$store === void 0 ? void 0 : _cardContext$store.getState()[url]),
_useState2 = _slicedToArray(_useState, 2),
urlState = _useState2[0],
setUrlState = _useState2[1];
var _ref2 = (pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$c = pluginInjectionApi.card) === null || _pluginInjectionApi$c === void 0 || (_pluginInjectionApi$c = _pluginInjectionApi$c.sharedState) === null || _pluginInjectionApi$c === void 0 ? void 0 : _pluginInjectionApi$c.currentState()) || {},
overlayCandidatePosition = _ref2.overlayCandidatePosition,
inlineCardAwarenessCandidatePosition = _ref2.inlineCardAwarenessCandidatePosition;
useEffect(function () {
var _cardContext$store2;
var unsubscribe = cardContext === null || cardContext === void 0 || (_cardContext$store2 = cardContext.store) === null || _cardContext$store2 === void 0 ? void 0 : _cardContext$store2.subscribe(function () {
var _cardContext$store3;
setUrlState(cardContext === null || cardContext === void 0 || (_cardContext$store3 = cardContext.store) === null || _cardContext$store3 === void 0 ? void 0 : _cardContext$store3.getState()[url]);
});
return function () {
if (unsubscribe) {
unsubscribe();
}
};
}, [cardContext === null || cardContext === void 0 ? void 0 : cardContext.store, url]);
var canBeUpgradedToEmbed = useMemo(function () {
var _cardContext$extracto;
var 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]);
var shouldShowLinkPulse = useMemo(function () {
return isPulseEnabled && linkPosition === inlineCardAwarenessCandidatePosition && canBeUpgradedToEmbed;
}, [canBeUpgradedToEmbed, isPulseEnabled, linkPosition, inlineCardAwarenessCandidatePosition]);
var shouldShowLinkOverlay = (urlState === null || urlState === void 0 ? void 0 : urlState.status) === 'resolved' && isOverlayEnabled;
var isLinkMostRecentlyInserted = overlayCandidatePosition === linkPosition;
var shouldShowToolbarPulse = useMemo(function () {
return isPulseEnabled && !isLocalStorageKeyDiscovered(LOCAL_STORAGE_DISCOVERY_KEY_TOOLBAR) && canBeUpgradedToEmbed;
}, [canBeUpgradedToEmbed, isPulseEnabled]);
return {
shouldShowToolbarPulse: shouldShowToolbarPulse,
shouldShowLinkPulse: shouldShowLinkPulse,
shouldShowLinkOverlay: shouldShowLinkOverlay,
isLinkMostRecentlyInserted: isLinkMostRecentlyInserted
};
};
export default useLinkUpgradeDiscoverability;