@delmaredigital/payload-experiments 0.4.0-rc.4 → 0.4.0-rc.6

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 (123) hide show
  1. package/dist/admin/copy/newTest.d.ts +1 -1
  2. package/dist/admin/copy/newTest.js +1 -1
  3. package/dist/admin/surface/ResultsRoute.js +2 -10
  4. package/dist/admin/surface/newTest/NewTest.js +16 -18
  5. package/dist/admin/surface/newTest/PageWizard.js +10 -12
  6. package/dist/admin/surface/newTest/newTest.css +3 -1
  7. package/dist/admin/surface/results/ResultsHeader.js +15 -8
  8. package/dist/blocks/bindings.js +6 -13
  9. package/dist/blocks/placementLocale.d.ts +2 -0
  10. package/dist/blocks/placementLocale.js +16 -12
  11. package/dist/goals/fairness.d.ts +1 -1
  12. package/dist/goals/fairness.js +9 -7
  13. package/dist/hooks/blockRemovalRule.d.ts +2 -1
  14. package/dist/hooks/blockRemovalRule.js +27 -5
  15. package/dist/hooks/pageGuard.js +2 -2
  16. package/dist/puck/ExperimentPicker.d.ts +2 -1
  17. package/dist/puck/ExperimentPicker.js +34 -30
  18. package/dist/puck/ExperimentSlot.js +3 -1
  19. package/dist/puck/copy.d.ts +1 -0
  20. package/dist/puck/copy.js +1 -0
  21. package/dist/puck/editor/ConfigureTestDrawer.js +2 -2
  22. package/dist/puck/editor/CreateExperimentDrawer.d.ts +2 -1
  23. package/dist/puck/editor/CreateExperimentDrawer.js +24 -16
  24. package/dist/puck/editor/EditorControls.d.ts +1 -1
  25. package/dist/puck/editor/EditorControls.js +1 -1
  26. package/dist/puck/editor/ExperimentSlotEditor.js +624 -299
  27. package/dist/puck/editor/LeftoverVersions.d.ts +15 -0
  28. package/dist/puck/editor/LeftoverVersions.js +97 -0
  29. package/dist/puck/editor/PlacementNotice.d.ts +8 -0
  30. package/dist/puck/editor/PlacementNotice.js +22 -0
  31. package/dist/puck/editor/QuickTestActions.d.ts +3 -0
  32. package/dist/puck/editor/QuickTestActions.js +50 -0
  33. package/dist/puck/editor/ResultsReadout.js +5 -0
  34. package/dist/puck/editor/VersionBuilder.d.ts +37 -12
  35. package/dist/puck/editor/VersionBuilder.js +273 -184
  36. package/dist/puck/editor/armWrites.d.ts +11 -2
  37. package/dist/puck/editor/armWrites.js +42 -3
  38. package/dist/puck/editor/armsCache.d.ts +1 -1
  39. package/dist/puck/editor/armsCache.js +2 -1
  40. package/dist/puck/editor/cloneSlot.d.ts +2 -0
  41. package/dist/puck/editor/cloneSlot.js +7 -0
  42. package/dist/puck/editor/copy.d.ts +35 -20
  43. package/dist/puck/editor/copy.js +35 -20
  44. package/dist/puck/editor/createTest.d.ts +3 -0
  45. package/dist/puck/editor/createTest.js +33 -0
  46. package/dist/puck/editor/editor.chrome.css +32 -14
  47. package/dist/puck/editor/editor.css +58 -17
  48. package/dist/puck/editor/editor.model.d.ts +1 -1
  49. package/dist/puck/editor/editor.model.js +2 -2
  50. package/dist/puck/editor/experimentOutlinePlugin.js +96 -28
  51. package/dist/puck/editor/localPlacements.d.ts +4 -2
  52. package/dist/puck/editor/localPlacements.js +20 -18
  53. package/dist/puck/editor/outlineVersion.d.ts +5 -0
  54. package/dist/puck/editor/outlineVersion.js +36 -0
  55. package/dist/puck/editor/placementsCache.d.ts +33 -2
  56. package/dist/puck/editor/placementsCache.js +208 -33
  57. package/dist/puck/editor/sidebar.d.ts +8 -0
  58. package/dist/puck/editor/sidebar.js +74 -0
  59. package/dist/puck/editor/usePlacementNotice.d.ts +3 -0
  60. package/dist/puck/editor/usePlacementNotice.js +14 -0
  61. package/dist/puck/visibility.d.ts +21 -0
  62. package/dist/puck/visibility.js +77 -0
  63. package/dist/server/recordContentChange.js +3 -1
  64. package/dist/server/resolveExperimentData.js +4 -1
  65. package/dist/testSurface/api.js +1 -1
  66. package/dist/testSurface/copy/common.d.ts +1 -1
  67. package/dist/testSurface/copy/common.js +3 -1
  68. package/dist/testSurface/copy/readiness.d.ts +18 -1
  69. package/dist/testSurface/copy/readiness.js +23 -1
  70. package/dist/testSurface/copy/testPage.d.ts +8 -6
  71. package/dist/testSurface/copy/testPage.js +8 -6
  72. package/dist/testSurface/readPreflight.d.ts +9 -0
  73. package/dist/testSurface/readPreflight.js +38 -0
  74. package/dist/testSurface/readinessRead.d.ts +50 -0
  75. package/dist/testSurface/readinessRead.js +46 -0
  76. package/dist/testSurface/results/results.css +1 -1
  77. package/dist/testSurface/results/results.model.d.ts +1 -1
  78. package/dist/testSurface/surface/contracts.d.ts +6 -2
  79. package/dist/testSurface/surface/editorPlacement.d.ts +21 -0
  80. package/dist/testSurface/surface/editorPlacement.js +60 -0
  81. package/dist/testSurface/surface/pageBlock/parts.js +13 -3
  82. package/dist/testSurface/surface/readiness/ActionDialogs.js +65 -41
  83. package/dist/testSurface/surface/readiness/Banner.d.ts +1 -4
  84. package/dist/testSurface/surface/readiness/Banner.js +106 -68
  85. package/dist/testSurface/surface/readiness/BlockerList.d.ts +11 -0
  86. package/dist/testSurface/surface/readiness/BlockerList.js +31 -0
  87. package/dist/testSurface/surface/readiness/SaveStatus.d.ts +6 -2
  88. package/dist/testSurface/surface/readiness/SaveStatus.js +35 -22
  89. package/dist/testSurface/surface/readiness/parts.js +0 -2
  90. package/dist/testSurface/surface/readiness/readiness.model.d.ts +13 -0
  91. package/dist/testSurface/surface/readiness/readiness.model.js +51 -36
  92. package/dist/testSurface/surface/readiness/useTransition.d.ts +5 -2
  93. package/dist/testSurface/surface/readiness/useTransition.js +69 -7
  94. package/dist/testSurface/surface/test/AdvancedSection.js +1 -0
  95. package/dist/testSurface/surface/test/GoalSection.js +102 -74
  96. package/dist/testSurface/surface/test/LengthSection.d.ts +1 -1
  97. package/dist/testSurface/surface/test/LengthSection.js +2 -1
  98. package/dist/testSurface/surface/test/Rail.js +7 -4
  99. package/dist/testSurface/surface/test/ReadinessBar.js +1 -2
  100. package/dist/testSurface/surface/test/Section.d.ts +3 -1
  101. package/dist/testSurface/surface/test/Section.js +38 -6
  102. package/dist/testSurface/surface/test/TargetingSection.js +5 -0
  103. package/dist/testSurface/surface/test/TestSurface.d.ts +2 -1
  104. package/dist/testSurface/surface/test/TestSurface.js +68 -7
  105. package/dist/testSurface/surface/test/VersionsSection.js +1 -0
  106. package/dist/testSurface/surface/test/fields.d.ts +1 -3
  107. package/dist/testSurface/surface/test/fields.js +5 -35
  108. package/dist/testSurface/surface/test/testPage.css +12 -0
  109. package/dist/testSurface/surface/useTestDocument.d.ts +5 -2
  110. package/dist/testSurface/surface/useTestDocument.js +61 -46
  111. package/dist/testSurface/useReadinessPolling.d.ts +3 -0
  112. package/dist/testSurface/useReadinessPolling.js +24 -0
  113. package/dist/transitions/makeContent.js +24 -5
  114. package/dist/transitions/placements.js +23 -34
  115. package/dist/transitions/preflightData.js +1 -1
  116. package/dist/types/puck.d.ts +1 -0
  117. package/dist/ui/Card.d.ts +2 -1
  118. package/dist/ui/Card.js +4 -4
  119. package/dist/ui/Inputs.js +8 -5
  120. package/dist/ui/Tabs.d.ts +2 -2
  121. package/dist/ui/Tabs.js +33 -32
  122. package/dist/ui/ui.css +26 -3
  123. package/package.json +1 -1
@@ -1,47 +1,130 @@
1
1
  'use client';
2
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { useEffect, useId, useRef, useState } from 'react';
4
- import { addVariant, removeArm, balanceWeight, splitEvenly, totalWeight, withSlotsFor } from '../../arms/model.js';
4
+ import { balanceWeight, splitEvenly, totalWeight, withSlotsFor } from '../../arms/model.js';
5
5
  import { checkArms } from '../../collections/experiments/armInvariants.js';
6
6
  import { issueMessage } from '../../arms/copy.js';
7
7
  import { Button } from '../../ui/Button.js';
8
8
  import { Dialog } from '../../ui/Dialog.js';
9
9
  import { NumberInput, TextInput } from '../../ui/Inputs.js';
10
- import { ArmWriteError, writeArms } from './armWrites.js';
10
+ import { addVersionArms, ArmWriteError, removeVersionArms, renameVersionArms, setVersionTraffic, writeArms } from './armWrites.js';
11
11
  import { editorCopy as C } from './copy.js';
12
- export function useArmWrites(url, publish, isCurrent, onWrite) {
12
+ export function useArmWrites({ url, bindingKey, status, controlKeys, publish, isCurrent, onWrite, onSaved }) {
13
13
  const guard = useRef(false);
14
+ const running = useRef(Promise.resolve());
14
15
  const [busy, setBusy] = useState(false);
15
- const [error, setError] = useState(null);
16
- async function write(transform) {
17
- if (guard.current) return;
16
+ const [errorState, setErrors] = useState({
17
+ bindingKey,
18
+ errors: {}
19
+ });
20
+ const errors = errorState.bindingKey === bindingKey ? errorState.errors : {};
21
+ const controls = controlKeys.join(',');
22
+ const scope = useRef({
23
+ bindingKey,
24
+ status,
25
+ keys: new Set(controlKeys)
26
+ });
27
+ scope.current = {
28
+ bindingKey,
29
+ status,
30
+ keys: new Set(controlKeys)
31
+ };
32
+ useEffect(()=>{
33
+ const keys = new Set(controls.split(','));
34
+ setErrors((current)=>({
35
+ bindingKey,
36
+ errors: current.bindingKey === bindingKey ? Object.fromEntries(Object.entries(current.errors).filter(([key, refusal])=>keys.has(key) && (key !== 'split' && key !== 'add' || refusal.status === status))) : {}
37
+ }));
38
+ }, [
39
+ bindingKey,
40
+ controls,
41
+ status
42
+ ]);
43
+ const clearError = (key)=>setErrors((current)=>{
44
+ if (current.bindingKey !== bindingKey) return current;
45
+ const next = {
46
+ ...current.errors
47
+ };
48
+ delete next[key];
49
+ return {
50
+ bindingKey,
51
+ errors: next
52
+ };
53
+ });
54
+ async function write(transform, options) {
55
+ if (options?.queue) {
56
+ while(guard.current)await running.current;
57
+ if (options.cancelled?.()) return;
58
+ if (!isCurrent()) {
59
+ options.onError?.(C.staleEdit);
60
+ return;
61
+ }
62
+ } else if (guard.current) return;
63
+ let release;
64
+ running.current = new Promise((resolve)=>{
65
+ release = resolve;
66
+ });
18
67
  guard.current = true;
68
+ options?.onStart?.();
19
69
  onWrite();
20
70
  setBusy(true);
21
- setError(null);
71
+ const errorKey = options?.errorKey ?? 'add';
72
+ if (!options?.onError) clearError(errorKey);
22
73
  try {
23
- return await writeArms(url, transform, publish);
74
+ const saved = await writeArms(url, transform, (doc)=>{
75
+ // Publishing and refusing can finish in one React batch. Remember the fresh
76
+ // status now so the lock change that caused this refusal does not erase it.
77
+ const accepted = publish(doc) ?? doc;
78
+ if (scope.current.bindingKey === bindingKey) scope.current.status = accepted.status;
79
+ return accepted;
80
+ }, options);
81
+ if (isCurrent()) setErrors({
82
+ bindingKey,
83
+ errors: {}
84
+ });
85
+ onSaved?.();
86
+ return saved;
24
87
  } catch (error) {
25
- if (isCurrent()) setError(error instanceof ArmWriteError ? error.message : C.writeNetwork);
88
+ if (isCurrent()) {
89
+ const message = error instanceof ArmWriteError ? error.message : C.writeNetwork;
90
+ if (options?.onError) options.onError(message);
91
+ else if (scope.current.bindingKey === bindingKey && scope.current.keys.has(errorKey)) {
92
+ const refusal = {
93
+ message,
94
+ status: scope.current.status
95
+ };
96
+ setErrors((current)=>({
97
+ bindingKey,
98
+ errors: {
99
+ ...current.bindingKey === bindingKey ? current.errors : {},
100
+ [errorKey]: refusal
101
+ }
102
+ }));
103
+ }
104
+ }
26
105
  } finally{
27
106
  guard.current = false;
28
107
  setBusy(false);
108
+ release();
29
109
  }
30
110
  }
31
111
  return {
32
112
  write,
33
113
  busy,
34
- error
114
+ error: Object.entries(errors).filter(([key])=>!key.startsWith('remove:')).map(([, refusal])=>refusal.message).join(' '),
115
+ errorFor: (key)=>errors[key]?.message,
116
+ clearError
35
117
  };
36
118
  }
37
119
  export function AddVariant({ experiment, writes, onSelect }) {
38
120
  if (experiment.arms.filter((arm)=>arm.kind === 'variant').length >= 3) return null;
39
121
  return /*#__PURE__*/ _jsx(Button, {
40
- disabled: writes.busy,
122
+ "aria-disabled": writes.busy || undefined,
41
123
  onClick: async ()=>{
124
+ if (writes.busy) return;
42
125
  let added;
43
126
  const saved = await writes.write((arms)=>{
44
- const next = addVariant(arms, 'block');
127
+ const next = addVersionArms(arms);
45
128
  added = next.find((arm)=>!arms.some((existing)=>existing.key === arm.key))?.key;
46
129
  return next;
47
130
  });
@@ -50,197 +133,150 @@ export function AddVariant({ experiment, writes, onSelect }) {
50
133
  children: C.add
51
134
  });
52
135
  }
53
- export function VersionActions({ arm, writes, editable, open, onOpenChange, canCopy, onCopy, onRequestRemove }) {
136
+ /** Always-visible editor for the saved version name. */ export function VersionName({ arm, writes, editable }) {
54
137
  const id = useId();
55
- const [name, setName] = useState(null);
138
+ const [name, setName] = useState(arm.label);
139
+ const draft = useRef(arm.label);
140
+ const pending = useRef(null);
141
+ const [error, setError] = useState();
56
142
  const [invalid, setInvalid] = useState(false);
57
- const trigger = useRef(null);
58
- const focusTrigger = ()=>trigger.current?.querySelector('button')?.focus();
59
- const restoreFocus = useRef(false);
143
+ const [saved, setSaved] = useState(false);
60
144
  useEffect(()=>{
61
- if (restoreFocus.current && !writes.busy) {
62
- restoreFocus.current = false;
63
- focusTrigger();
64
- }
145
+ draft.current = arm.label;
146
+ setName(arm.label);
65
147
  }, [
66
- writes.busy,
67
- name
148
+ arm.label
68
149
  ]);
69
150
  useEffect(()=>{
70
- if (!open) return;
71
- const node = trigger.current;
72
- const doc = node?.ownerDocument;
73
- const outside = (event)=>{
74
- if (node && !event.composedPath().includes(node)) onOpenChange(false);
75
- };
76
- // Focus leaving the canvas iframe (Puck's own panels) or the window fires window blur, not a document pointerdown.
77
- const leave = ()=>onOpenChange(false);
78
- const view = doc?.defaultView;
79
- doc?.addEventListener('pointerdown', outside, true);
80
- view?.addEventListener('blur', leave);
81
- return ()=>{
82
- doc?.removeEventListener('pointerdown', outside, true);
83
- view?.removeEventListener('blur', leave);
84
- };
151
+ if (!saved) return;
152
+ const timer = setTimeout(()=>setSaved(false), 2000);
153
+ return ()=>clearTimeout(timer);
85
154
  }, [
86
- open,
87
- onOpenChange
155
+ saved
88
156
  ]);
89
- const closeMenu = ()=>{
90
- onOpenChange(false);
91
- focusTrigger();
92
- };
93
- const slot = arm.key;
94
- async function rename() {
95
- if (!name?.trim()) {
157
+ async function commit() {
158
+ if (!editable || pending.current || draft.current === arm.label) return;
159
+ if (!draft.current.trim()) {
96
160
  setInvalid(true);
97
161
  return;
98
162
  }
99
- const label = name.trim();
100
- const saved = await writes.write((arms)=>arms.map((current)=>current.key === arm.key ? {
101
- ...current,
102
- label
103
- } : current));
104
- if (saved) {
105
- restoreFocus.current = true;
106
- setName(null);
163
+ const label = draft.current.trim();
164
+ const attempt = {
165
+ started: false,
166
+ cancelled: false
167
+ };
168
+ pending.current = attempt;
169
+ setError(undefined);
170
+ setSaved(false);
171
+ try {
172
+ const result = await writes.write((arms)=>renameVersionArms(arms, arm.key, label), {
173
+ onError: setError,
174
+ queue: true,
175
+ cancelled: ()=>attempt.cancelled,
176
+ onStart: ()=>{
177
+ attempt.started = true;
178
+ }
179
+ });
180
+ if (result) setSaved(true);
181
+ } finally{
182
+ pending.current = null;
107
183
  }
108
184
  }
109
185
  return /*#__PURE__*/ _jsxs("div", {
110
- ref: trigger,
111
- className: "xp-slot-version-actions",
112
- onBlur: (event)=>{
113
- // Safari/Firefox may blur a keyboard-focused trigger without focusing the clicked item.
114
- // Outside pointerdown handles real outside clicks, including non-focusable targets.
115
- if (event.relatedTarget !== null && !event.currentTarget.contains(event.relatedTarget)) onOpenChange(false);
116
- },
117
- onKeyDown: (event)=>{
118
- if (event.key === 'Escape' && !writes.busy) {
119
- event.preventDefault();
120
- closeMenu();
121
- setName(null);
122
- }
123
- },
186
+ className: "xp-slot-version-name",
124
187
  children: [
125
- /*#__PURE__*/ _jsxs("div", {
126
- className: "xp-slot-version-anchor",
127
- children: [
128
- editable || arm.kind !== 'control' ? /*#__PURE__*/ _jsx(Button, {
129
- size: "sm",
130
- variant: "quiet",
131
- "aria-label": C.menu(arm.label),
132
- "aria-expanded": open,
133
- "aria-controls": `${id}-menu`,
134
- disabled: writes.busy,
135
- onClick: ()=>onOpenChange(!open),
136
- children: "⋯"
137
- }) : null,
138
- open ? /*#__PURE__*/ _jsxs("div", {
139
- id: `${id}-menu`,
140
- className: "xp-slot-version-menu",
141
- role: "group",
142
- tabIndex: -1,
143
- "aria-label": C.menu(arm.label),
144
- children: [
145
- editable ? /*#__PURE__*/ _jsx(Button, {
146
- variant: "quiet",
147
- size: "sm",
148
- onClick: ()=>{
149
- setName(arm.label);
150
- setInvalid(false);
151
- closeMenu();
152
- },
153
- children: C.rename
154
- }) : null,
155
- arm.kind !== 'control' ? /*#__PURE__*/ _jsxs(_Fragment, {
156
- children: [
157
- /*#__PURE__*/ _jsx(Button, {
158
- variant: "quiet",
159
- size: "sm",
160
- disabled: !canCopy,
161
- onClick: ()=>{
162
- closeMenu();
163
- onCopy(slot);
164
- },
165
- children: C.copy
166
- }),
167
- editable ? /*#__PURE__*/ _jsx(Button, {
168
- variant: "quiet",
169
- size: "sm",
170
- onClick: ()=>{
171
- closeMenu();
172
- onRequestRemove();
173
- },
174
- children: C.remove
175
- }) : null
176
- ]
177
- }) : null
178
- ]
179
- }) : null
180
- ]
181
- }),
182
- name !== null ? /*#__PURE__*/ _jsx("div", {
183
- className: "xp-slot-version-rename",
184
- children: /*#__PURE__*/ _jsx(TextInput, {
185
- id: `${id}-name`,
186
- label: C.renameLabel,
187
- autoFocus: true,
188
- value: name,
189
- disabled: writes.busy,
190
- error: invalid ? C.emptyLabel : undefined,
191
- onChange: (event)=>{
192
- setName(event.target.value);
188
+ /*#__PURE__*/ _jsx(TextInput, {
189
+ id: id,
190
+ label: C.name,
191
+ "aria-label": `${arm.label} ${C.name}`,
192
+ value: name,
193
+ readOnly: !editable || writes.busy,
194
+ "aria-disabled": writes.busy || undefined,
195
+ error: invalid ? C.emptyLabel : error,
196
+ onChange: (event)=>{
197
+ draft.current = event.target.value;
198
+ setName(event.target.value);
199
+ setInvalid(false);
200
+ setError(undefined);
201
+ setSaved(false);
202
+ },
203
+ onBlur: ()=>void commit(),
204
+ onKeyDown: (event)=>{
205
+ if (event.key === 'Enter') {
206
+ event.preventDefault();
207
+ void commit();
208
+ }
209
+ if (event.key === 'Escape') {
210
+ event.preventDefault();
211
+ if (pending.current?.started) return;
212
+ if (pending.current) pending.current.cancelled = true;
213
+ draft.current = arm.label;
214
+ setName(arm.label);
193
215
  setInvalid(false);
194
- },
195
- onKeyDown: (event)=>{
196
- if (event.key === 'Enter') {
197
- event.preventDefault();
198
- void rename();
199
- }
216
+ setError(undefined);
217
+ setSaved(false);
200
218
  }
201
- })
219
+ }
220
+ }),
221
+ saved ? /*#__PURE__*/ _jsx("span", {
222
+ role: "status",
223
+ className: "xp-ui-help",
224
+ children: C.saved
202
225
  }) : null
203
226
  ]
204
227
  });
205
228
  }
206
- export function RemoveVersion({ arm, writes, onRemove, onClose }) {
207
- const slot = arm.key;
208
- const [tried, setTried] = useState(false);
229
+ export function RemoveVersion({ arm, writes, count, shared, onRemove, onClose }) {
230
+ const slot = arm.key === 'holdout' ? undefined : arm.key;
231
+ const close = ()=>{
232
+ writes.clearError(`remove:${arm.key}`);
233
+ onClose();
234
+ };
209
235
  return /*#__PURE__*/ _jsx(Dialog, {
210
236
  open: true,
211
237
  title: C.removeTitle(arm.label),
212
238
  initialFocus: "cancel",
213
239
  dismissible: !writes.busy,
214
- onClose: ()=>onClose(),
240
+ onClose: close,
215
241
  children: /*#__PURE__*/ _jsxs("div", {
216
242
  className: "xp-slot-drawer",
217
243
  children: [
218
244
  /*#__PURE__*/ _jsx("p", {
219
- children: C.removeHelp
245
+ children: C.removeHelp(count)
220
246
  }),
221
- tried && writes.error ? /*#__PURE__*/ _jsx("p", {
247
+ shared === undefined ? /*#__PURE__*/ _jsx("p", {
248
+ children: C.removeScopeUnknown
249
+ }) : shared ? /*#__PURE__*/ _jsx("p", {
250
+ children: C.removeShared
251
+ }) : null,
252
+ writes.errorFor(`remove:${arm.key}`) ? /*#__PURE__*/ _jsx("p", {
222
253
  role: "alert",
223
254
  className: "xp-ui-error",
224
- children: writes.error
255
+ children: writes.errorFor(`remove:${arm.key}`)
225
256
  }) : null,
226
257
  /*#__PURE__*/ _jsxs("div", {
227
258
  className: "xp-ui-button-group",
228
259
  children: [
229
260
  /*#__PURE__*/ _jsx(Button, {
230
261
  "data-dialog-cancel": true,
231
- disabled: writes.busy,
232
- onClick: ()=>onClose(),
262
+ "aria-disabled": writes.busy || undefined,
263
+ onClick: ()=>{
264
+ if (!writes.busy) close();
265
+ },
233
266
  children: C.cancel
234
267
  }),
235
268
  /*#__PURE__*/ _jsx(Button, {
236
269
  variant: "danger",
237
- busy: writes.busy,
270
+ "aria-busy": writes.busy || undefined,
271
+ "aria-disabled": writes.busy || undefined,
238
272
  onClick: async ()=>{
239
- setTried(true);
240
- const saved = await writes.write((arms)=>splitEvenly(removeArm(arms, arm.key)));
273
+ if (writes.busy) return;
274
+ const saved = await writes.write((arms)=>removeVersionArms(arms, arm.key), {
275
+ errorKey: `remove:${arm.key}`
276
+ });
241
277
  if (saved) {
242
- onRemove(slot);
243
- onClose();
278
+ if (slot) onRemove(slot);
279
+ close();
244
280
  }
245
281
  },
246
282
  children: C.remove
@@ -251,67 +287,120 @@ export function RemoveVersion({ arm, writes, onRemove, onClose }) {
251
287
  })
252
288
  });
253
289
  }
254
- export function VersionWeights({ experiment, writes }) {
290
+ export function VersionWeights({ experiment, writes, edits, setEdits, groupId, splitIntent, editable = true, beforeSplit, renderRow }) {
255
291
  const id = useId();
256
292
  const split = useRef(null);
257
- const [edits, setEdits] = useState({});
293
+ const [errors, setErrors] = useState({});
294
+ const armKeys = experiment.arms.map((arm)=>arm.key).join(',');
295
+ useEffect(()=>{
296
+ const keys = new Set(armKeys.split(','));
297
+ setErrors((current)=>Object.fromEntries(Object.entries(current).filter(([key])=>keys.has(key))));
298
+ }, [
299
+ armKeys
300
+ ]);
301
+ const clearErrors = (keys)=>setErrors((current)=>Object.fromEntries(Object.entries(current).filter(([key])=>!keys.includes(key))));
258
302
  const invalidKeys = new Set(Object.entries(edits).filter(([, raw])=>raw.trim() === '' || !Number.isInteger(Number(raw)) || Number(raw) < 0 || Number(raw) > 100).map(([key])=>key));
259
- const apply = (arms)=>arms.reduce((next, arm)=>edits[arm.key] === undefined ? next : (()=>{
260
- const value = edits[arm.key].trim() === '' ? NaN : Number(edits[arm.key]);
261
- return invalidKeys.has(arm.key) ? next : balanceWeight(next, arm.key, value);
262
- })(), arms);
263
- const live = withSlotsFor(apply(experiment.arms), 'block');
303
+ const apply = (arms, key)=>balanceWeight(arms, key, Number(edits[key]));
304
+ const keys = Object.keys(edits);
305
+ const clearDraft = (key)=>setEdits((current)=>Object.fromEntries(Object.entries(current).filter(([candidate])=>candidate !== key)));
306
+ const live = withSlotsFor(keys.length === 1 && !invalidKeys.has(keys[0]) && !errors[keys[0]] ? apply(experiment.arms, keys[0]) : experiment.arms, 'block');
264
307
  const issues = checkArms(live, 'block');
265
- async function commit() {
266
- if (!Object.keys(edits).length || invalidKeys.size || issues.length) return;
267
- const saved = await writes.write(apply);
268
- if (saved) setEdits({});
308
+ async function commit(key) {
309
+ if (writes.busy || edits[key] === undefined || invalidKeys.has(key)) return;
310
+ clearErrors([
311
+ key
312
+ ]);
313
+ const saved = await writes.write((arms)=>setVersionTraffic(arms, key, Number(edits[key])), {
314
+ onError: (message)=>setErrors((current)=>({
315
+ ...current,
316
+ [key]: message
317
+ }))
318
+ });
319
+ if (saved) clearDraft(key);
269
320
  }
270
321
  return /*#__PURE__*/ _jsxs("div", {
271
322
  className: "xp-slot-weights",
272
323
  children: [
273
324
  /*#__PURE__*/ _jsx("div", {
274
325
  className: "xp-slot-weight-inputs",
275
- children: experiment.arms.map((arm)=>/*#__PURE__*/ _jsx(NumberInput, {
326
+ children: experiment.arms.map((arm)=>{
327
+ const input = /*#__PURE__*/ _jsx(NumberInput, {
276
328
  id: `${id}-${arm.key}`,
277
- label: C.weight(arm.label),
329
+ label: C.traffic,
330
+ "aria-label": C.weight(arm.label),
278
331
  min: 0,
279
332
  max: 100,
280
333
  step: 1,
281
334
  value: edits[arm.key] ?? live.find((current)=>current.key === arm.key)?.weight ?? '',
282
- disabled: writes.busy,
283
- error: invalidKeys.has(arm.key) ? C.invalidShare : undefined,
284
- onChange: (event)=>setEdits({
285
- [arm.key]: event.target.value
286
- }),
335
+ readOnly: !editable || writes.busy,
336
+ "aria-disabled": writes.busy || undefined,
337
+ error: invalidKeys.has(arm.key) ? C.invalidShare : errors[arm.key],
338
+ onChange: (event)=>{
339
+ clearErrors([
340
+ arm.key
341
+ ]);
342
+ const value = event.target.value;
343
+ setEdits((current)=>({
344
+ ...current,
345
+ [arm.key]: value
346
+ }));
347
+ },
287
348
  onBlur: (event)=>{
288
- if (!split.current?.contains(event.relatedTarget)) void commit();
349
+ const target = event.relatedTarget;
350
+ if (!splitIntent.current && !(typeof target?.closest === 'function' && target.closest('[data-xp-split]')?.getAttribute('data-xp-split') === groupId)) void commit(arm.key);
289
351
  },
290
352
  onKeyDown: (event)=>{
291
353
  if (event.key === 'Enter') {
292
354
  event.preventDefault();
293
- void commit();
355
+ void commit(arm.key);
356
+ }
357
+ if (event.key === 'Escape' && !writes.busy) {
358
+ event.preventDefault();
359
+ clearErrors([
360
+ arm.key
361
+ ]);
362
+ clearDraft(arm.key);
294
363
  }
295
364
  }
296
- }, arm.key))
365
+ });
366
+ return /*#__PURE__*/ _jsx("div", {
367
+ children: renderRow ? renderRow(arm, input) : input
368
+ }, arm.key);
369
+ })
297
370
  }),
298
371
  Object.keys(edits).length && !invalidKeys.size ? issues.map((issue, i)=>/*#__PURE__*/ _jsx("p", {
299
372
  role: "alert",
300
373
  className: "xp-ui-error",
301
374
  children: issueMessage(issue, totalWeight(live))
302
375
  }, i)) : null,
303
- /*#__PURE__*/ _jsx("div", {
376
+ beforeSplit,
377
+ editable ? /*#__PURE__*/ _jsx("div", {
304
378
  ref: split,
379
+ "data-xp-split": groupId,
380
+ onPointerDownCapture: ()=>{
381
+ // Pointer intent reaches the destination before the source iframe loses focus.
382
+ splitIntent.current = true;
383
+ setTimeout(()=>{
384
+ splitIntent.current = false;
385
+ }, 0);
386
+ },
305
387
  children: /*#__PURE__*/ _jsx(Button, {
306
388
  variant: "quiet",
307
389
  size: "sm",
308
- disabled: writes.busy,
390
+ "aria-disabled": writes.busy || undefined,
309
391
  onClick: async ()=>{
310
- if (await writes.write(splitEvenly)) setEdits({});
392
+ if (writes.busy) return;
393
+ const saved = await writes.write(splitEvenly, {
394
+ errorKey: 'split'
395
+ });
396
+ if (saved) {
397
+ setEdits(()=>({}));
398
+ setErrors({});
399
+ }
311
400
  },
312
401
  children: C.split
313
402
  })
314
- })
403
+ }) : null
315
404
  ]
316
405
  });
317
406
  }
@@ -1,7 +1,16 @@
1
- import type { Arm } from '../../types/contracts.js';
1
+ import type { Arm, ArmKey } from '../../types/contracts.js';
2
+ import type { ArmSlot } from '../../types/puck.js';
2
3
  import type { EditorExperiment } from './editor.model.js';
3
4
  export declare class ArmWriteError extends Error {
4
5
  }
6
+ /** Refuse obsolete intents before a no-op can become a successful PATCH. */
7
+ export declare function removeVersionArms(arms: Arm[], key: ArmKey): Arm[];
8
+ export declare function addVersionArms(arms: Arm[]): Arm[];
9
+ export declare function setVersionTraffic(arms: Arm[], key: ArmKey, weight: number): Arm[];
10
+ export declare function renameVersionArms(arms: Arm[], key: ArmKey, label: string): Arm[];
11
+ export declare function addLeftoverArms(arms: Arm[], slot: ArmSlot): Arm[];
5
12
  export declare function readExperiment(url: string, signal?: AbortSignal): Promise<EditorExperiment>;
6
13
  /** Each intent is applied to a fresh document, guarded by the collection's compare-and-set header. */
7
- export declare function writeArms(url: string, transform: (arms: Arm[]) => Arm[], publish: (doc: EditorExperiment) => void): Promise<Arm[]>;
14
+ export declare function writeArms(url: string, transform: (arms: Arm[]) => Arm[], publish: (doc: EditorExperiment) => EditorExperiment | void, { draftOnly }?: {
15
+ draftOnly?: boolean;
16
+ }): Promise<Arm[]>;