@delmaredigital/payload-experiments 0.4.0-rc.7 → 0.4.0-rc.9

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 (221) hide show
  1. package/README.md +12 -12
  2. package/dist/admin/blocks/model.js +2 -2
  3. package/dist/admin/copy/blocks.js +1 -1
  4. package/dist/admin/surface/ResultsRoute.js +17 -11
  5. package/dist/admin/surface/TestsHome.js +53 -20
  6. package/dist/admin/surface/newTest/PageWizard.js +8 -2
  7. package/dist/admin/surface/results/ResultsHeader.js +11 -6
  8. package/dist/admin/surface/test/TestPage.js +3 -3
  9. package/dist/blocks/formState.js +1 -1
  10. package/dist/collections/experiments/config.js +8 -3
  11. package/dist/collections/experiments/deleteGuard.js +2 -2
  12. package/dist/collections/experiments/lockHook.js +21 -2
  13. package/dist/collections/experiments/pickSlug.js +2 -0
  14. package/dist/collections/metricsGuards.js +2 -0
  15. package/dist/collections/serialize.js +5 -4
  16. package/dist/constants.d.ts +1 -0
  17. package/dist/constants.js +1 -0
  18. package/dist/db/adapter.d.ts +9 -1
  19. package/dist/db/adapter.js +77 -1
  20. package/dist/db/delete.d.ts +1 -1
  21. package/dist/db/delete.js +2 -4
  22. package/dist/db/events.d.ts +9 -0
  23. package/dist/db/events.js +42 -8
  24. package/dist/db/executor.d.ts +7 -1
  25. package/dist/db/executor.js +7 -3
  26. package/dist/db/results.d.ts +5 -1
  27. package/dist/db/results.js +21 -5
  28. package/dist/db/snapshots.d.ts +1 -0
  29. package/dist/endpoints/deleteImpact.js +2 -1
  30. package/dist/endpoints/events.js +103 -39
  31. package/dist/endpoints/index.js +2 -8
  32. package/dist/endpoints/placementStatus.js +7 -4
  33. package/dist/endpoints/results.d.ts +1 -1
  34. package/dist/endpoints/results.js +6 -2
  35. package/dist/endpoints/routeId.d.ts +2 -2
  36. package/dist/endpoints/routeId.js +11 -5
  37. package/dist/endpoints/summary.js +46 -12
  38. package/dist/endpoints/transitions.js +37 -24
  39. package/dist/errors.d.ts +4 -0
  40. package/dist/errors.js +6 -0
  41. package/dist/events/request.d.ts +6 -2
  42. package/dist/events/request.js +35 -8
  43. package/dist/events/rows.d.ts +1 -1
  44. package/dist/events/rows.js +24 -8
  45. package/dist/events/writable.d.ts +9 -0
  46. package/dist/events/writable.js +13 -0
  47. package/dist/exports/index.d.ts +1 -1
  48. package/dist/exports/rsc.d.ts +1 -0
  49. package/dist/exports/rsc.js +1 -0
  50. package/dist/goals/fairness.js +2 -2
  51. package/dist/hooks/hostHook.js +6 -2
  52. package/dist/hooks/pageGuard.js +4 -0
  53. package/dist/lib/fetchAllDocs.d.ts +1 -1
  54. package/dist/manifest/compile.js +8 -5
  55. package/dist/onInit.js +14 -0
  56. package/dist/options.js +5 -0
  57. package/dist/proxy/pageNavigation.d.ts +4 -0
  58. package/dist/proxy/pageNavigation.js +28 -0
  59. package/dist/proxy/resolve.js +52 -25
  60. package/dist/puck/editor/ConfigureTestDrawer.js +5 -4
  61. package/dist/puck/editor/ExperimentSlotEditor.js +182 -58
  62. package/dist/puck/editor/QuickTestActions.d.ts +5 -2
  63. package/dist/puck/editor/QuickTestActions.js +16 -4
  64. package/dist/puck/editor/ResultsReadout.d.ts +2 -1
  65. package/dist/puck/editor/ResultsReadout.js +2 -1
  66. package/dist/puck/editor/VersionBuilder.d.ts +13 -2
  67. package/dist/puck/editor/VersionBuilder.js +133 -49
  68. package/dist/puck/editor/armWrites.js +1 -1
  69. package/dist/puck/editor/armsCache.js +1 -3
  70. package/dist/puck/editor/copy.d.ts +4 -1
  71. package/dist/puck/editor/copy.js +4 -1
  72. package/dist/puck/editor/editor.chrome.css +1 -0
  73. package/dist/puck/editor/editor.css +1 -0
  74. package/dist/puck/editor/editor.model.d.ts +1 -0
  75. package/dist/puck/editor/placementsCache.d.ts +5 -1
  76. package/dist/puck/editor/placementsCache.js +17 -9
  77. package/dist/puck/editor/usePageSaveReadiness.d.ts +9 -0
  78. package/dist/puck/editor/usePageSaveReadiness.js +34 -0
  79. package/dist/puck/editor/usePlacementNotice.d.ts +2 -2
  80. package/dist/puck/editor/usePlacementNotice.js +5 -4
  81. package/dist/results/aggregate.d.ts +6 -3
  82. package/dist/results/aggregate.js +9 -9
  83. package/dist/results/health.d.ts +8 -0
  84. package/dist/results/health.js +28 -0
  85. package/dist/results/identityCredit.d.ts +1 -0
  86. package/dist/results/identityCredit.js +4 -1
  87. package/dist/results/verdict.js +2 -1
  88. package/dist/runtime/cookie.d.ts +10 -2
  89. package/dist/runtime/cookie.js +38 -6
  90. package/dist/runtime/cookieOptions.d.ts +6 -0
  91. package/dist/runtime/cookieOptions.js +14 -0
  92. package/dist/runtime/proxyRuntime.d.ts +1 -0
  93. package/dist/runtime/proxyRuntime.js +18 -8
  94. package/dist/runtime/signing.d.ts +1 -0
  95. package/dist/runtime/signing.js +5 -0
  96. package/dist/sanitizeReadinessDetail.d.ts +2 -2
  97. package/dist/sanitizeReadinessDetail.js +20 -2
  98. package/dist/server/getAssignments.js +11 -10
  99. package/dist/server/recordContentChange.d.ts +1 -1
  100. package/dist/server/recordContentChange.js +11 -2
  101. package/dist/server/resolveExperimentBlocks.js +2 -4
  102. package/dist/server/resolveExperimentData.js +2 -5
  103. package/dist/server/runResults.js +52 -14
  104. package/dist/server/startExperiment.d.ts +20 -0
  105. package/dist/server/startExperiment.js +70 -0
  106. package/dist/server/trackConversion.js +10 -13
  107. package/dist/testSurface/actions/DecideDialog.d.ts +4 -1
  108. package/dist/testSurface/actions/DecideDialog.js +32 -14
  109. package/dist/testSurface/actions/MakeContentResult.d.ts +4 -3
  110. package/dist/testSurface/actions/MakeContentResult.js +9 -1
  111. package/dist/testSurface/api.d.ts +5 -5
  112. package/dist/testSurface/api.js +21 -9
  113. package/dist/testSurface/copy/actions.d.ts +1 -0
  114. package/dist/testSurface/copy/actions.js +1 -0
  115. package/dist/testSurface/copy/common.d.ts +3 -1
  116. package/dist/testSurface/copy/common.js +4 -2
  117. package/dist/testSurface/copy/languages.d.ts +1 -0
  118. package/dist/testSurface/copy/languages.js +1 -0
  119. package/dist/testSurface/copy/metrics.d.ts +1 -0
  120. package/dist/testSurface/copy/metrics.js +1 -0
  121. package/dist/testSurface/copy/readiness.d.ts +12 -1
  122. package/dist/testSurface/copy/readiness.js +18 -1
  123. package/dist/testSurface/copy/results.d.ts +5 -2
  124. package/dist/testSurface/copy/results.js +5 -2
  125. package/dist/testSurface/copy/testPage.d.ts +20 -2
  126. package/dist/testSurface/copy/testPage.js +34 -3
  127. package/dist/testSurface/readPreflight.d.ts +5 -0
  128. package/dist/testSurface/readPreflight.js +7 -0
  129. package/dist/testSurface/readinessRead.d.ts +3 -0
  130. package/dist/testSurface/readinessRead.js +4 -0
  131. package/dist/testSurface/results/VerdictHero.d.ts +2 -1
  132. package/dist/testSurface/results/VerdictHero.js +5 -1
  133. package/dist/testSurface/results/results.model.d.ts +1 -1
  134. package/dist/testSurface/results/results.model.js +6 -2
  135. package/dist/testSurface/surface/contracts.d.ts +8 -1
  136. package/dist/testSurface/surface/editorPlacement.d.ts +2 -2
  137. package/dist/testSurface/surface/editorPlacement.js +1 -1
  138. package/dist/testSurface/surface/pageBlock/pageBlock.model.js +2 -2
  139. package/dist/testSurface/surface/pagePicker/PageCombobox.js +20 -6
  140. package/dist/testSurface/surface/readiness/ActionDialogs.d.ts +4 -2
  141. package/dist/testSurface/surface/readiness/ActionDialogs.js +39 -34
  142. package/dist/testSurface/surface/readiness/Banner.js +23 -12
  143. package/dist/testSurface/surface/readiness/SaveStatus.d.ts +4 -3
  144. package/dist/testSurface/surface/readiness/SaveStatus.js +15 -15
  145. package/dist/testSurface/surface/readiness/readiness.model.d.ts +6 -1
  146. package/dist/testSurface/surface/readiness/readiness.model.js +108 -11
  147. package/dist/testSurface/surface/readiness/useTransition.d.ts +4 -4
  148. package/dist/testSurface/surface/readiness/useTransition.js +12 -10
  149. package/dist/testSurface/surface/results/ReleasePages.d.ts +4 -2
  150. package/dist/testSurface/surface/results/ReleasePages.js +5 -4
  151. package/dist/testSurface/surface/results/ResultsBody.d.ts +4 -2
  152. package/dist/testSurface/surface/results/ResultsBody.js +86 -43
  153. package/dist/testSurface/surface/results/useResults.d.ts +7 -6
  154. package/dist/testSurface/surface/results/useResults.js +3 -2
  155. package/dist/testSurface/surface/setting/parts.js +58 -18
  156. package/dist/testSurface/surface/test/AdvancedSection.js +1 -0
  157. package/dist/testSurface/surface/test/DeleteTest.d.ts +6 -2
  158. package/dist/testSurface/surface/test/DeleteTest.js +145 -54
  159. package/dist/testSurface/surface/test/GoalSection.js +7 -3
  160. package/dist/testSurface/surface/test/LengthSection.js +3 -0
  161. package/dist/testSurface/surface/test/PageHoldout.js +4 -1
  162. package/dist/testSurface/surface/test/ReadinessBar.js +1 -1
  163. package/dist/testSurface/surface/test/TargetingSection.js +77 -12
  164. package/dist/testSurface/surface/test/TestSurface.js +81 -19
  165. package/dist/testSurface/surface/test/VersionsSection.js +56 -29
  166. package/dist/testSurface/surface/test/fields.d.ts +29 -4
  167. package/dist/testSurface/surface/test/fields.js +159 -39
  168. package/dist/testSurface/surface/test/pageParts.js +14 -5
  169. package/dist/testSurface/surface/test/testPage.model.d.ts +1 -0
  170. package/dist/testSurface/surface/test/testPage.model.js +6 -0
  171. package/dist/testSurface/surface/testDocument.model.d.ts +3 -0
  172. package/dist/testSurface/surface/testDocument.model.js +4 -1
  173. package/dist/testSurface/surface/useTestDocument.d.ts +22 -4
  174. package/dist/testSurface/surface/useTestDocument.js +407 -160
  175. package/dist/testSurface/useApi.d.ts +5 -5
  176. package/dist/testSurface/useOnTestReturn.d.ts +2 -0
  177. package/dist/testSurface/useOnTestReturn.js +27 -0
  178. package/dist/testSurface/useReadinessPolling.js +3 -1
  179. package/dist/transitions/addMetric.d.ts +1 -4
  180. package/dist/transitions/allocate.d.ts +2 -2
  181. package/dist/transitions/allocate.js +3 -1
  182. package/dist/transitions/archive.d.ts +1 -1
  183. package/dist/transitions/archive.js +3 -2
  184. package/dist/transitions/checkStart.d.ts +1 -0
  185. package/dist/transitions/checkStart.js +17 -6
  186. package/dist/transitions/decide.d.ts +1 -1
  187. package/dist/transitions/decide.js +5 -3
  188. package/dist/transitions/deleteExperiment.d.ts +2 -2
  189. package/dist/transitions/deleteExperiment.js +17 -7
  190. package/dist/transitions/kernel.d.ts +2 -0
  191. package/dist/transitions/kernel.js +9 -4
  192. package/dist/transitions/makeContent.d.ts +2 -1
  193. package/dist/transitions/makeContent.js +12 -8
  194. package/dist/transitions/nestedPlacementNames.d.ts +2 -2
  195. package/dist/transitions/nestedPlacementNames.js +4 -5
  196. package/dist/transitions/phaseRecord.d.ts +2 -0
  197. package/dist/transitions/phaseRecord.js +5 -0
  198. package/dist/transitions/placementInteractions.d.ts +3 -3
  199. package/dist/transitions/placementInteractions.js +8 -7
  200. package/dist/transitions/placements.d.ts +2 -0
  201. package/dist/transitions/placements.js +5 -5
  202. package/dist/transitions/preflightData.d.ts +3 -2
  203. package/dist/transitions/preflightData.js +67 -41
  204. package/dist/transitions/releaseHoldout.d.ts +2 -1
  205. package/dist/transitions/releaseHoldout.js +3 -1
  206. package/dist/transitions/releasePages.d.ts +2 -1
  207. package/dist/transitions/releasePages.js +3 -1
  208. package/dist/transitions/resolveCaps.d.ts +2 -4
  209. package/dist/transitions/start.d.ts +6 -8
  210. package/dist/transitions/start.js +67 -34
  211. package/dist/transitions/startReads.d.ts +7 -0
  212. package/dist/transitions/startReads.js +28 -0
  213. package/dist/transitions/stop.d.ts +2 -1
  214. package/dist/transitions/stop.js +3 -2
  215. package/dist/types/contracts.d.ts +6 -3
  216. package/dist/types/endpoints.d.ts +52 -9
  217. package/dist/types/runtime.d.ts +15 -9
  218. package/dist/types/snapshot.d.ts +2 -0
  219. package/dist/ui/ChipsInput.d.ts +8 -1
  220. package/dist/ui/ChipsInput.js +17 -4
  221. package/package.json +2 -2
@@ -1,11 +1,11 @@
1
1
  'use client';
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { DeleteTest } from './DeleteTest.js';
4
4
  import { surfaceUrls } from '../../surfaceUrls.js';
5
5
  import { useEffect, useState, useRef } from 'react';
6
6
  import { useTestDocument } from '../useTestDocument.js';
7
7
  import { API_MESSAGES } from '../../copy/common.js';
8
- import { TEST_PAGE as C } from '../../copy/testPage.js';
8
+ import { DELETE_TEST, TEST_PAGE as C } from '../../copy/testPage.js';
9
9
  import { pageBlockParts } from '../pageBlock/parts.js';
10
10
  import { settingParts } from '../setting/parts.js';
11
11
  import { pageParts } from './pageParts.js';
@@ -32,6 +32,7 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
32
32
  apiRoute: surface.apiRoute,
33
33
  contentLocale: surface.contentLocale,
34
34
  refetchPreflightOnFocus: true,
35
+ refetchDocumentOnFocus: true,
35
36
  ...!surface.canManage ? {
36
37
  enabled: false
37
38
  } : {},
@@ -73,6 +74,14 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
73
74
  surface.contentLocale
74
75
  ]);
75
76
  const currentRead = documentState.loadState === 'ready' && placementRead?.key === placementKey ? placementRead : undefined;
77
+ const answer = currentRead?.pending ? undefined : currentRead?.read;
78
+ const { syncDocument } = documentState;
79
+ useEffect(()=>{
80
+ syncDocument?.(answer);
81
+ }, [
82
+ answer,
83
+ syncDocument
84
+ ]);
76
85
  const preflight = currentRead ? currentRead.preflight ?? (currentRead.pending && !currentRead.error ? documentState.preflight : null) : documentState.preflight;
77
86
  const sharedDocument = currentRead ? {
78
87
  ...documentState,
@@ -95,7 +104,7 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
95
104
  editorPage,
96
105
  editorPlacementMessage: message
97
106
  };
98
- usePlacementNotice(surface.apiRoute, id, watching ? sharedDocument.preflightError ?? (placementFailed ? placementReadError(surface.apiRoute, id, surface.contentLocale) : message) : undefined, surface.contentLocale);
107
+ usePlacementNotice(surface.apiRoute, id, watching ? sharedDocument.preflightError ?? (placementFailed ? placementReadError(surface.apiRoute, id, surface.contentLocale) : message) : undefined, surface.contentLocale, watching);
99
108
  const setup = documentState.doc?.status === 'draft' || documentState.doc?.status === 'stopped';
100
109
  const test = setup ? {
101
110
  ...sharedDocument,
@@ -117,6 +126,23 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
117
126
  children
118
127
  }) : children;
119
128
  };
129
+ const detachedEdits = (test.detachedDrafts ?? []).map((key)=>{
130
+ const draft = test.fieldDrafts?.[key];
131
+ return draft && !draft.saving ? /*#__PURE__*/ _jsxs(Banner, {
132
+ tone: "warning",
133
+ action: /*#__PURE__*/ _jsx(Button, {
134
+ onClick: ()=>test.trackDraft(key, null),
135
+ children: C.discardEdit
136
+ }),
137
+ children: [
138
+ C.detachedEdit(draft.label),
139
+ " ",
140
+ /*#__PURE__*/ _jsx("strong", {
141
+ children: draft.value || C.emptyValue
142
+ })
143
+ ]
144
+ }, key) : null;
145
+ });
120
146
  // Results readers have no permission to load the manager-only test/preflight.
121
147
  if (!surface.canManage) return frame(/*#__PURE__*/ _jsx(ResultsBody, {
122
148
  id: id,
@@ -126,6 +152,20 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
126
152
  className: "xp-test-page",
127
153
  role: test.loadState === 'error' ? 'alert' : 'status',
128
154
  children: [
155
+ Object.entries(test.fieldDrafts ?? {}).map(([key, draft])=>/*#__PURE__*/ _jsxs(Banner, {
156
+ tone: "warning",
157
+ action: /*#__PURE__*/ _jsx(Button, {
158
+ onClick: ()=>test.trackDraft(key, null),
159
+ children: C.discardEdit
160
+ }),
161
+ children: [
162
+ C.detachedEdit(draft.label),
163
+ " ",
164
+ /*#__PURE__*/ _jsx("strong", {
165
+ children: draft.value || C.emptyValue
166
+ })
167
+ ]
168
+ }, key)),
129
169
  /*#__PURE__*/ _jsx("p", {
130
170
  children: test.preflightRead?.kind === 'signed-out' ? API_MESSAGES.unauthenticated : test.loadState === 'forbidden' ? C.forbidden : test.loadState === 'missing' ? C.missing : test.loadState === 'error' ? C.error : C.loading
131
171
  }),
@@ -155,14 +195,25 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
155
195
  /*#__PURE__*/ _jsxs("div", {
156
196
  className: "xp-test-content",
157
197
  children: [
158
- test.errors._section ? /*#__PURE__*/ _jsx(Banner, {
198
+ test.errors._section ? /*#__PURE__*/ _jsxs(Banner, {
159
199
  tone: "error",
160
200
  action: /*#__PURE__*/ _jsx(Button, {
161
- onClick: ()=>void test.reload(),
201
+ onClick: ()=>void test.reload({
202
+ discardDrafts: true
203
+ }),
162
204
  children: C.reload
163
205
  }),
164
- children: test.errors._section
206
+ children: [
207
+ test.errors._section,
208
+ test.stale ? null : /*#__PURE__*/ _jsxs(_Fragment, {
209
+ children: [
210
+ " ",
211
+ C.reloadAfterError
212
+ ]
213
+ })
214
+ ]
165
215
  }) : null,
216
+ detachedEdits,
166
217
  /*#__PURE__*/ _jsxs(Section, {
167
218
  surface: surface,
168
219
  test: test,
@@ -197,20 +248,31 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
197
248
  /*#__PURE__*/ _jsx(AdvancedSection, {
198
249
  ...props
199
250
  }),
200
- !embedded ? /*#__PURE__*/ _jsx("div", {
201
- children: /*#__PURE__*/ _jsx(DeleteTest, {
202
- test: test.doc,
203
- canManage: surface.canManage,
204
- apiRoute: surface.apiRoute,
205
- contentLocale: surface.contentLocale,
206
- onDeleted: (outcome)=>{
207
- window.location.assign(surfaceUrls.home(surface.adminRoute, {
208
- deleted: outcome
209
- }));
210
- }
211
- })
251
+ !embedded ? /*#__PURE__*/ _jsxs("div", {
252
+ children: [
253
+ /*#__PURE__*/ _jsx(DeleteTest, {
254
+ test: test.doc,
255
+ canManage: surface.canManage && !test.statusReadPending,
256
+ apiRoute: surface.apiRoute,
257
+ contentLocale: surface.contentLocale,
258
+ onStatusChanged: ()=>{
259
+ void test.reload();
260
+ },
261
+ onDeleted: (outcome)=>{
262
+ window.location.assign(surfaceUrls.home(surface.adminRoute, {
263
+ deleted: outcome
264
+ }));
265
+ }
266
+ }, test.doc.id),
267
+ !test.statusReadPending && (test.doc.status === 'stopped' || test.doc.status === 'decided') ? /*#__PURE__*/ _jsx("p", {
268
+ children: DELETE_TEST.archiveFirst
269
+ }) : null
270
+ ]
212
271
  }) : null,
213
272
  embedded && surface.canReadResults && test.doc.status !== 'draft' ? /*#__PURE__*/ _jsx(ResultsBody, {
273
+ documentVersion: `${test.doc.phase}/${test.doc.status}`,
274
+ liveDocument: test.doc,
275
+ statusReadPending: test.statusReadPending,
214
276
  id: id,
215
277
  surface: surface,
216
278
  onChanged: (doc)=>{
@@ -219,7 +281,7 @@ export function TestSurface({ id, surface, onTitleChange, renderFrame, embedded
219
281
  });
220
282
  void test.reload();
221
283
  }
222
- }, `${test.doc.id}/${test.doc.phase}/${test.doc.status}`) : null
284
+ }) : null
223
285
  ]
224
286
  })
225
287
  ]
@@ -1,15 +1,14 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useId } from 'react';
3
+ import { useId, useState } from 'react';
4
4
  import { Button } from '../../../ui/Button.js';
5
5
  import { Range } from '../../../ui/Inputs.js';
6
- import { useEffect, useRef, useState } from 'react';
7
6
  import { TEST_PAGE as C } from '../../copy/testPage.js';
8
7
  import { ARMS_COPY, GROUP_LABELS, issueMessage } from '../../copy/arms.js';
9
8
  import { addHoldoutArm, addVariant, issuesByRow, removeArm, splitEvenly, totalWeight, withSlotsFor } from '../../arms/arms.model.js';
10
9
  import { allocationSentence } from '../../allocation.model.js';
11
- import { balanceWeight, orderedVersions, trafficShares } from './testPage.model.js';
12
- import { FieldErrors, ReadOnly, reasonFor, save, TextEdit } from './fields.js';
10
+ import { balanceWeight, orderedVersions, trafficShares, trafficSplit } from './testPage.model.js';
11
+ import { FieldErrors, ReadOnly, reasonFor, save, SavedValueChanged, TextEdit, useDraftAnnouncement, useFieldDraft } from './fields.js';
13
12
  import { PAGE_COPY } from '../../copy/pages.js';
14
13
  import { testPageCollection } from '../pagePicker/pagePicker.model.js';
15
14
  import { Section } from './Section.js';
@@ -24,28 +23,31 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
24
23
  const id = useId();
25
24
  const doc = test.doc;
26
25
  const [weightEdit, setWeightEdit] = useState(null);
27
- const releasedAllocation = useRef(null);
28
- const [allocation, setAllocation] = useState(doc.allocation);
29
- useEffect(()=>setWeightEdit(null), [
30
- doc.arms
31
- ]);
32
- useEffect(()=>setAllocation(doc.allocation), [
33
- doc.allocation
34
- ]);
26
+ const split = trafficSplit(doc.arms);
27
+ const allocationEdit = useFieldDraft(test, 'allocation', C.allocation, String(doc.allocation));
28
+ const [announcement, announce] = useDraftAnnouncement();
29
+ const allocation = Number(allocationEdit.value);
30
+ const allocationReason = reasonFor(test, 'allocation');
31
+ const allocationConflict = allocationEdit.dirty && !allocationEdit.saving && (allocationEdit.conflict || allocationEdit.retry || allocationReason) ? `${id}-new-value` : undefined;
32
+ if (weightEdit && (weightEdit.split !== split || test.mountDraft && !test.fieldDrafts?.[`share:${weightEdit.key}`])) setWeightEdit(null);
35
33
  const reason = reasonFor(test, 'arms');
36
- const live = weightEdit ? balanceWeight(doc.arms, weightEdit.key, weightEdit.weight) : doc.arms;
34
+ const live = weightEdit && !reason ? balanceWeight(doc.arms, weightEdit.key, weightEdit.weight) : doc.arms;
37
35
  const ordered = orderedVersions(live);
38
36
  const shares = trafficShares(ordered.map((row)=>row.arm));
39
- const issues = issuesByRow(live, doc.kind);
37
+ const issues = issuesByRow(doc.kind === 'block' ? withSlotsFor(live, 'block') : live, doc.kind);
40
38
  const update = (transform)=>save(test, 'arms', ()=>({
41
39
  arms: withSlotsFor(transform(test.doc.arms), test.doc.kind)
42
40
  }));
43
- const commitAllocation = ()=>{
44
- if (allocation === test.doc.allocation || releasedAllocation.current === allocation) return;
45
- releasedAllocation.current = allocation;
46
- save(test, 'allocation', ()=>({
47
- allocation
48
- }));
41
+ const commitAllocation = (confirmed = false)=>{
42
+ if (!allocationEdit.dirty || allocationEdit.saving) return;
43
+ if (allocationReason || test.stale || allocationConflict && !confirmed) {
44
+ announce();
45
+ return;
46
+ }
47
+ if (allocation === doc.allocation) allocationEdit.discard();
48
+ else allocationEdit.submit(()=>save(test, 'allocation', ()=>({
49
+ allocation
50
+ })));
49
51
  };
50
52
  return /*#__PURE__*/ _jsxs(Section, {
51
53
  surface: surface,
@@ -87,6 +89,7 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
87
89
  contentOnCanvas ? /*#__PURE__*/ _jsx("span", {
88
90
  children: arm.label
89
91
  }) : /*#__PURE__*/ _jsx(TextEdit, {
92
+ test: test,
90
93
  label: `${GROUP_LABELS[arm.key]}: ${C.label}`,
91
94
  value: arm.label,
92
95
  reason: reason,
@@ -98,17 +101,22 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
98
101
  } : current))
99
102
  }),
100
103
  /*#__PURE__*/ _jsx(TextEdit, {
104
+ test: test,
105
+ draftKey: `share:${arm.key}`,
101
106
  label: `${GROUP_LABELS[arm.key]}: ${C.weight}`,
102
107
  type: "number",
103
108
  min: 0,
104
109
  max: 100,
105
110
  value: doc.arms[index].weight,
111
+ preview: weightEdit?.key !== arm.key ? arm.weight : undefined,
112
+ scope: split,
106
113
  reason: reason,
107
114
  hideLabel: true,
108
115
  disabled: Boolean(reason),
109
116
  isValid: (raw)=>raw.trim() !== '' && Number.isInteger(Number(raw)) && Number(raw) >= 0 && Number(raw) <= 100,
110
117
  invalidMessage: C.invalidShare,
111
- onDraft: (value)=>setWeightEdit({
118
+ onDraft: (value)=>setWeightEdit(value === null ? null : {
119
+ split,
112
120
  key: arm.key,
113
121
  weight: Number(value)
114
122
  }),
@@ -207,10 +215,10 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
207
215
  className: "xp-ui-help",
208
216
  children: ARMS_COPY.holdout
209
217
  }) : null,
210
- reasonFor(test, 'allocation') ? /*#__PURE__*/ _jsx(ReadOnly, {
218
+ allocationReason && !allocationEdit.dirty ? /*#__PURE__*/ _jsx(ReadOnly, {
211
219
  label: C.allocation,
212
220
  value: `${doc.allocation}%`,
213
- reason: reasonFor(test, 'allocation')
221
+ reason: allocationReason
214
222
  }) : /*#__PURE__*/ _jsx(Range, {
215
223
  id: id,
216
224
  label: C.allocation,
@@ -218,11 +226,21 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
218
226
  min: 1,
219
227
  max: 100,
220
228
  value: allocation,
221
- onChange: (e)=>{
222
- releasedAllocation.current = null;
223
- setAllocation(Number(e.target.value));
229
+ "aria-describedby": allocationConflict,
230
+ disabled: Boolean(allocationReason || test.stale || allocationEdit.saving),
231
+ onChange: (e)=>allocationEdit.change(e.target.value),
232
+ onPointerUp: ()=>commitAllocation(),
233
+ onKeyDown: (e)=>{
234
+ if (e.key === 'Enter' && allocationConflict) {
235
+ e.preventDefault();
236
+ announce();
237
+ }
238
+ if (e.key === 'Escape' && allocationEdit.dirty && !allocationEdit.saving) {
239
+ e.preventDefault();
240
+ e.stopPropagation();
241
+ allocationEdit.discard();
242
+ }
224
243
  },
225
- onPointerUp: commitAllocation,
226
244
  onKeyUp: (e)=>{
227
245
  if ([
228
246
  'ArrowLeft',
@@ -235,11 +253,20 @@ export function VersionsSection({ test, surface, parts, contentOnCanvas = false
235
253
  'PageDown'
236
254
  ].includes(e.key)) commitAllocation();
237
255
  },
238
- onBlur: commitAllocation
256
+ onBlur: ()=>commitAllocation()
239
257
  }),
258
+ allocationConflict ? /*#__PURE__*/ _jsx(SavedValueChanged, {
259
+ id: allocationConflict,
260
+ value: `${doc.allocation}%`,
261
+ reason: allocationReason,
262
+ conflict: allocationEdit.conflict,
263
+ announce: announcement,
264
+ onKeep: !allocationReason && !test.stale ? ()=>commitAllocation(true) : undefined,
265
+ onDiscard: allocationEdit.discard
266
+ }) : null,
240
267
  /*#__PURE__*/ _jsx("p", {
241
268
  children: allocationSentence({
242
- allocation: reasonFor(test, 'allocation') ? doc.allocation : allocation,
269
+ allocation: allocationReason ? doc.allocation : allocation,
243
270
  unitType: doc.unitType,
244
271
  arms: live,
245
272
  status: doc.status
@@ -3,7 +3,7 @@ import type { SectionProps, TestDocApi } from '../contracts.js';
3
3
  import type { EditableField } from '../../../types/admin.js';
4
4
  import type { TestDocumentPatch } from '../testDocument.model.js';
5
5
  export declare function reasonFor(test: TestDocApi, field: string): string | null;
6
- export declare function save(test: TestDocApi, field: EditableField, patch: () => TestDocumentPatch): void;
6
+ export declare function save(test: TestDocApi, field: EditableField, patch: () => TestDocumentPatch): boolean;
7
7
  export declare function FieldErrors({ test, path }: {
8
8
  test: TestDocApi;
9
9
  path: string;
@@ -14,13 +14,38 @@ export declare function ReadOnly({ label, value, reason, labelAction }: {
14
14
  reason: string | null;
15
15
  labelAction?: ReactNode;
16
16
  }): import("react").JSX.Element;
17
+ export declare function useFieldDraft(test: TestDocApi, key: string, label: string, saved: string, scope?: string): {
18
+ value: string;
19
+ dirty: boolean;
20
+ saving: boolean;
21
+ retry: boolean;
22
+ conflict: boolean;
23
+ change(value: string): void;
24
+ submit(save: () => boolean | void): void;
25
+ discard(): void;
26
+ };
27
+ export declare function SavedValueChanged({ id, value, reason, conflict, announce, onKeep, onDiscard }: {
28
+ id: string;
29
+ value: string;
30
+ reason?: string | null;
31
+ conflict: boolean;
32
+ announce: number;
33
+ onKeep?: () => void;
34
+ onDiscard: () => void;
35
+ }): import("react").JSX.Element;
36
+ /** Announce the existing choices again when blur or Enter cannot commit. */
37
+ export declare function useDraftAnnouncement(): readonly [number, () => void];
17
38
  /** Local typing is intentionally separate from staging: build the current group on commit. */
18
- export declare function TextEdit({ label, value, onCommit, reason, multiline, placeholder, type, min, max, required, onDraft, isValid, invalidMessage, hideLabel, disabled, help }: {
39
+ export declare function TextEdit({ test, label, draftKey, value, preview, onCommit, reason, multiline, placeholder, type, min, max, required, onDraft, scope, isValid, invalidMessage, hideLabel, disabled, help }: {
40
+ test: TestDocApi;
19
41
  label: string;
42
+ draftKey?: string;
20
43
  value: string | number;
21
- onCommit: (value: string) => void;
44
+ onCommit: (value: string) => boolean | void;
22
45
  reason?: string | null;
23
- onDraft?: (value: string) => void;
46
+ onDraft?: (value: string | null) => void;
47
+ scope?: string;
48
+ preview?: number;
24
49
  multiline?: boolean;
25
50
  placeholder?: string;
26
51
  type?: 'text' | 'number';
@@ -1,7 +1,9 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { useEffect, useId, useState } from 'react';
3
+ import { useId, useLayoutEffect, useState } from 'react';
4
4
  import { TextInput, NumberInput, Textarea, Select, CheckboxList as UICheckboxList } from '../../../ui/Inputs.js';
5
+ import { Button } from '../../../ui/Button.js';
6
+ import { Banner } from '../../../ui/Feedback.js';
5
7
  import { TEST_PAGE as C } from '../../copy/testPage.js';
6
8
  import { EDITABLE_BY_STATUS } from '../../../collections/experiments/editability.js';
7
9
  import { lockedReason } from '../../lockedNotice.model.js';
@@ -9,9 +11,9 @@ export function reasonFor(test, field) {
9
11
  return test.doc ? lockedReason(test.doc.status, field, EDITABLE_BY_STATUS) : null;
10
12
  }
11
13
  export function save(test, field, patch) {
12
- if (!test.doc || reasonFor(test, field)) return;
13
- test.stage(patch());
14
+ if (!test.doc || reasonFor(test, field) || !test.stage(patch())) return false;
14
15
  void test.commit();
16
+ return true;
15
17
  }
16
18
  export function FieldErrors({ test, path }) {
17
19
  return /*#__PURE__*/ _jsx(_Fragment, {
@@ -45,55 +47,171 @@ export function ReadOnly({ label, value, reason, labelAction }) {
45
47
  ]
46
48
  });
47
49
  }
48
- /** Local typing is intentionally separate from staging: build the current group on commit. */ export function TextEdit({ label, value, onCommit, reason, multiline, placeholder, type = 'text', min, max, required, onDraft, isValid, invalidMessage, hideLabel = false, disabled = false, help }) {
50
+ export function useFieldDraft(test, key, label, saved, scope) {
51
+ const { mountDraft, submitDraft, trackDraft } = test;
52
+ const [localEdit, setLocalEdit] = useState(null);
53
+ const edit = mountDraft ? test.fieldDrafts?.[key] : localEdit;
54
+ const conflict = Boolean(edit && !edit.saving && (edit.scope !== scope || edit.base !== saved && edit.value !== saved));
55
+ useLayoutEffect(()=>mountDraft?.(key), [
56
+ key,
57
+ mountDraft
58
+ ]);
59
+ const update = (draft)=>mountDraft ? trackDraft(key, draft) : setLocalEdit(draft);
60
+ return {
61
+ value: edit?.value ?? saved,
62
+ dirty: Boolean(edit),
63
+ saving: Boolean(edit?.saving),
64
+ retry: Boolean(edit?.retry),
65
+ conflict,
66
+ change (value) {
67
+ update(value === saved && !conflict ? null : {
68
+ label,
69
+ base: edit?.base ?? saved,
70
+ scope: edit ? edit.scope : scope,
71
+ value
72
+ });
73
+ },
74
+ submit (save) {
75
+ if (submitDraft) submitDraft(key, save);
76
+ else if (save() !== false) update(null);
77
+ },
78
+ discard () {
79
+ update(null);
80
+ }
81
+ };
82
+ }
83
+ export function SavedValueChanged({ id, value, reason, conflict, announce, onKeep, onDiscard }) {
84
+ return /*#__PURE__*/ _jsx("div", {
85
+ id: id,
86
+ className: "xp-field-draft-notice",
87
+ children: /*#__PURE__*/ _jsx(Banner, {
88
+ tone: "error",
89
+ action: /*#__PURE__*/ _jsxs("div", {
90
+ className: "xp-ui-button-group",
91
+ children: [
92
+ onKeep ? /*#__PURE__*/ _jsx(Button, {
93
+ size: "sm",
94
+ onClick: onKeep,
95
+ children: conflict ? C.keepEdit : C.retryEdit
96
+ }) : null,
97
+ /*#__PURE__*/ _jsx(Button, {
98
+ size: "sm",
99
+ variant: "quiet",
100
+ onClick: onDiscard,
101
+ children: C.useSavedValue
102
+ })
103
+ ]
104
+ }),
105
+ children: /*#__PURE__*/ _jsxs("span", {
106
+ children: [
107
+ reason ?? (conflict ? C.savedValueChanged : null),
108
+ ' ',
109
+ onKeep && conflict ? C.conflictChoice : C.draftNotSaved,
110
+ ' ',
111
+ C.savedValue,
112
+ " ",
113
+ /*#__PURE__*/ _jsx("strong", {
114
+ children: value || C.emptyValue
115
+ })
116
+ ]
117
+ }, announce)
118
+ })
119
+ });
120
+ }
121
+ /** Announce the existing choices again when blur or Enter cannot commit. */ export function useDraftAnnouncement() {
122
+ const [announcement, setAnnouncement] = useState(0);
123
+ return [
124
+ announcement,
125
+ ()=>setAnnouncement((count)=>count + 1)
126
+ ];
127
+ }
128
+ /** Local typing is intentionally separate from staging: build the current group on commit. */ export function TextEdit({ test, label, draftKey = label, value, preview, onCommit, reason, multiline, placeholder, type = 'text', min, max, required, onDraft, scope, isValid, invalidMessage, hideLabel = false, disabled = false, help }) {
49
129
  const id = useId();
50
- const [draft, setDraft] = useState(String(value));
130
+ const edit = useFieldDraft(test, draftKey, label, String(value), scope);
131
+ const [announcement, announce] = useDraftAnnouncement();
51
132
  const [error, setError] = useState(null);
52
- useEffect(()=>setDraft(String(value)), [
53
- value
54
- ]);
55
- const commit = ()=>{
56
- if (draft === String(value)) return;
57
- // Named isValid, not validate: Payload 4's Local API has a validate method, which the access gate flags.
133
+ if (error && !edit.dirty) setError(null);
134
+ const discard = ()=>{
135
+ edit.discard();
136
+ onDraft?.(null);
137
+ setError(null);
138
+ };
139
+ const locked = Boolean(reason || disabled);
140
+ const choice = edit.dirty && !edit.saving && (edit.conflict || edit.retry || locked || Boolean(error));
141
+ const choiceId = choice ? `${id}-new-value` : undefined;
142
+ const commit = (confirmed = false)=>{
143
+ if (!edit.dirty || edit.saving) return;
144
+ if (locked || test.stale || choice && !confirmed) {
145
+ announce();
146
+ return;
147
+ }
148
+ const draft = edit.value;
149
+ if (draft === String(value)) {
150
+ discard();
151
+ return;
152
+ }
58
153
  if (isValid && !isValid(draft)) {
59
- setError(draft.trim() ? invalidMessage ?? null : null);
60
- setDraft(String(value));
61
- onDraft?.(String(value));
154
+ onDraft?.(null);
155
+ setError(invalidMessage ?? C.editInvalid);
62
156
  return;
63
157
  }
64
- onCommit(draft);
158
+ edit.submit(()=>onCommit(draft));
159
+ onDraft?.(null);
65
160
  };
66
- if (reason && !disabled) return /*#__PURE__*/ _jsx(ReadOnly, {
161
+ if (reason && !disabled && !edit.dirty) return /*#__PURE__*/ _jsx(ReadOnly, {
67
162
  label: label,
68
163
  value: value === '' && placeholder ? placeholder : value,
69
164
  reason: reason
70
165
  });
71
166
  const Input = multiline ? Textarea : type === 'number' ? NumberInput : TextInput;
72
- return /*#__PURE__*/ _jsx("div", {
167
+ return /*#__PURE__*/ _jsxs("div", {
73
168
  className: hideLabel ? 'xp-grid-field' : undefined,
74
169
  title: reason ?? undefined,
75
- children: /*#__PURE__*/ _jsx(Input, {
76
- id: id,
77
- label: label,
78
- "aria-label": label,
79
- value: draft,
80
- disabled: disabled,
81
- placeholder: placeholder,
82
- min: min,
83
- max: max,
84
- required: required,
85
- help: help,
86
- error: error ?? undefined,
87
- onChange: (e)=>{
88
- setError(null);
89
- setDraft(e.target.value);
90
- onDraft?.(e.target.value);
91
- },
92
- onBlur: commit,
93
- onKeyDown: (e)=>{
94
- if (!multiline && e.key === 'Enter') e.currentTarget.blur();
95
- }
96
- })
170
+ children: [
171
+ /*#__PURE__*/ _jsx(Input, {
172
+ id: id,
173
+ label: label,
174
+ "aria-label": label,
175
+ value: edit.dirty ? edit.value : preview ?? edit.value,
176
+ disabled: disabled && !edit.dirty,
177
+ readOnly: locked || test.stale || edit.saving,
178
+ "aria-describedby": choiceId,
179
+ placeholder: placeholder,
180
+ min: min,
181
+ max: max,
182
+ required: required,
183
+ help: help,
184
+ error: error ?? undefined,
185
+ onChange: (e)=>{
186
+ if (locked || test.stale || edit.saving) return;
187
+ const draft = e.target.value;
188
+ setError(null);
189
+ edit.change(draft);
190
+ onDraft?.(draft === String(value) || edit.conflict ? null : draft);
191
+ },
192
+ onBlur: ()=>commit(),
193
+ onKeyDown: (e)=>{
194
+ if (e.key === 'Escape' && edit.dirty && !edit.saving) {
195
+ e.preventDefault();
196
+ e.stopPropagation();
197
+ discard();
198
+ }
199
+ if (e.key === 'Enter' && !e.nativeEvent.isComposing && (choice || !multiline)) {
200
+ e.preventDefault();
201
+ commit();
202
+ }
203
+ }
204
+ }),
205
+ choiceId ? /*#__PURE__*/ _jsx(SavedValueChanged, {
206
+ id: choiceId,
207
+ value: String(value),
208
+ reason: reason,
209
+ conflict: edit.conflict,
210
+ announce: announcement,
211
+ onKeep: !locked && !test.stale && !error ? ()=>commit(true) : undefined,
212
+ onDiscard: discard
213
+ }) : null
214
+ ]
97
215
  });
98
216
  }
99
217
  export const positiveInteger = (raw)=>raw.trim() !== '' && Number.isInteger(Number(raw)) && Number(raw) > 0;
@@ -150,6 +268,7 @@ export function TestName({ test }) {
150
268
  return /*#__PURE__*/ _jsxs(_Fragment, {
151
269
  children: [
152
270
  /*#__PURE__*/ _jsx(TextEdit, {
271
+ test: test,
153
272
  label: C.name,
154
273
  value: test.doc?.name ?? '',
155
274
  reason: reasonFor(test, 'name'),
@@ -168,6 +287,7 @@ export function Hypothesis({ test }) {
168
287
  return /*#__PURE__*/ _jsxs(_Fragment, {
169
288
  children: [
170
289
  /*#__PURE__*/ _jsx(TextEdit, {
290
+ test: test,
171
291
  label: C.hypothesis,
172
292
  value: test.doc?.hypothesis ?? '',
173
293
  multiline: true,