sanity-plugin-workflow 1.0.0-beta.1 → 1.0.0-beta.10

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 (56) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +81 -13
  3. package/lib/{src/index.d.ts → index.d.ts} +4 -3
  4. package/lib/index.esm.js +2106 -1
  5. package/lib/index.esm.js.map +1 -1
  6. package/lib/index.js +2119 -1
  7. package/lib/index.js.map +1 -1
  8. package/package.json +51 -40
  9. package/src/actions/AssignWorkflow.tsx +49 -0
  10. package/src/actions/BeginWorkflow.tsx +63 -0
  11. package/src/actions/CompleteWorkflow.tsx +41 -0
  12. package/src/actions/UpdateWorkflow.tsx +126 -0
  13. package/src/badges/AssigneesBadge.tsx +53 -0
  14. package/src/badges/StateBadge.tsx +28 -0
  15. package/src/components/DocumentCard/AvatarGroup.tsx +12 -8
  16. package/src/components/DocumentCard/CompleteButton.tsx +68 -0
  17. package/src/components/DocumentCard/EditButton.tsx +3 -2
  18. package/src/components/DocumentCard/Field.tsx +38 -0
  19. package/src/components/DocumentCard/Validate.tsx +21 -0
  20. package/src/components/DocumentCard/ValidationStatus.tsx +37 -0
  21. package/src/components/DocumentCard/core/DraftStatus.tsx +32 -0
  22. package/src/components/DocumentCard/core/PublishedStatus.tsx +39 -0
  23. package/src/components/DocumentCard/core/TimeAgo.tsx +11 -0
  24. package/src/components/DocumentCard/index.tsx +177 -68
  25. package/src/components/DocumentList.tsx +169 -0
  26. package/src/components/Filters.tsx +168 -0
  27. package/src/components/FloatingCard.tsx +29 -0
  28. package/src/components/StateTitle/Status.tsx +27 -0
  29. package/src/components/StateTitle/index.tsx +78 -0
  30. package/src/components/UserAssignment.tsx +57 -75
  31. package/src/components/UserAssignmentInput.tsx +27 -0
  32. package/src/components/UserDisplay.tsx +57 -0
  33. package/src/components/Verify.tsx +297 -0
  34. package/src/components/WorkflowContext.tsx +71 -0
  35. package/src/components/WorkflowSignal.tsx +30 -0
  36. package/src/components/WorkflowTool.tsx +373 -162
  37. package/src/constants/index.ts +31 -0
  38. package/src/helpers/arraysContainMatchingString.ts +6 -0
  39. package/src/helpers/filterItemsAndSort.ts +41 -0
  40. package/src/helpers/generateMultipleOrderRanks.ts +80 -0
  41. package/src/helpers/initialRank.ts +13 -0
  42. package/src/hooks/useWorkflowDocuments.tsx +76 -78
  43. package/src/hooks/useWorkflowMetadata.tsx +31 -26
  44. package/src/index.ts +60 -57
  45. package/src/schema/workflow/workflow.metadata.ts +68 -0
  46. package/src/tools/index.ts +15 -0
  47. package/src/types/index.ts +27 -6
  48. package/src/actions/DemoteAction.tsx +0 -62
  49. package/src/actions/PromoteAction.tsx +0 -62
  50. package/src/actions/RequestReviewAction.js +0 -61
  51. package/src/actions/index.js +0 -21
  52. package/src/badges/index.tsx +0 -31
  53. package/src/components/Mutate.tsx +0 -54
  54. package/src/components/StateTimeline.tsx +0 -98
  55. package/src/components/UserSelectInput.tsx +0 -43
  56. package/src/schema/workflow/metadata.ts +0 -38
package/lib/index.js CHANGED
@@ -1 +1,2119 @@
1
- "use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function t(t){for(var r=1;r<arguments.length;r++){var o=null!=arguments[r]?arguments[r]:{};r%2?e(Object(o),!0).forEach((function(e){n(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function n(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}Object.defineProperty(exports,"__esModule",{value:!0});var r=require("sanity"),o=require("@sanity/icons"),i=require("react/jsx-runtime"),a=require("react"),s=require("@sanity/ui"),l=require("sanity-plugin-utils"),d=require("react-beautiful-dnd"),c=require("sanity/router");function u(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var p=u(a);function m(e){const{id:t,type:n}=e,{navigateIntent:r}=c.useRouter();return i.jsx(s.Button,{onClick:()=>r("edit",{id:t,type:n}),mode:"ghost",fontSize:1,padding:2,tabIndex:-1,icon:o.EditIcon,text:"Edit"})}function f(e){const{users:t,max:n=3}=e,o=null==t?void 0:t.length,a=p.default.useMemo((()=>t.slice(0,n)),[t]);return(null==t?void 0:t.length)?i.jsxs(s.Flex,{align:"center",children:[a.map((e=>i.jsx(s.Box,{style:{marginRight:-5},children:i.jsx(r.UserAvatar,{user:e})},e.id))),o>n&&i.jsx(s.Box,{paddingLeft:2,children:i.jsxs(s.Text,{size:1,children:["+",o-n]})})]}):null}function h(e){const{assignees:t,userList:n,documentId:a}=e,d=r.useClient(),c=s.useToast(),[u,m]=p.default.useState(""),h=p.default.useCallback((e=>{if(!e)return c.push({status:"error",title:"No user selected"});d.patch("workflow-metadata.".concat(a)).setIfMissing({assignees:[]}).insert("after","assignees[-1]",[e]).commit().then((()=>c.push({title:"Assigned user to document",description:e,status:"success"}))).catch((t=>(console.error(t),c.push({title:"Failed to add assignee",description:e,status:"error"}))))}),[a,d,c]),g=p.default.useCallback(((e,t)=>{d.patch("workflow-metadata.".concat(e)).unset(['assignees[@ == "'.concat(t,'"]')]).commit().then((e=>e)).catch((t=>(console.error(t),c.push({title:"Failed to remove assignee",description:e,status:"error"}))))}),[d,c]),v=p.default.useCallback((e=>{d.patch("workflow-metadata.".concat(e)).unset(["assignees"]).commit().then((e=>e)).catch((t=>(console.error(t),c.push({title:"Failed to clear assignees",description:e,status:"error"}))))}),[d,c]);return i.jsx(s.Popover,{content:i.jsx(l.UserSelectMenu,{style:{maxHeight:300},value:t||[],userList:n,onAdd:h,onClear:v,onRemove:g,open:u===a}),portal:!0,open:u===a,children:t&&0!==t.length?i.jsx(s.Button,{onClick:()=>m(a),padding:0,mode:"bleed",style:{width:"100%"},children:i.jsx(f,{users:n.filter((e=>t.includes(e.id)))})}):i.jsx(s.Button,{onClick:()=>m(a),fontSize:1,padding:2,tabIndex:-1,icon:o.AddIcon,text:"Assign",tone:"positive"})})}function g(e){var t;const{userList:n,isDragging:a,item:l}=e,{assignees:d=[],documentId:c}=null!=(t=l._metadata)?t:{},u=r.useSchema(),p=s.useTheme().sanity.color.dark?"transparent":"default";return i.jsx(s.Box,{paddingY:2,paddingX:3,children:i.jsx(s.Card,{radius:2,shadow:a?3:1,tone:a?"positive":p,children:i.jsxs(s.Stack,{children:[i.jsx(s.Card,{borderBottom:!0,radius:2,padding:3,paddingLeft:2,tone:"inherit",style:{pointerEvents:"none"},children:i.jsxs(s.Flex,{align:"center",justify:"space-between",gap:1,children:[i.jsx(r.Preview,{layout:"default",value:l,schemaType:u.get(l._type)}),i.jsx(o.DragHandleIcon,{style:{flexShrink:0}})]})}),i.jsx(s.Card,{padding:2,radius:2,tone:"inherit",children:i.jsxs(s.Flex,{align:"center",justify:"space-between",gap:1,children:[c&&i.jsx(h,{userList:n,assignees:d,documentId:c}),i.jsx(m,{id:l._id,type:l._type})]})})]})})})}function v(e){const{_id:t,_type:n,documentId:o,state:a,onComplete:l}=e,d=r.useDocumentOperation(o,n),c=t.startsWith("drafts."),u=s.useToast();return c&&"publish"===a.operation?d.publish.disabled||(d.publish.execute(),l(t),u.push({title:"Published Document",description:o,status:"success"})):c||"unpublish"!==a.operation?l(t):d.unpublish.disabled||(d.unpublish.execute(),l(t),u.push({title:"Unpublished Document",description:o,status:"success"})),i.jsxs(s.Card,{padding:3,shadow:2,tone:"primary",children:["Mutating: ",t," to ",a.title]})}const y='{\n "documents": '.concat("*[_type in $schemaTypes]{ _id, _type, _rev }",',\n "metadata": ').concat('*[_type == "workflow.metadata"]{\n _rev,\n assignees,\n documentId,\n state\n}',"\n}"),b={documents:[],metadata:[]};function x(e){var n,o;const{schemaTypes:a=[],states:c=[]}=null!=(o=null==(n=null==e?void 0:e.tool)?void 0:n.options)?o:{},[u,m]=p.default.useState([]),f=p.default.useCallback((e=>{m((t=>t.filter((t=>t._id!==e))))}),[]),h=r.useClient(),x=s.useToast(),j=s.useTheme().sanity.color.dark?"default":"transparent",w=l.useProjectUsers()||[],{workflowData:I,operations:k}=function(e){const n=s.useToast(),o=r.useClient(),[i,a]=p.default.useState([]),{data:d,loading:c,error:u}=l.useListeningQuery(y,{params:{schemaTypes:e},initialValue:b});p.default.useEffect((()=>{if(d){const e=d.documents.reduce(((e,n)=>{const r=d.metadata.find((e=>e.documentId===n._id.replace("drafts.","")));if(!r)return[...e,t({_metadata:null},n)];const o=t({_metadata:r},n);return n._id.startsWith("drafts.")?[...e,o]:Boolean(d.documents.find((e=>e._id==="drafts.".concat(n._id))))?e:[...e,o]}),[]);a(e)}}),[d]);const m=p.default.useCallback(((e,r,s)=>{const l=i,d=i.map((n=>{var o;return(null==(o=null==n?void 0:n._metadata)?void 0:o.documentId)===e?t(t({},n),{},{_metadata:t(t({},n._metadata),{},{state:r.droppableId})}):n}));a(d);const c=r.droppableId,u=s.find((e=>e.id===c)),p=i.find((t=>{var n;return(null==(n=null==t?void 0:t._metadata)?void 0:n.documentId)===e}));if(!(null==u?void 0:u.id))return n.push({title:"Could not find target state ".concat(c),status:"error"}),null;if(!p)return n.push({title:"Could not find dragged document in data",status:"error"}),null;const{_id:m,_type:f}=p,{_rev:h,documentId:g}=p._metadata||{};return o.patch("workflow-metadata.".concat(g)).ifRevisionId(h).set({state:c}).commit().then((()=>{var e;return n.push({title:'Moved to "'.concat(null!=(e=null==u?void 0:u.title)?e:c,'"'),description:g,status:"success"})})).catch((()=>{var e;return a(l),n.push({title:'Failed to move to "'.concat(null!=(e=null==u?void 0:u.title)?e:c,'"'),description:g,status:"error"})})),{_id:m,_type:f,documentId:g,state:u}}),[o,n,i]);return{workflowData:{data:i,loading:c,error:u},operations:{move:m}}}(a),{data:_,loading:C,error:D}=I,{move:O}=k,P=_.filter((e=>!e._metadata)).map((e=>e._id.replace("drafts.",""))),T=p.default.useCallback((async e=>{x.push({title:"Importing documents",status:"info"});const t=e.reduce(((e,t)=>e.createOrReplace({_id:"workflow-metadata.".concat(t),_type:"workflow.metadata",state:c[0].id,documentId:t})),h.transaction());await t.commit(),x.push({title:"Imported documents",status:"success"})}),[]),S=p.default.useCallback((e=>{const{draggableId:t,source:n,destination:r}=e;if(console.log("sending ".concat(t," from ").concat(n.droppableId," to ").concat(null==r?void 0:r.droppableId)),!r||r.droppableId===n.droppableId)return;const o=O(t,r,c);o&&m((e=>[...e,o]))}),[O,c]);return(null==c?void 0:c.length)?D?i.jsx(s.Container,{width:1,padding:5,children:i.jsx(l.Feedback,{tone:"critical",title:"Error with query"})}):i.jsxs(i.Fragment,{children:[u.length?i.jsx("div",{style:{position:"absolute",bottom:0,background:"red"},children:u.map((e=>i.jsx(v,t(t({},e),{},{onComplete:f}),e._id)))}):null,P.length>0&&i.jsx(s.Box,{padding:5,children:i.jsx(s.Card,{border:!0,padding:3,tone:"caution",children:i.jsx(s.Flex,{align:"center",justify:"center",children:i.jsxs(s.Button,{onClick:()=>T(P),children:["Import ",P.length," Missing"," ",1===P.length?"Document":"Documents"," ","into Workflow"]})})})}),i.jsx(d.DragDropContext,{onDragEnd:S,children:i.jsx(s.Grid,{columns:c.length,height:"fill",children:c.map(((e,n)=>i.jsxs(s.Card,{borderLeft:n>0,children:[i.jsx(s.Card,{paddingY:4,padding:3,style:{pointerEvents:"none"},children:i.jsx(s.Label,{children:e.title})}),i.jsx(d.Droppable,{droppableId:e.id,children:(n,r)=>{return i.jsxs(s.Card,{ref:n.innerRef,tone:r.isDraggingOver?"primary":j,height:"fill",children:[C?i.jsx(s.Flex,{padding:5,align:"center",justify:"center",children:i.jsx(s.Spinner,{muted:!0})}):null,_.length>0&&(o=_,a=e.id,o.filter((e=>{var t;return(null==(t=null==e?void 0:e._metadata)?void 0:t.state)===a}))).map(((e,n)=>{var r,o;return i.jsx(d.Draggable,{draggableId:null==(o=null==e?void 0:e._metadata)?void 0:o.documentId,index:n,children:(n,r)=>i.jsx("div",t(t(t({ref:n.innerRef},n.draggableProps),n.dragHandleProps),{},{children:i.jsx(g,{isDragging:r.isDragging,item:e,userList:w})}))},null==(r=null==e?void 0:e._metadata)?void 0:r.documentId)}))]});var o,a}})]},e.id)))})})]}):i.jsx(s.Container,{width:1,padding:5,children:i.jsx(l.Feedback,{tone:"caution",title:"Plugin options error",description:"No States defined in plugin config"})})}var j=e=>r.defineType({type:"document",name:"workflow.metadata",title:"Workflow metadata",liveEdit:!0,fields:[r.defineField({name:"state",type:"string",options:{list:e.map((e=>({value:e.id,title:e.title})))}}),r.defineField({name:"documentId",title:"Document ID",type:"string",readOnly:!0}),r.defineField({type:"array",name:"assignees",description:"The people who are assigned to move this further in the workflow.",of:[r.defineArrayMember({type:"string"})]})]});function w(e,t){const{data:n,loading:r,error:o}=l.useListeningQuery('*[_type == "workflow.metadata" && documentId == $id][0]',{params:{id:e}});return(null==n?void 0:n.state)?{data:{metadata:n,state:t.find((e=>e.id===n.state))},loading:r,error:o}:{data:{},loading:r,error:o}}function I(e,t){const{id:n}=e,{data:r,loading:o,error:i}=w(n,t),{state:a}=r;return o||i?(i&&console.error(i),null):a?{label:a.title,title:a.title,color:null==a?void 0:a.color}:null}function k(e,t){const{id:n}=e,{data:i,loading:a,error:l}=w(n,t),{state:d}=i,c=r.useClient(),u=s.useToast();if(a||l)return l&&console.error(l),null;if(!d)return null;const p=t.findIndex((e=>e.id===d.id)),m=t[p+1];return m?{icon:o.ArrowRightIcon,label:"Promote",title:'Promote State to "'.concat(m.title,'"'),onHandle:()=>{return t=n,r=m,void c.patch("workflow-metadata.".concat(t)).set({state:r.id}).commit().then((()=>{e.onComplete(),u.push({status:"success",title:"Document promoted to ".concat(r.title)})})).catch((t=>{e.onComplete(),console.error(t),u.push({status:"error",title:"Document promotion failed"})}));var t,r}}:null}function _(e,t){const{id:n}=e,{data:i,loading:a,error:l}=w(n,t),{state:d}=i,c=r.useClient(),u=s.useToast();if(a||l)return l&&console.error(l),null;if(!d)return null;const p=t.findIndex((e=>e.id===d.id)),m=t[p-1];return m?{icon:o.ArrowLeftIcon,label:"Demote",title:'Demote State to "'.concat(m.title,'"'),onHandle:()=>{return t=n,r=m,void c.patch("workflow-metadata.".concat(t)).set({state:r.id}).commit().then((()=>{e.onComplete(),u.push({status:"success",title:"Document demoted to ".concat(r.title)})})).catch((t=>{e.onComplete(),console.error(t),u.push({status:"error",title:"Document demotion failed"})}));var t,r}}:null}const C={schemaTypes:[],states:[{id:"draft",title:"Draft",operation:"unpublish"},{id:"inReview",title:"In review",operation:null,color:"primary"},{id:"approved",title:"Approved",operation:null,color:"success",icon:o.CheckmarkIcon},{id:"changesRequested",title:"Changes requested",operation:null,color:"warning"},{id:"published",title:"Published",operation:"publish",color:"success"}]},D=r.definePlugin((function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:C;const{schemaTypes:n,states:r}=t(t({},C),e);if(!(null==r?void 0:r.length))throw new Error("Workflow: Missing states in config");return{name:"sanity-plugin-workflow",schema:{types:[j(r)]},document:{actions:(e,t)=>n.includes(t.schemaType)?[e=>k(e,r),e=>_(e,r),...e]:e,badges:(e,t)=>n.includes(t.schemaType)?[e=>I(e,r),...e]:e},tools:[{name:"workflow",title:"Workflow",component:x,icon:o.SplitVerticalIcon,options:{schemaTypes:n,states:r}}]}}));exports.workflow=D;//# sourceMappingURL=index.js.map
1
+ 'use strict';
2
+
3
+ var _templateObject, _templateObject2, _templateObject3;
4
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
5
+ Object.defineProperty(exports, '__esModule', {
6
+ value: true
7
+ });
8
+ var sanity = require('sanity');
9
+ var jsxRuntime = require('react/jsx-runtime');
10
+ var icons = require('@sanity/icons');
11
+ var React = require('react');
12
+ var sanityPluginUtils = require('sanity-plugin-utils');
13
+ var ui = require('@sanity/ui');
14
+ var lexorank = require('lexorank');
15
+ var router = require('sanity/router');
16
+ var dnd = require('@hello-pangea/dnd');
17
+ var groq = require('groq');
18
+ var reactVirtual = require('@tanstack/react-virtual');
19
+ var styled = require('styled-components');
20
+ var framerMotion = require('framer-motion');
21
+ function _interopDefaultCompat(e) {
22
+ return e && typeof e === 'object' && 'default' in e ? e : {
23
+ default: e
24
+ };
25
+ }
26
+ var React__default = /*#__PURE__*/_interopDefaultCompat(React);
27
+ var groq__default = /*#__PURE__*/_interopDefaultCompat(groq);
28
+ var styled__default = /*#__PURE__*/_interopDefaultCompat(styled);
29
+ function defineStates(states) {
30
+ return states;
31
+ }
32
+ const API_VERSION = "2023-01-01";
33
+ const DEFAULT_CONFIG = {
34
+ schemaTypes: [],
35
+ states: defineStates([{
36
+ id: "inReview",
37
+ title: "In review",
38
+ color: "primary",
39
+ roles: ["editor", "administrator"],
40
+ transitions: ["changesRequested", "approved"]
41
+ }, {
42
+ id: "changesRequested",
43
+ title: "Changes requested",
44
+ color: "warning",
45
+ roles: ["editor", "administrator"],
46
+ transitions: ["approved"]
47
+ }, {
48
+ id: "approved",
49
+ title: "Approved",
50
+ color: "success",
51
+ roles: ["administrator"],
52
+ transitions: ["changesRequested"],
53
+ requireAssignment: true
54
+ }])
55
+ };
56
+ function UserAssignment(props) {
57
+ const {
58
+ assignees,
59
+ userList,
60
+ documentId
61
+ } = props;
62
+ const client = sanity.useClient({
63
+ apiVersion: API_VERSION
64
+ });
65
+ const toast = ui.useToast();
66
+ const addAssignee = React__default.default.useCallback(userId => {
67
+ const user = userList.find(u => u.id === userId);
68
+ if (!userId || !user) {
69
+ return toast.push({
70
+ status: "error",
71
+ title: "Could not find User"
72
+ });
73
+ }
74
+ return client.patch("workflow-metadata.".concat(documentId)).setIfMissing({
75
+ assignees: []
76
+ }).insert("after", "assignees[-1]", [userId]).commit().then(() => {
77
+ return toast.push({
78
+ title: "Added ".concat(user.displayName, " to assignees"),
79
+ status: "success"
80
+ });
81
+ }).catch(err => {
82
+ console.error(err);
83
+ return toast.push({
84
+ title: "Failed to add assignee",
85
+ description: userId,
86
+ status: "error"
87
+ });
88
+ });
89
+ }, [documentId, client, toast, userList]);
90
+ const removeAssignee = React__default.default.useCallback(userId => {
91
+ const user = userList.find(u => u.id === userId);
92
+ if (!userId || !user) {
93
+ return toast.push({
94
+ status: "error",
95
+ title: "Could not find User"
96
+ });
97
+ }
98
+ return client.patch("workflow-metadata.".concat(documentId)).unset(["assignees[@ == \"".concat(userId, "\"]")]).commit().then(() => {
99
+ return toast.push({
100
+ title: "Removed ".concat(user.displayName, " from assignees"),
101
+ status: "success"
102
+ });
103
+ }).catch(err => {
104
+ console.error(err);
105
+ return toast.push({
106
+ title: "Failed to remove assignee",
107
+ description: documentId,
108
+ status: "error"
109
+ });
110
+ });
111
+ }, [client, toast, documentId, userList]);
112
+ const clearAssignees = React__default.default.useCallback(() => {
113
+ return client.patch("workflow-metadata.".concat(documentId)).unset(["assignees"]).commit().then(() => {
114
+ return toast.push({
115
+ title: "Cleared assignees",
116
+ status: "success"
117
+ });
118
+ }).catch(err => {
119
+ console.error(err);
120
+ return toast.push({
121
+ title: "Failed to clear assignees",
122
+ description: documentId,
123
+ status: "error"
124
+ });
125
+ });
126
+ }, [client, toast, documentId]);
127
+ return /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.UserSelectMenu, {
128
+ style: {
129
+ maxHeight: 300
130
+ },
131
+ value: assignees || [],
132
+ userList,
133
+ onAdd: addAssignee,
134
+ onClear: clearAssignees,
135
+ onRemove: removeAssignee
136
+ });
137
+ }
138
+ function useWorkflowMetadata(ids) {
139
+ const {
140
+ data: rawData,
141
+ loading,
142
+ error
143
+ } = sanityPluginUtils.useListeningQuery("*[_type == \"workflow.metadata\" && documentId in $ids]{\n _id,\n _type,\n _rev,\n assignees,\n documentId,\n state,\n orderRank\n }", {
144
+ params: {
145
+ ids
146
+ },
147
+ options: {
148
+ apiVersion: API_VERSION
149
+ }
150
+ });
151
+ const keyedMetadata = React.useMemo(() => {
152
+ if (!rawData || rawData.length === 0) return {};
153
+ return rawData.reduce((acc, cur) => {
154
+ return {
155
+ ...acc,
156
+ [cur.documentId]: cur
157
+ };
158
+ }, {});
159
+ }, [rawData]);
160
+ return {
161
+ data: keyedMetadata,
162
+ loading,
163
+ error
164
+ };
165
+ }
166
+ const WorkflowContext = React.createContext({
167
+ data: {},
168
+ loading: false,
169
+ error: false,
170
+ ids: [],
171
+ addId: () => null,
172
+ removeId: () => null,
173
+ ...DEFAULT_CONFIG
174
+ });
175
+ function useWorkflowContext(id) {
176
+ const current = React.useContext(WorkflowContext);
177
+ return {
178
+ ...current,
179
+ metadata: id ? current.data[id] : null
180
+ };
181
+ }
182
+ function WorkflowProvider(props) {
183
+ const [ids, setIds] = React.useState([]);
184
+ const addId = React.useCallback(id => setIds(current => current.includes(id) ? current : [...current, id]), []);
185
+ const removeId = React.useCallback(id => setIds(current => current.filter(i => i !== id)), []);
186
+ const {
187
+ data,
188
+ loading,
189
+ error
190
+ } = useWorkflowMetadata(ids);
191
+ return /* @__PURE__ */jsxRuntime.jsx(WorkflowContext.Provider, {
192
+ value: {
193
+ data,
194
+ loading,
195
+ error,
196
+ ids,
197
+ addId,
198
+ removeId,
199
+ states: props.workflow.states,
200
+ schemaTypes: props.workflow.schemaTypes
201
+ },
202
+ children: props.renderDefault(props)
203
+ });
204
+ }
205
+ function AssignWorkflow(props) {
206
+ const {
207
+ id
208
+ } = props;
209
+ const {
210
+ metadata,
211
+ loading,
212
+ error
213
+ } = useWorkflowContext(id);
214
+ const [isDialogOpen, setDialogOpen] = React.useState(false);
215
+ const userList = sanityPluginUtils.useProjectUsers({
216
+ apiVersion: API_VERSION
217
+ });
218
+ if (error) {
219
+ console.error(error);
220
+ }
221
+ if (!metadata) {
222
+ return null;
223
+ }
224
+ return {
225
+ icon: icons.UsersIcon,
226
+ type: "dialog",
227
+ disabled: !metadata || loading || error,
228
+ label: "Assign",
229
+ title: metadata ? null : "Document is not in Workflow",
230
+ dialog: isDialogOpen && {
231
+ type: "popover",
232
+ onClose: () => {
233
+ setDialogOpen(false);
234
+ },
235
+ content: /* @__PURE__ */jsxRuntime.jsx(UserAssignment, {
236
+ userList,
237
+ assignees: metadata && metadata.assignees.length > 0 ? metadata.assignees : [],
238
+ documentId: id
239
+ })
240
+ },
241
+ onHandle: () => {
242
+ setDialogOpen(true);
243
+ }
244
+ };
245
+ }
246
+ function BeginWorkflow(props) {
247
+ const {
248
+ id,
249
+ draft
250
+ } = props;
251
+ const {
252
+ metadata,
253
+ loading,
254
+ error,
255
+ states
256
+ } = useWorkflowContext(id);
257
+ const client = sanity.useClient({
258
+ apiVersion: API_VERSION
259
+ });
260
+ const toast = ui.useToast();
261
+ const [beginning, setBeginning] = React.useState(false);
262
+ const [complete, setComplete] = React.useState(false);
263
+ if (error) {
264
+ console.error(error);
265
+ }
266
+ const handle = React.useCallback(async () => {
267
+ setBeginning(true);
268
+ const lowestOrderFirstState = await client.fetch("*[_type == \"workflow.metadata\" && state == $state]|order(orderRank)[0].orderRank", {
269
+ state: states[0].id
270
+ });
271
+ client.createIfNotExists({
272
+ _id: "workflow-metadata.".concat(id),
273
+ _type: "workflow.metadata",
274
+ documentId: id,
275
+ state: states[0].id,
276
+ orderRank: lowestOrderFirstState ? lexorank.LexoRank.parse(lowestOrderFirstState).genNext().toString() : lexorank.LexoRank.min().toString()
277
+ }).then(() => {
278
+ toast.push({
279
+ status: "success",
280
+ title: "Workflow started",
281
+ description: "Document is now \"".concat(states[0].title, "\"")
282
+ });
283
+ setBeginning(false);
284
+ setComplete(true);
285
+ });
286
+ }, [id, states, client, toast]);
287
+ if (!draft || complete || metadata) {
288
+ return null;
289
+ }
290
+ return {
291
+ icon: icons.SplitVerticalIcon,
292
+ type: "dialog",
293
+ disabled: metadata || loading || error || beginning || complete,
294
+ label: beginning ? "Beginning..." : "Begin Workflow",
295
+ onHandle: () => {
296
+ handle();
297
+ }
298
+ };
299
+ }
300
+ function CompleteWorkflow(props) {
301
+ const {
302
+ id
303
+ } = props;
304
+ const {
305
+ metadata,
306
+ loading,
307
+ error,
308
+ states
309
+ } = useWorkflowContext(id);
310
+ const client = sanity.useClient({
311
+ apiVersion: API_VERSION
312
+ });
313
+ if (error) {
314
+ console.error(error);
315
+ }
316
+ const handle = React.useCallback(() => {
317
+ client.delete("workflow-metadata.".concat(id));
318
+ }, [id, client]);
319
+ if (!metadata) {
320
+ return null;
321
+ }
322
+ const state = states.find(s => s.id === metadata.state);
323
+ const isLastState = (state == null ? void 0 : state.id) === states[states.length - 1].id;
324
+ return {
325
+ icon: icons.CheckmarkIcon,
326
+ type: "dialog",
327
+ disabled: loading || error || !isLastState,
328
+ label: "Complete Workflow",
329
+ title: isLastState ? "Removes the document from the Workflow process" : "Cannot remove from workflow until in the last state",
330
+ onHandle: () => {
331
+ handle();
332
+ },
333
+ color: "positive"
334
+ };
335
+ }
336
+ function arraysContainMatchingString(one, two) {
337
+ return one.some(item => two.includes(item));
338
+ }
339
+ function UpdateWorkflow(props, actionState) {
340
+ var _a, _b, _c;
341
+ const {
342
+ id,
343
+ type
344
+ } = props;
345
+ const user = sanity.useCurrentUser();
346
+ const client = sanity.useClient({
347
+ apiVersion: API_VERSION
348
+ });
349
+ const toast = ui.useToast();
350
+ const currentUser = sanity.useCurrentUser();
351
+ const {
352
+ metadata,
353
+ loading,
354
+ error,
355
+ states
356
+ } = useWorkflowContext(id);
357
+ const currentState = states.find(s => s.id === (metadata == null ? void 0 : metadata.state));
358
+ const {
359
+ assignees = []
360
+ } = metadata != null ? metadata : {};
361
+ const {
362
+ validation,
363
+ isValidating
364
+ } = sanity.useValidationStatus(id, type);
365
+ const hasValidationErrors = (currentState == null ? void 0 : currentState.requireValidation) && !isValidating && (validation == null ? void 0 : validation.length) > 0 && validation.find(v => v.level === "error");
366
+ if (error) {
367
+ console.error(error);
368
+ }
369
+ const onHandle = (documentId, newState) => {
370
+ client.patch("workflow-metadata.".concat(documentId)).set({
371
+ state: newState.id
372
+ }).commit().then(() => {
373
+ props.onComplete();
374
+ toast.push({
375
+ status: "success",
376
+ title: "Document state now \"".concat(newState.title, "\"")
377
+ });
378
+ }).catch(err => {
379
+ props.onComplete();
380
+ console.error(err);
381
+ toast.push({
382
+ status: "error",
383
+ title: "Document state update failed"
384
+ });
385
+ });
386
+ };
387
+ if (!metadata || currentState && currentState.id === actionState.id) {
388
+ return null;
389
+ }
390
+ const currentStateIndex = states.findIndex(s => s.id === (currentState == null ? void 0 : currentState.id));
391
+ const actionStateIndex = states.findIndex(s => s.id === actionState.id);
392
+ const direction = actionStateIndex > currentStateIndex ? "promote" : "demote";
393
+ const DirectionIcon = direction === "promote" ? icons.ArrowRightIcon : icons.ArrowLeftIcon;
394
+ const directionLabel = direction === "promote" ? "Promote" : "Demote";
395
+ const userRoleCanUpdateState = ((_a = user == null ? void 0 : user.roles) == null ? void 0 : _a.length) && ((_b = actionState == null ? void 0 : actionState.roles) == null ? void 0 : _b.length) ?
396
+ // If the Action state is limited to specific roles
397
+ // check that the current user has one of those roles
398
+ arraysContainMatchingString(user.roles.map(r => r.name), actionState.roles) :
399
+ // No roles specified on the next state, so anyone can update
400
+ ((_c = actionState == null ? void 0 : actionState.roles) == null ? void 0 : _c.length) !== 0;
401
+ const actionStateIsAValidTransition = (currentState == null ? void 0 : currentState.id) && currentState.transitions.length ?
402
+ // If the Current State limits transitions to specific States
403
+ // Check that the Action State is in Current State's transitions array
404
+ currentState.transitions.includes(actionState.id) :
405
+ // Otherwise this isn't a problem
406
+ true;
407
+ const userAssignmentCanUpdateState = actionState.requireAssignment ?
408
+ // If the Action State requires assigned users
409
+ // Check the current user ID is in the assignees array
410
+ currentUser && assignees.length && assignees.includes(currentUser.id) :
411
+ // Otherwise this isn't a problem
412
+ true;
413
+ let title = "".concat(directionLabel, " State to \"").concat(actionState.title, "\"");
414
+ if (!userRoleCanUpdateState) {
415
+ title = "Your User role cannot ".concat(directionLabel, " State to \"").concat(actionState.title, "\"");
416
+ } else if (!actionStateIsAValidTransition) {
417
+ title = "You cannot ".concat(directionLabel, " State to \"").concat(actionState.title, "\" from \"").concat(currentState == null ? void 0 : currentState.title, "\"");
418
+ } else if (!userAssignmentCanUpdateState) {
419
+ title = "You must be assigned to the document to ".concat(directionLabel, " State to \"").concat(actionState.title, "\"");
420
+ } else if ((currentState == null ? void 0 : currentState.requireValidation) && isValidating) {
421
+ title = "Document is validating, cannot ".concat(directionLabel, " State to \"").concat(actionState.title, "\"");
422
+ } else if (hasValidationErrors) {
423
+ title = "Document has validation errors, cannot ".concat(directionLabel, " State to \"").concat(actionState.title, "\"");
424
+ }
425
+ return {
426
+ icon: DirectionIcon,
427
+ disabled: loading || error || (currentState == null ? void 0 : currentState.requireValidation) && isValidating || hasValidationErrors || !currentState || !userRoleCanUpdateState || !actionStateIsAValidTransition || !userAssignmentCanUpdateState,
428
+ title,
429
+ label: actionState.title,
430
+ onHandle: () => onHandle(id, actionState)
431
+ };
432
+ }
433
+ function AssigneesBadge(documentId, currentUser) {
434
+ var _a;
435
+ const {
436
+ metadata,
437
+ loading,
438
+ error
439
+ } = useWorkflowContext(documentId);
440
+ const userList = sanityPluginUtils.useProjectUsers({
441
+ apiVersion: API_VERSION
442
+ });
443
+ if (loading || error || !metadata) {
444
+ if (error) {
445
+ console.error(error);
446
+ }
447
+ return null;
448
+ }
449
+ if (!((_a = metadata == null ? void 0 : metadata.assignees) == null ? void 0 : _a.length)) {
450
+ return {
451
+ label: "Unassigned"
452
+ };
453
+ }
454
+ const {
455
+ assignees
456
+ } = metadata != null ? metadata : [];
457
+ const hasMe = currentUser ? assignees.some(assignee => assignee === currentUser.id) : false;
458
+ const assigneesCount = hasMe ? assignees.length - 1 : assignees.length;
459
+ const assigneeUsers = userList.filter(user => assignees.includes(user.id));
460
+ const title = assigneeUsers.map(user => user.displayName).join(", ");
461
+ let label;
462
+ if (hasMe && assigneesCount === 0) {
463
+ label = "Assigned to Me";
464
+ } else if (hasMe && assigneesCount > 0) {
465
+ label = "Me and ".concat(assigneesCount, " ").concat(assigneesCount === 1 ? "other" : "others");
466
+ } else {
467
+ label = "".concat(assigneesCount, " assigned");
468
+ }
469
+ return {
470
+ label,
471
+ title,
472
+ color: "primary"
473
+ };
474
+ }
475
+ function StateBadge(documentId) {
476
+ const {
477
+ metadata,
478
+ loading,
479
+ error,
480
+ states
481
+ } = useWorkflowContext(documentId);
482
+ const state = states.find(s => s.id === (metadata == null ? void 0 : metadata.state));
483
+ if (loading || error) {
484
+ if (error) {
485
+ console.error(error);
486
+ }
487
+ return null;
488
+ }
489
+ if (!state) {
490
+ return null;
491
+ }
492
+ return {
493
+ label: state.title,
494
+ // title: state.title,
495
+ color: state == null ? void 0 : state.color
496
+ };
497
+ }
498
+ function WorkflowSignal(props) {
499
+ var _a;
500
+ const documentId = ((_a = props == null ? void 0 : props.value) == null ? void 0 : _a._id) ? props.value._id.replace("drafts.", "") : null;
501
+ const {
502
+ addId,
503
+ removeId
504
+ } = useWorkflowContext();
505
+ React.useEffect(() => {
506
+ if (documentId) {
507
+ addId(documentId);
508
+ }
509
+ return () => {
510
+ if (documentId) {
511
+ removeId(documentId);
512
+ }
513
+ };
514
+ }, [documentId, addId, removeId]);
515
+ return props.renderDefault(props);
516
+ }
517
+ function EditButton(props) {
518
+ const {
519
+ id,
520
+ type,
521
+ disabled = false
522
+ } = props;
523
+ const {
524
+ navigateIntent
525
+ } = router.useRouter();
526
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
527
+ onClick: () => navigateIntent("edit", {
528
+ id,
529
+ type
530
+ }),
531
+ mode: "ghost",
532
+ fontSize: 1,
533
+ padding: 2,
534
+ tabIndex: -1,
535
+ icon: icons.EditIcon,
536
+ text: "Edit",
537
+ disabled
538
+ });
539
+ }
540
+ function Field(props) {
541
+ var _a;
542
+ const schema = sanity.useSchema();
543
+ const {
544
+ data,
545
+ loading,
546
+ error
547
+ } = sanityPluginUtils.useListeningQuery("*[_id in [$id, $draftId]]|order(_updatedAt)[0]", {
548
+ params: {
549
+ id: String(props.value),
550
+ draftId: "drafts.".concat(String(props.value))
551
+ }
552
+ });
553
+ if (loading) {
554
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Spinner, {});
555
+ }
556
+ const schemaType = schema.get((_a = data == null ? void 0 : data._type) != null ? _a : "");
557
+ if (error || !(data == null ? void 0 : data._type) || !schemaType) {
558
+ return /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.Feedback, {
559
+ tone: "critical",
560
+ title: "Error with query"
561
+ });
562
+ }
563
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
564
+ border: true,
565
+ padding: 2,
566
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
567
+ align: "center",
568
+ justify: "space-between",
569
+ gap: 2,
570
+ children: [/* @__PURE__ */jsxRuntime.jsx(sanity.Preview, {
571
+ layout: "default",
572
+ value: data,
573
+ schemaType
574
+ }), /* @__PURE__ */jsxRuntime.jsx(EditButton, {
575
+ id: data._id,
576
+ type: data._type
577
+ })]
578
+ })
579
+ });
580
+ }
581
+ const UserAssignmentInput = props => {
582
+ var _a;
583
+ const documentId = sanity.useFormValue(["documentId"]);
584
+ const userList = sanityPluginUtils.useProjectUsers({
585
+ apiVersion: API_VERSION
586
+ });
587
+ const stringValue = Array.isArray(props == null ? void 0 : props.value) && ((_a = props == null ? void 0 : props.value) == null ? void 0 : _a.length) ? props.value.map(item => String(item)) : [];
588
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
589
+ border: true,
590
+ padding: 1,
591
+ children: /* @__PURE__ */jsxRuntime.jsx(UserAssignment, {
592
+ userList,
593
+ assignees: stringValue,
594
+ documentId: String(documentId)
595
+ })
596
+ });
597
+ };
598
+ function initialRank() {
599
+ let lastRankValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
600
+ const lastRank = lastRankValue && typeof lastRankValue === "string" ? lexorank.LexoRank.parse(lastRankValue) : lexorank.LexoRank.min();
601
+ const nextRank = lastRank.genNext().genNext();
602
+ return nextRank.value;
603
+ }
604
+ var metadata = states => sanity.defineType({
605
+ type: "document",
606
+ name: "workflow.metadata",
607
+ title: "Workflow metadata",
608
+ liveEdit: true,
609
+ fields: [sanity.defineField({
610
+ name: "state",
611
+ description: "The current \"State\" of the document. Field is read only as changing it would not fire the state's \"operation\" setting. These are fired in the Document Actions and in the custom Tool.",
612
+ readOnly: true,
613
+ type: "string",
614
+ options: {
615
+ list: states.length ? states.map(state => ({
616
+ value: state.id,
617
+ title: state.title
618
+ })) : [],
619
+ layout: "radio"
620
+ }
621
+ }), sanity.defineField({
622
+ name: "documentId",
623
+ title: "Document ID",
624
+ description: "Used to help identify the target document that this metadata is tracking state for.",
625
+ type: "string",
626
+ readOnly: true,
627
+ components: {
628
+ input: Field
629
+ }
630
+ }), sanity.defineField({
631
+ name: "orderRank",
632
+ description: "Used to maintain order position of cards in the Tool.",
633
+ type: "string",
634
+ readOnly: true,
635
+ initialValue: async (p, _ref) => {
636
+ let {
637
+ getClient
638
+ } = _ref;
639
+ const lastDocOrderRank = await getClient({
640
+ apiVersion: API_VERSION
641
+ }).fetch("*[_type == $type]|order(@[$order] desc)[0][$order]", {
642
+ order: "orderRank",
643
+ type: "workflow.metadata"
644
+ });
645
+ return initialRank(lastDocOrderRank);
646
+ }
647
+ }), sanity.defineField({
648
+ type: "array",
649
+ name: "assignees",
650
+ of: [{
651
+ type: "string"
652
+ }],
653
+ components: {
654
+ input: UserAssignmentInput
655
+ }
656
+ })]
657
+ });
658
+ function filterItemsAndSort(items, stateId) {
659
+ let selectedUsers = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
660
+ let selectedSchemaTypes = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
661
+ return items.filter(item => item == null ? void 0 : item._id).filter(item => {
662
+ var _a;
663
+ return ((_a = item == null ? void 0 : item._metadata) == null ? void 0 : _a.state) === stateId;
664
+ }).filter(item => {
665
+ var _a, _b, _c;
666
+ return selectedUsers.length && ((_b = (_a = item._metadata) == null ? void 0 : _a.assignees) == null ? void 0 : _b.length) ? (_c = item._metadata) == null ? void 0 : _c.assignees.some(assignee => selectedUsers.includes(assignee)) : !selectedUsers.length;
667
+ }).filter(item => {
668
+ if (!selectedSchemaTypes) {
669
+ return true;
670
+ }
671
+ return selectedSchemaTypes.length ? selectedSchemaTypes.includes(item._type) : false;
672
+ }).sort((a, b) => {
673
+ var _a, _b;
674
+ const aOrderRank = ((_a = a._metadata) == null ? void 0 : _a.orderRank) || "0";
675
+ const bOrderRank = ((_b = b._metadata) == null ? void 0 : _b.orderRank) || "0";
676
+ return aOrderRank.localeCompare(bOrderRank);
677
+ });
678
+ }
679
+ const QUERY = groq__default.default(_templateObject || (_templateObject = _taggedTemplateLiteral(["*[_type == \"workflow.metadata\"]|order(orderRank){\n \"_metadata\": {\n _rev,\n assignees,\n documentId,\n state,\n orderRank\n },\n ...(\n *[_id in [^.documentId, \"drafts.\" + ^.documentId]]|order(_updatedAt)[0]{ \n _id, \n _type, \n _rev, \n _updatedAt \n }\n )\n}"])));
680
+ function useWorkflowDocuments(schemaTypes) {
681
+ const toast = ui.useToast();
682
+ const client = sanity.useClient({
683
+ apiVersion: API_VERSION
684
+ });
685
+ const {
686
+ data,
687
+ loading,
688
+ error
689
+ } = sanityPluginUtils.useListeningQuery(QUERY, {
690
+ params: {
691
+ schemaTypes
692
+ },
693
+ initialValue: []
694
+ });
695
+ const [localDocuments, setLocalDocuments] = React__default.default.useState([]);
696
+ React__default.default.useEffect(() => {
697
+ if (data) {
698
+ setLocalDocuments(data);
699
+ }
700
+ }, [data]);
701
+ const move = React__default.default.useCallback(async (draggedId, destination, states, newOrder) => {
702
+ const currentLocalData = localDocuments;
703
+ const newLocalDocuments = localDocuments.map(item => {
704
+ var _a;
705
+ if (((_a = item == null ? void 0 : item._metadata) == null ? void 0 : _a.documentId) === draggedId) {
706
+ return {
707
+ ...item,
708
+ _metadata: {
709
+ ...item._metadata,
710
+ state: destination.droppableId,
711
+ orderRank: newOrder,
712
+ // This value won't be written to the document
713
+ // It's done so that un/publish operations don't happen twice
714
+ // Because a moved document's card will update once optimistically
715
+ // and then again when the document is updated
716
+ optimistic: true
717
+ }
718
+ };
719
+ }
720
+ return item;
721
+ });
722
+ setLocalDocuments(newLocalDocuments);
723
+ const newStateId = destination.droppableId;
724
+ const newState = states.find(s => s.id === newStateId);
725
+ const document = localDocuments.find(d => {
726
+ var _a;
727
+ return ((_a = d == null ? void 0 : d._metadata) == null ? void 0 : _a.documentId) === draggedId;
728
+ });
729
+ if (!(newState == null ? void 0 : newState.id)) {
730
+ toast.push({
731
+ title: "Could not find target state ".concat(newStateId),
732
+ status: "error"
733
+ });
734
+ return null;
735
+ }
736
+ if (!document) {
737
+ toast.push({
738
+ title: "Could not find dragged document in data",
739
+ status: "error"
740
+ });
741
+ return null;
742
+ }
743
+ const {
744
+ _id,
745
+ _type
746
+ } = document;
747
+ const {
748
+ documentId,
749
+ _rev
750
+ } = document._metadata || {};
751
+ await client.patch("workflow-metadata.".concat(documentId)).ifRevisionId(_rev).set({
752
+ state: newStateId,
753
+ orderRank: newOrder
754
+ }).commit().then(res => {
755
+ var _a, _b;
756
+ toast.push({
757
+ 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, "\""),
758
+ status: "success"
759
+ });
760
+ return res;
761
+ }).catch(err => {
762
+ var _a;
763
+ setLocalDocuments(currentLocalData);
764
+ toast.push({
765
+ title: "Failed to move to \"".concat((_a = newState == null ? void 0 : newState.title) != null ? _a : newStateId, "\""),
766
+ description: err.message,
767
+ status: "error"
768
+ });
769
+ return null;
770
+ });
771
+ return {
772
+ _id,
773
+ _type,
774
+ documentId,
775
+ state: newState
776
+ };
777
+ }, [client, toast, localDocuments]);
778
+ return {
779
+ workflowData: {
780
+ data: localDocuments,
781
+ loading,
782
+ error
783
+ },
784
+ operations: {
785
+ move
786
+ }
787
+ };
788
+ }
789
+ function AvatarGroup(props) {
790
+ const currentUser = sanity.useCurrentUser();
791
+ const {
792
+ users,
793
+ max = 4
794
+ } = props;
795
+ const len = users == null ? void 0 : users.length;
796
+ const {
797
+ me,
798
+ visibleUsers
799
+ } = React__default.default.useMemo(() => {
800
+ return {
801
+ me: (currentUser == null ? void 0 : currentUser.id) ? users.find(u => u.id === currentUser.id) : void 0,
802
+ visibleUsers: users.filter(u => u.id !== (currentUser == null ? void 0 : currentUser.id)).slice(0, max - 1)
803
+ };
804
+ }, [users, max, currentUser]);
805
+ if (!(users == null ? void 0 : users.length)) {
806
+ return null;
807
+ }
808
+ return /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
809
+ align: "center",
810
+ gap: 1,
811
+ children: [me ? /* @__PURE__ */jsxRuntime.jsx(sanity.UserAvatar, {
812
+ user: me
813
+ }) : null, visibleUsers.map(user => /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
814
+ style: {
815
+ marginRight: -8
816
+ },
817
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.UserAvatar, {
818
+ user
819
+ })
820
+ }, user.id)), len > max && /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
821
+ paddingLeft: 2,
822
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Text, {
823
+ size: 1,
824
+ children: ["+", len - max]
825
+ })
826
+ })]
827
+ });
828
+ }
829
+ function UserDisplay(props) {
830
+ const {
831
+ assignees,
832
+ userList,
833
+ documentId,
834
+ disabled = false
835
+ } = props;
836
+ const [button] = React__default.default.useState(null);
837
+ const [popover, setPopover] = React__default.default.useState(null);
838
+ const [isOpen, setIsOpen] = React__default.default.useState(false);
839
+ const close = React__default.default.useCallback(() => setIsOpen(false), []);
840
+ const open = React__default.default.useCallback(() => setIsOpen(true), []);
841
+ ui.useClickOutside(close, [button, popover]);
842
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Popover, {
843
+ ref: setPopover,
844
+ content: /* @__PURE__ */jsxRuntime.jsx(UserAssignment, {
845
+ userList,
846
+ assignees,
847
+ documentId
848
+ }),
849
+ portal: true,
850
+ open: isOpen,
851
+ children: !assignees || assignees.length === 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
852
+ onClick: open,
853
+ fontSize: 1,
854
+ padding: 2,
855
+ tabIndex: -1,
856
+ icon: icons.AddIcon,
857
+ text: "Assign",
858
+ tone: "positive",
859
+ mode: "ghost",
860
+ disabled
861
+ }) : /* @__PURE__ */jsxRuntime.jsx(ui.Grid, {
862
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
863
+ onClick: open,
864
+ padding: 0,
865
+ mode: "bleed",
866
+ disabled,
867
+ children: /* @__PURE__ */jsxRuntime.jsx(AvatarGroup, {
868
+ users: userList.filter(u => assignees.includes(u.id))
869
+ })
870
+ })
871
+ })
872
+ });
873
+ }
874
+ function CompleteButton(props) {
875
+ const {
876
+ documentId,
877
+ disabled = false
878
+ } = props;
879
+ const client = sanity.useClient({
880
+ apiVersion: API_VERSION
881
+ });
882
+ const toast = ui.useToast();
883
+ const handleComplete = React__default.default.useCallback(event => {
884
+ const id = event.currentTarget.value;
885
+ if (!id) {
886
+ return;
887
+ }
888
+ client.delete("workflow-metadata.".concat(id)).then(() => {
889
+ toast.push({
890
+ status: "success",
891
+ title: "Workflow completed"
892
+ });
893
+ }).catch(() => {
894
+ toast.push({
895
+ status: "error",
896
+ title: "Could not complete Workflow"
897
+ });
898
+ });
899
+ }, [client, toast]);
900
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Tooltip, {
901
+ portal: true,
902
+ content: /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
903
+ padding: 2,
904
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
905
+ size: 1,
906
+ children: "Remove this document from Workflow"
907
+ })
908
+ }),
909
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
910
+ value: documentId,
911
+ onClick: handleComplete,
912
+ text: "Complete",
913
+ icon: icons.CheckmarkIcon,
914
+ tone: "positive",
915
+ mode: "ghost",
916
+ fontSize: 1,
917
+ padding: 2,
918
+ tabIndex: -1,
919
+ disabled
920
+ })
921
+ });
922
+ }
923
+ function TimeAgo(_ref2) {
924
+ let {
925
+ time
926
+ } = _ref2;
927
+ const timeAgo = sanity.useTimeAgo(time);
928
+ return /* @__PURE__ */jsxRuntime.jsxs("span", {
929
+ title: timeAgo,
930
+ children: [timeAgo, " ago"]
931
+ });
932
+ }
933
+ function DraftStatus(props) {
934
+ const {
935
+ document
936
+ } = props;
937
+ const updatedAt = document && "_updatedAt" in document && document._updatedAt;
938
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Tooltip, {
939
+ portal: true,
940
+ content: /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
941
+ padding: 2,
942
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
943
+ size: 1,
944
+ children: document ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
945
+ children: ["Edited ", updatedAt && /* @__PURE__ */jsxRuntime.jsx(TimeAgo, {
946
+ time: updatedAt
947
+ })]
948
+ }) : /* @__PURE__ */jsxRuntime.jsx(jsxRuntime.Fragment, {
949
+ children: "No unpublished edits"
950
+ })
951
+ })
952
+ }),
953
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.TextWithTone, {
954
+ tone: "caution",
955
+ dimmed: !document,
956
+ muted: !document,
957
+ size: 1,
958
+ children: /* @__PURE__ */jsxRuntime.jsx(icons.EditIcon, {})
959
+ })
960
+ });
961
+ }
962
+ function PublishedStatus(props) {
963
+ const {
964
+ document
965
+ } = props;
966
+ const updatedAt = document && "_updatedAt" in document && document._updatedAt;
967
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Tooltip, {
968
+ portal: true,
969
+ content: /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
970
+ padding: 2,
971
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
972
+ size: 1,
973
+ children: document ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
974
+ children: ["Published ", updatedAt && /* @__PURE__ */jsxRuntime.jsx(TimeAgo, {
975
+ time: updatedAt
976
+ })]
977
+ }) : /* @__PURE__ */jsxRuntime.jsx(jsxRuntime.Fragment, {
978
+ children: "Not published"
979
+ })
980
+ })
981
+ }),
982
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.TextWithTone, {
983
+ tone: "positive",
984
+ dimmed: !document,
985
+ muted: !document,
986
+ size: 1,
987
+ children: /* @__PURE__ */jsxRuntime.jsx(icons.PublishIcon, {})
988
+ })
989
+ });
990
+ }
991
+ function Validate(props) {
992
+ const {
993
+ documentId,
994
+ type,
995
+ onChange
996
+ } = props;
997
+ const {
998
+ isValidating,
999
+ validation = []
1000
+ } = sanity.useValidationStatus(documentId, type);
1001
+ React.useEffect(() => {
1002
+ onChange({
1003
+ isValidating,
1004
+ validation
1005
+ });
1006
+ }, [onChange, isValidating, validation]);
1007
+ return null;
1008
+ }
1009
+ function ValidationStatus(props) {
1010
+ const {
1011
+ validation = []
1012
+ } = props;
1013
+ if (!validation.length) {
1014
+ return null;
1015
+ }
1016
+ const hasError = validation.some(item => item.level === "error");
1017
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Tooltip, {
1018
+ portal: true,
1019
+ content: /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1020
+ padding: 2,
1021
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
1022
+ size: 1,
1023
+ children: validation.length === 1 ? "1 validation issue" : "".concat(validation.length, " validation issues")
1024
+ })
1025
+ }),
1026
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.TextWithTone, {
1027
+ tone: hasError ? "critical" : "caution",
1028
+ size: 1,
1029
+ children: hasError ? /* @__PURE__ */jsxRuntime.jsx(icons.ErrorOutlineIcon, {}) : /* @__PURE__ */jsxRuntime.jsx(icons.WarningOutlineIcon, {})
1030
+ })
1031
+ });
1032
+ }
1033
+ function DocumentCard(props) {
1034
+ var _a, _b;
1035
+ const {
1036
+ isDragDisabled,
1037
+ isPatching,
1038
+ userRoleCanDrop,
1039
+ isDragging,
1040
+ item,
1041
+ states,
1042
+ toggleInvalidDocumentId,
1043
+ userList
1044
+ } = props;
1045
+ const {
1046
+ assignees = [],
1047
+ documentId
1048
+ } = (_a = item._metadata) != null ? _a : {};
1049
+ const schema = sanity.useSchema();
1050
+ const state = states.find(s => {
1051
+ var _a2;
1052
+ return s.id === ((_a2 = item._metadata) == null ? void 0 : _a2.state);
1053
+ });
1054
+ const isDarkMode = ui.useTheme().sanity.color.dark;
1055
+ const defaultCardTone = isDarkMode ? "transparent" : "default";
1056
+ const [optimisticValidation, setOptimisticValidation] = React.useState({
1057
+ isValidating: (_b = state == null ? void 0 : state.requireValidation) != null ? _b : false,
1058
+ validation: []
1059
+ });
1060
+ const {
1061
+ isValidating,
1062
+ validation
1063
+ } = optimisticValidation;
1064
+ const handleValidation = React.useCallback(updates => {
1065
+ setOptimisticValidation(updates);
1066
+ }, []);
1067
+ const cardTone = React.useMemo(() => {
1068
+ let tone = defaultCardTone;
1069
+ if (!userRoleCanDrop) return isDarkMode ? "default" : "transparent";
1070
+ if (!documentId) return tone;
1071
+ if (isPatching) tone = isDarkMode ? "default" : "transparent";
1072
+ if (isDragging) tone = "positive";
1073
+ if ((state == null ? void 0 : state.requireValidation) && !isValidating && validation.length > 0) {
1074
+ if (validation.some(v => v.level === "error")) {
1075
+ tone = "critical";
1076
+ } else {
1077
+ tone = "caution";
1078
+ }
1079
+ }
1080
+ return tone;
1081
+ }, [defaultCardTone, userRoleCanDrop, isPatching, isDarkMode, documentId, isDragging, isValidating, validation, state == null ? void 0 : state.requireValidation]);
1082
+ React.useEffect(() => {
1083
+ if (!isValidating && validation.length > 0) {
1084
+ if (validation.some(v => v.level === "error")) {
1085
+ toggleInvalidDocumentId(documentId, "ADD");
1086
+ } else {
1087
+ toggleInvalidDocumentId(documentId, "REMOVE");
1088
+ }
1089
+ } else {
1090
+ toggleInvalidDocumentId(documentId, "REMOVE");
1091
+ }
1092
+ }, [documentId, isValidating, toggleInvalidDocumentId, validation]);
1093
+ const hasError = React.useMemo(() => isValidating ? false : validation.some(v => v.level === "error"), [isValidating, validation]);
1094
+ const isLastState = React.useMemo(() => {
1095
+ var _a2;
1096
+ return states[states.length - 1].id === ((_a2 = item._metadata) == null ? void 0 : _a2.state);
1097
+ }, [states, item._metadata.state]);
1098
+ return /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1099
+ children: [(state == null ? void 0 : state.requireValidation) ? /* @__PURE__ */jsxRuntime.jsx(Validate, {
1100
+ documentId,
1101
+ type: item._type,
1102
+ onChange: handleValidation
1103
+ }) : null, /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1104
+ paddingBottom: 3,
1105
+ paddingX: 3,
1106
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1107
+ radius: 2,
1108
+ shadow: isDragging ? 3 : 1,
1109
+ tone: cardTone,
1110
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Stack, {
1111
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1112
+ borderBottom: true,
1113
+ radius: 2,
1114
+ padding: 3,
1115
+ paddingLeft: 2,
1116
+ tone: cardTone,
1117
+ style: {
1118
+ pointerEvents: "none"
1119
+ },
1120
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1121
+ align: "center",
1122
+ justify: "space-between",
1123
+ gap: 1,
1124
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1125
+ flex: 1,
1126
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.Preview, {
1127
+ layout: "default",
1128
+ skipVisibilityCheck: true,
1129
+ value: item,
1130
+ schemaType: schema.get(item._type)
1131
+ })
1132
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1133
+ style: {
1134
+ flexShrink: 0
1135
+ },
1136
+ children: hasError || isDragDisabled || isPatching ? null : /* @__PURE__ */jsxRuntime.jsx(icons.DragHandleIcon, {})
1137
+ })]
1138
+ })
1139
+ }), /* @__PURE__ */jsxRuntime.jsxs(ui.Card, {
1140
+ padding: 2,
1141
+ radius: 2,
1142
+ tone: "inherit",
1143
+ children: [/* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1144
+ align: "center",
1145
+ justify: "space-between",
1146
+ gap: 3,
1147
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1148
+ flex: 1,
1149
+ children: documentId && /* @__PURE__ */jsxRuntime.jsx(UserDisplay, {
1150
+ userList,
1151
+ assignees,
1152
+ documentId,
1153
+ disabled: !userRoleCanDrop
1154
+ })
1155
+ }), validation.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ValidationStatus, {
1156
+ validation
1157
+ }) : null, /* @__PURE__ */jsxRuntime.jsx(DraftStatus, {
1158
+ document: item
1159
+ }), /* @__PURE__ */jsxRuntime.jsx(PublishedStatus, {
1160
+ document: item
1161
+ }), /* @__PURE__ */jsxRuntime.jsx(EditButton, {
1162
+ id: item._id,
1163
+ type: item._type,
1164
+ disabled: !userRoleCanDrop
1165
+ }), isLastState && states.length <= 3 ? /* @__PURE__ */jsxRuntime.jsx(CompleteButton, {
1166
+ documentId,
1167
+ disabled: !userRoleCanDrop
1168
+ }) : null]
1169
+ }), isLastState && states.length > 3 ? /* @__PURE__ */jsxRuntime.jsx(ui.Stack, {
1170
+ paddingTop: 2,
1171
+ children: /* @__PURE__ */jsxRuntime.jsx(CompleteButton, {
1172
+ documentId,
1173
+ disabled: !userRoleCanDrop
1174
+ })
1175
+ }) : null]
1176
+ })]
1177
+ })
1178
+ })
1179
+ })]
1180
+ });
1181
+ }
1182
+ function getStyle(draggableStyle, virtualItem) {
1183
+ let transform = "translateY(".concat(virtualItem.start, "px)");
1184
+ if (draggableStyle && draggableStyle.transform) {
1185
+ const draggableTransformY = parseInt(draggableStyle.transform.split(",")[1].split("px")[0], 10);
1186
+ transform = "translateY(".concat(virtualItem.start + draggableTransformY, "px)");
1187
+ }
1188
+ return {
1189
+ position: "absolute",
1190
+ top: 0,
1191
+ left: 0,
1192
+ width: "100%",
1193
+ height: "".concat(virtualItem.size, "px"),
1194
+ transform
1195
+ };
1196
+ }
1197
+ function DocumentList(props) {
1198
+ const {
1199
+ data = [],
1200
+ invalidDocumentIds,
1201
+ patchingIds,
1202
+ selectedSchemaTypes,
1203
+ selectedUserIds,
1204
+ state,
1205
+ states,
1206
+ toggleInvalidDocumentId,
1207
+ user,
1208
+ userList,
1209
+ userRoleCanDrop
1210
+ } = props;
1211
+ const dataFiltered = React.useMemo(() => {
1212
+ return data.length ? filterItemsAndSort(data, state.id, selectedUserIds, selectedSchemaTypes) : [];
1213
+ }, [data, selectedSchemaTypes, selectedUserIds, state.id]);
1214
+ const parentRef = React.useRef(null);
1215
+ const virtualizer = reactVirtual.useVirtualizer({
1216
+ count: dataFiltered.length,
1217
+ getScrollElement: () => parentRef.current,
1218
+ getItemKey: index => {
1219
+ var _a, _b, _c;
1220
+ return (_c = (_b = (_a = dataFiltered[index]) == null ? void 0 : _a._metadata) == null ? void 0 : _b.documentId) != null ? _c : index;
1221
+ },
1222
+ estimateSize: () => 115,
1223
+ overscan: 7,
1224
+ measureElement: element => {
1225
+ return element.getBoundingClientRect().height || 115;
1226
+ }
1227
+ });
1228
+ if (!data.length || !dataFiltered.length) {
1229
+ return null;
1230
+ }
1231
+ return /* @__PURE__ */jsxRuntime.jsx("div", {
1232
+ ref: parentRef,
1233
+ style: {
1234
+ height: "100%",
1235
+ overflow: "auto",
1236
+ // Smooths scrollbar behaviour
1237
+ overflowAnchor: "none",
1238
+ scrollBehavior: "auto",
1239
+ paddingTop: 1
1240
+ },
1241
+ children: /* @__PURE__ */jsxRuntime.jsx("div", {
1242
+ style: {
1243
+ height: "".concat(virtualizer.getTotalSize(), "px"),
1244
+ width: "100%",
1245
+ position: "relative"
1246
+ },
1247
+ children: virtualizer.getVirtualItems().map(virtualItem => {
1248
+ var _a;
1249
+ const item = dataFiltered[virtualItem.index];
1250
+ const {
1251
+ documentId,
1252
+ assignees
1253
+ } = (_a = item == null ? void 0 : item._metadata) != null ? _a : {};
1254
+ const isInvalid = invalidDocumentIds.includes(documentId);
1255
+ const meInAssignees = (user == null ? void 0 : user.id) ? assignees == null ? void 0 : assignees.includes(user.id) : false;
1256
+ const isDragDisabled = patchingIds.includes(documentId) || !userRoleCanDrop || isInvalid || !(state.requireAssignment ? state.requireAssignment && meInAssignees : true);
1257
+ return /* @__PURE__ */jsxRuntime.jsx(dnd.Draggable, {
1258
+ draggableId: documentId,
1259
+ index: virtualItem.index,
1260
+ isDragDisabled,
1261
+ children: (draggableProvided, draggableSnapshot) => /* @__PURE__ */jsxRuntime.jsx("div", {
1262
+ ref: draggableProvided.innerRef,
1263
+ ...draggableProvided.draggableProps,
1264
+ ...draggableProvided.dragHandleProps,
1265
+ style: getStyle(draggableProvided.draggableProps.style, virtualItem),
1266
+ children: /* @__PURE__ */jsxRuntime.jsx("div", {
1267
+ ref: virtualizer.measureElement,
1268
+ "data-index": virtualItem.index,
1269
+ children: /* @__PURE__ */jsxRuntime.jsx(DocumentCard, {
1270
+ userRoleCanDrop,
1271
+ isDragDisabled,
1272
+ isPatching: patchingIds.includes(documentId),
1273
+ isDragging: draggableSnapshot.isDragging,
1274
+ item,
1275
+ toggleInvalidDocumentId,
1276
+ userList,
1277
+ states
1278
+ })
1279
+ })
1280
+ })
1281
+ }, virtualItem.key);
1282
+ })
1283
+ })
1284
+ });
1285
+ }
1286
+ function Filters(props) {
1287
+ const {
1288
+ uniqueAssignedUsers = [],
1289
+ selectedUserIds,
1290
+ schemaTypes,
1291
+ selectedSchemaTypes,
1292
+ toggleSelectedUser,
1293
+ resetSelectedUsers,
1294
+ toggleSelectedSchemaType
1295
+ } = props;
1296
+ const currentUser = sanity.useCurrentUser();
1297
+ const schema = sanity.useSchema();
1298
+ const onAdd = React.useCallback(id => {
1299
+ if (!selectedUserIds.includes(id)) {
1300
+ toggleSelectedUser(id);
1301
+ }
1302
+ }, [selectedUserIds, toggleSelectedUser]);
1303
+ const onRemove = React.useCallback(id => {
1304
+ if (selectedUserIds.includes(id)) {
1305
+ toggleSelectedUser(id);
1306
+ }
1307
+ }, [selectedUserIds, toggleSelectedUser]);
1308
+ const onClear = React.useCallback(() => {
1309
+ resetSelectedUsers();
1310
+ }, [resetSelectedUsers]);
1311
+ if (uniqueAssignedUsers.length === 0 && schemaTypes.length < 2) {
1312
+ return null;
1313
+ }
1314
+ const meInUniqueAssignees = (currentUser == null ? void 0 : currentUser.id) && uniqueAssignedUsers.find(u => u.id === currentUser.id);
1315
+ const uniqueAssigneesNotMe = uniqueAssignedUsers.filter(u => u.id !== (currentUser == null ? void 0 : currentUser.id));
1316
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1317
+ tone: "primary",
1318
+ padding: 2,
1319
+ borderBottom: true,
1320
+ style: {
1321
+ overflowX: "hidden"
1322
+ },
1323
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1324
+ align: "center",
1325
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
1326
+ align: "center",
1327
+ gap: 1,
1328
+ flex: 1,
1329
+ children: uniqueAssignedUsers.length > 5 ? /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1330
+ tone: "default",
1331
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.MenuButton, {
1332
+ button: /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1333
+ text: "Filter Assignees",
1334
+ tone: "primary",
1335
+ icon: icons.UserIcon
1336
+ }),
1337
+ id: "user-filters",
1338
+ menu: /* @__PURE__ */jsxRuntime.jsx(ui.Menu, {
1339
+ children: /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.UserSelectMenu, {
1340
+ value: selectedUserIds,
1341
+ userList: uniqueAssignedUsers,
1342
+ onAdd,
1343
+ onRemove,
1344
+ onClear,
1345
+ labels: {
1346
+ addMe: "Filter mine",
1347
+ removeMe: "Clear mine",
1348
+ clear: "Clear filters"
1349
+ }
1350
+ })
1351
+ }),
1352
+ popover: {
1353
+ portal: true
1354
+ }
1355
+ })
1356
+ }) : /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1357
+ children: [meInUniqueAssignees ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1358
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1359
+ padding: 0,
1360
+ mode: selectedUserIds.includes(currentUser.id) ? "default" : "bleed",
1361
+ onClick: () => toggleSelectedUser(currentUser.id),
1362
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
1363
+ padding: 1,
1364
+ align: "center",
1365
+ justify: "center",
1366
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.UserAvatar, {
1367
+ user: currentUser.id,
1368
+ size: 1,
1369
+ withTooltip: true
1370
+ })
1371
+ })
1372
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1373
+ borderRight: true,
1374
+ style: {
1375
+ height: 30
1376
+ },
1377
+ tone: "inherit"
1378
+ })]
1379
+ }) : null, uniqueAssigneesNotMe.map(user => /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1380
+ padding: 0,
1381
+ mode: selectedUserIds.includes(user.id) ? "default" : "bleed",
1382
+ onClick: () => toggleSelectedUser(user.id),
1383
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
1384
+ padding: 1,
1385
+ align: "center",
1386
+ justify: "center",
1387
+ children: /* @__PURE__ */jsxRuntime.jsx(sanity.UserAvatar, {
1388
+ user,
1389
+ size: 1,
1390
+ withTooltip: true
1391
+ })
1392
+ })
1393
+ }, user.id)), selectedUserIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1394
+ text: "Clear",
1395
+ onClick: resetSelectedUsers,
1396
+ mode: "ghost",
1397
+ icon: icons.ResetIcon
1398
+ }) : null]
1399
+ })
1400
+ }), schemaTypes.length > 1 ? /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
1401
+ align: "center",
1402
+ gap: 1,
1403
+ children: schemaTypes.map(typeName => {
1404
+ var _a, _b;
1405
+ const schemaType = schema.get(typeName);
1406
+ if (!schemaType) {
1407
+ return null;
1408
+ }
1409
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1410
+ text: (_a = schemaType == null ? void 0 : schemaType.title) != null ? _a : typeName,
1411
+ icon: (_b = schemaType == null ? void 0 : schemaType.icon) != null ? _b : void 0,
1412
+ mode: selectedSchemaTypes.includes(typeName) ? "default" : "ghost",
1413
+ onClick: () => toggleSelectedSchemaType(typeName)
1414
+ }, typeName);
1415
+ })
1416
+ }) : null]
1417
+ })
1418
+ });
1419
+ }
1420
+ function Status(props) {
1421
+ const {
1422
+ text,
1423
+ icon
1424
+ } = props;
1425
+ const Icon = icon;
1426
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Tooltip, {
1427
+ portal: true,
1428
+ content: /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1429
+ padding: 2,
1430
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
1431
+ size: 1,
1432
+ children: text
1433
+ })
1434
+ }),
1435
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
1436
+ size: 1,
1437
+ children: /* @__PURE__ */jsxRuntime.jsx(Icon, {})
1438
+ })
1439
+ });
1440
+ }
1441
+ const StyledStickyCard = styled__default.default(ui.Card)(() => styled.css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n position: sticky;\n top: 0;\n z-index: 1;\n "]))));
1442
+ function StateTitle(props) {
1443
+ const {
1444
+ state,
1445
+ requireAssignment,
1446
+ userRoleCanDrop,
1447
+ isDropDisabled,
1448
+ draggingFrom,
1449
+ documentCount
1450
+ } = props;
1451
+ let tone = "default";
1452
+ const isSource = draggingFrom === state.id;
1453
+ if (draggingFrom) {
1454
+ tone = isDropDisabled || isSource ? "default" : "positive";
1455
+ }
1456
+ return /* @__PURE__ */jsxRuntime.jsx(StyledStickyCard, {
1457
+ paddingY: 4,
1458
+ padding: 3,
1459
+ tone: "inherit",
1460
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1461
+ gap: 3,
1462
+ align: "center",
1463
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Badge, {
1464
+ mode: draggingFrom && !isDropDisabled || isSource ? "default" : "outline",
1465
+ tone,
1466
+ muted: !userRoleCanDrop || isDropDisabled,
1467
+ children: state.title
1468
+ }), userRoleCanDrop ? null : /* @__PURE__ */jsxRuntime.jsx(Status, {
1469
+ text: "You do not have permissions to move documents to this State",
1470
+ icon: icons.InfoOutlineIcon
1471
+ }), requireAssignment ? /* @__PURE__ */jsxRuntime.jsx(Status, {
1472
+ text: "You must be assigned to the document to move documents to this State",
1473
+ icon: icons.UserIcon
1474
+ }) : null, /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1475
+ flex: 1,
1476
+ children: documentCount > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
1477
+ weight: "semibold",
1478
+ align: "right",
1479
+ size: 1,
1480
+ children: documentCount
1481
+ }) : null
1482
+ })]
1483
+ })
1484
+ });
1485
+ }
1486
+ function generateMiddleValue(ranks) {
1487
+ if (!ranks.some(rank => !rank)) {
1488
+ return ranks;
1489
+ }
1490
+ const firstUndefined = ranks.findIndex(rank => !rank);
1491
+ const firstDefinedAfter = ranks.findIndex((rank, index) => rank && index > firstUndefined);
1492
+ const firstDefinedBefore = ranks.findLastIndex((rank, index) => rank && index < firstUndefined);
1493
+ if (firstDefinedAfter === -1 || firstDefinedBefore === -1) {
1494
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1495
+ }
1496
+ const beforeRank = ranks[firstDefinedBefore];
1497
+ const afterRank = ranks[firstDefinedAfter];
1498
+ if (!beforeRank || typeof beforeRank === "undefined" || !afterRank || typeof afterRank === "undefined") {
1499
+ throw new Error("Unable to generate middle value between indexes ".concat(firstDefinedBefore, " and ").concat(firstDefinedAfter));
1500
+ }
1501
+ const between = beforeRank.between(afterRank);
1502
+ const middle = Math.floor((firstDefinedAfter + firstDefinedBefore) / 2);
1503
+ if (ranks[middle]) {
1504
+ throw new Error("Should not have overwritten value at index ".concat(middle));
1505
+ }
1506
+ ranks[middle] = between;
1507
+ return ranks;
1508
+ }
1509
+ function generateMultipleOrderRanks(count, start, end) {
1510
+ let ranks = [...Array(count)];
1511
+ const rankStart = start != null ? start : lexorank.LexoRank.min().genNext().genNext();
1512
+ const rankEnd = end != null ? end : lexorank.LexoRank.max().genPrev().genPrev();
1513
+ ranks[0] = rankStart;
1514
+ ranks[count - 1] = rankEnd;
1515
+ for (let i = 0; i < count; i++) {
1516
+ ranks = generateMiddleValue(ranks);
1517
+ }
1518
+ return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1519
+ }
1520
+ 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 "]))));
1521
+ function FloatingCard(_ref3) {
1522
+ let {
1523
+ children
1524
+ } = _ref3;
1525
+ const childrenHaveValues = Array.isArray(children) ? children.some(Boolean) : Boolean(children);
1526
+ return /* @__PURE__ */jsxRuntime.jsx(framerMotion.AnimatePresence, {
1527
+ children: childrenHaveValues ? /* @__PURE__ */jsxRuntime.jsx(framerMotion.motion.div, {
1528
+ initial: {
1529
+ opacity: 0
1530
+ },
1531
+ animate: {
1532
+ opacity: 1
1533
+ },
1534
+ exit: {
1535
+ opacity: 0
1536
+ },
1537
+ children: /* @__PURE__ */jsxRuntime.jsx(StyledFloatingCard, {
1538
+ shadow: 3,
1539
+ padding: 3,
1540
+ margin: 3,
1541
+ radius: 3,
1542
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Grid, {
1543
+ gap: 2,
1544
+ children
1545
+ })
1546
+ })
1547
+ }, "floater") : null
1548
+ });
1549
+ }
1550
+ function Verify(props) {
1551
+ const {
1552
+ data,
1553
+ userList,
1554
+ states
1555
+ } = props;
1556
+ const client = sanity.useClient({
1557
+ apiVersion: API_VERSION
1558
+ });
1559
+ const toast = ui.useToast();
1560
+ const documentsWithoutValidMetadataIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1561
+ var _a;
1562
+ const {
1563
+ documentId,
1564
+ state
1565
+ } = (_a = cur._metadata) != null ? _a : {};
1566
+ const stateExists = states.find(s => s.id === state);
1567
+ return !stateExists && documentId ? [...acc, documentId] : acc;
1568
+ }, []) : [];
1569
+ const documentsWithInvalidUserIds = (data == null ? void 0 : data.length) && (userList == null ? void 0 : userList.length) ? data.reduce((acc, cur) => {
1570
+ var _a;
1571
+ const {
1572
+ documentId,
1573
+ assignees
1574
+ } = (_a = cur._metadata) != null ? _a : {};
1575
+ const allAssigneesExist = (assignees == null ? void 0 : assignees.length) ? assignees == null ? void 0 : assignees.every(a => userList.find(u => u.id === a)) : true;
1576
+ return !allAssigneesExist && documentId ? [...acc, documentId] : acc;
1577
+ }, []) : [];
1578
+ const documentsWithoutOrderIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1579
+ var _a;
1580
+ const {
1581
+ documentId,
1582
+ orderRank
1583
+ } = (_a = cur._metadata) != null ? _a : {};
1584
+ return !orderRank && documentId ? [...acc, documentId] : acc;
1585
+ }, []) : [];
1586
+ const documentsWithDuplicatedOrderIds = (data == null ? void 0 : data.length) ? data.reduce((acc, cur) => {
1587
+ var _a;
1588
+ const {
1589
+ documentId,
1590
+ orderRank
1591
+ } = (_a = cur._metadata) != null ? _a : {};
1592
+ return orderRank && data.filter(d => {
1593
+ var _a2;
1594
+ return ((_a2 = d._metadata) == null ? void 0 : _a2.orderRank) === orderRank;
1595
+ }).length > 1 && documentId ? [...acc, documentId] : acc;
1596
+ }, []) : [];
1597
+ const correctDocuments = React__default.default.useCallback(async ids => {
1598
+ toast.push({
1599
+ title: "Correcting...",
1600
+ status: "info"
1601
+ });
1602
+ const tx = ids.reduce((item, documentId) => {
1603
+ return item.patch("workflow-metadata.".concat(documentId), {
1604
+ set: {
1605
+ state: states[0].id
1606
+ }
1607
+ });
1608
+ }, client.transaction());
1609
+ await tx.commit();
1610
+ toast.push({
1611
+ title: "Corrected ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1612
+ status: "success"
1613
+ });
1614
+ }, [client, states, toast]);
1615
+ const removeUsersFromDocuments = React__default.default.useCallback(async ids => {
1616
+ toast.push({
1617
+ title: "Removing users...",
1618
+ status: "info"
1619
+ });
1620
+ const tx = ids.reduce((item, documentId) => {
1621
+ var _a, _b;
1622
+ const {
1623
+ assignees
1624
+ } = (_b = (_a = data.find(d => d._id === documentId)) == null ? void 0 : _a._metadata) != null ? _b : {};
1625
+ const validAssignees = (assignees == null ? void 0 : assignees.length) ?
1626
+ // eslint-disable-next-line max-nested-callbacks
1627
+ assignees.filter(a => {
1628
+ var _a2;
1629
+ return (_a2 = userList.find(u => u.id === a)) == null ? void 0 : _a2.id;
1630
+ }) : [];
1631
+ return item.patch("workflow-metadata.".concat(documentId), {
1632
+ set: {
1633
+ assignees: validAssignees
1634
+ }
1635
+ });
1636
+ }, client.transaction());
1637
+ await tx.commit();
1638
+ toast.push({
1639
+ title: "Corrected ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1640
+ status: "success"
1641
+ });
1642
+ }, [client, data, toast, userList]);
1643
+ const addOrderToDocuments = React__default.default.useCallback(async ids => {
1644
+ toast.push({
1645
+ title: "Adding ordering...",
1646
+ status: "info"
1647
+ });
1648
+ const [firstOrder, secondOrder] = [...data].slice(0, 2).map(d => {
1649
+ var _a;
1650
+ return (_a = d._metadata) == null ? void 0 : _a.orderRank;
1651
+ });
1652
+ const minLexo = firstOrder ? lexorank.LexoRank.parse(firstOrder) : void 0;
1653
+ const maxLexo = secondOrder ? lexorank.LexoRank.parse(secondOrder) : void 0;
1654
+ const ranks = generateMultipleOrderRanks(ids.length, minLexo, maxLexo);
1655
+ const tx = client.transaction();
1656
+ for (let index = 0; index < ids.length; index += 1) {
1657
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1658
+ set: {
1659
+ orderRank: ranks[index].toString()
1660
+ }
1661
+ });
1662
+ }
1663
+ await tx.commit();
1664
+ toast.push({
1665
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1666
+ status: "success"
1667
+ });
1668
+ }, [data, client, toast]);
1669
+ const resetOrderOfAllDocuments = React__default.default.useCallback(async ids => {
1670
+ toast.push({
1671
+ title: "Adding ordering...",
1672
+ status: "info"
1673
+ });
1674
+ const ranks = generateMultipleOrderRanks(ids.length);
1675
+ const tx = client.transaction();
1676
+ for (let index = 0; index < ids.length; index += 1) {
1677
+ tx.patch("workflow-metadata.".concat(ids[index]), {
1678
+ set: {
1679
+ orderRank: ranks[index].toString()
1680
+ }
1681
+ });
1682
+ }
1683
+ await tx.commit();
1684
+ toast.push({
1685
+ title: "Added order to ".concat(ids.length === 1 ? "1 Document" : "".concat(ids.length, " Documents")),
1686
+ status: "success"
1687
+ });
1688
+ }, [data, client, toast]);
1689
+ const orphanedMetadataDocumentIds = React__default.default.useMemo(() => {
1690
+ return data.length ? data.filter(doc => !(doc == null ? void 0 : doc._id)).map(doc => doc._metadata.documentId) : [];
1691
+ }, [data]);
1692
+ const handleOrphans = React__default.default.useCallback(() => {
1693
+ toast.push({
1694
+ title: "Removing orphaned metadata...",
1695
+ status: "info"
1696
+ });
1697
+ const tx = client.transaction();
1698
+ orphanedMetadataDocumentIds.forEach(id => {
1699
+ tx.delete("workflow-metadata.".concat(id));
1700
+ });
1701
+ tx.commit();
1702
+ toast.push({
1703
+ title: "Removed ".concat(orphanedMetadataDocumentIds.length, " orphaned metadata documents"),
1704
+ status: "success"
1705
+ });
1706
+ }, [client, orphanedMetadataDocumentIds, toast]);
1707
+ return /* @__PURE__ */jsxRuntime.jsxs(FloatingCard, {
1708
+ children: [documentsWithoutValidMetadataIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1709
+ tone: "caution",
1710
+ mode: "ghost",
1711
+ onClick: () => correctDocuments(documentsWithoutValidMetadataIds),
1712
+ text: documentsWithoutValidMetadataIds.length === 1 ? "Correct 1 Document State" : "Correct ".concat(documentsWithoutValidMetadataIds.length, " Document States")
1713
+ }) : null, documentsWithInvalidUserIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1714
+ tone: "caution",
1715
+ mode: "ghost",
1716
+ onClick: () => removeUsersFromDocuments(documentsWithInvalidUserIds),
1717
+ text: documentsWithInvalidUserIds.length === 1 ? "Remove Invalid Users from 1 Document" : "Remove Invalid Users from ".concat(documentsWithInvalidUserIds.length, " Documents")
1718
+ }) : null, documentsWithoutOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1719
+ tone: "caution",
1720
+ mode: "ghost",
1721
+ onClick: () => addOrderToDocuments(documentsWithoutOrderIds),
1722
+ text: documentsWithoutOrderIds.length === 1 ? "Set Order for 1 Document" : "Set Order for ".concat(documentsWithoutOrderIds.length, " Documents")
1723
+ }) : null, documentsWithDuplicatedOrderIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment, {
1724
+ children: [/* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1725
+ tone: "caution",
1726
+ mode: "ghost",
1727
+ onClick: () => addOrderToDocuments(documentsWithDuplicatedOrderIds),
1728
+ text: documentsWithDuplicatedOrderIds.length === 1 ? "Set Unique Order for 1 Document" : "Set Unique Order for ".concat(documentsWithDuplicatedOrderIds.length, " Documents")
1729
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1730
+ tone: "caution",
1731
+ mode: "ghost",
1732
+ onClick: () => resetOrderOfAllDocuments(data.map(doc => {
1733
+ var _a;
1734
+ return String((_a = doc._metadata) == null ? void 0 : _a.documentId);
1735
+ })),
1736
+ text: data.length === 1 ? "Reset Order for 1 Document" : "Reset Order for all ".concat(data.length, " Documents")
1737
+ })]
1738
+ }) : null, orphanedMetadataDocumentIds.length > 0 ? /* @__PURE__ */jsxRuntime.jsx(ui.Button, {
1739
+ text: "Cleanup orphaned metadata",
1740
+ onClick: handleOrphans,
1741
+ tone: "caution",
1742
+ mode: "ghost"
1743
+ }) : null]
1744
+ });
1745
+ }
1746
+ function WorkflowTool(props) {
1747
+ var _a, _b, _c;
1748
+ const {
1749
+ schemaTypes = [],
1750
+ states = []
1751
+ } = (_b = (_a = props == null ? void 0 : props.tool) == null ? void 0 : _a.options) != null ? _b : {};
1752
+ const isDarkMode = ui.useTheme().sanity.color.dark;
1753
+ const defaultCardTone = isDarkMode ? "default" : "transparent";
1754
+ const toast = ui.useToast();
1755
+ const userList = sanityPluginUtils.useProjectUsers({
1756
+ apiVersion: API_VERSION
1757
+ });
1758
+ const user = sanity.useCurrentUser();
1759
+ const userRoleNames = ((_c = user == null ? void 0 : user.roles) == null ? void 0 : _c.length) ? user == null ? void 0 : user.roles.map(r => r.name) : [];
1760
+ const {
1761
+ workflowData,
1762
+ operations
1763
+ } = useWorkflowDocuments(schemaTypes);
1764
+ const [patchingIds, setPatchingIds] = React__default.default.useState([]);
1765
+ const {
1766
+ data,
1767
+ loading,
1768
+ error
1769
+ } = workflowData;
1770
+ const {
1771
+ move
1772
+ } = operations;
1773
+ const [undroppableStates, setUndroppableStates] = React__default.default.useState([]);
1774
+ const [draggingFrom, setDraggingFrom] = React__default.default.useState("");
1775
+ const handleDragStart = React__default.default.useCallback(start => {
1776
+ var _a2, _b2;
1777
+ const {
1778
+ draggableId,
1779
+ source
1780
+ } = start;
1781
+ const {
1782
+ droppableId: currentStateId
1783
+ } = source;
1784
+ setDraggingFrom(currentStateId);
1785
+ const document = data.find(item => {
1786
+ var _a3;
1787
+ return ((_a3 = item._metadata) == null ? void 0 : _a3.documentId) === draggableId;
1788
+ });
1789
+ const state = states.find(s => s.id === currentStateId);
1790
+ if (!document || !state) return;
1791
+ const undroppableStateIds = [];
1792
+ const statesThatRequireAssignmentIds = states.filter(s => s.requireAssignment).map(s => s.id);
1793
+ if (statesThatRequireAssignmentIds.length) {
1794
+ const documentAssignees = (_b2 = (_a2 = document._metadata) == null ? void 0 : _a2.assignees) != null ? _b2 : [];
1795
+ const userIsAssignedToDocument = (user == null ? void 0 : user.id) ? documentAssignees.includes(user.id) : false;
1796
+ if (!userIsAssignedToDocument) {
1797
+ undroppableStateIds.push(...statesThatRequireAssignmentIds);
1798
+ }
1799
+ }
1800
+ const statesThatCannotBeTransitionedToIds = state.transitions && state.transitions.length ? states.filter(s => {
1801
+ var _a3;
1802
+ return !((_a3 = state.transitions) == null ? void 0 : _a3.includes(s.id));
1803
+ }).map(s => s.id) : [];
1804
+ if (statesThatCannotBeTransitionedToIds.length) {
1805
+ undroppableStateIds.push(...statesThatCannotBeTransitionedToIds);
1806
+ }
1807
+ const undroppableExceptSelf = undroppableStateIds.filter(id => id !== currentStateId);
1808
+ if (undroppableExceptSelf.length) {
1809
+ setUndroppableStates(undroppableExceptSelf);
1810
+ }
1811
+ }, [data, states, user]);
1812
+ const handleDragEnd = React__default.default.useCallback(async result => {
1813
+ var _a2, _b2, _c2, _d, _e, _f;
1814
+ setUndroppableStates([]);
1815
+ setDraggingFrom("");
1816
+ const {
1817
+ draggableId,
1818
+ source,
1819
+ destination
1820
+ } = result;
1821
+ if (
1822
+ // No destination?
1823
+ !destination ||
1824
+ // No change in position?
1825
+ destination.droppableId === source.droppableId && destination.index === source.index) {
1826
+ return;
1827
+ }
1828
+ const destinationStateItems = [...filterItemsAndSort(data, destination.droppableId, [], null)];
1829
+ const destinationStateIndex = states.findIndex(s => s.id === destination.droppableId);
1830
+ const globalStateMinimumRank = data[0]._metadata.orderRank;
1831
+ const globalStateMaximumRank = data[data.length - 1]._metadata.orderRank;
1832
+ let newOrder;
1833
+ if (!destinationStateItems.length) {
1834
+ if (destinationStateIndex === 0) {
1835
+ newOrder = lexorank.LexoRank.min().toString();
1836
+ } else {
1837
+ newOrder = lexorank.LexoRank.parse(globalStateMinimumRank).between(lexorank.LexoRank.min()).toString();
1838
+ }
1839
+ } else if (destination.index === 0) {
1840
+ const firstItemOrderRank = (_b2 = (_a2 = [...destinationStateItems].shift()) == null ? void 0 : _a2._metadata) == null ? void 0 : _b2.orderRank;
1841
+ if (firstItemOrderRank && typeof firstItemOrderRank === "string") {
1842
+ newOrder = lexorank.LexoRank.parse(firstItemOrderRank).genPrev().toString();
1843
+ } else if (destinationStateIndex === 0) {
1844
+ newOrder = lexorank.LexoRank.min().toString();
1845
+ } else {
1846
+ newOrder = lexorank.LexoRank.parse(globalStateMinimumRank).between(lexorank.LexoRank.min()).toString();
1847
+ }
1848
+ } else if (destination.index + 1 === destinationStateItems.length) {
1849
+ const lastItemOrderRank = (_d = (_c2 = [...destinationStateItems].pop()) == null ? void 0 : _c2._metadata) == null ? void 0 : _d.orderRank;
1850
+ if (lastItemOrderRank && typeof lastItemOrderRank === "string") {
1851
+ newOrder = lexorank.LexoRank.parse(lastItemOrderRank).genNext().toString();
1852
+ } else if (destinationStateIndex === states.length - 1) {
1853
+ newOrder = lexorank.LexoRank.max().toString();
1854
+ } else {
1855
+ newOrder = lexorank.LexoRank.parse(globalStateMaximumRank).between(lexorank.LexoRank.min()).toString();
1856
+ }
1857
+ } else {
1858
+ const itemBefore = destinationStateItems[destination.index - 1];
1859
+ const itemBeforeRank = (_e = itemBefore == null ? void 0 : itemBefore._metadata) == null ? void 0 : _e.orderRank;
1860
+ let itemBeforeRankParsed;
1861
+ if (itemBeforeRank) {
1862
+ itemBeforeRankParsed = lexorank.LexoRank.parse(itemBeforeRank);
1863
+ } else if (destinationStateIndex === 0) {
1864
+ itemBeforeRankParsed = lexorank.LexoRank.min();
1865
+ } else {
1866
+ itemBeforeRankParsed = lexorank.LexoRank.parse(globalStateMinimumRank);
1867
+ }
1868
+ const itemAfter = destinationStateItems[destination.index];
1869
+ const itemAfterRank = (_f = itemAfter == null ? void 0 : itemAfter._metadata) == null ? void 0 : _f.orderRank;
1870
+ let itemAfterRankParsed;
1871
+ if (itemAfterRank) {
1872
+ itemAfterRankParsed = lexorank.LexoRank.parse(itemAfterRank);
1873
+ } else if (destinationStateIndex === states.length - 1) {
1874
+ itemAfterRankParsed = lexorank.LexoRank.max();
1875
+ } else {
1876
+ itemAfterRankParsed = lexorank.LexoRank.parse(globalStateMaximumRank);
1877
+ }
1878
+ newOrder = itemBeforeRankParsed.between(itemAfterRankParsed).toString();
1879
+ }
1880
+ setPatchingIds([...patchingIds, draggableId]);
1881
+ toast.push({
1882
+ status: "info",
1883
+ title: "Updating document state..."
1884
+ });
1885
+ await move(draggableId, destination, states, newOrder);
1886
+ setPatchingIds(ids => ids.filter(id => id !== draggableId));
1887
+ }, [data, patchingIds, toast, move, states]);
1888
+ const uniqueAssignedUsers = React__default.default.useMemo(() => {
1889
+ const uniqueUserIds = data.reduce((acc, item) => {
1890
+ var _a2;
1891
+ const {
1892
+ assignees = []
1893
+ } = (_a2 = item._metadata) != null ? _a2 : {};
1894
+ const newAssignees = (assignees == null ? void 0 : assignees.length) ? assignees.filter(a => !acc.includes(a)) : [];
1895
+ return newAssignees.length ? [...acc, ...newAssignees] : acc;
1896
+ }, []);
1897
+ return userList.filter(u => uniqueUserIds.includes(u.id));
1898
+ }, [data, userList]);
1899
+ const [selectedUserIds, setSelectedUserIds] = React__default.default.useState(uniqueAssignedUsers.map(u => u.id));
1900
+ const toggleSelectedUser = React__default.default.useCallback(userId => {
1901
+ setSelectedUserIds(prev => prev.includes(userId) ? prev.filter(u => u !== userId) : [...prev, userId]);
1902
+ }, []);
1903
+ const resetSelectedUsers = React__default.default.useCallback(() => {
1904
+ setSelectedUserIds([]);
1905
+ }, []);
1906
+ const [selectedSchemaTypes, setSelectedSchemaTypes] = React__default.default.useState(schemaTypes);
1907
+ const toggleSelectedSchemaType = React__default.default.useCallback(schemaType => {
1908
+ setSelectedSchemaTypes(prev => prev.includes(schemaType) ? prev.filter(u => u !== schemaType) : [...prev, schemaType]);
1909
+ }, []);
1910
+ const [invalidDocumentIds, setInvalidDocumentIds] = React__default.default.useState([]);
1911
+ const toggleInvalidDocumentId = React__default.default.useCallback((docId, action) => {
1912
+ setInvalidDocumentIds(prev => action === "ADD" ? [...prev, docId] : prev.filter(id => id !== docId));
1913
+ }, []);
1914
+ const Clone = React__default.default.useCallback((provided, snapshot, rubric) => {
1915
+ const item = data.find(doc => {
1916
+ var _a2;
1917
+ return ((_a2 = doc == null ? void 0 : doc._metadata) == null ? void 0 : _a2.documentId) === rubric.draggableId;
1918
+ });
1919
+ return /* @__PURE__ */jsxRuntime.jsx("div", {
1920
+ ...provided.draggableProps,
1921
+ ...provided.dragHandleProps,
1922
+ ref: provided.innerRef,
1923
+ children: item ? /* @__PURE__ */jsxRuntime.jsx(DocumentCard, {
1924
+ isDragDisabled: false,
1925
+ isPatching: false,
1926
+ userRoleCanDrop: true,
1927
+ isDragging: snapshot.isDragging,
1928
+ item,
1929
+ states,
1930
+ toggleInvalidDocumentId,
1931
+ userList
1932
+ }) : /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.Feedback, {
1933
+ title: "Item not found",
1934
+ tone: "caution"
1935
+ })
1936
+ });
1937
+ }, [data, states, toggleInvalidDocumentId, userList]);
1938
+ if (!(states == null ? void 0 : states.length)) {
1939
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Container, {
1940
+ width: 1,
1941
+ padding: 5,
1942
+ children: /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.Feedback, {
1943
+ tone: "caution",
1944
+ title: "Plugin options error",
1945
+ description: "No States defined in plugin config"
1946
+ })
1947
+ });
1948
+ }
1949
+ if (error && !data.length) {
1950
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Container, {
1951
+ width: 1,
1952
+ padding: 5,
1953
+ children: /* @__PURE__ */jsxRuntime.jsx(sanityPluginUtils.Feedback, {
1954
+ tone: "critical",
1955
+ title: "Error querying for Workflow documents"
1956
+ })
1957
+ });
1958
+ }
1959
+ return /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1960
+ direction: "column",
1961
+ height: "fill",
1962
+ overflow: "hidden",
1963
+ children: [/* @__PURE__ */jsxRuntime.jsx(Verify, {
1964
+ data,
1965
+ userList,
1966
+ states
1967
+ }), /* @__PURE__ */jsxRuntime.jsx(Filters, {
1968
+ uniqueAssignedUsers,
1969
+ selectedUserIds,
1970
+ toggleSelectedUser,
1971
+ resetSelectedUsers,
1972
+ schemaTypes,
1973
+ selectedSchemaTypes,
1974
+ toggleSelectedSchemaType
1975
+ }), /* @__PURE__ */jsxRuntime.jsx(dnd.DragDropContext, {
1976
+ onDragStart: handleDragStart,
1977
+ onDragEnd: handleDragEnd,
1978
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Grid, {
1979
+ columns: states.length,
1980
+ height: "fill",
1981
+ children: states.map((state, stateIndex) => {
1982
+ var _a2, _b2;
1983
+ const userRoleCanDrop = ((_a2 = state == null ? void 0 : state.roles) == null ? void 0 : _a2.length) ? arraysContainMatchingString(state.roles, userRoleNames) : true;
1984
+ const isDropDisabled = !userRoleCanDrop || undroppableStates.includes(state.id);
1985
+ return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
1986
+ borderLeft: stateIndex > 0,
1987
+ tone: defaultCardTone,
1988
+ children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
1989
+ direction: "column",
1990
+ height: "fill",
1991
+ children: [/* @__PURE__ */jsxRuntime.jsx(StateTitle, {
1992
+ state,
1993
+ requireAssignment: (_b2 = state.requireAssignment) != null ? _b2 : false,
1994
+ userRoleCanDrop,
1995
+ isDropDisabled,
1996
+ draggingFrom,
1997
+ documentCount: filterItemsAndSort(data, state.id, selectedUserIds, selectedSchemaTypes).length
1998
+ }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1999
+ flex: 1,
2000
+ children: /* @__PURE__ */jsxRuntime.jsx(dnd.Droppable, {
2001
+ droppableId: state.id,
2002
+ isDropDisabled,
2003
+ mode: "virtual",
2004
+ renderClone: Clone,
2005
+ children: (provided, snapshot) => /* @__PURE__ */jsxRuntime.jsxs(ui.Card, {
2006
+ ref: provided.innerRef,
2007
+ tone: snapshot.isDraggingOver ? "primary" : defaultCardTone,
2008
+ height: "fill",
2009
+ children: [loading ? /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
2010
+ padding: 5,
2011
+ align: "center",
2012
+ justify: "center",
2013
+ children: /* @__PURE__ */jsxRuntime.jsx(ui.Spinner, {
2014
+ muted: true
2015
+ })
2016
+ }) : null, /* @__PURE__ */jsxRuntime.jsx(DocumentList, {
2017
+ data,
2018
+ invalidDocumentIds,
2019
+ patchingIds,
2020
+ selectedSchemaTypes,
2021
+ selectedUserIds,
2022
+ state,
2023
+ states,
2024
+ toggleInvalidDocumentId,
2025
+ user,
2026
+ userList,
2027
+ userRoleCanDrop
2028
+ })]
2029
+ })
2030
+ })
2031
+ })]
2032
+ })
2033
+ }, state.id);
2034
+ })
2035
+ })
2036
+ })]
2037
+ });
2038
+ }
2039
+ const workflowTool = options => ({
2040
+ name: "workflow",
2041
+ title: "Workflow",
2042
+ component: WorkflowTool,
2043
+ icon: icons.SplitVerticalIcon,
2044
+ options
2045
+ });
2046
+ const workflow = sanity.definePlugin(function () {
2047
+ let config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_CONFIG;
2048
+ const {
2049
+ schemaTypes,
2050
+ states
2051
+ } = {
2052
+ ...DEFAULT_CONFIG,
2053
+ ...config
2054
+ };
2055
+ if (!(states == null ? void 0 : states.length)) {
2056
+ throw new Error("Workflow plugin: Missing \"states\" in config");
2057
+ }
2058
+ if (!(schemaTypes == null ? void 0 : schemaTypes.length)) {
2059
+ throw new Error("Workflow plugin: Missing \"schemaTypes\" in config");
2060
+ }
2061
+ return {
2062
+ name: "sanity-plugin-workflow",
2063
+ schema: {
2064
+ types: [metadata(states)]
2065
+ },
2066
+ // TODO: Remove 'workflow.metadata' from list of new document types
2067
+ // ...
2068
+ studio: {
2069
+ components: {
2070
+ layout: props => WorkflowProvider({
2071
+ ...props,
2072
+ workflow: {
2073
+ schemaTypes,
2074
+ states
2075
+ }
2076
+ })
2077
+ }
2078
+ },
2079
+ form: {
2080
+ components: {
2081
+ input: props => {
2082
+ if (props.id === "root" && sanity.isObjectInputProps(props) && schemaTypes.includes(props.schemaType.name)) {
2083
+ return WorkflowSignal(props);
2084
+ }
2085
+ return props.renderDefault(props);
2086
+ }
2087
+ }
2088
+ },
2089
+ document: {
2090
+ actions: (prev, context) => {
2091
+ if (!schemaTypes.includes(context.schemaType)) {
2092
+ return prev;
2093
+ }
2094
+ return [props => BeginWorkflow(props), props => AssignWorkflow(props), ...states.map(state => props => UpdateWorkflow(props, state)), props => CompleteWorkflow(props), ...prev];
2095
+ },
2096
+ badges: (prev, context) => {
2097
+ if (!schemaTypes.includes(context.schemaType)) {
2098
+ return prev;
2099
+ }
2100
+ const {
2101
+ documentId,
2102
+ currentUser
2103
+ } = context;
2104
+ if (!documentId) {
2105
+ return prev;
2106
+ }
2107
+ return [() => StateBadge(documentId), () => AssigneesBadge(documentId, currentUser), ...prev];
2108
+ }
2109
+ },
2110
+ tools: [
2111
+ // TODO: These configs could be read from Context
2112
+ workflowTool({
2113
+ schemaTypes,
2114
+ states
2115
+ })]
2116
+ };
2117
+ });
2118
+ exports.workflow = workflow;
2119
+ //# sourceMappingURL=index.js.map