@sanity/cross-dataset-duplicator 0.3.5 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +5 -2
  2. package/config.dist.json +2 -1
  3. package/lib/actions/DuplicateToAction.js +5 -20
  4. package/lib/actions/DuplicateToAction.js.map +1 -1
  5. package/lib/actions/index.js +2 -11
  6. package/lib/actions/index.js.map +1 -1
  7. package/lib/components/CrossDatasetDuplicator.js +11 -37
  8. package/lib/components/CrossDatasetDuplicator.js.map +1 -1
  9. package/lib/components/DuplicatorQuery.js +13 -35
  10. package/lib/components/DuplicatorQuery.js.map +1 -1
  11. package/lib/components/DuplicatorTool.js +131 -150
  12. package/lib/components/DuplicatorTool.js.map +1 -1
  13. package/lib/components/Feedback.js +2 -6
  14. package/lib/components/Feedback.js.map +1 -1
  15. package/lib/components/ResetSecret.js +0 -9
  16. package/lib/components/ResetSecret.js.map +1 -1
  17. package/lib/components/SelectButtons.js +5 -30
  18. package/lib/components/SelectButtons.js.map +1 -1
  19. package/lib/components/StatusBadge.js +1 -8
  20. package/lib/components/StatusBadge.js.map +1 -1
  21. package/lib/helpers/clientConfig.js.map +1 -1
  22. package/lib/helpers/constants.js.map +1 -1
  23. package/lib/helpers/getDocumentsInArray.js +13 -20
  24. package/lib/helpers/getDocumentsInArray.js.map +1 -1
  25. package/lib/helpers/index.js +0 -3
  26. package/lib/helpers/index.js.map +1 -1
  27. package/lib/index.js +0 -3
  28. package/lib/index.js.map +1 -1
  29. package/lib/tool/index.js +0 -6
  30. package/lib/tool/index.js.map +1 -1
  31. package/package.json +3 -3
  32. package/src/components/CrossDatasetDuplicator.tsx +2 -2
  33. package/src/components/DuplicatorTool.tsx +61 -2
  34. package/src/helpers/getDocumentsInArray.ts +8 -5
@@ -3,82 +3,103 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.DuplicatorToolWrapper = DuplicatorToolWrapper;
6
7
  exports.default = DuplicatorTool;
7
-
8
8
  var _react = _interopRequireWildcard(require("react"));
9
-
10
9
  var _mapLimit = _interopRequireDefault(require("async/mapLimit"));
11
-
12
10
  var _asyncify = _interopRequireDefault(require("async/asyncify"));
13
-
14
11
  var _mutator = require("@sanity/mutator");
15
-
16
12
  var _dset = require("dset");
17
-
18
13
  var _ui = require("@sanity/ui");
19
-
20
14
  var _icons = require("@sanity/icons");
21
-
22
15
  var _client = _interopRequireDefault(require("part:@sanity/base/client"));
23
-
24
16
  var _preview = _interopRequireDefault(require("part:@sanity/base/preview"));
25
-
26
17
  var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
27
-
28
18
  var _configSanity = _interopRequireDefault(require("config:sanity"));
29
-
19
+ var _crossDatasetDuplicator = _interopRequireDefault(require("config:@sanity/cross-dataset-duplicator"));
30
20
  var _helpers = require("../helpers");
31
-
32
21
  var _getDocumentsInArray = require("../helpers/getDocumentsInArray");
33
-
34
22
  var _SelectButtons = _interopRequireDefault(require("./SelectButtons"));
35
-
36
23
  var _StatusBadge = _interopRequireDefault(require("./StatusBadge"));
37
-
38
24
  var _Feedback = _interopRequireDefault(require("./Feedback"));
39
-
40
25
  var _clientConfig = require("../helpers/clientConfig");
41
-
42
26
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
43
-
44
27
  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); }
45
-
46
28
  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; }
47
-
29
+ 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; }
30
+ 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; }
31
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
48
32
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
49
-
50
33
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
51
-
52
34
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
53
-
54
35
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
55
-
56
36
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
57
-
58
37
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
59
-
60
38
  function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
61
-
62
39
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
63
-
64
- 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; }
65
-
66
- 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; }
67
-
68
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
69
-
40
+ function DuplicatorToolWrapper(props) {
41
+ var docs = props.docs,
42
+ token = props.token;
43
+ var _useState = (0, _react.useState)('outbound'),
44
+ _useState2 = _slicedToArray(_useState, 2),
45
+ mode = _useState2[0],
46
+ setMode = _useState2[1];
47
+ var _useState3 = (0, _react.useState)([]),
48
+ _useState4 = _slicedToArray(_useState3, 2),
49
+ inbound = _useState4[0],
50
+ setInbound = _useState4[1];
51
+ var _duplicatorConfig$fol = _crossDatasetDuplicator.default.follow,
52
+ follow = _duplicatorConfig$fol === void 0 ? [] : _duplicatorConfig$fol;
53
+ (0, _react.useEffect)(() => {
54
+ ;
55
+ _asyncToGenerator(function* () {
56
+ var inboundReferences = yield _client.default.fetch('*[references($id)]', {
57
+ id: docs[0]._id
58
+ });
59
+ setInbound([...props.docs, ...inboundReferences]);
60
+ })();
61
+ }, []);
62
+ console.log(docs, inbound);
63
+ return /*#__PURE__*/_react.default.createElement(_ui.Container, null, follow.includes("inbound") || follow.includes("outbound") ? /*#__PURE__*/_react.default.createElement(_ui.Card, {
64
+ paddingX: 4,
65
+ paddingBottom: 4,
66
+ marginBottom: 4,
67
+ borderBottom: true
68
+ }, /*#__PURE__*/_react.default.createElement(_ui.Grid, {
69
+ columns: 2,
70
+ gap: 4
71
+ }, follow.includes("outbound") ? /*#__PURE__*/_react.default.createElement(_ui.Button, {
72
+ mode: "ghost",
73
+ tone: "primary",
74
+ selected: mode === 'outbound',
75
+ onClick: () => setMode('outbound'),
76
+ text: "Outbound"
77
+ }) : null, follow.includes("inbound") ? /*#__PURE__*/_react.default.createElement(_ui.Button, {
78
+ mode: "ghost",
79
+ tone: "primary",
80
+ selected: mode === 'inbound',
81
+ onClick: () => setMode('inbound'),
82
+ disabled: inbound.length === 0,
83
+ text: inbound.length > 0 ? "Inbound (".concat(inbound.length, ")") : 'No inbound references'
84
+ }) : null)) : null, /*#__PURE__*/_react.default.createElement(DuplicatorTool, {
85
+ key: mode,
86
+ docs: mode === 'outbound' ? docs : inbound,
87
+ token: token,
88
+ draftIds: []
89
+ }));
90
+ }
70
91
  function DuplicatorTool(props) {
71
92
  var _config$__experimenta, _destination$title, _spacesOptions$find, _message$tone;
72
-
73
93
  var docs = props.docs,
74
- draftIds = props.draftIds,
75
- token = props.token; // Prepare origin (this Studio) client
94
+ draftIds = props.draftIds,
95
+ token = props.token;
96
+
97
+ // Prepare origin (this Studio) client
76
98
  // In function-scope so it is up to date on every render
99
+ var originClient = _client.default.withConfig(_clientConfig.clientConfig);
77
100
 
78
- var originClient = _client.default.withConfig(_clientConfig.clientConfig); // Create list of dataset options
101
+ // Create list of dataset options
79
102
  // and set initial value of dropdown
80
-
81
-
82
103
  var spacesOptions = _configSanity.default !== null && _configSanity.default !== void 0 && (_config$__experimenta = _configSanity.default.__experimental_spaces) !== null && _config$__experimenta !== void 0 && _config$__experimenta.length ? _configSanity.default.__experimental_spaces.map(space => _objectSpread(_objectSpread({}, space), {}, {
83
104
  api: _objectSpread(_objectSpread({}, space.api), {}, {
84
105
  projectId: space.api.projectId || process.env.SANITY_STUDIO_API_PROJECT_ID
@@ -86,48 +107,41 @@ function DuplicatorTool(props) {
86
107
  usingEnvForProjectId: !space.api.projectId && process.env.SANITY_STUDIO_API_PROJECT_ID,
87
108
  disabled: space.api.dataset === originClient.config().dataset && space.api.projectId === originClient.config().projectId
88
109
  })) : [];
89
-
90
- var _useState = (0, _react.useState)(spacesOptions.length ? spacesOptions.find(space => !space.disabled) : {}),
91
- _useState2 = _slicedToArray(_useState, 2),
92
- destination = _useState2[0],
93
- setDestination = _useState2[1];
94
-
95
- var _useState3 = (0, _react.useState)({}),
96
- _useState4 = _slicedToArray(_useState3, 2),
97
- message = _useState4[0],
98
- setMessage = _useState4[1];
99
-
100
- var _useState5 = (0, _react.useState)(docs.length ? docs.map(item => ({
101
- doc: item,
102
- include: true,
103
- status: null,
104
- hasDraft: draftIds !== null && draftIds !== void 0 && draftIds.length ? draftIds.includes("drafts.".concat(item._id)) : false
105
- })) : []),
106
- _useState6 = _slicedToArray(_useState5, 2),
107
- payload = _useState6[0],
108
- setPayload = _useState6[1];
109
-
110
- var _useState7 = (0, _react.useState)(false),
111
- _useState8 = _slicedToArray(_useState7, 2),
112
- hasReferences = _useState8[0],
113
- setHasReferences = _useState8[1];
114
-
115
- var _useState9 = (0, _react.useState)(false),
116
- _useState10 = _slicedToArray(_useState9, 2),
117
- isDuplicating = _useState10[0],
118
- setIsDuplicating = _useState10[1];
119
-
110
+ var _useState5 = (0, _react.useState)(spacesOptions.length ? spacesOptions.find(space => !space.disabled) : {}),
111
+ _useState6 = _slicedToArray(_useState5, 2),
112
+ destination = _useState6[0],
113
+ setDestination = _useState6[1];
114
+ var _useState7 = (0, _react.useState)({}),
115
+ _useState8 = _slicedToArray(_useState7, 2),
116
+ message = _useState8[0],
117
+ setMessage = _useState8[1];
118
+ var _useState9 = (0, _react.useState)(docs.length ? docs.map(item => ({
119
+ doc: item,
120
+ include: true,
121
+ status: null,
122
+ hasDraft: draftIds !== null && draftIds !== void 0 && draftIds.length ? draftIds.includes("drafts.".concat(item._id)) : false
123
+ })) : []),
124
+ _useState10 = _slicedToArray(_useState9, 2),
125
+ payload = _useState10[0],
126
+ setPayload = _useState10[1];
120
127
  var _useState11 = (0, _react.useState)(false),
121
- _useState12 = _slicedToArray(_useState11, 2),
122
- isGathering = _useState12[0],
123
- setIsGathering = _useState12[1];
124
-
125
- var _useState13 = (0, _react.useState)([0, 0]),
126
- _useState14 = _slicedToArray(_useState13, 2),
127
- progress = _useState14[0],
128
- setProgress = _useState14[1]; // Check for References and update message
129
-
130
-
128
+ _useState12 = _slicedToArray(_useState11, 2),
129
+ hasReferences = _useState12[0],
130
+ setHasReferences = _useState12[1];
131
+ var _useState13 = (0, _react.useState)(false),
132
+ _useState14 = _slicedToArray(_useState13, 2),
133
+ isDuplicating = _useState14[0],
134
+ setIsDuplicating = _useState14[1];
135
+ var _useState15 = (0, _react.useState)(false),
136
+ _useState16 = _slicedToArray(_useState15, 2),
137
+ isGathering = _useState16[0],
138
+ setIsGathering = _useState16[1];
139
+ var _useState17 = (0, _react.useState)([0, 0]),
140
+ _useState18 = _slicedToArray(_useState17, 2),
141
+ progress = _useState18[0],
142
+ setProgress = _useState18[1];
143
+
144
+ // Check for References and update message
131
145
  (0, _react.useEffect)(() => {
132
146
  var expr = ".._ref";
133
147
  var initialRefs = [];
@@ -142,7 +156,6 @@ function DuplicatorTool(props) {
142
156
  setPayload(initialPayload);
143
157
  var docCount = docs.length;
144
158
  var refsCount = initialRefs.length;
145
-
146
159
  if (initialRefs.length) {
147
160
  setHasReferences(true);
148
161
  setMessage({
@@ -150,44 +163,39 @@ function DuplicatorTool(props) {
150
163
  text: (0, _helpers.createInitialMessage)(docCount, refsCount)
151
164
  });
152
165
  }
153
- }, [docs]); // Re-check payload on destination when value changes
154
- // (On initial render + select change)
166
+ }, [docs]);
155
167
 
168
+ // Re-check payload on destination when value changes
169
+ // (On initial render + select change)
156
170
  (0, _react.useEffect)(() => {
157
171
  updatePayloadStatuses();
158
- }, [destination, docs]); // Check if payload documents exist at destination
172
+ }, [destination, docs]);
159
173
 
174
+ // Check if payload documents exist at destination
160
175
  function updatePayloadStatuses() {
161
176
  return _updatePayloadStatuses.apply(this, arguments);
162
177
  }
163
-
164
178
  function _updatePayloadStatuses() {
165
179
  _updatePayloadStatuses = _asyncToGenerator(function* () {
166
180
  var newPayload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
167
181
  var payloadActual = newPayload.length ? newPayload : payload;
168
-
169
182
  if (!payloadActual.length || !(destination !== null && destination !== void 0 && destination.name)) {
170
183
  return;
171
184
  }
172
-
173
- var payloadIds = payloadActual.map(_ref3 => {
174
- var doc = _ref3.doc;
185
+ var payloadIds = payloadActual.map(_ref4 => {
186
+ var doc = _ref4.doc;
175
187
  return doc._id;
176
188
  });
177
-
178
189
  var destinationClient = _client.default.withConfig(_objectSpread(_objectSpread({}, _clientConfig.clientConfig), {}, {
179
190
  dataset: destination.api.dataset,
180
191
  projectId: destination.api.projectId
181
192
  }));
182
-
183
193
  var destinationData = yield destinationClient.fetch("*[_id in $payloadIds]{ _id, _updatedAt }", {
184
194
  payloadIds
185
195
  });
186
196
  var updatedPayload = payloadActual.map(item => {
187
197
  var _item$doc;
188
-
189
198
  var existingDoc = destinationData.find(doc => doc._id === item.doc._id);
190
-
191
199
  if (existingDoc !== null && existingDoc !== void 0 && existingDoc._updatedAt && item !== null && item !== void 0 && (_item$doc = item.doc) !== null && _item$doc !== void 0 && _item$doc._updatedAt) {
192
200
  if (existingDoc._updatedAt === item.doc._updatedAt) {
193
201
  // Exact same document exists at destination
@@ -201,42 +209,38 @@ function DuplicatorTool(props) {
201
209
  } else {
202
210
  item.status = 'CREATE';
203
211
  }
204
-
205
212
  return item;
206
213
  });
207
214
  setPayload(updatedPayload);
208
215
  });
209
216
  return _updatePayloadStatuses.apply(this, arguments);
210
217
  }
211
-
212
218
  function handleCheckbox(_id) {
213
219
  var updatedPayload = payload.map(item => {
214
220
  if (item.doc._id === _id) {
215
221
  item.include = !item.include;
216
222
  }
217
-
218
223
  return item;
219
224
  });
220
225
  setPayload(updatedPayload);
221
- } // Find and recursively follow references beginning with this document
222
-
226
+ }
223
227
 
228
+ // Find and recursively follow references beginning with this document
224
229
  function handleReferences() {
225
230
  return _handleReferences.apply(this, arguments);
226
231
  } // Duplicate payload to destination dataset
227
-
228
-
229
232
  function _handleReferences() {
230
233
  _handleReferences = _asyncToGenerator(function* () {
231
234
  setIsGathering(true);
232
235
  var docIds = docs.map(doc => doc._id);
233
236
  var payloadDocs = yield (0, _getDocumentsInArray.getDocumentsInArray)(docIds, originClient, null);
234
237
  var draftDocs = yield (0, _getDocumentsInArray.getDocumentsInArray)(docIds.map(id => "drafts.".concat(id)), originClient, null, "{_id}");
235
- var draftDocsIds = new Set(draftDocs.map(_ref4 => {
236
- var _id = _ref4._id;
238
+ var draftDocsIds = new Set(draftDocs.map(_ref5 => {
239
+ var _id = _ref5._id;
237
240
  return _id;
238
- })); // Shape it up
241
+ }));
239
242
 
243
+ // Shape it up
240
244
  var payloadShaped = payloadDocs.map(doc => ({
241
245
  doc,
242
246
  // Include this in the transaction?
@@ -252,17 +256,15 @@ function DuplicatorTool(props) {
252
256
  });
253
257
  return _handleReferences.apply(this, arguments);
254
258
  }
255
-
256
259
  function handleDuplicate() {
257
260
  return _handleDuplicate.apply(this, arguments);
258
261
  }
259
-
260
262
  function _handleDuplicate() {
261
263
  _handleDuplicate = _asyncToGenerator(function* () {
262
264
  setIsDuplicating(true);
263
- var assetsCount = payload.filter(_ref5 => {
264
- var doc = _ref5.doc,
265
- include = _ref5.include;
265
+ var assetsCount = payload.filter(_ref6 => {
266
+ var doc = _ref6.doc,
267
+ include = _ref6.include;
266
268
  return include && (0, _helpers.typeIsAsset)(doc._type);
267
269
  }).length;
268
270
  var currentProgress = 0;
@@ -270,27 +272,23 @@ function DuplicatorTool(props) {
270
272
  setMessage({
271
273
  text: 'Duplicating...'
272
274
  });
273
-
274
275
  var destinationClient = _client.default.withConfig(_objectSpread(_objectSpread({}, _clientConfig.clientConfig), {}, {
275
276
  dataset: destination.api.dataset,
276
277
  projectId: destination.api.projectId
277
278
  }));
278
-
279
279
  var transactionDocs = [];
280
- var svgMaps = []; // Upload assets and then add to transaction
280
+ var svgMaps = [];
281
281
 
282
+ // Upload assets and then add to transaction
282
283
  function fetchDoc(_x) {
283
284
  return _fetchDoc.apply(this, arguments);
284
285
  } // Promises are limited to three at once
285
-
286
-
287
286
  function _fetchDoc() {
288
287
  _fetchDoc = _asyncToGenerator(function* (doc) {
289
288
  if ((0, _helpers.typeIsAsset)(doc._type)) {
290
289
  // Download and upload asset
291
290
  // Get the *original* image with this dlRaw param to create the same determenistic _id
292
291
  var uploadType = doc._type.split('.').pop().replace('Asset', '');
293
-
294
292
  var downloadUrl = uploadType === 'image' ? "".concat(doc.url, "?dlRaw=true") : doc.url;
295
293
  var downloadConfig = uploadType === 'image' ? {
296
294
  headers: {
@@ -298,25 +296,24 @@ function DuplicatorTool(props) {
298
296
  }
299
297
  } : {};
300
298
  yield fetch(downloadUrl, downloadConfig).then( /*#__PURE__*/function () {
301
- var _ref6 = _asyncToGenerator(function* (res) {
299
+ var _ref7 = _asyncToGenerator(function* (res) {
302
300
  var assetData = yield res.blob();
303
301
  var options = {
304
302
  filename: doc.originalFilename
305
303
  };
306
- var assetDoc = yield destinationClient.assets.upload(uploadType, assetData, options); // SVG _id's need remapping before transaction
304
+ var assetDoc = yield destinationClient.assets.upload(uploadType, assetData, options);
307
305
 
306
+ // SVG _id's need remapping before transaction
308
307
  if ((doc === null || doc === void 0 ? void 0 : doc.extension) === 'svg') {
309
308
  svgMaps.push({
310
309
  old: doc._id,
311
310
  new: assetDoc._id
312
311
  });
313
312
  }
314
-
315
313
  transactionDocs.push(assetDoc);
316
314
  });
317
-
318
315
  return function (_x2) {
319
- return _ref6.apply(this, arguments);
316
+ return _ref7.apply(this, arguments);
320
317
  };
321
318
  }());
322
319
  currentProgress += 1;
@@ -325,12 +322,10 @@ function DuplicatorTool(props) {
325
322
  });
326
323
  return setProgress([currentProgress, assetsCount]);
327
324
  }
328
-
329
325
  return transactionDocs.push(doc);
330
326
  });
331
327
  return _fetchDoc.apply(this, arguments);
332
328
  }
333
-
334
329
  var result = new Promise((resolve, reject) => {
335
330
  var payloadIncludedDocs = payload.filter(item => item.include).map(item => item.doc);
336
331
  (0, _mapLimit.default)(payloadIncludedDocs, 3, (0, _asyncify.default)(fetchDoc), err => {
@@ -343,34 +338,32 @@ function DuplicatorTool(props) {
343
338
  console.error(err);
344
339
  reject(new Error('Duplication Failed'));
345
340
  }
346
-
347
341
  resolve();
348
342
  });
349
343
  });
350
- yield result; // Remap SVG references to new _id's
344
+ yield result;
351
345
 
346
+ // Remap SVG references to new _id's
352
347
  var transactionDocsMapped = transactionDocs.map(doc => {
353
348
  var expr = ".._ref";
354
349
  var references = (0, _mutator.extractWithPath)(expr, doc);
355
-
356
350
  if (!references.length) {
357
351
  return doc;
358
- } // For every found _ref, search for an SVG asset _id and update
359
-
352
+ }
360
353
 
354
+ // For every found _ref, search for an SVG asset _id and update
361
355
  references.forEach(ref => {
362
356
  var _svgMaps$find;
363
-
364
357
  var newRefValue = (_svgMaps$find = svgMaps.find(asset => asset.old === ref.value)) === null || _svgMaps$find === void 0 ? void 0 : _svgMaps$find.new;
365
-
366
358
  if (newRefValue) {
367
359
  var refPath = ref.path.join('.');
368
360
  (0, _dset.dset)(doc, refPath, newRefValue);
369
361
  }
370
362
  });
371
363
  return doc;
372
- }); // Create transaction
364
+ });
373
365
 
366
+ // Create transaction
374
367
  var transaction = destinationClient.transaction();
375
368
  transactionDocsMapped.forEach(doc => {
376
369
  transaction.createOrReplace(doc);
@@ -392,20 +385,17 @@ function DuplicatorTool(props) {
392
385
  });
393
386
  return _handleDuplicate.apply(this, arguments);
394
387
  }
395
-
396
388
  function handleChange(e) {
397
389
  setDestination(spacesOptions.find(space => space.name === e.currentTarget.value));
398
390
  }
399
-
400
391
  if (!spacesOptions.length) {
401
392
  return /*#__PURE__*/_react.default.createElement(_Feedback.default, {
402
393
  tone: "critical"
403
394
  }, /*#__PURE__*/_react.default.createElement("code", null, "__experimental_spaces"), " not found in ", /*#__PURE__*/_react.default.createElement("code", null, "sanity.json"));
404
395
  }
405
-
406
396
  var payloadCount = payload.length;
407
- var firstSvgIndex = payload.findIndex(_ref => {
408
- var doc = _ref.doc;
397
+ var firstSvgIndex = payload.findIndex(_ref2 => {
398
+ var doc = _ref2.doc;
409
399
  return doc.extension === 'svg';
410
400
  });
411
401
  var selectedDocumentsCount = payload.filter(item => item.include && !(0, _helpers.typeIsAsset)(item.doc._type)).length;
@@ -414,30 +404,23 @@ function DuplicatorTool(props) {
414
404
  var destinationTitle = (_destination$title = destination === null || destination === void 0 ? void 0 : destination.title) !== null && _destination$title !== void 0 ? _destination$title : destination === null || destination === void 0 ? void 0 : destination.name;
415
405
  var hasMultipleProjectIds = new Set(spacesOptions.map(space => {
416
406
  var _space$api;
417
-
418
407
  return space === null || space === void 0 ? void 0 : (_space$api = space.api) === null || _space$api === void 0 ? void 0 : _space$api.projectId;
419
408
  }).filter(Boolean)).size > 1;
420
409
  var headingText = [selectedTotal, "/", payloadCount, "Documents and Assets selected"].join(" ");
421
-
422
410
  var buttonText = _react.default.useMemo(() => {
423
411
  var text = ["Duplicate"];
424
-
425
412
  if (selectedDocumentsCount > 1) {
426
413
  text.push(selectedDocumentsCount, selectedDocumentsCount === 1 ? "Document" : "Documents");
427
414
  }
428
-
429
415
  if (selectedAssetsCount > 1) {
430
416
  text.push("and", selectedAssetsCount, selectedAssetsCount === 1 ? "Asset" : "Assets");
431
417
  }
432
-
433
418
  if (originClient.config().projectId !== destination.api.projectId) {
434
419
  text.push("between Projects");
435
420
  }
436
-
437
421
  text.push("to", destinationTitle);
438
422
  return text.join(" ");
439
423
  }, [selectedDocumentsCount, selectedAssetsCount, destinationTitle]);
440
-
441
424
  return /*#__PURE__*/_react.default.createElement(_ui.Container, {
442
425
  width: 1
443
426
  }, /*#__PURE__*/_react.default.createElement(_ui.Card, null, /*#__PURE__*/_react.default.createElement(_ui.Stack, null, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Card, {
@@ -458,7 +441,6 @@ function DuplicatorTool(props) {
458
441
  value: (_spacesOptions$find = spacesOptions.find(space => space.disabled)) === null || _spacesOptions$find === void 0 ? void 0 : _spacesOptions$find.name
459
442
  }, spacesOptions.filter(space => space.disabled).map(space => {
460
443
  var _space$title;
461
-
462
444
  return /*#__PURE__*/_react.default.createElement("option", {
463
445
  key: space.name,
464
446
  value: space.name,
@@ -479,7 +461,6 @@ function DuplicatorTool(props) {
479
461
  onChange: handleChange
480
462
  }, spacesOptions.map(space => {
481
463
  var _space$title2;
482
-
483
464
  return /*#__PURE__*/_react.default.createElement("option", {
484
465
  key: space.name,
485
466
  value: space.name,
@@ -514,11 +495,11 @@ function DuplicatorTool(props) {
514
495
  }, message.text))), payload.length > 0 && /*#__PURE__*/_react.default.createElement(_ui.Stack, {
515
496
  padding: 4,
516
497
  space: 3
517
- }, payload.map((_ref2, index) => {
518
- var doc = _ref2.doc,
519
- include = _ref2.include,
520
- status = _ref2.status,
521
- hasDraft = _ref2.hasDraft;
498
+ }, payload.map((_ref3, index) => {
499
+ var doc = _ref3.doc,
500
+ include = _ref3.include,
501
+ status = _ref3.status,
502
+ hasDraft = _ref3.hasDraft;
522
503
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, {
523
504
  key: doc._id
524
505
  }, /*#__PURE__*/_react.default.createElement(_ui.Flex, {