@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
58 lines (57 loc) • 3.88 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _localStorage = require("../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 = (0, _react.useState)(cardContext === null || cardContext === void 0 || (_cardContext$store = cardContext.store) === null || _cardContext$store === void 0 ? void 0 : _cardContext$store.getState()[url]),
_useState2 = (0, _slicedToArray2.default)(_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;
(0, _react.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 = (0, _react.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 = (0, _react.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 = (0, _react.useMemo)(function () {
return isPulseEnabled && !(0, _localStorage.isLocalStorageKeyDiscovered)(_localStorage.LOCAL_STORAGE_DISCOVERY_KEY_TOOLBAR) && canBeUpgradedToEmbed;
}, [canBeUpgradedToEmbed, isPulseEnabled]);
return {
shouldShowToolbarPulse: shouldShowToolbarPulse,
shouldShowLinkPulse: shouldShowLinkPulse,
shouldShowLinkOverlay: shouldShowLinkOverlay,
isLinkMostRecentlyInserted: isLinkMostRecentlyInserted
};
};
var _default = exports.default = useLinkUpgradeDiscoverability;