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.esm.js CHANGED
@@ -687,9 +687,9 @@ function useWorkflowDocuments(schemaTypes) {
687
687
  state: newStateId,
688
688
  orderRank: newOrder
689
689
  }).commit().then(res => {
690
- var _a;
690
+ var _a, _b;
691
691
  toast.push({
692
- title: "Moved to \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\""),
692
+ 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, "\""),
693
693
  status: "success"
694
694
  });
695
695
  return res;
@@ -1186,7 +1186,7 @@ function DocumentList(props) {
1186
1186
  states
1187
1187
  })
1188
1188
  })
1189
- }, documentId);
1189
+ }, virtualItem.key);
1190
1190
  })
1191
1191
  });
1192
1192
  }
@@ -1390,6 +1390,40 @@ function StateTitle(props) {
1390
1390
  })
1391
1391
  });
1392
1392
  }
1393
+ function generateMiddleValue(ranks) {
1394
+ if (!ranks.some(rank => !rank)) {
1395
+ return ranks;
1396
+ }
1397
+ const firstUndefined = ranks.findIndex(rank => !rank);
1398
+ const firstDefinedAfter = ranks.findIndex((rank, index) => rank && index > firstUndefined);
1399
+ const firstDefinedBefore = ranks.findLastIndex((rank, index) => rank && index < firstUndefined);
1400
+ if (firstDefinedAfter === -1 || firstDefinedBefore === -1) {
1401
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1402
+ }
1403
+ const beforeRank = ranks[firstDefinedBefore];
1404
+ const afterRank = ranks[firstDefinedAfter];
1405
+ if (!beforeRank || typeof beforeRank === "undefined" || !afterRank || typeof afterRank === "undefined") {
1406
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1407
+ }
1408
+ const between = beforeRank.between(afterRank);
1409
+ const middle = Math.floor((firstDefinedAfter + firstDefinedBefore) / 2);
1410
+ if (ranks[middle]) {
1411
+ throw new Error("Should not have overwritten value at index ".concat(middle));
1412
+ }
1413
+ ranks[middle] = between;
1414
+ return ranks;
1415
+ }
1416
+ function generateMultipleOrderRanks(count, start, end) {
1417
+ let ranks = [...Array(count)];
1418
+ const rankStart = start != null ? start : LexoRank.min().genNext().genNext();
1419
+ const rankEnd = end != null ? end : LexoRank.max().genPrev().genPrev();
1420
+ ranks[0] = rankStart;
1421
+ ranks[count - 1] = rankEnd;
1422
+ for (let i = 0; i < count; i++) {
1423
+ ranks = generateMiddleValue(ranks);
1424
+ }
1425
+ return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1426
+ }
1393
1427
  const StyledFloatingCard = styled(Card)(() => css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: fixed;\n bottom: 0;\n left: 0;\n z-index: 1000;\n "]))));
1394
1428
  function FloatingCard(_ref3) {
1395
1429
  let {
@@ -1439,7 +1473,7 @@ function Verify(props) {
1439
1473
  const stateExists = states.find(s => s.id === state);
1440
1474
  return !stateExists && documentId ? [...acc, documentId] : acc;
1441
1475
  }, []) : [];
1442
- const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1476
+ const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) && (userList == null ? void 0 : userList.length) ? data.reduce((acc, cur) => {
1443
1477
  var _a;
1444
1478
  const {
1445
1479
  documentId,
@@ -1522,16 +1556,34 @@ function Verify(props) {
1522
1556
  var _a;
1523
1557
  return (_a = d._metadata) == null ? void 0 : _a.orderRank;
1524
1558
  });
1525
- const minLexo = firstOrder && data.length !== ids.length ? LexoRank.parse(firstOrder) : LexoRank.min();
1526
- const maxLexo = secondOrder && data.length !== ids.length ? LexoRank.parse(secondOrder) : LexoRank.max();
1527
- let newLexo = minLexo.between(maxLexo);
1528
- const lastLexo = maxLexo;
1559
+ const minLexo = firstOrder ? LexoRank.parse(firstOrder) : void 0;
1560
+ const maxLexo = secondOrder ? LexoRank.parse(secondOrder) : void 0;
1561
+ const ranks = generateMultipleOrderRanks(ids.length, minLexo, maxLexo);
1562
+ const tx = client.transaction();
1563
+ for (let index = 0; index < ids.length; index += 1) {
1564
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1565
+ set: {
1566
+ orderRank: ranks[index].toString()
1567
+ }
1568
+ });
1569
+ }
1570
+ await tx.commit();
1571
+ toast.push({
1572
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1573
+ status: "success"
1574
+ });
1575
+ }, [data, client, toast]);
1576
+ const resetOrderOfAllDocuments = React.useCallback(async ids => {
1577
+ toast.push({
1578
+ title: "Adding ordering...",
1579
+ status: "info"
1580
+ });
1581
+ const ranks = generateMultipleOrderRanks(ids.length);
1529
1582
  const tx = client.transaction();
1530
1583
  for (let index = 0; index < ids.length; index += 1) {
1531
- newLexo = newLexo.between(lastLexo);
1532
1584
  tx.patch("workflow-metadata.".concat(ids[index]), {
1533
1585
  set: {
1534
- orderRank: newLexo.toString()
1586
+ orderRank: ranks[index].toString()
1535
1587
  }
1536
1588
  });
1537
1589
  }
@@ -1562,24 +1614,39 @@ function Verify(props) {
1562
1614
  return /* @__PURE__ */jsxs(FloatingCard, {
1563
1615
  children: [documentsWithoutValidMetadataIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1564
1616
  tone: "caution",
1617
+ mode: "ghost",
1565
1618
  onClick: () => correctDocuments(documentsWithoutValidMetadataIds),
1566
1619
  text: documentsWithoutValidMetadataIds.length === 1 ? "Correct 1 Document State" : "Correct ".concat(documentsWithoutValidMetadataIds.length, " Document States")
1567
1620
  }) : null, documentsWithInvalidUserIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1568
1621
  tone: "caution",
1622
+ mode: "ghost",
1569
1623
  onClick: () => removeUsersFromDocuments(documentsWithInvalidUserIds),
1570
1624
  text: documentsWithInvalidUserIds.length === 1 ? "Remove Invalid Users from 1 Document" : "Remove Invalid Users from ".concat(documentsWithInvalidUserIds.length, " Documents")
1571
1625
  }) : null, documentsWithoutOrderIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1572
1626
  tone: "caution",
1627
+ mode: "ghost",
1573
1628
  onClick: () => addOrderToDocuments(documentsWithoutOrderIds),
1574
1629
  text: documentsWithoutOrderIds.length === 1 ? "Set Order for 1 Document" : "Set Order for ".concat(documentsWithoutOrderIds.length, " Documents")
1575
- }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1576
- tone: "caution",
1577
- onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1578
- text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1630
+ }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxs(Fragment, {
1631
+ children: [/* @__PURE__ */jsx(Button, {
1632
+ tone: "caution",
1633
+ mode: "ghost",
1634
+ onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1635
+ text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1636
+ }), /* @__PURE__ */jsx(Button, {
1637
+ tone: "caution",
1638
+ mode: "ghost",
1639
+ onClick: () => resetOrderOfAllDocuments(data.map(doc => {
1640
+ var _a;
1641
+ return String((_a = doc._metadata) == null ? void 0 : _a.documentId);
1642
+ })),
1643
+ text: data.length === 1 ? "Reset Order for 1 Document" : "Reset Order for all ".concat(data.length, " Documents")
1644
+ })]
1579
1645
  }) : null, orphanedMetadataDocumentIds.length > 0 ? /* @__PURE__ */jsx(Button, {
1580
1646
  text: "Cleanup orphaned metadata",
1581
1647
  onClick: handleOrphans,
1582
- tone: "caution"
1648
+ tone: "caution",
1649
+ mode: "ghost"
1583
1650
  }) : null]
1584
1651
  });
1585
1652
  }