@byline/admin 4.19.0 → 5.0.0

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.
@@ -20,6 +20,10 @@ import { RelationPicker } from '../fields/relation/relation-picker.js'
20
20
  import styles from './tree-placement-widget.module.css'
21
21
 
22
22
  export interface TreePlacementWidgetProps {
23
+ disabled?: boolean
24
+ onMutationError?: (error: unknown) => 'blocked' | 'committed' | null | void
25
+ onCommitted?: (receipt: import('@byline/core').StructuralMutationReceipt) => void
26
+ expectedRevision: number
23
27
  /** The collection path (`tree: true`). */
24
28
  collectionPath: string
25
29
  /** The logical id of the document being edited. */
@@ -44,6 +48,10 @@ export interface TreePlacementWidgetProps {
44
48
  */
45
49
  export const TreePlacementWidget = ({
46
50
  collectionPath,
51
+ disabled = false,
52
+ onMutationError,
53
+ onCommitted,
54
+ expectedRevision,
47
55
  documentId,
48
56
  useAsTitle,
49
57
  }: TreePlacementWidgetProps) => {
@@ -99,7 +107,7 @@ export const TreePlacementWidget = ({
99
107
  // placement (including making the node a root) leaves it placed.
100
108
  const place = useCallback(
101
109
  async (parentDocumentId: string | null, optimistic: { id: string; title: string } | null) => {
102
- if (placeTreeNode == null || busy) return
110
+ if (disabled || placeTreeNode == null || busy) return
103
111
  const previousParent = parent
104
112
  const previousPlaced = placed
105
113
  setError(null)
@@ -107,8 +115,15 @@ export const TreePlacementWidget = ({
107
115
  setParent(optimistic)
108
116
  setPlaced(true)
109
117
  try {
110
- await placeTreeNode({ collection: collectionPath, documentId, parentDocumentId })
111
- } catch {
118
+ const result = await placeTreeNode({
119
+ expectedRevision,
120
+ collection: collectionPath,
121
+ documentId,
122
+ parentDocumentId,
123
+ })
124
+ onCommitted?.(result)
125
+ } catch (error) {
126
+ if (onMutationError?.(error) === 'committed') return
112
127
  setParent(previousParent)
113
128
  setPlaced(previousPlaced)
114
129
  setError(t('treeWidget.error'))
@@ -116,7 +131,19 @@ export const TreePlacementWidget = ({
116
131
  setBusy(false)
117
132
  }
118
133
  },
119
- [placeTreeNode, busy, parent, placed, collectionPath, documentId, t]
134
+ [
135
+ disabled,
136
+ onMutationError,
137
+ onCommitted,
138
+ expectedRevision,
139
+ placeTreeNode,
140
+ busy,
141
+ parent,
142
+ placed,
143
+ collectionPath,
144
+ documentId,
145
+ t,
146
+ ]
120
147
  )
121
148
 
122
149
  const handlePick = useCallback(
@@ -156,7 +183,7 @@ export const TreePlacementWidget = ({
156
183
  size="xs"
157
184
  variant="outlined"
158
185
  intent="noeffect"
159
- disabled={loading || busy}
186
+ disabled={disabled || loading || busy}
160
187
  onClick={() => setPickerOpen(true)}
161
188
  >
162
189
  {t('treeWidget.choose')}
@@ -165,7 +192,7 @@ export const TreePlacementWidget = ({
165
192
  <button
166
193
  type="button"
167
194
  className={cx('byline-form-tree-link', styles.link)}
168
- disabled={loading || busy}
195
+ disabled={disabled || loading || busy}
169
196
  onClick={() => place(null, null)}
170
197
  >
171
198
  {t('treeWidget.addToTree')}
@@ -175,7 +202,7 @@ export const TreePlacementWidget = ({
175
202
  <button
176
203
  type="button"
177
204
  className={cx('byline-form-tree-link', styles.link)}
178
- disabled={busy}
205
+ disabled={disabled || busy}
179
206
  onClick={() => place(null, null)}
180
207
  >
181
208
  {t('treeWidget.makeRoot')}