UNPKG

@sanity/desk-tool

Version:

Tool for managing all sorts of content in a structured manner

47 lines (44 loc) 3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useConditionalToast = useConditionalToast; var _react = require("react"); var _ui = require("@sanity/ui"); function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function usePrevious(value) { var ref = (0, _react.useRef)(); (0, _react.useEffect)(() => { ref.current = value; }, [value]); return ref.current; } // https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value var LONG_ENOUGH_BUT_NOT_TOO_LONG = 1000 * 60 * 60 * 24 * 24; /** * Workaround to support conditional toast (e.g. a toast that is visible as long as a condition holds true) * @param params */ function useConditionalToast(params) { var toast = (0, _ui.useToast)(); var wasEnabled = usePrevious(params.enabled); // note1: there's a `duration || 0` in Sanity UI's pushToast(), so make it non-falsey // note2: cannot use `Infinity` as duration, since it exceeds setTimeout's maximum delay value (0, _react.useEffect)(() => { if (!wasEnabled && params.enabled) { toast.push(_objectSpread(_objectSpread({}, params), {}, { duration: LONG_ENOUGH_BUT_NOT_TOO_LONG })); } if (wasEnabled && !params.enabled) { toast.push(_objectSpread(_objectSpread({}, params), {}, { // Note: @sanity/ui fallbacks to the default duration of 4s in case of falsey values duration: 0.01 })); } }, [params, toast, wasEnabled]); }