sanity-plugin-workflow 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,16 +1,19 @@
1
- import { useClient, useCurrentUser, useValidationStatus, useSchema, Preview, useFormValue, defineType, defineField, UserAvatar, useTimeAgo, TextWithTone, definePlugin, isObjectInputProps } from "sanity";
2
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
3
- import { UsersIcon, SplitVerticalIcon, CheckmarkIcon, ArrowRightIcon, ArrowLeftIcon, EditIcon, AddIcon, PublishIcon, ErrorOutlineIcon, WarningOutlineIcon, DragHandleIcon, UserIcon, ResetIcon, InfoOutlineIcon } from "@sanity/icons";
4
- import { createContext, useContext, useState, useCallback, useEffect, useMemo, useRef } from "react";
5
- import { UserSelectMenu, useListeningQuery, useProjectUsers, Feedback } from "sanity-plugin-utils";
2
+ import { useCurrentUser, useClient, useValidationStatus, getPublishedId, useSchema, Preview, useFormValue, defineType, defineField, UserAvatar, useTimeAgo, TextWithTone, definePlugin, isObjectInputProps } from "sanity";
6
3
  import { c } from "react-compiler-runtime";
4
+ import { ArrowRightIcon, ArrowLeftIcon, UsersIcon, SplitVerticalIcon, CheckmarkIcon, EditIcon, AddIcon, PublishIcon, ErrorOutlineIcon, WarningOutlineIcon, DragHandleIcon, UserIcon, ResetIcon, InfoOutlineIcon } from "@sanity/icons";
7
5
  import { useToast, Button, Spinner, Card, Flex, Box, Text, useClickOutside, Grid, Popover, Tooltip, useTheme, Stack, MenuButton, Menu, Badge, Container } from "@sanity/ui";
6
+ import { createContext, useContext, useState, useEffect, useMemo, useRef } from "react";
7
+ import { useListeningQuery, UserSelectMenu, useProjectUsers, Feedback } from "sanity-plugin-utils";
8
8
  import { LexoRank } from "lexorank";
9
9
  import { useRouter } from "sanity/router";
10
10
  import { Draggable, Droppable, DragDropContext } from "@hello-pangea/dnd";
11
11
  import { useVirtualizer } from "@tanstack/react-virtual";
12
- import { styled, css } from "styled-components";
12
+ import { styled } from "styled-components";
13
13
  import { AnimatePresence, motion } from "motion/react";
14
+ function defineStates(states) {
15
+ return states;
16
+ }
14
17
  const API_VERSION = "2023-01-01", DEFAULT_CONFIG = {
15
18
  schemaTypes: [],
16
19
  states: [{
@@ -34,70 +37,6 @@ const API_VERSION = "2023-01-01", DEFAULT_CONFIG = {
34
37
  requireAssignment: !0
35
38
  }]
36
39
  };
37
- function UserAssignment(props) {
38
- const $ = c(24), {
39
- assignees,
40
- userList,
41
- documentId
42
- } = props;
43
- let t0;
44
- $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t0 = {
45
- apiVersion: API_VERSION
46
- }, $[0] = t0) : t0 = $[0];
47
- const client = useClient(t0), toast = useToast();
48
- let t1;
49
- $[1] !== client || $[2] !== documentId || $[3] !== toast || $[4] !== userList ? (t1 = (userId) => {
50
- const user = userList.find((u) => u.id === userId);
51
- return !userId || !user ? toast.push({
52
- status: "error",
53
- title: "Could not find User"
54
- }) : client.patch(`workflow-metadata.${documentId}`).setIfMissing({
55
- assignees: []
56
- }).insert("after", "assignees[-1]", [userId]).commit().then(() => toast.push({
57
- title: `Added ${user.displayName} to assignees`,
58
- status: "success"
59
- })).catch((err) => (console.error(err), toast.push({
60
- title: "Failed to add assignee",
61
- description: userId,
62
- status: "error"
63
- })));
64
- }, $[1] = client, $[2] = documentId, $[3] = toast, $[4] = userList, $[5] = t1) : t1 = $[5];
65
- const addAssignee = t1;
66
- let t2;
67
- $[6] !== client || $[7] !== documentId || $[8] !== toast || $[9] !== userList ? (t2 = (userId_0) => {
68
- const user_0 = userList.find((u_0) => u_0.id === userId_0);
69
- return !userId_0 || !user_0 ? toast.push({
70
- status: "error",
71
- title: "Could not find User"
72
- }) : client.patch(`workflow-metadata.${documentId}`).unset([`assignees[@ == "${userId_0}"]`]).commit().then(() => toast.push({
73
- title: `Removed ${user_0.displayName} from assignees`,
74
- status: "success"
75
- })).catch((err_0) => (console.error(err_0), toast.push({
76
- title: "Failed to remove assignee",
77
- description: documentId,
78
- status: "error"
79
- })));
80
- }, $[6] = client, $[7] = documentId, $[8] = toast, $[9] = userList, $[10] = t2) : t2 = $[10];
81
- const removeAssignee = t2;
82
- let t3;
83
- $[11] !== client || $[12] !== documentId || $[13] !== toast ? (t3 = () => client.patch(`workflow-metadata.${documentId}`).unset(["assignees"]).commit().then(() => toast.push({
84
- title: "Cleared assignees",
85
- status: "success"
86
- })).catch((err_1) => (console.error(err_1), toast.push({
87
- title: "Failed to clear assignees",
88
- description: documentId,
89
- status: "error"
90
- }))), $[11] = client, $[12] = documentId, $[13] = toast, $[14] = t3) : t3 = $[14];
91
- const clearAssignees = t3;
92
- let t4;
93
- $[15] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t4 = {
94
- maxHeight: 300
95
- }, $[15] = t4) : t4 = $[15];
96
- let t5;
97
- $[16] !== assignees ? (t5 = assignees || [], $[16] = assignees, $[17] = t5) : t5 = $[17];
98
- let t6;
99
- return $[18] !== addAssignee || $[19] !== clearAssignees || $[20] !== removeAssignee || $[21] !== t5 || $[22] !== userList ? (t6 = /* @__PURE__ */ jsx(UserSelectMenu, { style: t4, value: t5, userList, onAdd: addAssignee, onClear: clearAssignees, onRemove: removeAssignee }), $[18] = addAssignee, $[19] = clearAssignees, $[20] = removeAssignee, $[21] = t5, $[22] = userList, $[23] = t6) : t6 = $[23], t6;
100
- }
101
40
  function useWorkflowMetadata(ids) {
102
41
  const $ = c(12);
103
42
  let t0;
@@ -134,7 +73,7 @@ function useWorkflowMetadata(ids) {
134
73
  break bb0;
135
74
  }
136
75
  let t42;
137
- $[6] !== rawData ? (t42 = rawData.reduce(_temp$6, {}), $[6] = rawData, $[7] = t42) : t42 = $[7], t3 = t42;
76
+ $[6] !== rawData ? (t42 = rawData.reduce(_temp$8, {}), $[6] = rawData, $[7] = t42) : t42 = $[7], t3 = t42;
138
77
  }
139
78
  const keyedMetadata = t3;
140
79
  let t4;
@@ -144,7 +83,7 @@ function useWorkflowMetadata(ids) {
144
83
  error
145
84
  }, $[8] = error, $[9] = keyedMetadata, $[10] = loading, $[11] = t4) : t4 = $[11], t4;
146
85
  }
147
- function _temp$6(acc, cur) {
86
+ function _temp$8(acc, cur) {
148
87
  return {
149
88
  ...acc,
150
89
  [cur.documentId]: cur
@@ -183,63 +122,224 @@ function WorkflowProvider(props) {
183
122
  error
184
123
  } = useWorkflowMetadata(ids);
185
124
  let t3;
186
- $[3] !== data || $[4] !== error || $[5] !== ids || $[6] !== loading || $[7] !== props.workflow.schemaTypes || $[8] !== props.workflow.states ? (t3 = {
125
+ $[3] !== data || $[4] !== error || $[5] !== ids || $[6] !== loading || $[7] !== props.schemaTypes || $[8] !== props.states ? (t3 = {
187
126
  data,
188
127
  loading,
189
128
  error,
190
129
  ids,
191
130
  addId,
192
131
  removeId,
193
- states: props.workflow.states,
194
- schemaTypes: props.workflow.schemaTypes
195
- }, $[3] = data, $[4] = error, $[5] = ids, $[6] = loading, $[7] = props.workflow.schemaTypes, $[8] = props.workflow.states, $[9] = t3) : t3 = $[9];
132
+ states: props.states,
133
+ schemaTypes: props.schemaTypes
134
+ }, $[3] = data, $[4] = error, $[5] = ids, $[6] = loading, $[7] = props.schemaTypes, $[8] = props.states, $[9] = t3) : t3 = $[9];
196
135
  let t4;
197
136
  $[10] !== props ? (t4 = props.renderDefault(props), $[10] = props, $[11] = t4) : t4 = $[11];
198
137
  let t5;
199
138
  return $[12] !== t3 || $[13] !== t4 ? (t5 = /* @__PURE__ */ jsx(WorkflowContext.Provider, { value: t3, children: t4 }), $[12] = t3, $[13] = t4, $[14] = t5) : t5 = $[14], t5;
200
139
  }
201
- function AssignWorkflow(props) {
140
+ function arraysContainMatchingString(one, two) {
141
+ return one.some((item) => two.includes(item));
142
+ }
143
+ function useUpdateWorkflow(t0, actionState) {
144
+ const $ = c(28), {
145
+ id,
146
+ type,
147
+ onComplete
148
+ } = t0, user = useCurrentUser();
149
+ let t1;
150
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t1 = {
151
+ apiVersion: API_VERSION
152
+ }, $[0] = t1) : t1 = $[0];
153
+ const client = useClient(t1), toast = useToast(), currentUser = useCurrentUser(), {
154
+ metadata: metadata2,
155
+ loading,
156
+ error,
157
+ states
158
+ } = useWorkflowContext(id);
159
+ let t2;
160
+ $[1] !== metadata2?.state ? (t2 = (s) => s.id === metadata2?.state, $[1] = metadata2?.state, $[2] = t2) : t2 = $[2];
161
+ const currentState = states.find(t2);
162
+ let t3;
163
+ $[3] !== metadata2 ? (t3 = metadata2 ?? {}, $[3] = metadata2, $[4] = t3) : t3 = $[4];
202
164
  const {
165
+ assignees: t4
166
+ } = t3;
167
+ let t5;
168
+ $[5] !== t4 ? (t5 = t4 === void 0 ? [] : t4, $[5] = t4, $[6] = t5) : t5 = $[6];
169
+ const assignees = t5, {
170
+ validation,
171
+ isValidating
172
+ } = useValidationStatus(id, type), hasValidationErrors = currentState?.requireValidation && !isValidating && validation?.length > 0 && validation.find(_temp$7);
173
+ error && console.error(error);
174
+ let t6;
175
+ $[7] !== client || $[8] !== onComplete || $[9] !== toast ? (t6 = (documentId, newState) => {
176
+ client.patch(`workflow-metadata.${documentId}`).set({
177
+ state: newState.id
178
+ }).commit().then(() => {
179
+ onComplete(), toast.push({
180
+ status: "success",
181
+ title: `Document state now "${newState.title}"`
182
+ });
183
+ }).catch((err) => {
184
+ onComplete(), console.error(err), toast.push({
185
+ status: "error",
186
+ title: "Document state update failed"
187
+ });
188
+ });
189
+ }, $[7] = client, $[8] = onComplete, $[9] = toast, $[10] = t6) : t6 = $[10];
190
+ const onHandle = t6;
191
+ if (!metadata2 || currentState && currentState.id === actionState.id)
192
+ return null;
193
+ const currentStateIndex = states.findIndex((s_0) => s_0.id === currentState?.id), direction = states.findIndex((s_1) => s_1.id === actionState.id) > currentStateIndex ? "promote" : "demote", DirectionIcon = direction === "promote" ? ArrowRightIcon : ArrowLeftIcon, directionLabel = direction === "promote" ? "Promote" : "Demote";
194
+ let t7;
195
+ $[11] !== actionState.roles || $[12] !== user ? (t7 = user?.roles?.length && actionState?.roles?.length ? arraysContainMatchingString(user.roles.map(_temp2$3), actionState.roles) : actionState?.roles?.length !== 0, $[11] = actionState.roles, $[12] = user, $[13] = t7) : t7 = $[13];
196
+ const userRoleCanUpdateState = t7, actionStateIsAValidTransition = currentState?.id && currentState?.transitions?.length ? currentState.transitions.includes(actionState.id) : !0;
197
+ let t8;
198
+ $[14] !== actionState.requireAssignment || $[15] !== assignees || $[16] !== currentUser ? (t8 = actionState.requireAssignment ? currentUser && assignees?.length && assignees.includes(currentUser.id) : !0, $[14] = actionState.requireAssignment, $[15] = assignees, $[16] = currentUser, $[17] = t8) : t8 = $[17];
199
+ const userAssignmentCanUpdateState = t8;
200
+ let title = `${directionLabel} State to "${actionState.title}"`;
201
+ userRoleCanUpdateState ? actionStateIsAValidTransition ? userAssignmentCanUpdateState ? currentState?.requireValidation && isValidating ? title = `Document is validating, cannot ${directionLabel} State to "${actionState.title}"` : hasValidationErrors && (title = `Document has validation errors, cannot ${directionLabel} State to "${actionState.title}"`) : title = `You must be assigned to the document to ${directionLabel} State to "${actionState.title}"` : title = `You cannot ${directionLabel} State to "${actionState.title}" from "${currentState?.title}"` : title = `Your User role cannot ${directionLabel} State to "${actionState.title}"`;
202
+ const t9 = loading || !!error || currentState?.requireValidation && isValidating || !!hasValidationErrors || !currentState || !userRoleCanUpdateState || !actionStateIsAValidTransition || !userAssignmentCanUpdateState;
203
+ let t10;
204
+ $[18] !== actionState || $[19] !== id || $[20] !== onHandle ? (t10 = () => onHandle(id, actionState), $[18] = actionState, $[19] = id, $[20] = onHandle, $[21] = t10) : t10 = $[21];
205
+ let t11;
206
+ return $[22] !== DirectionIcon || $[23] !== actionState.title || $[24] !== t10 || $[25] !== t9 || $[26] !== title ? (t11 = {
207
+ icon: DirectionIcon,
208
+ disabled: t9,
209
+ title,
210
+ label: actionState.title,
211
+ onHandle: t10
212
+ }, $[22] = DirectionIcon, $[23] = actionState.title, $[24] = t10, $[25] = t9, $[26] = title, $[27] = t11) : t11 = $[27], t11;
213
+ }
214
+ function _temp2$3(r) {
215
+ return r.name;
216
+ }
217
+ function _temp$7(v) {
218
+ return v.level === "error";
219
+ }
220
+ function defineUpdateWorkflow(actionState) {
221
+ return (props) => useUpdateWorkflow(props, actionState);
222
+ }
223
+ function UserAssignment(props) {
224
+ const $ = c(24), {
225
+ assignees,
226
+ userList,
227
+ documentId
228
+ } = props;
229
+ let t0;
230
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t0 = {
231
+ apiVersion: API_VERSION
232
+ }, $[0] = t0) : t0 = $[0];
233
+ const client = useClient(t0), toast = useToast();
234
+ let t1;
235
+ $[1] !== client || $[2] !== documentId || $[3] !== toast || $[4] !== userList ? (t1 = (userId) => {
236
+ const user = userList.find((u) => u.id === userId);
237
+ return !userId || !user ? toast.push({
238
+ status: "error",
239
+ title: "Could not find User"
240
+ }) : client.patch(`workflow-metadata.${documentId}`).setIfMissing({
241
+ assignees: []
242
+ }).insert("after", "assignees[-1]", [userId]).commit().then(() => toast.push({
243
+ title: `Added ${user.displayName} to assignees`,
244
+ status: "success"
245
+ })).catch((err) => (console.error(err), toast.push({
246
+ title: "Failed to add assignee",
247
+ description: userId,
248
+ status: "error"
249
+ })));
250
+ }, $[1] = client, $[2] = documentId, $[3] = toast, $[4] = userList, $[5] = t1) : t1 = $[5];
251
+ const addAssignee = t1;
252
+ let t2;
253
+ $[6] !== client || $[7] !== documentId || $[8] !== toast || $[9] !== userList ? (t2 = (userId_0) => {
254
+ const user_0 = userList.find((u_0) => u_0.id === userId_0);
255
+ return !userId_0 || !user_0 ? toast.push({
256
+ status: "error",
257
+ title: "Could not find User"
258
+ }) : client.patch(`workflow-metadata.${documentId}`).unset([`assignees[@ == "${userId_0}"]`]).commit().then(() => toast.push({
259
+ title: `Removed ${user_0.displayName} from assignees`,
260
+ status: "success"
261
+ })).catch((err_0) => (console.error(err_0), toast.push({
262
+ title: "Failed to remove assignee",
263
+ description: documentId,
264
+ status: "error"
265
+ })));
266
+ }, $[6] = client, $[7] = documentId, $[8] = toast, $[9] = userList, $[10] = t2) : t2 = $[10];
267
+ const removeAssignee = t2;
268
+ let t3;
269
+ $[11] !== client || $[12] !== documentId || $[13] !== toast ? (t3 = () => client.patch(`workflow-metadata.${documentId}`).unset(["assignees"]).commit().then(() => toast.push({
270
+ title: "Cleared assignees",
271
+ status: "success"
272
+ })).catch((err_1) => (console.error(err_1), toast.push({
273
+ title: "Failed to clear assignees",
274
+ description: documentId,
275
+ status: "error"
276
+ }))), $[11] = client, $[12] = documentId, $[13] = toast, $[14] = t3) : t3 = $[14];
277
+ const clearAssignees = t3;
278
+ let t4;
279
+ $[15] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t4 = {
280
+ maxHeight: 300
281
+ }, $[15] = t4) : t4 = $[15];
282
+ let t5;
283
+ $[16] !== assignees ? (t5 = assignees || [], $[16] = assignees, $[17] = t5) : t5 = $[17];
284
+ let t6;
285
+ return $[18] !== addAssignee || $[19] !== clearAssignees || $[20] !== removeAssignee || $[21] !== t5 || $[22] !== userList ? (t6 = /* @__PURE__ */ jsx(UserSelectMenu, { style: t4, value: t5, userList, onAdd: addAssignee, onClear: clearAssignees, onRemove: removeAssignee }), $[18] = addAssignee, $[19] = clearAssignees, $[20] = removeAssignee, $[21] = t5, $[22] = userList, $[23] = t6) : t6 = $[23], t6;
286
+ }
287
+ function useAssignWorkflow(t0) {
288
+ const $ = c(11), {
203
289
  id
204
- } = props, {
290
+ } = t0, {
205
291
  metadata: metadata2,
206
292
  loading,
207
293
  error
208
- } = useWorkflowContext(id), [isDialogOpen, setDialogOpen] = useState(!1), userList = useProjectUsers({
294
+ } = useWorkflowContext(id), [isDialogOpen, setDialogOpen] = useState(!1);
295
+ let t1;
296
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t1 = {
209
297
  apiVersion: API_VERSION
210
- });
211
- return error && console.error(error), metadata2 ? {
298
+ }, $[0] = t1) : t1 = $[0];
299
+ const userList = useProjectUsers(t1);
300
+ if (error && console.error(error), !metadata2)
301
+ return null;
302
+ const t2 = !metadata2 || loading || !!error, t3 = metadata2 ? null : "Document is not in Workflow";
303
+ let t4;
304
+ $[1] !== id || $[2] !== isDialogOpen || $[3] !== metadata2 || $[4] !== userList ? (t4 = isDialogOpen && {
305
+ type: "popover",
306
+ onClose: () => {
307
+ setDialogOpen(!1);
308
+ },
309
+ content: /* @__PURE__ */ jsx(UserAssignment, { userList, assignees: metadata2?.assignees?.length > 0 ? metadata2.assignees : [], documentId: id })
310
+ }, $[1] = id, $[2] = isDialogOpen, $[3] = metadata2, $[4] = userList, $[5] = t4) : t4 = $[5];
311
+ let t5;
312
+ $[6] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t5 = () => {
313
+ setDialogOpen(!0);
314
+ }, $[6] = t5) : t5 = $[6];
315
+ let t6;
316
+ return $[7] !== t2 || $[8] !== t3 || $[9] !== t4 ? (t6 = {
212
317
  icon: UsersIcon,
213
- type: "dialog",
214
- disabled: !metadata2 || loading || error,
318
+ disabled: t2,
215
319
  label: "Assign",
216
- title: metadata2 ? null : "Document is not in Workflow",
217
- dialog: isDialogOpen && {
218
- type: "popover",
219
- onClose: () => {
220
- setDialogOpen(!1);
221
- },
222
- content: /* @__PURE__ */ jsx(UserAssignment, { userList, assignees: metadata2?.assignees?.length > 0 ? metadata2.assignees : [], documentId: id })
223
- },
224
- onHandle: () => {
225
- setDialogOpen(!0);
226
- }
227
- } : null;
320
+ title: t3,
321
+ dialog: t4,
322
+ onHandle: t5
323
+ }, $[7] = t2, $[8] = t3, $[9] = t4, $[10] = t6) : t6 = $[10], t6;
228
324
  }
229
- function BeginWorkflow(props) {
230
- const {
325
+ function useBeginWorkflow(t0) {
326
+ const $ = c(11), {
231
327
  id,
232
328
  draft
233
- } = props, {
329
+ } = t0, {
234
330
  metadata: metadata2,
235
331
  loading,
236
332
  error,
237
333
  states
238
- } = useWorkflowContext(id), client = useClient({
334
+ } = useWorkflowContext(id);
335
+ let t1;
336
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t1 = {
239
337
  apiVersion: API_VERSION
240
- }), toast = useToast(), [beginning, setBeginning] = useState(!1), [complete, setComplete] = useState(!1);
338
+ }, $[0] = t1) : t1 = $[0];
339
+ const client = useClient(t1), toast = useToast(), [beginning, setBeginning] = useState(!1), [complete, setComplete] = useState(!1);
241
340
  error && console.error(error);
242
- const handle = useCallback(async () => {
341
+ let t2;
342
+ $[1] !== client || $[2] !== id || $[3] !== states?.[0]?.id || $[4] !== states?.[0]?.title || $[5] !== toast ? (t2 = async () => {
243
343
  setBeginning(!0);
244
344
  const lowestOrderFirstState = await client.fetch('*[_type == "workflow.metadata" && state == $state]|order(orderRank)[0].orderRank', {
245
345
  state: states?.[0]?.id
@@ -257,16 +357,18 @@ function BeginWorkflow(props) {
257
357
  description: `Document is now "${states?.[0]?.title}"`
258
358
  }), setBeginning(!1), setComplete(!0);
259
359
  });
260
- }, [id, states, client, toast]);
261
- return !draft || complete || metadata2 ? null : {
360
+ }, $[1] = client, $[2] = id, $[3] = states?.[0]?.id, $[4] = states?.[0]?.title, $[5] = toast, $[6] = t2) : t2 = $[6];
361
+ const handle = t2;
362
+ if (!draft || complete || metadata2)
363
+ return null;
364
+ const t3 = metadata2 || loading || !!error || beginning || complete, t4 = beginning ? "Beginning..." : "Begin Workflow";
365
+ let t5;
366
+ return $[7] !== handle || $[8] !== t3 || $[9] !== t4 ? (t5 = {
262
367
  icon: SplitVerticalIcon,
263
- type: "dialog",
264
- disabled: metadata2 || loading || error || beginning || complete,
265
- label: beginning ? "Beginning..." : "Begin Workflow",
266
- onHandle: async () => {
267
- await handle();
268
- }
269
- };
368
+ disabled: t3,
369
+ label: t4,
370
+ onHandle: handle
371
+ }, $[7] = handle, $[8] = t3, $[9] = t4, $[10] = t5) : t5 = $[10], t5;
270
372
  }
271
373
  const handleDeleteMetadata = async (client, toast, id) => {
272
374
  try {
@@ -281,146 +383,126 @@ const handleDeleteMetadata = async (client, toast, id) => {
281
383
  });
282
384
  }
283
385
  };
284
- function CompleteWorkflow(props) {
285
- const {
286
- id
287
- } = props, {
386
+ function useCompleteWorkflow(t0) {
387
+ const $ = c(17), {
388
+ id,
389
+ onComplete
390
+ } = t0, {
288
391
  metadata: metadata2,
289
392
  loading,
290
393
  error,
291
394
  states
292
- } = useWorkflowContext(id), client = useClient({
395
+ } = useWorkflowContext(id);
396
+ let t1;
397
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t1 = {
293
398
  apiVersion: API_VERSION
294
- }), toast = useToast();
399
+ }, $[0] = t1) : t1 = $[0];
400
+ const client = useClient(t1), toast = useToast();
295
401
  error && console.error(error);
296
- const handle = useCallback(async () => {
402
+ let t2;
403
+ $[1] !== client || $[2] !== id || $[3] !== toast ? (t2 = async () => {
297
404
  await handleDeleteMetadata(client, toast, id);
298
- }, [client, toast, id]);
405
+ }, $[1] = client, $[2] = id, $[3] = toast, $[4] = t2) : t2 = $[4];
406
+ const handle = t2;
299
407
  if (!metadata2)
300
408
  return null;
301
- const isLastState = states.find((s) => s.id === metadata2.state)?.id === states?.[states?.length - 1]?.id;
302
- return {
409
+ let t3;
410
+ if ($[5] !== metadata2 || $[6] !== states) {
411
+ let t42;
412
+ $[8] !== metadata2 ? (t42 = (s) => s.id === metadata2.state, $[8] = metadata2, $[9] = t42) : t42 = $[9], t3 = states.find(t42), $[5] = metadata2, $[6] = states, $[7] = t3;
413
+ } else
414
+ t3 = $[7];
415
+ const isLastState = t3?.id === states?.[states?.length - 1]?.id, t4 = loading || !!error || !isLastState, t5 = isLastState ? "Removes the document from the Workflow process" : "Cannot remove from workflow until in the last state";
416
+ let t6;
417
+ $[10] !== handle || $[11] !== onComplete ? (t6 = async () => {
418
+ await handle(), onComplete();
419
+ }, $[10] = handle, $[11] = onComplete, $[12] = t6) : t6 = $[12];
420
+ let t7;
421
+ return $[13] !== t4 || $[14] !== t5 || $[15] !== t6 ? (t7 = {
303
422
  icon: CheckmarkIcon,
304
- type: "dialog",
305
- disabled: loading || error || !isLastState,
423
+ disabled: t4,
306
424
  label: "Complete Workflow",
307
- title: isLastState ? "Removes the document from the Workflow process" : "Cannot remove from workflow until in the last state",
308
- onHandle: async () => {
309
- await handle(), props.onComplete();
310
- },
311
- color: "positive"
312
- };
425
+ title: t5,
426
+ onHandle: t6,
427
+ tone: "positive"
428
+ }, $[13] = t4, $[14] = t5, $[15] = t6, $[16] = t7) : t7 = $[16], t7;
313
429
  }
314
- function arraysContainMatchingString(one, two) {
315
- return one.some((item) => two.includes(item));
316
- }
317
- function UpdateWorkflow(props, actionState) {
318
- const {
319
- id,
320
- type
321
- } = props, user = useCurrentUser(), client = useClient({
322
- apiVersion: API_VERSION
323
- }), toast = useToast(), currentUser = useCurrentUser(), {
324
- metadata: metadata2,
325
- loading,
326
- error,
327
- states
328
- } = useWorkflowContext(id), currentState = states.find((s) => s.id === metadata2?.state), {
329
- assignees = []
330
- } = metadata2 ?? {}, {
331
- validation,
332
- isValidating
333
- } = useValidationStatus(id, type), hasValidationErrors = currentState?.requireValidation && !isValidating && validation?.length > 0 && validation.find((v) => v.level === "error");
334
- error && console.error(error);
335
- const onHandle = (documentId, newState) => {
336
- client.patch(`workflow-metadata.${documentId}`).set({
337
- state: newState.id
338
- }).commit().then(() => {
339
- props.onComplete(), toast.push({
340
- status: "success",
341
- title: `Document state now "${newState.title}"`
342
- });
343
- }).catch((err) => {
344
- props.onComplete(), console.error(err), toast.push({
345
- status: "error",
346
- title: "Document state update failed"
347
- });
348
- });
349
- };
350
- if (!metadata2 || currentState && currentState.id === actionState.id)
351
- return null;
352
- const currentStateIndex = states.findIndex((s) => s.id === currentState?.id), direction = states.findIndex((s) => s.id === actionState.id) > currentStateIndex ? "promote" : "demote", DirectionIcon = direction === "promote" ? ArrowRightIcon : ArrowLeftIcon, directionLabel = direction === "promote" ? "Promote" : "Demote", userRoleCanUpdateState = user?.roles?.length && actionState?.roles?.length ? (
353
- // If the Action state is limited to specific roles
354
- // check that the current user has one of those roles
355
- arraysContainMatchingString(user.roles.map((r) => r.name), actionState.roles)
356
- ) : (
357
- // No roles specified on the next state, so anyone can update
358
- actionState?.roles?.length !== 0
359
- ), actionStateIsAValidTransition = currentState?.id && currentState?.transitions?.length ? (
360
- // If the Current State limits transitions to specific States
361
- // Check that the Action State is in Current State's transitions array
362
- currentState.transitions.includes(actionState.id)
363
- ) : (
364
- // Otherwise this isn't a problem
365
- !0
366
- ), userAssignmentCanUpdateState = actionState.requireAssignment ? (
367
- // If the Action State requires assigned users
368
- // Check the current user ID is in the assignees array
369
- currentUser && assignees?.length && assignees.includes(currentUser.id)
370
- ) : (
371
- // Otherwise this isn't a problem
372
- !0
373
- );
374
- let title = `${directionLabel} State to "${actionState.title}"`;
375
- return userRoleCanUpdateState ? actionStateIsAValidTransition ? userAssignmentCanUpdateState ? currentState?.requireValidation && isValidating ? title = `Document is validating, cannot ${directionLabel} State to "${actionState.title}"` : hasValidationErrors && (title = `Document has validation errors, cannot ${directionLabel} State to "${actionState.title}"`) : title = `You must be assigned to the document to ${directionLabel} State to "${actionState.title}"` : title = `You cannot ${directionLabel} State to "${actionState.title}" from "${currentState?.title}"` : title = `Your User role cannot ${directionLabel} State to "${actionState.title}"`, {
376
- icon: DirectionIcon,
377
- disabled: loading || error || currentState?.requireValidation && isValidating || hasValidationErrors || !currentState || !userRoleCanUpdateState || !actionStateIsAValidTransition || !userAssignmentCanUpdateState,
378
- title,
379
- label: actionState.title,
380
- onHandle: () => onHandle(id, actionState)
381
- };
382
- }
383
- function AssigneesBadge(documentId, currentUser) {
384
- const {
430
+ function useAssigneesBadge(t0) {
431
+ const $ = c(12), {
432
+ id: documentId
433
+ } = t0, currentUser = useCurrentUser(), {
385
434
  metadata: metadata2,
386
435
  loading,
387
436
  error
388
- } = useWorkflowContext(documentId), userList = useProjectUsers({
437
+ } = useWorkflowContext(documentId);
438
+ let t1;
439
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t1 = {
389
440
  apiVersion: API_VERSION
390
- });
441
+ }, $[0] = t1) : t1 = $[0];
442
+ const userList = useProjectUsers(t1);
391
443
  if (loading || error || !metadata2)
392
444
  return error && console.error(error), null;
393
- if (!metadata2?.assignees?.length)
394
- return {
445
+ if (!metadata2?.assignees?.length) {
446
+ let t22;
447
+ return $[1] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t22 = {
395
448
  label: "Unassigned"
396
- };
449
+ }, $[1] = t22) : t22 = $[1], t22;
450
+ }
451
+ let t2;
452
+ $[2] !== metadata2 ? (t2 = metadata2 ?? [], $[2] = metadata2, $[3] = t2) : t2 = $[3];
397
453
  const {
398
454
  assignees
399
- } = metadata2 ?? [], hasMe = currentUser ? assignees.some((assignee) => assignee === currentUser.id) : !1, assigneesCount = hasMe ? assignees.length - 1 : assignees.length, title = userList.filter((user) => assignees.includes(user.id)).map((user) => user.displayName).join(", ");
455
+ } = t2, hasMe = currentUser ? assignees.some((assignee) => assignee === currentUser.id) : !1, assigneesCount = hasMe ? assignees.length - 1 : assignees.length;
456
+ let t3;
457
+ if ($[4] !== assignees || $[5] !== userList) {
458
+ let t42;
459
+ $[7] !== assignees ? (t42 = (user) => assignees.includes(user.id), $[7] = assignees, $[8] = t42) : t42 = $[8], t3 = userList.filter(t42).map(_temp$6).join(", "), $[4] = assignees, $[5] = userList, $[6] = t3;
460
+ } else
461
+ t3 = $[6];
462
+ const title = t3;
400
463
  let label;
401
- return hasMe && assigneesCount === 0 ? label = "Assigned to Me" : hasMe && assigneesCount > 0 ? label = `Me and ${assigneesCount} ${assigneesCount === 1 ? "other" : "others"}` : label = `${assigneesCount} assigned`, {
464
+ hasMe && assigneesCount === 0 ? label = "Assigned to Me" : hasMe && assigneesCount > 0 ? label = `Me and ${assigneesCount} ${assigneesCount === 1 ? "other" : "others"}` : label = `${assigneesCount} assigned`;
465
+ let t4;
466
+ return $[9] !== label || $[10] !== title ? (t4 = {
402
467
  label,
403
468
  title,
404
469
  color: "primary"
405
- };
470
+ }, $[9] = label, $[10] = title, $[11] = t4) : t4 = $[11], t4;
406
471
  }
407
- function StateBadge(documentId) {
408
- const {
472
+ function _temp$6(user_0) {
473
+ return user_0.displayName;
474
+ }
475
+ function useStateBadge(t0) {
476
+ const $ = c(8), {
477
+ id: documentId
478
+ } = t0, {
409
479
  metadata: metadata2,
410
480
  loading,
411
481
  error,
412
482
  states
413
- } = useWorkflowContext(documentId), state = states.find((s) => s.id === metadata2?.state);
414
- return loading || error ? (error && console.error(error), null) : state ? {
483
+ } = useWorkflowContext(documentId);
484
+ let t1;
485
+ if ($[0] !== metadata2?.state || $[1] !== states) {
486
+ let t22;
487
+ $[3] !== metadata2?.state ? (t22 = (s) => s.id === metadata2?.state, $[3] = metadata2?.state, $[4] = t22) : t22 = $[4], t1 = states.find(t22), $[0] = metadata2?.state, $[1] = states, $[2] = t1;
488
+ } else
489
+ t1 = $[2];
490
+ const state = t1;
491
+ if (loading || error)
492
+ return error && console.error(error), null;
493
+ if (!state)
494
+ return null;
495
+ const t2 = state?.color;
496
+ let t3;
497
+ return $[5] !== state.title || $[6] !== t2 ? (t3 = {
415
498
  label: state.title,
416
- // title: state.title,
417
- color: state?.color
418
- } : null;
499
+ color: t2
500
+ }, $[5] = state.title, $[6] = t2, $[7] = t3) : t3 = $[7], t3;
419
501
  }
420
502
  function WorkflowSignal(props) {
421
503
  const $ = c(9);
422
504
  let t0;
423
- $[0] !== props.value ? (t0 = props?.value?._id ? props.value._id.replace("drafts.", "") : null, $[0] = props.value, $[1] = t0) : t0 = $[1];
505
+ $[0] !== props.value ? (t0 = props?.value?._id ? getPublishedId(props.value._id) : null, $[0] = props.value, $[1] = t0) : t0 = $[1];
424
506
  const documentId = t0, {
425
507
  addId,
426
508
  removeId
@@ -541,10 +623,14 @@ var metadata = (states) => defineType({
541
623
  }) => {
542
624
  const lastDocOrderRank = await getClient({
543
625
  apiVersion: API_VERSION
544
- }).fetch("*[_type == $type]|order(@[$order] desc)[0][$order]", {
545
- order: "orderRank",
546
- type: "workflow.metadata"
547
- });
626
+ }).fetch(
627
+ /* groq */
628
+ "*[_type == $type]|order(@[$order] desc)[0][$order]",
629
+ {
630
+ order: "orderRank",
631
+ type: "workflow.metadata"
632
+ }
633
+ );
548
634
  return initialRank(lastDocOrderRank);
549
635
  }
550
636
  }), defineField({
@@ -1133,14 +1219,10 @@ function Status(props) {
1133
1219
  let t2;
1134
1220
  return $[4] !== t0 || $[5] !== t1 ? (t2 = /* @__PURE__ */ jsx(Tooltip, { portal: !0, content: t0, children: t1 }), $[4] = t0, $[5] = t1, $[6] = t2) : t2 = $[6], t2;
1135
1221
  }
1136
- const StyledStickyCard = /* @__PURE__ */ styled(Card).withConfig({
1222
+ const StyledStickyCard = styled(Card).withConfig({
1137
1223
  displayName: "StyledStickyCard",
1138
1224
  componentId: "sc-akqrc2-0"
1139
- })(() => css`
1140
- position: sticky;
1141
- top: 0;
1142
- z-index: 1;
1143
- `);
1225
+ })`position:sticky;top:0;z-index:1;`;
1144
1226
  function StateTitle(props) {
1145
1227
  const $ = c(18), {
1146
1228
  state,
@@ -1194,15 +1276,10 @@ function generateMultipleOrderRanks(count, start, end) {
1194
1276
  ranks = generateMiddleValue(ranks);
1195
1277
  return ranks.sort((a, b) => a.toString().localeCompare(b.toString()));
1196
1278
  }
1197
- const StyledFloatingCard = /* @__PURE__ */ styled(Card).withConfig({
1279
+ const StyledFloatingCard = styled(Card).withConfig({
1198
1280
  displayName: "StyledFloatingCard",
1199
1281
  componentId: "sc-ywzruw-0"
1200
- })(() => css`
1201
- position: fixed;
1202
- bottom: 0;
1203
- left: 0;
1204
- z-index: 1000;
1205
- `);
1282
+ })`position:fixed;bottom:0;left:0;z-index:1000;`;
1206
1283
  function FloatingCard(t0) {
1207
1284
  const $ = c(7), {
1208
1285
  children
@@ -1597,6 +1674,7 @@ const workflowTool = (options) => ({
1597
1674
  throw new Error('Workflow plugin: Missing "states" in config');
1598
1675
  if (!schemaTypes?.length)
1599
1676
  throw new Error('Workflow plugin: Missing "schemaTypes" in config');
1677
+ const useUpdateActions = states.map((state) => defineUpdateWorkflow(state));
1600
1678
  return {
1601
1679
  name: "sanity-plugin-workflow",
1602
1680
  schema: {
@@ -1606,31 +1684,17 @@ const workflowTool = (options) => ({
1606
1684
  // ...
1607
1685
  studio: {
1608
1686
  components: {
1609
- layout: (props) => WorkflowProvider({
1610
- ...props,
1611
- workflow: {
1612
- schemaTypes,
1613
- states
1614
- }
1615
- })
1687
+ layout: (props) => /* @__PURE__ */ jsx(WorkflowProvider, { ...props, schemaTypes, states })
1616
1688
  }
1617
1689
  },
1618
1690
  form: {
1619
1691
  components: {
1620
- input: (props) => props.id === "root" && isObjectInputProps(props) && schemaTypes.includes(props.schemaType.name) ? WorkflowSignal(props) : props.renderDefault(props)
1692
+ input: (props) => props.id === "root" && isObjectInputProps(props) && schemaTypes.includes(props.schemaType.name) ? /* @__PURE__ */ jsx(WorkflowSignal, { ...props }) : props.renderDefault(props)
1621
1693
  }
1622
1694
  },
1623
1695
  document: {
1624
- actions: (prev, context) => schemaTypes.includes(context.schemaType) ? [(props) => BeginWorkflow(props), (props) => AssignWorkflow(props), ...states.map((state) => (props) => UpdateWorkflow(props, state)), (props) => CompleteWorkflow(props), ...prev] : prev,
1625
- badges: (prev, context) => {
1626
- if (!schemaTypes.includes(context.schemaType))
1627
- return prev;
1628
- const {
1629
- documentId,
1630
- currentUser
1631
- } = context;
1632
- return documentId ? [() => StateBadge(documentId), () => AssigneesBadge(documentId, currentUser), ...prev] : prev;
1633
- }
1696
+ actions: (prev, context) => schemaTypes.includes(context.schemaType) ? [useBeginWorkflow, useAssignWorkflow, ...useUpdateActions, useCompleteWorkflow, ...prev] : prev,
1697
+ badges: (prev, context) => !schemaTypes.includes(context.schemaType) || !context.documentId ? prev : [useStateBadge, useAssigneesBadge, ...prev]
1634
1698
  },
1635
1699
  tools: [
1636
1700
  // TODO: These configs could be read from Context
@@ -1642,6 +1706,7 @@ const workflowTool = (options) => ({
1642
1706
  };
1643
1707
  });
1644
1708
  export {
1709
+ defineStates,
1645
1710
  workflow
1646
1711
  };
1647
1712
  //# sourceMappingURL=index.js.map