sanity-plugin-workflow 1.0.0-beta.9 → 1.0.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/README.md +15 -19
- package/lib/index.esm.js +157 -78
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +155 -76
- package/lib/index.js.map +1 -1
- package/package.json +2 -2
- package/src/actions/AssignWorkflow.tsx +9 -10
- package/src/actions/BeginWorkflow.tsx +16 -21
- package/src/actions/CompleteWorkflow.tsx +8 -8
- package/src/actions/UpdateWorkflow.tsx +11 -25
- package/src/badges/AssigneesBadge.tsx +9 -8
- package/src/badges/StateBadge.tsx +6 -5
- package/src/components/WorkflowContext.tsx +71 -0
- package/src/components/WorkflowSignal.tsx +30 -0
- package/src/components/WorkflowTool.tsx +2 -4
- package/src/constants/index.ts +1 -1
- package/src/hooks/useWorkflowDocuments.tsx +1 -1
- package/src/hooks/useWorkflowMetadata.tsx +32 -26
- package/src/index.ts +39 -16
- package/src/types/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sanity-plugin-workflow",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A demonstration of a custom content publishing workflow using Sanity.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"groq": "^3.3.1",
|
|
59
59
|
"lexorank": "^1.0.5",
|
|
60
60
|
"react-fast-compare": "^3.2.1",
|
|
61
|
-
"sanity-plugin-utils": "^1.
|
|
61
|
+
"sanity-plugin-utils": "^1.6.2"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@commitlint/cli": "^17.4.4",
|
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
import {UsersIcon} from '@sanity/icons'
|
|
2
|
-
import {DocumentActionProps} from 'sanity'
|
|
3
2
|
import {useState} from 'react'
|
|
3
|
+
import {DocumentActionProps} from 'sanity'
|
|
4
4
|
import {useProjectUsers} from 'sanity-plugin-utils'
|
|
5
5
|
|
|
6
|
-
import {API_VERSION} from '../constants'
|
|
7
6
|
import UserAssignment from '../components/UserAssignment'
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
8
|
+
import {API_VERSION} from '../constants'
|
|
10
9
|
|
|
11
|
-
export function AssignWorkflow(props: DocumentActionProps
|
|
10
|
+
export function AssignWorkflow(props: DocumentActionProps) {
|
|
12
11
|
const {id} = props
|
|
12
|
+
const {metadata, loading, error} = useWorkflowContext(id)
|
|
13
13
|
const [isDialogOpen, setDialogOpen] = useState(false)
|
|
14
14
|
const userList = useProjectUsers({apiVersion: API_VERSION})
|
|
15
|
-
const {data, loading, error} = useWorkflowMetadata(id, states)
|
|
16
15
|
|
|
17
16
|
if (error) {
|
|
18
17
|
console.error(error)
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
if (!
|
|
20
|
+
if (!metadata) {
|
|
22
21
|
return null
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
return {
|
|
26
25
|
icon: UsersIcon,
|
|
27
26
|
type: 'dialog',
|
|
28
|
-
disabled: !
|
|
27
|
+
disabled: !metadata || loading || error,
|
|
29
28
|
label: `Assign`,
|
|
30
|
-
title:
|
|
29
|
+
title: metadata ? null : `Document is not in Workflow`,
|
|
31
30
|
dialog: isDialogOpen && {
|
|
32
31
|
type: 'popover',
|
|
33
32
|
onClose: () => {
|
|
@@ -36,7 +35,7 @@ export function AssignWorkflow(props: DocumentActionProps, states: State[]) {
|
|
|
36
35
|
content: (
|
|
37
36
|
<UserAssignment
|
|
38
37
|
userList={userList}
|
|
39
|
-
assignees={
|
|
38
|
+
assignees={metadata?.assignees?.length > 0 ? metadata.assignees : []}
|
|
40
39
|
documentId={id}
|
|
41
40
|
/>
|
|
42
41
|
),
|
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import {useCallback, useState} from 'react'
|
|
2
1
|
import {SplitVerticalIcon} from '@sanity/icons'
|
|
3
|
-
import {DocumentActionProps, useClient} from 'sanity'
|
|
4
2
|
import {useToast} from '@sanity/ui'
|
|
5
3
|
import {LexoRank} from 'lexorank'
|
|
4
|
+
import {useCallback, useState} from 'react'
|
|
5
|
+
import {DocumentActionProps, useClient} from 'sanity'
|
|
6
6
|
|
|
7
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
7
8
|
import {API_VERSION} from '../constants'
|
|
8
|
-
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
9
|
-
import {State} from '../types'
|
|
10
9
|
|
|
11
|
-
export function BeginWorkflow(props: DocumentActionProps
|
|
10
|
+
export function BeginWorkflow(props: DocumentActionProps) {
|
|
12
11
|
const {id, draft} = props
|
|
13
|
-
const {
|
|
12
|
+
const {metadata, loading, error, states} = useWorkflowContext(id)
|
|
14
13
|
const client = useClient({apiVersion: API_VERSION})
|
|
15
14
|
const toast = useToast()
|
|
16
15
|
const [beginning, setBeginning] = useState(false)
|
|
@@ -27,19 +26,15 @@ export function BeginWorkflow(props: DocumentActionProps, states: State[]) {
|
|
|
27
26
|
{state: states[0].id}
|
|
28
27
|
)
|
|
29
28
|
client
|
|
30
|
-
.createIfNotExists(
|
|
31
|
-
{
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
// Faster!
|
|
41
|
-
{visibility: 'async'}
|
|
42
|
-
)
|
|
29
|
+
.createIfNotExists({
|
|
30
|
+
_id: `workflow-metadata.${id}`,
|
|
31
|
+
_type: `workflow.metadata`,
|
|
32
|
+
documentId: id,
|
|
33
|
+
state: states[0].id,
|
|
34
|
+
orderRank: lowestOrderFirstState
|
|
35
|
+
? LexoRank.parse(lowestOrderFirstState).genNext().toString()
|
|
36
|
+
: LexoRank.min().toString(),
|
|
37
|
+
})
|
|
43
38
|
.then(() => {
|
|
44
39
|
toast.push({
|
|
45
40
|
status: 'success',
|
|
@@ -52,14 +47,14 @@ export function BeginWorkflow(props: DocumentActionProps, states: State[]) {
|
|
|
52
47
|
})
|
|
53
48
|
}, [id, states, client, toast])
|
|
54
49
|
|
|
55
|
-
if (!draft || complete ||
|
|
50
|
+
if (!draft || complete || metadata) {
|
|
56
51
|
return null
|
|
57
52
|
}
|
|
58
53
|
|
|
59
54
|
return {
|
|
60
55
|
icon: SplitVerticalIcon,
|
|
61
56
|
type: 'dialog',
|
|
62
|
-
disabled:
|
|
57
|
+
disabled: metadata || loading || error || beginning || complete,
|
|
63
58
|
label: beginning ? `Beginning...` : `Begin Workflow`,
|
|
64
59
|
onHandle: () => {
|
|
65
60
|
handle()
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {useCallback} from 'react'
|
|
2
1
|
import {CheckmarkIcon} from '@sanity/icons'
|
|
2
|
+
import {useCallback} from 'react'
|
|
3
3
|
import {DocumentActionProps, useClient} from 'sanity'
|
|
4
4
|
|
|
5
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
5
6
|
import {API_VERSION} from '../constants'
|
|
6
|
-
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
7
|
-
import {State} from '../types'
|
|
8
7
|
|
|
9
|
-
export function CompleteWorkflow(props: DocumentActionProps
|
|
8
|
+
export function CompleteWorkflow(props: DocumentActionProps) {
|
|
10
9
|
const {id} = props
|
|
11
|
-
const {
|
|
10
|
+
const {metadata, loading, error, states} = useWorkflowContext(id)
|
|
12
11
|
const client = useClient({apiVersion: API_VERSION})
|
|
13
12
|
|
|
14
13
|
if (error) {
|
|
@@ -19,12 +18,13 @@ export function CompleteWorkflow(props: DocumentActionProps, states: State[]) {
|
|
|
19
18
|
client.delete(`workflow-metadata.${id}`)
|
|
20
19
|
}, [id, client])
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (!data.metadata) {
|
|
21
|
+
if (!metadata) {
|
|
25
22
|
return null
|
|
26
23
|
}
|
|
27
24
|
|
|
25
|
+
const state = states.find((s) => s.id === metadata.state)
|
|
26
|
+
const isLastState = state?.id === states[states.length - 1].id
|
|
27
|
+
|
|
28
28
|
return {
|
|
29
29
|
icon: CheckmarkIcon,
|
|
30
30
|
type: 'dialog',
|
|
@@ -3,17 +3,13 @@ import {useToast} from '@sanity/ui'
|
|
|
3
3
|
import {useCurrentUser, useValidationStatus} from 'sanity'
|
|
4
4
|
import {DocumentActionProps, useClient} from 'sanity'
|
|
5
5
|
|
|
6
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
6
7
|
import {API_VERSION} from '../constants'
|
|
7
8
|
import {arraysContainMatchingString} from '../helpers/arraysContainMatchingString'
|
|
8
|
-
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
9
9
|
import {State} from '../types'
|
|
10
10
|
|
|
11
11
|
// eslint-disable-next-line complexity
|
|
12
|
-
export function UpdateWorkflow(
|
|
13
|
-
props: DocumentActionProps,
|
|
14
|
-
allStates: State[],
|
|
15
|
-
actionState: State
|
|
16
|
-
) {
|
|
12
|
+
export function UpdateWorkflow(props: DocumentActionProps, actionState: State) {
|
|
17
13
|
const {id, type} = props
|
|
18
14
|
|
|
19
15
|
const user = useCurrentUser()
|
|
@@ -21,10 +17,11 @@ export function UpdateWorkflow(
|
|
|
21
17
|
const toast = useToast()
|
|
22
18
|
const currentUser = useCurrentUser()
|
|
23
19
|
|
|
24
|
-
const {
|
|
25
|
-
const
|
|
26
|
-
const {assignees = []} =
|
|
20
|
+
const {metadata, loading, error, states} = useWorkflowContext(id)
|
|
21
|
+
const currentState = states.find((s) => s.id === metadata?.state)
|
|
22
|
+
const {assignees = []} = metadata ?? {}
|
|
27
23
|
|
|
24
|
+
// TODO: Shouldn't the document action props contain this?
|
|
28
25
|
const {validation, isValidating} = useValidationStatus(id, type)
|
|
29
26
|
const hasValidationErrors =
|
|
30
27
|
currentState?.requireValidation &&
|
|
@@ -47,15 +44,6 @@ export function UpdateWorkflow(
|
|
|
47
44
|
status: 'success',
|
|
48
45
|
title: `Document state now "${newState.title}"`,
|
|
49
46
|
})
|
|
50
|
-
// Perform document operations after State changes
|
|
51
|
-
// If State has changed and the document needs to be un/published
|
|
52
|
-
// This functionality was deemed too dangerous / unexpected
|
|
53
|
-
// Revisit with improved UX
|
|
54
|
-
// if (!ops.publish.disabled && nextOperation === 'publish') {
|
|
55
|
-
// ops.publish.execute()
|
|
56
|
-
// } else if (!ops.unpublish.disabled && nextOperation === 'unpublish') {
|
|
57
|
-
// ops.unpublish.execute()
|
|
58
|
-
// }
|
|
59
47
|
})
|
|
60
48
|
.catch((err) => {
|
|
61
49
|
props.onComplete()
|
|
@@ -70,14 +58,12 @@ export function UpdateWorkflow(
|
|
|
70
58
|
// Remove button if:
|
|
71
59
|
// Document is not in Workflow OR
|
|
72
60
|
// The current State is the same as this actions State
|
|
73
|
-
if (!
|
|
61
|
+
if (!metadata || (currentState && currentState.id === actionState.id)) {
|
|
74
62
|
return null
|
|
75
63
|
}
|
|
76
64
|
|
|
77
|
-
const currentStateIndex =
|
|
78
|
-
|
|
79
|
-
)
|
|
80
|
-
const actionStateIndex = allStates.findIndex((s) => s.id === actionState.id)
|
|
65
|
+
const currentStateIndex = states.findIndex((s) => s.id === currentState?.id)
|
|
66
|
+
const actionStateIndex = states.findIndex((s) => s.id === actionState.id)
|
|
81
67
|
const direction = actionStateIndex > currentStateIndex ? 'promote' : 'demote'
|
|
82
68
|
const DirectionIcon = direction === 'promote' ? ArrowRightIcon : ArrowLeftIcon
|
|
83
69
|
const directionLabel = direction === 'promote' ? 'Promote' : 'Demote'
|
|
@@ -94,7 +80,7 @@ export function UpdateWorkflow(
|
|
|
94
80
|
actionState?.roles?.length !== 0
|
|
95
81
|
|
|
96
82
|
const actionStateIsAValidTransition =
|
|
97
|
-
currentState?.id && currentState
|
|
83
|
+
currentState?.id && currentState?.transitions?.length
|
|
98
84
|
? // If the Current State limits transitions to specific States
|
|
99
85
|
// Check that the Action State is in Current State's transitions array
|
|
100
86
|
currentState.transitions.includes(actionState.id)
|
|
@@ -104,7 +90,7 @@ export function UpdateWorkflow(
|
|
|
104
90
|
const userAssignmentCanUpdateState = actionState.requireAssignment
|
|
105
91
|
? // If the Action State requires assigned users
|
|
106
92
|
// Check the current user ID is in the assignees array
|
|
107
|
-
currentUser && assignees
|
|
93
|
+
currentUser && assignees?.length && assignees.includes(currentUser.id)
|
|
108
94
|
: // Otherwise this isn't a problem
|
|
109
95
|
true
|
|
110
96
|
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import {CurrentUser, DocumentBadgeDescription} from 'sanity'
|
|
2
2
|
import {useProjectUsers} from 'sanity-plugin-utils'
|
|
3
|
-
import {API_VERSION} from '../constants'
|
|
4
|
-
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
5
3
|
|
|
6
|
-
import {
|
|
4
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
5
|
+
import {API_VERSION} from '../constants'
|
|
7
6
|
|
|
8
7
|
export function AssigneesBadge(
|
|
9
|
-
states: State[],
|
|
10
8
|
documentId: string,
|
|
11
9
|
currentUser: CurrentUser | null
|
|
12
10
|
): DocumentBadgeDescription | null {
|
|
13
|
-
const {
|
|
14
|
-
const {metadata} = data
|
|
11
|
+
const {metadata, loading, error} = useWorkflowContext(documentId)
|
|
15
12
|
const userList = useProjectUsers({apiVersion: API_VERSION})
|
|
16
13
|
|
|
17
14
|
if (loading || error || !metadata) {
|
|
@@ -29,7 +26,9 @@ export function AssigneesBadge(
|
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
const {assignees} = metadata ?? []
|
|
32
|
-
const hasMe = currentUser
|
|
29
|
+
const hasMe = currentUser
|
|
30
|
+
? assignees.some((assignee) => assignee === currentUser.id)
|
|
31
|
+
: false
|
|
33
32
|
const assigneesCount = hasMe ? assignees.length - 1 : assignees.length
|
|
34
33
|
const assigneeUsers = userList.filter((user) => assignees.includes(user.id))
|
|
35
34
|
const title = assigneeUsers.map((user) => user.displayName).join(', ')
|
|
@@ -39,7 +38,9 @@ export function AssigneesBadge(
|
|
|
39
38
|
if (hasMe && assigneesCount === 0) {
|
|
40
39
|
label = 'Assigned to Me'
|
|
41
40
|
} else if (hasMe && assigneesCount > 0) {
|
|
42
|
-
label = `Me and ${assigneesCount} ${
|
|
41
|
+
label = `Me and ${assigneesCount} ${
|
|
42
|
+
assigneesCount === 1 ? 'other' : 'others'
|
|
43
|
+
}`
|
|
43
44
|
} else {
|
|
44
45
|
label = `${assigneesCount} assigned`
|
|
45
46
|
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import {DocumentBadgeDescription} from 'sanity'
|
|
2
|
-
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
3
2
|
|
|
4
|
-
import {
|
|
3
|
+
import {useWorkflowContext} from '../components/WorkflowContext'
|
|
5
4
|
|
|
6
|
-
export function StateBadge(
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
export function StateBadge(
|
|
6
|
+
documentId: string
|
|
7
|
+
): DocumentBadgeDescription | null {
|
|
8
|
+
const {metadata, loading, error, states} = useWorkflowContext(documentId)
|
|
9
|
+
const state = states.find((s) => s.id === metadata?.state)
|
|
9
10
|
|
|
10
11
|
if (loading || error) {
|
|
11
12
|
if (error) {
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {useCallback, useContext, useState} from 'react'
|
|
2
|
+
import {createContext} from 'react'
|
|
3
|
+
import {LayoutProps} from 'sanity'
|
|
4
|
+
|
|
5
|
+
import {DEFAULT_CONFIG} from '../constants'
|
|
6
|
+
import {useWorkflowMetadata} from '../hooks/useWorkflowMetadata'
|
|
7
|
+
import {KeyedMetadata, WorkflowConfig} from '../types'
|
|
8
|
+
|
|
9
|
+
export type WorkflowContextValue = Required<WorkflowConfig> & {
|
|
10
|
+
data: KeyedMetadata
|
|
11
|
+
loading: boolean
|
|
12
|
+
error: boolean | unknown | ProgressEvent
|
|
13
|
+
ids: string[]
|
|
14
|
+
addId: (id: string) => void
|
|
15
|
+
removeId: (id: string) => void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const WorkflowContext = createContext<WorkflowContextValue>({
|
|
19
|
+
data: {},
|
|
20
|
+
loading: false,
|
|
21
|
+
error: false,
|
|
22
|
+
ids: [],
|
|
23
|
+
addId: () => null,
|
|
24
|
+
removeId: () => null,
|
|
25
|
+
...DEFAULT_CONFIG,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export function useWorkflowContext(id?: string) {
|
|
29
|
+
const current = useContext(WorkflowContext)
|
|
30
|
+
|
|
31
|
+
return {...current, metadata: id ? current.data[id] : null}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type WorkflowProviderProps = LayoutProps & {workflow: Required<WorkflowConfig>}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This Provider wraps the Studio and provides the workflow context to document actions and badges.
|
|
38
|
+
* This is so individual actions and badges do not need to all register their own listeners.
|
|
39
|
+
* Instead, each document "signals" its ID up to the provider, which then registers a single listener
|
|
40
|
+
* This is performed inside of a component loaded at the root level of the Document Form
|
|
41
|
+
*/
|
|
42
|
+
export function WorkflowProvider(props: WorkflowProviderProps) {
|
|
43
|
+
const [ids, setIds] = useState<string[]>([])
|
|
44
|
+
const addId = useCallback(
|
|
45
|
+
(id: string) =>
|
|
46
|
+
setIds((current) => (current.includes(id) ? current : [...current, id])),
|
|
47
|
+
[]
|
|
48
|
+
)
|
|
49
|
+
const removeId = useCallback(
|
|
50
|
+
(id: string) => setIds((current) => current.filter((i) => i !== id)),
|
|
51
|
+
[]
|
|
52
|
+
)
|
|
53
|
+
const {data, loading, error} = useWorkflowMetadata(ids)
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<WorkflowContext.Provider
|
|
57
|
+
value={{
|
|
58
|
+
data,
|
|
59
|
+
loading,
|
|
60
|
+
error,
|
|
61
|
+
ids,
|
|
62
|
+
addId,
|
|
63
|
+
removeId,
|
|
64
|
+
states: props.workflow.states,
|
|
65
|
+
schemaTypes: props.workflow.schemaTypes,
|
|
66
|
+
}}
|
|
67
|
+
>
|
|
68
|
+
{props.renderDefault(props)}
|
|
69
|
+
</WorkflowContext.Provider>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import {useEffect} from 'react'
|
|
2
|
+
import {ObjectInputProps} from 'sanity'
|
|
3
|
+
|
|
4
|
+
import {useWorkflowContext} from './WorkflowContext'
|
|
5
|
+
|
|
6
|
+
// This component is loaded at the root level of the Document Form
|
|
7
|
+
// It is used to signal the document ID to the WorkflowProvider
|
|
8
|
+
export default function WorkflowSignal(props: ObjectInputProps) {
|
|
9
|
+
const documentId = props?.value?._id
|
|
10
|
+
? props.value._id.replace(`drafts.`, ``)
|
|
11
|
+
: null
|
|
12
|
+
|
|
13
|
+
const {addId, removeId} = useWorkflowContext()
|
|
14
|
+
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
// On mount, add to the query of listening documents
|
|
17
|
+
if (documentId) {
|
|
18
|
+
addId(documentId)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// On unmount, remove from the query of listening documents
|
|
22
|
+
return () => {
|
|
23
|
+
if (documentId) {
|
|
24
|
+
removeId(documentId)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}, [documentId, addId, removeId])
|
|
28
|
+
|
|
29
|
+
return props.renderDefault(props)
|
|
30
|
+
}
|
|
@@ -155,10 +155,8 @@ export default function WorkflowTool(props: WorkflowToolProps) {
|
|
|
155
155
|
// Only the first state should generate an absolute minimum rank
|
|
156
156
|
newOrder = LexoRank.min().toString()
|
|
157
157
|
} else {
|
|
158
|
-
// Otherwise create
|
|
159
|
-
newOrder = LexoRank.
|
|
160
|
-
.between(LexoRank.min())
|
|
161
|
-
.toString()
|
|
158
|
+
// Otherwise create one rank above the minimum
|
|
159
|
+
newOrder = LexoRank.min().genNext().toString()
|
|
162
160
|
}
|
|
163
161
|
} else if (destination.index === 0) {
|
|
164
162
|
// Now first item in order
|
package/src/constants/index.ts
CHANGED
|
@@ -1,43 +1,49 @@
|
|
|
1
|
+
import {useMemo} from 'react'
|
|
1
2
|
import {useListeningQuery} from 'sanity-plugin-utils'
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import {API_VERSION} from '../constants'
|
|
5
|
+
import {KeyedMetadata, Metadata} from '../types'
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
* Takes the published ID of
|
|
8
|
+
* Takes the published ID of documents and return the metadata for those documents.
|
|
7
9
|
*
|
|
8
|
-
* @param
|
|
9
|
-
* @param states Array of States defined in plugin config
|
|
10
|
-
* @returns State
|
|
10
|
+
* @param ids Source document published IDs
|
|
11
11
|
*/
|
|
12
|
-
export function useWorkflowMetadata(
|
|
13
|
-
|
|
14
|
-
states: State[]
|
|
15
|
-
): {
|
|
16
|
-
data: {metadata?: Metadata; state?: State}
|
|
12
|
+
export function useWorkflowMetadata(ids: string[]): {
|
|
13
|
+
data: KeyedMetadata
|
|
17
14
|
loading: boolean
|
|
18
|
-
error: boolean
|
|
15
|
+
error: boolean | unknown | ProgressEvent
|
|
19
16
|
} {
|
|
20
17
|
const {
|
|
21
|
-
data:
|
|
18
|
+
data: rawData,
|
|
22
19
|
loading,
|
|
23
20
|
error,
|
|
24
|
-
} = useListeningQuery<Metadata>(
|
|
25
|
-
`*[_type == "workflow.metadata" && documentId
|
|
21
|
+
} = useListeningQuery<Metadata[]>(
|
|
22
|
+
`*[_type == "workflow.metadata" && documentId in $ids]{
|
|
23
|
+
_id,
|
|
24
|
+
_type,
|
|
25
|
+
_rev,
|
|
26
|
+
assignees,
|
|
27
|
+
documentId,
|
|
28
|
+
state,
|
|
29
|
+
orderRank
|
|
30
|
+
}`,
|
|
26
31
|
{
|
|
27
|
-
params: {
|
|
32
|
+
params: {ids},
|
|
33
|
+
options: {apiVersion: API_VERSION},
|
|
28
34
|
}
|
|
29
35
|
)
|
|
30
36
|
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
}
|
|
37
|
+
const keyedMetadata = useMemo(() => {
|
|
38
|
+
if (!rawData || rawData.length === 0) return {}
|
|
39
|
+
|
|
40
|
+
return rawData.reduce<KeyedMetadata>((acc, cur) => {
|
|
41
|
+
return {
|
|
42
|
+
...acc,
|
|
43
|
+
[cur.documentId]: cur,
|
|
44
|
+
}
|
|
45
|
+
}, {})
|
|
46
|
+
}, [rawData])
|
|
41
47
|
|
|
42
|
-
return {data:
|
|
48
|
+
return {data: keyedMetadata, loading, error}
|
|
43
49
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {definePlugin, DocumentActionProps} from 'sanity'
|
|
1
|
+
import {definePlugin, DocumentActionProps, isObjectInputProps} from 'sanity'
|
|
2
2
|
|
|
3
3
|
import {AssignWorkflow} from './actions/AssignWorkflow'
|
|
4
4
|
import {BeginWorkflow} from './actions/BeginWorkflow'
|
|
@@ -6,6 +6,8 @@ import {CompleteWorkflow} from './actions/CompleteWorkflow'
|
|
|
6
6
|
import {UpdateWorkflow} from './actions/UpdateWorkflow'
|
|
7
7
|
import {AssigneesBadge} from './badges/AssigneesBadge'
|
|
8
8
|
import {StateBadge} from './badges/StateBadge'
|
|
9
|
+
import {WorkflowProvider} from './components/WorkflowContext'
|
|
10
|
+
import WorkflowSignal from './components/WorkflowSignal'
|
|
9
11
|
import {DEFAULT_CONFIG} from './constants'
|
|
10
12
|
import metadata from './schema/workflow/workflow.metadata'
|
|
11
13
|
import {workflowTool} from './tools'
|
|
@@ -16,7 +18,11 @@ export const workflow = definePlugin<WorkflowConfig>(
|
|
|
16
18
|
const {schemaTypes, states} = {...DEFAULT_CONFIG, ...config}
|
|
17
19
|
|
|
18
20
|
if (!states?.length) {
|
|
19
|
-
throw new Error(`Workflow: Missing states in config`)
|
|
21
|
+
throw new Error(`Workflow plugin: Missing "states" in config`)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!schemaTypes?.length) {
|
|
25
|
+
throw new Error(`Workflow plugin: Missing "schemaTypes" in config`)
|
|
20
26
|
}
|
|
21
27
|
|
|
22
28
|
return {
|
|
@@ -26,27 +32,41 @@ export const workflow = definePlugin<WorkflowConfig>(
|
|
|
26
32
|
},
|
|
27
33
|
// TODO: Remove 'workflow.metadata' from list of new document types
|
|
28
34
|
// ...
|
|
35
|
+
studio: {
|
|
36
|
+
components: {
|
|
37
|
+
layout: (props) =>
|
|
38
|
+
WorkflowProvider({...props, workflow: {schemaTypes, states}}),
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
form: {
|
|
42
|
+
components: {
|
|
43
|
+
input: (props) => {
|
|
44
|
+
if (
|
|
45
|
+
props.id === `root` &&
|
|
46
|
+
isObjectInputProps(props) &&
|
|
47
|
+
schemaTypes.includes(props.schemaType.name)
|
|
48
|
+
) {
|
|
49
|
+
return WorkflowSignal(props)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return props.renderDefault(props)
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
29
56
|
document: {
|
|
30
57
|
actions: (prev, context) => {
|
|
31
58
|
if (!schemaTypes.includes(context.schemaType)) {
|
|
32
59
|
return prev
|
|
33
60
|
}
|
|
34
61
|
|
|
35
|
-
// TODO: Augment 'publish' and 'unpublish' to be disabled if a document IS in Workflow
|
|
36
|
-
// This would be best done with a listening query here, but we don't have access to documentStore
|
|
37
|
-
|
|
38
|
-
// TODO: Performance improvements:
|
|
39
|
-
// Each of these actions registers their own listener!
|
|
40
|
-
// One should probably be responsible for listening and storing the response in context
|
|
41
|
-
|
|
42
62
|
return [
|
|
43
|
-
(props) => BeginWorkflow(props
|
|
44
|
-
(props) => AssignWorkflow(props
|
|
63
|
+
(props) => BeginWorkflow(props),
|
|
64
|
+
(props) => AssignWorkflow(props),
|
|
45
65
|
...states.map(
|
|
46
66
|
(state) => (props: DocumentActionProps) =>
|
|
47
|
-
UpdateWorkflow(props,
|
|
67
|
+
UpdateWorkflow(props, state)
|
|
48
68
|
),
|
|
49
|
-
(props) => CompleteWorkflow(props
|
|
69
|
+
(props) => CompleteWorkflow(props),
|
|
50
70
|
...prev,
|
|
51
71
|
]
|
|
52
72
|
},
|
|
@@ -62,13 +82,16 @@ export const workflow = definePlugin<WorkflowConfig>(
|
|
|
62
82
|
}
|
|
63
83
|
|
|
64
84
|
return [
|
|
65
|
-
() => StateBadge(
|
|
66
|
-
() => AssigneesBadge(
|
|
85
|
+
() => StateBadge(documentId),
|
|
86
|
+
() => AssigneesBadge(documentId, currentUser),
|
|
67
87
|
...prev,
|
|
68
88
|
]
|
|
69
89
|
},
|
|
70
90
|
},
|
|
71
|
-
tools: [
|
|
91
|
+
tools: [
|
|
92
|
+
// TODO: These configs could be read from Context
|
|
93
|
+
workflowTool({schemaTypes, states}),
|
|
94
|
+
],
|
|
72
95
|
}
|
|
73
96
|
}
|
|
74
97
|
)
|