@gechiui/nux
Version:
NUX (New User eXperience) module for GeChiUI.
95 lines (79 loc) • 2.55 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.areTipsEnabled = areTipsEnabled;
exports.getAssociatedGuide = void 0;
exports.isTipVisible = isTipVisible;
var _rememo = _interopRequireDefault(require("rememo"));
var _lodash = require("lodash");
/**
* External dependencies
*/
/**
* An object containing information about a guide.
*
* @typedef {Object} NUXGuideInfo
* @property {string[]} tipIds Which tips the guide contains.
* @property {?string} currentTipId The guide's currently showing tip.
* @property {?string} nextTipId The guide's next tip to show.
*/
/**
* Returns an object describing the guide, if any, that the given tip is a part
* of.
*
* @param {Object} state Global application state.
* @param {string} tipId The tip to query.
*
* @return {?NUXGuideInfo} Information about the associated guide.
*/
const getAssociatedGuide = (0, _rememo.default)((state, tipId) => {
for (const tipIds of state.guides) {
if ((0, _lodash.includes)(tipIds, tipId)) {
const nonDismissedTips = (0, _lodash.difference)(tipIds, (0, _lodash.keys)(state.preferences.dismissedTips));
const [currentTipId = null, nextTipId = null] = nonDismissedTips;
return {
tipIds,
currentTipId,
nextTipId
};
}
}
return null;
}, state => [state.guides, state.preferences.dismissedTips]);
/**
* Determines whether or not the given tip is showing. Tips are hidden if they
* are disabled, have been dismissed, or are not the current tip in any
* guide that they have been added to.
*
* @param {Object} state Global application state.
* @param {string} tipId The tip to query.
*
* @return {boolean} Whether or not the given tip is showing.
*/
exports.getAssociatedGuide = getAssociatedGuide;
function isTipVisible(state, tipId) {
if (!state.preferences.areTipsEnabled) {
return false;
}
if ((0, _lodash.has)(state.preferences.dismissedTips, [tipId])) {
return false;
}
const associatedGuide = getAssociatedGuide(state, tipId);
if (associatedGuide && associatedGuide.currentTipId !== tipId) {
return false;
}
return true;
}
/**
* Returns whether or not tips are globally enabled.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether tips are globally enabled.
*/
function areTipsEnabled(state) {
return state.preferences.areTipsEnabled;
}
//# sourceMappingURL=selectors.js.map