sanity-plugin-workflow 1.0.0-beta.7 → 1.0.0-beta.8

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.
package/lib/index.js CHANGED
@@ -700,9 +700,9 @@ function useWorkflowDocuments(schemaTypes) {
700
700
  state: newStateId,
701
701
  orderRank: newOrder
702
702
  }).commit().then(res => {
703
- var _a;
703
+ var _a, _b;
704
704
  toast.push({
705
- title: "Moved to \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\""),
705
+ title: newState.id === document._metadata.state ? "Reordered in \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\"") : "Moved to \"".concat((_b = newState == null ? void 0 : newState.title) != null ? _b : newStateId, "\""),
706
706
  status: "success"
707
707
  });
708
708
  return res;
@@ -1199,7 +1199,7 @@ function DocumentList(props) {
1199
1199
  states
1200
1200
  })
1201
1201
  })
1202
- }, documentId);
1202
+ }, virtualItem.key);
1203
1203
  })
1204
1204
  });
1205
1205
  }
@@ -1403,6 +1403,40 @@ function StateTitle(props) {
1403
1403
  })
1404
1404
  });
1405
1405
  }
1406
+ function generateMiddleValue(ranks) {
1407
+ if (!ranks.some(rank => !rank)) {
1408
+ return ranks;
1409
+ }
1410
+ const firstUndefined = ranks.findIndex(rank => !rank);
1411
+ const firstDefinedAfter = ranks.findIndex((rank, index) => rank && index > firstUndefined);
1412
+ const firstDefinedBefore = ranks.findLastIndex((rank, index) => rank && index < firstUndefined);
1413
+ if (firstDefinedAfter === -1 || firstDefinedBefore === -1) {
1414
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1415
+ }
1416
+ const beforeRank = ranks[firstDefinedBefore];
1417
+ const afterRank = ranks[firstDefinedAfter];
1418
+ if (!beforeRank || typeof beforeRank === "undefined" || !afterRank || typeof afterRank === "undefined") {
1419
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1420
+ }
1421
+ const between = beforeRank.between(afterRank);
1422
+ const middle = Math.floor((firstDefinedAfter + firstDefinedBefore) / 2);
1423
+ if (ranks[middle]) {
1424
+ throw new Error("Should not have overwritten value at index ".concat(middle));
1425
+ }
1426
+ ranks[middle] = between;
1427
+ return ranks;
1428
+ }
1429
+ function generateMultipleOrderRanks(count, start, end) {
1430
+ let ranks = [...Array(count)];
1431
+ const rankStart = start != null ? start : lexorank.LexoRank.min().genNext().genNext();
1432
+ const rankEnd = end != null ? end : lexorank.LexoRank.max().genPrev().genPrev();
1433
+ ranks[0] = rankStart;
1434
+ ranks[count - 1] = rankEnd;
1435
+ for (let i = 0; i < count; i++) {
1436
+ ranks = generateMiddleValue(ranks);
1437
+ }
1438
+ return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1439
+ }
1406
1440
  const StyledFloatingCard = styled__default.default(ui.Card)(() => styled.css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: fixed;\n bottom: 0;\n left: 0;\n z-index: 1000;\n "]))));
1407
1441
  function FloatingCard(_ref3) {
1408
1442
  let {
@@ -1452,7 +1486,7 @@ function Verify(props) {
1452
1486
  const stateExists = states.find(s => s.id === state);
1453
1487
  return !stateExists && documentId ? [...acc, documentId] : acc;
1454
1488
  }, []) : [];
1455
- const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1489
+ const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) && (userList == null ? void 0 : userList.length) ? data.reduce((acc, cur) => {
1456
1490
  var _a;
1457
1491
  const {
1458
1492
  documentId,
@@ -1535,16 +1569,34 @@ function Verify(props) {
1535
1569
  var _a;
1536
1570
  return (_a = d._metadata) == null ? void 0 : _a.orderRank;
1537
1571
  });
1538
- const minLexo = firstOrder && data.length !== ids.length ? lexorank.LexoRank.parse(firstOrder) : lexorank.LexoRank.min();
1539
- const maxLexo = secondOrder && data.length !== ids.length ? lexorank.LexoRank.parse(secondOrder) : lexorank.LexoRank.max();
1540
- let newLexo = minLexo.between(maxLexo);
1541
- const lastLexo = maxLexo;
1572
+ const minLexo = firstOrder ? lexorank.LexoRank.parse(firstOrder) : void 0;
1573
+ const maxLexo = secondOrder ? lexorank.LexoRank.parse(secondOrder) : void 0;
1574
+ const ranks = generateMultipleOrderRanks(ids.length, minLexo, maxLexo);
1575
+ const tx = client.transaction();
1576
+ for (let index = 0; index < ids.length; index += 1) {
1577
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1578
+ set: {
1579
+ orderRank: ranks[index].toString()
1580
+ }
1581
+ });
1582
+ }
1583
+ await tx.commit();
1584
+ toast.push({
1585
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1586
+ status: "success"
1587
+ });
1588
+ }, [data, client, toast]);
1589
+ const resetOrderOfAllDocuments = React__default.default.useCallback(async ids => {
1590
+ toast.push({
1591
+ title: "Adding ordering...",
1592
+ status: "info"
1593
+ });
1594
+ const ranks = generateMultipleOrderRanks(ids.length);
1542
1595
  const tx = client.transaction();
1543
1596
  for (let index = 0; index < ids.length; index += 1) {
1544
- newLexo = newLexo.between(lastLexo);
1545
1597
  tx.patch("workflow-metadata.".concat(ids[index]), {
1546
1598
  set: {
1547
- orderRank: newLexo.toString()
1599
+ orderRank: ranks[index].toString()
1548
1600
  }
1549
1601
  });
1550
1602
  }
@@ -1575,24 +1627,39 @@ function Verify(props) {
1575
1627
  return /* @__PURE__ */jsxRuntime.jsxs(FloatingCard, {
1576
1628
  children: [documentsWithoutValidMetadataIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1577
1629
  tone: "caution",
1630
+ mode: "ghost",
1578
1631
  onClick: () => correctDocuments(documentsWithoutValidMetadataIds),
1579
1632
  text: documentsWithoutValidMetadataIds.length === 1 ? "Correct 1 Document State" : "Correct ".concat(documentsWithoutValidMetadataIds.length, " Document States")
1580
1633
  }) : null, documentsWithInvalidUserIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1581
1634
  tone: "caution",
1635
+ mode: "ghost",
1582
1636
  onClick: () => removeUsersFromDocuments(documentsWithInvalidUserIds),
1583
1637
  text: documentsWithInvalidUserIds.length === 1 ? "Remove Invalid Users from 1 Document" : "Remove Invalid Users from ".concat(documentsWithInvalidUserIds.length, " Documents")
1584
1638
  }) : null, documentsWithoutOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1585
1639
  tone: "caution",
1640
+ mode: "ghost",
1586
1641
  onClick: () => addOrderToDocuments(documentsWithoutOrderIds),
1587
1642
  text: documentsWithoutOrderIds.length === 1 ? "Set Order for 1 Document" : "Set Order for ".concat(documentsWithoutOrderIds.length, " Documents")
1588
- }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1589
- tone: "caution",
1590
- onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1591
- text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1643
+ }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1644
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1645
+ tone: "caution",
1646
+ mode: "ghost",
1647
+ onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1648
+ text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1649
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1650
+ tone: "caution",
1651
+ mode: "ghost",
1652
+ onClick: () => resetOrderOfAllDocuments(data.map(doc => {
1653
+ var _a;
1654
+ return String((_a = doc._metadata) == null ? void 0 : _a.documentId);
1655
+ })),
1656
+ text: data.length === 1 ? "Reset Order for 1 Document" : "Reset Order for all ".concat(data.length, " Documents")
1657
+ })]
1592
1658
  }) : null, orphanedMetadataDocumentIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1593
1659
  text: "Cleanup orphaned metadata",
1594
1660
  onClick: handleOrphans,
1595
- tone: "caution"
1661
+ tone: "caution",
1662
+ mode: "ghost"
1596
1663
  }) : null]
1597
1664
  });
1598
1665
  }