@delmaredigital/payload-experiments 0.4.0-rc.5 → 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.
- package/dist/admin/copy/newTest.d.ts +1 -1
- package/dist/admin/copy/newTest.js +1 -1
- package/dist/admin/surface/newTest/NewTest.js +16 -18
- package/dist/admin/surface/newTest/newTest.css +3 -1
- package/dist/blocks/bindings.js +6 -13
- package/dist/blocks/placementLocale.d.ts +2 -0
- package/dist/blocks/placementLocale.js +16 -12
- package/dist/goals/fairness.js +8 -6
- package/dist/hooks/blockRemovalRule.d.ts +2 -1
- package/dist/hooks/blockRemovalRule.js +27 -5
- package/dist/hooks/pageGuard.js +2 -2
- package/dist/puck/ExperimentPicker.d.ts +2 -1
- package/dist/puck/ExperimentPicker.js +34 -30
- package/dist/puck/ExperimentSlot.js +3 -1
- package/dist/puck/copy.d.ts +1 -0
- package/dist/puck/copy.js +1 -0
- package/dist/puck/editor/CreateExperimentDrawer.d.ts +2 -1
- package/dist/puck/editor/CreateExperimentDrawer.js +24 -20
- package/dist/puck/editor/EditorControls.d.ts +1 -1
- package/dist/puck/editor/EditorControls.js +1 -1
- package/dist/puck/editor/ExperimentSlotEditor.js +354 -374
- package/dist/puck/editor/LeftoverVersions.d.ts +3 -1
- package/dist/puck/editor/LeftoverVersions.js +45 -63
- package/dist/puck/editor/QuickTestActions.js +2 -2
- package/dist/puck/editor/VersionBuilder.d.ts +24 -13
- package/dist/puck/editor/VersionBuilder.js +264 -205
- package/dist/puck/editor/armWrites.d.ts +9 -2
- package/dist/puck/editor/armWrites.js +40 -2
- package/dist/puck/editor/armsCache.d.ts +1 -1
- package/dist/puck/editor/armsCache.js +2 -1
- package/dist/puck/editor/copy.d.ts +19 -25
- package/dist/puck/editor/copy.js +19 -25
- package/dist/puck/editor/createTest.d.ts +3 -0
- package/dist/puck/editor/createTest.js +33 -0
- package/dist/puck/editor/editor.chrome.css +24 -17
- package/dist/puck/editor/editor.css +33 -21
- package/dist/puck/editor/editor.model.d.ts +1 -1
- package/dist/puck/editor/editor.model.js +2 -2
- package/dist/puck/editor/experimentOutlinePlugin.js +1 -1
- package/dist/puck/editor/localPlacements.d.ts +4 -2
- package/dist/puck/editor/localPlacements.js +20 -18
- package/dist/puck/editor/sidebar.d.ts +2 -6
- package/dist/puck/editor/sidebar.js +3 -6
- package/dist/puck/visibility.d.ts +21 -0
- package/dist/puck/visibility.js +77 -0
- package/dist/server/recordContentChange.js +3 -1
- package/dist/server/resolveExperimentData.js +4 -1
- package/dist/testSurface/copy/readiness.d.ts +1 -0
- package/dist/testSurface/copy/readiness.js +1 -0
- package/dist/testSurface/copy/testPage.d.ts +7 -6
- package/dist/testSurface/copy/testPage.js +7 -6
- package/dist/testSurface/results/results.css +1 -1
- package/dist/testSurface/results/results.model.d.ts +1 -1
- package/dist/testSurface/surface/readiness/ActionDialogs.js +28 -34
- package/dist/testSurface/surface/test/GoalSection.js +101 -74
- package/dist/testSurface/surface/test/fields.d.ts +1 -3
- package/dist/testSurface/surface/test/fields.js +5 -35
- package/dist/transitions/makeContent.js +24 -5
- package/dist/transitions/placements.js +23 -34
- package/dist/types/puck.d.ts +1 -0
- package/dist/ui/Inputs.js +8 -5
- package/dist/ui/Tabs.d.ts +0 -1
- package/dist/ui/Tabs.js +28 -40
- package/dist/ui/ui.css +9 -4
- package/package.json +1 -1
|
@@ -1,49 +1,130 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsx as _jsx, jsxs as _jsxs
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { useEffect, useId, useRef, useState } from 'react';
|
|
4
|
-
import {
|
|
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, onSaved) {
|
|
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 [
|
|
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
|
+
});
|
|
16
54
|
async function write(transform, options) {
|
|
17
|
-
if (
|
|
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
|
-
|
|
71
|
+
const errorKey = options?.errorKey ?? 'add';
|
|
72
|
+
if (!options?.onError) clearError(errorKey);
|
|
22
73
|
try {
|
|
23
|
-
const saved = await writeArms(url, transform,
|
|
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
|
+
});
|
|
24
85
|
onSaved?.();
|
|
25
86
|
return saved;
|
|
26
87
|
} catch (error) {
|
|
27
|
-
if (isCurrent())
|
|
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
|
+
}
|
|
28
105
|
} finally{
|
|
29
106
|
guard.current = false;
|
|
30
107
|
setBusy(false);
|
|
108
|
+
release();
|
|
31
109
|
}
|
|
32
110
|
}
|
|
33
111
|
return {
|
|
34
112
|
write,
|
|
35
113
|
busy,
|
|
36
|
-
error
|
|
114
|
+
error: Object.entries(errors).filter(([key])=>!key.startsWith('remove:')).map(([, refusal])=>refusal.message).join(' '),
|
|
115
|
+
errorFor: (key)=>errors[key]?.message,
|
|
116
|
+
clearError
|
|
37
117
|
};
|
|
38
118
|
}
|
|
39
119
|
export function AddVariant({ experiment, writes, onSelect }) {
|
|
40
120
|
if (experiment.arms.filter((arm)=>arm.kind === 'variant').length >= 3) return null;
|
|
41
121
|
return /*#__PURE__*/ _jsx(Button, {
|
|
42
|
-
disabled: writes.busy,
|
|
122
|
+
"aria-disabled": writes.busy || undefined,
|
|
43
123
|
onClick: async ()=>{
|
|
124
|
+
if (writes.busy) return;
|
|
44
125
|
let added;
|
|
45
126
|
const saved = await writes.write((arms)=>{
|
|
46
|
-
const next =
|
|
127
|
+
const next = addVersionArms(arms);
|
|
47
128
|
added = next.find((arm)=>!arms.some((existing)=>existing.key === arm.key))?.key;
|
|
48
129
|
return next;
|
|
49
130
|
});
|
|
@@ -52,179 +133,111 @@ export function AddVariant({ experiment, writes, onSelect }) {
|
|
|
52
133
|
children: C.add
|
|
53
134
|
});
|
|
54
135
|
}
|
|
55
|
-
|
|
136
|
+
/** Always-visible editor for the saved version name. */ export function VersionName({ arm, writes, editable }) {
|
|
56
137
|
const id = useId();
|
|
57
|
-
const [name, setName] = useState(
|
|
138
|
+
const [name, setName] = useState(arm.label);
|
|
139
|
+
const draft = useRef(arm.label);
|
|
140
|
+
const pending = useRef(null);
|
|
141
|
+
const [error, setError] = useState();
|
|
58
142
|
const [invalid, setInvalid] = useState(false);
|
|
59
|
-
const
|
|
60
|
-
const focusTrigger = ()=>trigger.current?.querySelector('button')?.focus();
|
|
61
|
-
const restoreFocus = useRef(false);
|
|
143
|
+
const [saved, setSaved] = useState(false);
|
|
62
144
|
useEffect(()=>{
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
focusTrigger();
|
|
66
|
-
}
|
|
145
|
+
draft.current = arm.label;
|
|
146
|
+
setName(arm.label);
|
|
67
147
|
}, [
|
|
68
|
-
|
|
69
|
-
name
|
|
148
|
+
arm.label
|
|
70
149
|
]);
|
|
71
150
|
useEffect(()=>{
|
|
72
|
-
if (!
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
const outside = (event)=>{
|
|
76
|
-
if (node && !event.composedPath().includes(node)) onOpenChange(false);
|
|
77
|
-
};
|
|
78
|
-
// Focus leaving the canvas iframe (Puck's own panels) or the window fires window blur, not a document pointerdown.
|
|
79
|
-
const leave = ()=>onOpenChange(false);
|
|
80
|
-
const view = doc?.defaultView;
|
|
81
|
-
doc?.addEventListener('pointerdown', outside, true);
|
|
82
|
-
view?.addEventListener('blur', leave);
|
|
83
|
-
return ()=>{
|
|
84
|
-
doc?.removeEventListener('pointerdown', outside, true);
|
|
85
|
-
view?.removeEventListener('blur', leave);
|
|
86
|
-
};
|
|
151
|
+
if (!saved) return;
|
|
152
|
+
const timer = setTimeout(()=>setSaved(false), 2000);
|
|
153
|
+
return ()=>clearTimeout(timer);
|
|
87
154
|
}, [
|
|
88
|
-
|
|
89
|
-
onOpenChange
|
|
155
|
+
saved
|
|
90
156
|
]);
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
95
|
-
const slot = arm.key;
|
|
96
|
-
async function rename() {
|
|
97
|
-
if (!name?.trim()) {
|
|
157
|
+
async function commit() {
|
|
158
|
+
if (!editable || pending.current || draft.current === arm.label) return;
|
|
159
|
+
if (!draft.current.trim()) {
|
|
98
160
|
setInvalid(true);
|
|
99
161
|
return;
|
|
100
162
|
}
|
|
101
|
-
const label =
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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;
|
|
109
183
|
}
|
|
110
184
|
}
|
|
111
185
|
return /*#__PURE__*/ _jsxs("div", {
|
|
112
|
-
|
|
113
|
-
className: "xp-slot-version-actions",
|
|
114
|
-
onBlur: (event)=>{
|
|
115
|
-
// Safari/Firefox may blur a keyboard-focused trigger without focusing the clicked item.
|
|
116
|
-
// Outside pointerdown handles real outside clicks, including non-focusable targets.
|
|
117
|
-
if (event.relatedTarget !== null && !event.currentTarget.contains(event.relatedTarget)) onOpenChange(false);
|
|
118
|
-
},
|
|
119
|
-
onKeyDown: (event)=>{
|
|
120
|
-
if (event.key === 'Escape' && !writes.busy) {
|
|
121
|
-
event.preventDefault();
|
|
122
|
-
closeMenu();
|
|
123
|
-
setName(null);
|
|
124
|
-
}
|
|
125
|
-
},
|
|
186
|
+
className: "xp-slot-version-name",
|
|
126
187
|
children: [
|
|
127
|
-
/*#__PURE__*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
setName(arm.label);
|
|
155
|
-
setInvalid(false);
|
|
156
|
-
closeMenu();
|
|
157
|
-
},
|
|
158
|
-
children: C.rename
|
|
159
|
-
}) : null,
|
|
160
|
-
arm.kind === 'variant' ? /*#__PURE__*/ _jsxs(_Fragment, {
|
|
161
|
-
children: [
|
|
162
|
-
/*#__PURE__*/ _jsx(Button, {
|
|
163
|
-
variant: "quiet",
|
|
164
|
-
size: "sm",
|
|
165
|
-
disabled: !canCopy,
|
|
166
|
-
onClick: ()=>{
|
|
167
|
-
closeMenu();
|
|
168
|
-
onCopy(slot);
|
|
169
|
-
},
|
|
170
|
-
children: C.copy
|
|
171
|
-
}),
|
|
172
|
-
!canCopy ? /*#__PURE__*/ _jsx("p", {
|
|
173
|
-
className: "xp-ui-help",
|
|
174
|
-
children: C.copyEmpty
|
|
175
|
-
}) : null
|
|
176
|
-
]
|
|
177
|
-
}) : null,
|
|
178
|
-
arm.kind !== 'control' && editable ? /*#__PURE__*/ _jsx(Button, {
|
|
179
|
-
variant: "quiet",
|
|
180
|
-
size: "sm",
|
|
181
|
-
onClick: ()=>{
|
|
182
|
-
closeMenu();
|
|
183
|
-
onRequestRemove();
|
|
184
|
-
},
|
|
185
|
-
children: C.removeVersion
|
|
186
|
-
}) : null,
|
|
187
|
-
running ? /*#__PURE__*/ _jsx("p", {
|
|
188
|
-
className: "xp-ui-help",
|
|
189
|
-
children: C.runningVersions
|
|
190
|
-
}) : null
|
|
191
|
-
]
|
|
192
|
-
}) : null
|
|
193
|
-
]
|
|
194
|
-
}),
|
|
195
|
-
name !== null ? /*#__PURE__*/ _jsx("div", {
|
|
196
|
-
className: "xp-slot-version-rename",
|
|
197
|
-
children: /*#__PURE__*/ _jsx(TextInput, {
|
|
198
|
-
id: `${id}-name`,
|
|
199
|
-
label: C.renameLabel,
|
|
200
|
-
autoFocus: true,
|
|
201
|
-
value: name,
|
|
202
|
-
disabled: writes.busy,
|
|
203
|
-
error: invalid ? C.emptyLabel : undefined,
|
|
204
|
-
onChange: (event)=>{
|
|
205
|
-
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);
|
|
206
215
|
setInvalid(false);
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if (event.key === 'Enter') {
|
|
210
|
-
event.preventDefault();
|
|
211
|
-
void rename();
|
|
212
|
-
}
|
|
216
|
+
setError(undefined);
|
|
217
|
+
setSaved(false);
|
|
213
218
|
}
|
|
214
|
-
}
|
|
219
|
+
}
|
|
220
|
+
}),
|
|
221
|
+
saved ? /*#__PURE__*/ _jsx("span", {
|
|
222
|
+
role: "status",
|
|
223
|
+
className: "xp-ui-help",
|
|
224
|
+
children: C.saved
|
|
215
225
|
}) : null
|
|
216
226
|
]
|
|
217
227
|
});
|
|
218
228
|
}
|
|
219
229
|
export function RemoveVersion({ arm, writes, count, shared, onRemove, onClose }) {
|
|
220
230
|
const slot = arm.key === 'holdout' ? undefined : arm.key;
|
|
221
|
-
const
|
|
231
|
+
const close = ()=>{
|
|
232
|
+
writes.clearError(`remove:${arm.key}`);
|
|
233
|
+
onClose();
|
|
234
|
+
};
|
|
222
235
|
return /*#__PURE__*/ _jsx(Dialog, {
|
|
223
236
|
open: true,
|
|
224
237
|
title: C.removeTitle(arm.label),
|
|
225
238
|
initialFocus: "cancel",
|
|
226
239
|
dismissible: !writes.busy,
|
|
227
|
-
onClose:
|
|
240
|
+
onClose: close,
|
|
228
241
|
children: /*#__PURE__*/ _jsxs("div", {
|
|
229
242
|
className: "xp-slot-drawer",
|
|
230
243
|
children: [
|
|
@@ -236,29 +249,34 @@ export function RemoveVersion({ arm, writes, count, shared, onRemove, onClose })
|
|
|
236
249
|
}) : shared ? /*#__PURE__*/ _jsx("p", {
|
|
237
250
|
children: C.removeShared
|
|
238
251
|
}) : null,
|
|
239
|
-
|
|
252
|
+
writes.errorFor(`remove:${arm.key}`) ? /*#__PURE__*/ _jsx("p", {
|
|
240
253
|
role: "alert",
|
|
241
254
|
className: "xp-ui-error",
|
|
242
|
-
children: writes.
|
|
255
|
+
children: writes.errorFor(`remove:${arm.key}`)
|
|
243
256
|
}) : null,
|
|
244
257
|
/*#__PURE__*/ _jsxs("div", {
|
|
245
258
|
className: "xp-ui-button-group",
|
|
246
259
|
children: [
|
|
247
260
|
/*#__PURE__*/ _jsx(Button, {
|
|
248
261
|
"data-dialog-cancel": true,
|
|
249
|
-
disabled: writes.busy,
|
|
250
|
-
onClick: ()=>
|
|
262
|
+
"aria-disabled": writes.busy || undefined,
|
|
263
|
+
onClick: ()=>{
|
|
264
|
+
if (!writes.busy) close();
|
|
265
|
+
},
|
|
251
266
|
children: C.cancel
|
|
252
267
|
}),
|
|
253
268
|
/*#__PURE__*/ _jsx(Button, {
|
|
254
269
|
variant: "danger",
|
|
255
|
-
busy: writes.busy,
|
|
270
|
+
"aria-busy": writes.busy || undefined,
|
|
271
|
+
"aria-disabled": writes.busy || undefined,
|
|
256
272
|
onClick: async ()=>{
|
|
257
|
-
|
|
258
|
-
const saved = await writes.write((arms)=>
|
|
273
|
+
if (writes.busy) return;
|
|
274
|
+
const saved = await writes.write((arms)=>removeVersionArms(arms, arm.key), {
|
|
275
|
+
errorKey: `remove:${arm.key}`
|
|
276
|
+
});
|
|
259
277
|
if (saved) {
|
|
260
278
|
if (slot) onRemove(slot);
|
|
261
|
-
|
|
279
|
+
close();
|
|
262
280
|
}
|
|
263
281
|
},
|
|
264
282
|
children: C.remove
|
|
@@ -269,54 +287,86 @@ export function RemoveVersion({ arm, writes, count, shared, onRemove, onClose })
|
|
|
269
287
|
})
|
|
270
288
|
});
|
|
271
289
|
}
|
|
272
|
-
export function VersionWeights({ experiment, writes, edits, setEdits, groupId, splitIntent, editable = true, beforeSplit,
|
|
290
|
+
export function VersionWeights({ experiment, writes, edits, setEdits, groupId, splitIntent, editable = true, beforeSplit, renderRow }) {
|
|
273
291
|
const id = useId();
|
|
274
292
|
const split = useRef(null);
|
|
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))));
|
|
275
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));
|
|
276
|
-
const apply = (arms)=>arms
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
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');
|
|
281
307
|
const issues = checkArms(live, 'block');
|
|
282
|
-
async function commit() {
|
|
283
|
-
if (
|
|
284
|
-
|
|
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);
|
|
285
320
|
}
|
|
286
321
|
return /*#__PURE__*/ _jsxs("div", {
|
|
287
322
|
className: "xp-slot-weights",
|
|
288
323
|
children: [
|
|
289
324
|
/*#__PURE__*/ _jsx("div", {
|
|
290
325
|
className: "xp-slot-weight-inputs",
|
|
291
|
-
children: experiment.arms.map((arm)
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
326
|
+
children: experiment.arms.map((arm)=>{
|
|
327
|
+
const input = /*#__PURE__*/ _jsx(NumberInput, {
|
|
328
|
+
id: `${id}-${arm.key}`,
|
|
329
|
+
label: C.traffic,
|
|
330
|
+
"aria-label": C.weight(arm.label),
|
|
331
|
+
min: 0,
|
|
332
|
+
max: 100,
|
|
333
|
+
step: 1,
|
|
334
|
+
value: edits[arm.key] ?? live.find((current)=>current.key === arm.key)?.weight ?? '',
|
|
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
|
+
},
|
|
348
|
+
onBlur: (event)=>{
|
|
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);
|
|
351
|
+
},
|
|
352
|
+
onKeyDown: (event)=>{
|
|
353
|
+
if (event.key === 'Enter') {
|
|
354
|
+
event.preventDefault();
|
|
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);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
return /*#__PURE__*/ _jsx("div", {
|
|
367
|
+
children: renderRow ? renderRow(arm, input) : input
|
|
368
|
+
}, arm.key);
|
|
369
|
+
})
|
|
320
370
|
}),
|
|
321
371
|
Object.keys(edits).length && !invalidKeys.size ? issues.map((issue, i)=>/*#__PURE__*/ _jsx("p", {
|
|
322
372
|
role: "alert",
|
|
@@ -337,8 +387,17 @@ export function VersionWeights({ experiment, writes, edits, setEdits, groupId, s
|
|
|
337
387
|
children: /*#__PURE__*/ _jsx(Button, {
|
|
338
388
|
variant: "quiet",
|
|
339
389
|
size: "sm",
|
|
340
|
-
disabled: writes.busy,
|
|
341
|
-
onClick: ()=>
|
|
390
|
+
"aria-disabled": writes.busy || undefined,
|
|
391
|
+
onClick: async ()=>{
|
|
392
|
+
if (writes.busy) return;
|
|
393
|
+
const saved = await writes.write(splitEvenly, {
|
|
394
|
+
errorKey: 'split'
|
|
395
|
+
});
|
|
396
|
+
if (saved) {
|
|
397
|
+
setEdits(()=>({}));
|
|
398
|
+
setErrors({});
|
|
399
|
+
}
|
|
400
|
+
},
|
|
342
401
|
children: C.split
|
|
343
402
|
})
|
|
344
403
|
}) : null
|
|
@@ -1,9 +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, { draftOnly }?: {
|
|
14
|
+
export declare function writeArms(url: string, transform: (arms: Arm[]) => Arm[], publish: (doc: EditorExperiment) => EditorExperiment | void, { draftOnly }?: {
|
|
8
15
|
draftOnly?: boolean;
|
|
9
16
|
}): Promise<Arm[]>;
|