@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
67 lines (66 loc) • 3.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DocumentOperationResults = void 0;
var _reactHooks = require("@sanity/react-hooks");
var _ui = require("@sanity/ui");
var _react = _interopRequireWildcard(require("react"));
var _useDocumentPane2 = require("./useDocumentPane");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function getOpErrorTitle(op) {
if (op === 'delete') {
return "An error occurred while attempting to delete this document.\n This usually means that there are other documents that refers to it.";
}
if (op === 'unpublish') {
return "An error occurred while attempting to unpublish this document.\n This usually means that there are other documents that refers to it.";
}
return "An error occurred during ".concat(op);
}
function getOpSuccessTitle(op) {
if (op === 'publish') {
return "The document was published";
}
if (op === 'unpublish') {
return "The document was unpublished. A draft has been created from the latest published version.";
}
if (op === 'discardChanges') {
return "All changes since last publish has now been discarded. The discarded draft can still be recovered from history";
}
if (op === 'delete') {
return "This document was deleted. It can still be recovered from history and if you continue editing it will be recreated.";
}
return "Successfully performed ".concat(op, " on document");
}
var IGNORE_OPS = ['patch', 'commit'];
var DocumentOperationResults = /*#__PURE__*/(0, _react.memo)(function DocumentOperationResults() {
var _useToast = (0, _ui.useToast)(),
pushToast = _useToast.push;
var _useDocumentPane = (0, _useDocumentPane2.useDocumentPane)(),
documentId = _useDocumentPane.documentId,
documentType = _useDocumentPane.documentType;
var event = (0, _reactHooks.useDocumentOperationEvent)(documentId, documentType);
(0, _react.useEffect)(() => {
if (!event) return;
if (event.type === 'error') {
pushToast({
closable: true,
duration: 30000,
// 30s
status: 'error',
title: getOpErrorTitle(event.op),
description: /*#__PURE__*/_react.default.createElement("details", null, /*#__PURE__*/_react.default.createElement("summary", null, "Details"), event.error.message)
});
}
if (event.type === 'success' && !IGNORE_OPS.includes(event.op)) {
pushToast({
closable: true,
status: 'success',
title: getOpSuccessTitle(event.op)
});
}
}, [event, pushToast]);
return null;
});
exports.DocumentOperationResults = DocumentOperationResults;