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

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 (164) hide show
  1. package/dist/admin/blocks/BlockPanel.js +6 -4
  2. package/dist/admin/blocks/inject.js +1 -0
  3. package/dist/admin/blocks/model.d.ts +3 -2
  4. package/dist/admin/blocks/model.js +8 -4
  5. package/dist/admin/copy/blocks.d.ts +3 -2
  6. package/dist/admin/copy/blocks.js +7 -6
  7. package/dist/admin/copy/newTest.d.ts +1 -1
  8. package/dist/admin/copy/newTest.js +1 -1
  9. package/dist/admin/surface/ResultsRoute.js +2 -1
  10. package/dist/admin/surface/TestsHome.d.ts +2 -1
  11. package/dist/admin/surface/TestsHome.js +91 -37
  12. package/dist/admin/surface/TestsSurface.js +3 -0
  13. package/dist/admin/surface/home.model.d.ts +2 -0
  14. package/dist/admin/surface/newTest/NewTest.js +16 -18
  15. package/dist/admin/surface/newTest/PageWizard.js +1 -0
  16. package/dist/admin/surface/newTest/newTest.css +3 -1
  17. package/dist/admin/views/TestsSurfaceClient.d.ts +3 -0
  18. package/dist/admin/views/TestsSurfaceClient.js +11 -0
  19. package/dist/admin/views/buildSurfaceProps.js +10 -3
  20. package/dist/blocks/bindings.js +8 -14
  21. package/dist/blocks/placementLanguages.d.ts +7 -0
  22. package/dist/blocks/placementLanguages.js +27 -0
  23. package/dist/blocks/placementLocale.d.ts +23 -7
  24. package/dist/blocks/placementLocale.js +221 -66
  25. package/dist/db/appends.d.ts +13 -0
  26. package/dist/db/appends.js +49 -0
  27. package/dist/db/bulkPlacementRemoval.js +8 -4
  28. package/dist/endpoints/contentLocale.d.ts +6 -0
  29. package/dist/endpoints/contentLocale.js +15 -0
  30. package/dist/endpoints/deleteImpact.d.ts +6 -0
  31. package/dist/endpoints/deleteImpact.js +206 -0
  32. package/dist/endpoints/index.js +7 -0
  33. package/dist/endpoints/pages.js +7 -3
  34. package/dist/endpoints/placementStatus.js +6 -1
  35. package/dist/endpoints/preflight.d.ts +1 -1
  36. package/dist/endpoints/preflight.js +17 -2
  37. package/dist/exports/adminClient.d.ts +1 -1
  38. package/dist/exports/adminClient.js +1 -1
  39. package/dist/exports/index.d.ts +1 -1
  40. package/dist/goals/fairness.js +43 -34
  41. package/dist/hooks/blockRemovalRule.d.ts +4 -3
  42. package/dist/hooks/blockRemovalRule.js +33 -9
  43. package/dist/hooks/pageGuard.d.ts +3 -0
  44. package/dist/hooks/pageGuard.js +34 -24
  45. package/dist/hooks/puckNestedTests.d.ts +8 -0
  46. package/dist/hooks/puckNestedTests.js +36 -0
  47. package/dist/lib/placementPages.d.ts +2 -1
  48. package/dist/lib/placementPages.js +10 -2
  49. package/dist/manifest/compile.d.ts +2 -1
  50. package/dist/manifest/compile.js +5 -1
  51. package/dist/puck/ExperimentPicker.d.ts +2 -1
  52. package/dist/puck/ExperimentPicker.js +37 -30
  53. package/dist/puck/ExperimentSlot.js +3 -1
  54. package/dist/puck/copy.d.ts +1 -0
  55. package/dist/puck/copy.js +1 -0
  56. package/dist/puck/editor/ConfigureTestDrawer.d.ts +8 -4
  57. package/dist/puck/editor/ConfigureTestDrawer.js +4 -2
  58. package/dist/puck/editor/CreateExperimentDrawer.d.ts +2 -1
  59. package/dist/puck/editor/CreateExperimentDrawer.js +24 -20
  60. package/dist/puck/editor/EditorControls.d.ts +1 -1
  61. package/dist/puck/editor/EditorControls.js +1 -1
  62. package/dist/puck/editor/ExperimentSlotEditor.js +405 -394
  63. package/dist/puck/editor/LeftoverVersions.d.ts +4 -2
  64. package/dist/puck/editor/LeftoverVersions.js +23 -44
  65. package/dist/puck/editor/QuickTestActions.js +2 -2
  66. package/dist/puck/editor/VersionBuilder.d.ts +41 -13
  67. package/dist/puck/editor/VersionBuilder.js +461 -224
  68. package/dist/puck/editor/armWrites.d.ts +9 -2
  69. package/dist/puck/editor/armWrites.js +40 -2
  70. package/dist/puck/editor/armsCache.d.ts +1 -1
  71. package/dist/puck/editor/armsCache.js +2 -1
  72. package/dist/puck/editor/blockClipboard.d.ts +16 -0
  73. package/dist/puck/editor/blockClipboard.js +20 -0
  74. package/dist/puck/editor/copy.d.ts +27 -26
  75. package/dist/puck/editor/copy.js +28 -26
  76. package/dist/puck/editor/createTest.d.ts +3 -0
  77. package/dist/puck/editor/createTest.js +33 -0
  78. package/dist/puck/editor/editor.chrome.css +138 -17
  79. package/dist/puck/editor/editor.css +156 -22
  80. package/dist/puck/editor/editor.model.d.ts +2 -1
  81. package/dist/puck/editor/editor.model.js +3 -3
  82. package/dist/puck/editor/experimentOutlinePlugin.js +1 -1
  83. package/dist/puck/editor/localPlacements.d.ts +5 -2
  84. package/dist/puck/editor/localPlacements.js +32 -18
  85. package/dist/puck/editor/placementsCache.d.ts +18 -12
  86. package/dist/puck/editor/placementsCache.js +52 -47
  87. package/dist/puck/editor/sidebar.d.ts +2 -6
  88. package/dist/puck/editor/sidebar.js +3 -6
  89. package/dist/puck/editor/usePlacementNotice.d.ts +1 -1
  90. package/dist/puck/editor/usePlacementNotice.js +4 -3
  91. package/dist/puck/experimentSlug.d.ts +2 -0
  92. package/dist/puck/experimentSlug.js +3 -0
  93. package/dist/puck/nestedTestCopy.d.ts +8 -0
  94. package/dist/puck/nestedTestCopy.js +22 -0
  95. package/dist/puck/nestedTests.d.ts +17 -0
  96. package/dist/puck/nestedTests.js +30 -0
  97. package/dist/puck/slotConfig.d.ts +1 -1
  98. package/dist/puck/slotConfig.js +17 -4
  99. package/dist/puck/visibility.d.ts +20 -0
  100. package/dist/puck/visibility.js +81 -0
  101. package/dist/server/recordContentChange.js +37 -20
  102. package/dist/server/resolveExperimentData.js +6 -3
  103. package/dist/testSurface/PreviewLinks.js +6 -2
  104. package/dist/testSurface/api.d.ts +6 -3
  105. package/dist/testSurface/api.js +10 -6
  106. package/dist/testSurface/contentLocaleURL.d.ts +2 -0
  107. package/dist/testSurface/contentLocaleURL.js +3 -0
  108. package/dist/testSurface/copy/common.d.ts +4 -0
  109. package/dist/testSurface/copy/common.js +8 -4
  110. package/dist/testSurface/copy/languages.d.ts +11 -0
  111. package/dist/testSurface/copy/languages.js +7 -0
  112. package/dist/testSurface/copy/readiness.d.ts +2 -1
  113. package/dist/testSurface/copy/readiness.js +2 -1
  114. package/dist/testSurface/copy/testPage.d.ts +28 -6
  115. package/dist/testSurface/copy/testPage.js +28 -6
  116. package/dist/testSurface/placementLabel.d.ts +8 -0
  117. package/dist/testSurface/placementLabel.js +24 -0
  118. package/dist/testSurface/placements/placementLabels.d.ts +2 -0
  119. package/dist/testSurface/placements/placementLabels.js +2 -1
  120. package/dist/testSurface/results/results.css +1 -1
  121. package/dist/testSurface/results/results.model.d.ts +1 -1
  122. package/dist/testSurface/surface/contracts.d.ts +2 -0
  123. package/dist/testSurface/surface/editorPlacement.d.ts +5 -0
  124. package/dist/testSurface/surface/editorPlacement.js +18 -0
  125. package/dist/testSurface/surface/pageBlock/pageBlock.model.js +2 -1
  126. package/dist/testSurface/surface/pageBlock/parts.js +24 -6
  127. package/dist/testSurface/surface/readiness/ActionDialogs.js +28 -34
  128. package/dist/testSurface/surface/readiness/readiness.model.js +9 -2
  129. package/dist/testSurface/surface/results/useResults.d.ts +4 -1
  130. package/dist/testSurface/surface/test/DeleteTest.d.ts +15 -0
  131. package/dist/testSurface/surface/test/DeleteTest.js +171 -0
  132. package/dist/testSurface/surface/test/GoalSection.js +101 -74
  133. package/dist/testSurface/surface/test/TestSurface.js +29 -9
  134. package/dist/testSurface/surface/test/fields.d.ts +1 -3
  135. package/dist/testSurface/surface/test/fields.js +5 -35
  136. package/dist/testSurface/surface/useTestDocument.d.ts +2 -1
  137. package/dist/testSurface/surface/useTestDocument.js +20 -4
  138. package/dist/testSurface/useApi.d.ts +4 -1
  139. package/dist/transitions/checkStart.d.ts +4 -1
  140. package/dist/transitions/checkStart.js +117 -14
  141. package/dist/transitions/contentFields.js +9 -5
  142. package/dist/transitions/makeContent.js +29 -9
  143. package/dist/transitions/nestedPlacementNames.d.ts +5 -0
  144. package/dist/transitions/nestedPlacementNames.js +33 -0
  145. package/dist/transitions/pageVersions.js +3 -1
  146. package/dist/transitions/placementInteractions.js +7 -4
  147. package/dist/transitions/placements.d.ts +26 -1
  148. package/dist/transitions/placements.js +239 -87
  149. package/dist/transitions/preflightData.d.ts +2 -1
  150. package/dist/transitions/preflightData.js +66 -3
  151. package/dist/transitions/start.js +4 -1
  152. package/dist/types/endpoints.d.ts +35 -1
  153. package/dist/types/endpoints.js +1 -1
  154. package/dist/types/puck.d.ts +1 -0
  155. package/dist/types/surface.d.ts +2 -0
  156. package/dist/ui/Button.js +2 -2
  157. package/dist/ui/Inputs.d.ts +4 -3
  158. package/dist/ui/Inputs.js +7 -4
  159. package/dist/ui/SelectControl.d.ts +33 -0
  160. package/dist/ui/SelectControl.js +504 -0
  161. package/dist/ui/Tabs.d.ts +0 -1
  162. package/dist/ui/Tabs.js +28 -40
  163. package/dist/ui/ui.css +18 -5
  164. package/package.json +3 -2
@@ -3,8 +3,18 @@ import type { Arm, DocId, FlagPoint, PreviewPlacement, ResolvedOptions } from '.
3
3
  import type { ArmSlot } from '../types/puck.js';
4
4
  import { type PlacementLocaleView } from '../blocks/placementLocale.js';
5
5
  export interface BlockPlacement extends PreviewPlacement {
6
+ nestedTests?: Array<{
7
+ outer: string;
8
+ inner: string;
9
+ outerSlug?: string;
10
+ innerSlug?: string;
11
+ }>;
6
12
  editor?: 'puck' | 'blocks' | 'page';
7
13
  doc?: Record<string, unknown>;
14
+ locales?: string[];
15
+ allLocales?: true;
16
+ locale?: string;
17
+ localeLabel?: string;
8
18
  slots: Record<ArmSlot, number>;
9
19
  duplicateRows?: boolean;
10
20
  identicalSlots?: string[];
@@ -25,7 +35,9 @@ interface ScanOptions {
25
35
  puckCollections: NonNullable<ResolvedOptions['puck']>['collections'];
26
36
  blocksCollections: NonNullable<ResolvedOptions['blocks']>['collections'];
27
37
  compareVersions?: boolean;
28
- /** Hook reads must see every placement and must never invoke nested access checks (CORE-21). */
38
+ /** Read the current visitor surface without fetching newer unpublished drafts. */
39
+ publishedOnly?: boolean;
40
+ /** Hooks and manager-authorized visitor-impact reads must see every placement (CORE-21). */
29
41
  pageGuard?: true;
30
42
  change?: PlacementChange;
31
43
  }
@@ -35,6 +47,19 @@ interface ScanOptions {
35
47
  export declare function hasOtherPublishedBlockPlacement(req: PayloadRequest, options: ResolvedOptions, slug: string, excluded: (collection: string, id: DocId, view: PlacementLocaleView, doc: Record<string, unknown> & {
36
48
  id: DocId;
37
49
  }) => boolean): Promise<boolean>;
50
+ interface PublishedBlockPlacement {
51
+ id: DocId;
52
+ editor: 'puck' | 'blocks';
53
+ slots: BlockPlacement['slots'];
54
+ locale?: string;
55
+ }
56
+ /** Visitor-impact scan: one published read per collection page. Resolve every
57
+ * visitor locale, including configured fallbacks; the endpoint groups by page.
58
+ */
59
+ export declare function publishedBlockPlacementPages(req: PayloadRequest, options: Pick<ResolvedOptions, 'puck' | 'blocks'>, slug: string): AsyncGenerator<{
60
+ collection: string;
61
+ placements: PublishedBlockPlacement[];
62
+ }>;
38
63
  /** Each matching ExperimentSlot is checked separately, including multiple slots
39
64
  * in one document: content elsewhere in the document cannot hide an empty slot.
40
65
  */
@@ -1,12 +1,15 @@
1
+ import { placementLanguages } from '../blocks/placementLanguages.js';
2
+ import { puckExperimentSlug } from '../puck/experimentSlug.js';
3
+ import { nestedTests, nestedTestLabel, nestedTestSlug } from '../puck/nestedTests.js';
4
+ import { walkVisiblePuck } from '../puck/visibility.js';
1
5
  import { BLOCK_SLOTS, EXPERIMENT_BLOCK_SLUG, hostBlocks, slotBlocks } from '../blocks/model.js';
2
6
  import { safeError } from '../log.js';
3
7
  import { pageVersionContent } from '../db/pageContent.js';
4
8
  import { comparableContent } from './pageVersions.js';
5
9
  import { isDeepStrictEqual } from 'node:util';
6
10
  import { hasLocalizedFields } from './contentFields.js';
7
- import { localeRequest, placementLocaleContent, placementLocaleViews } from '../blocks/placementLocale.js';
8
- import { placementSlugs } from '../blocks/bindings.js';
9
- import { documentEditor, puckCollections as normalisePuckCollections } from '../options.js';
11
+ import { localeRequest, placementReader } from '../blocks/placementLocale.js';
12
+ import { puckCollections as normalisePuckCollections } from '../options.js';
10
13
  const slots = [
11
14
  'control',
12
15
  'variantA',
@@ -14,7 +17,7 @@ const slots = [
14
17
  'variantC'
15
18
  ];
16
19
  async function placementDocuments(req, query, guard) {
17
- // §7.8 page guard: the hook must see test placements regardless of the editor's access.
20
+ // §7.8 page guard / visitor impact: count all placements regardless of the editor's access; impact details are checked separately.
18
21
  if (guard) return req.payload.find({
19
22
  ...query,
20
23
  req,
@@ -44,16 +47,17 @@ function changedDocuments(docs, change, collection, draft, page, draftsEnabled)
44
47
  /** Published rows only, one pass per collection across both editor registrations.
45
48
  * The exclusion predicate reads live transaction claims after each awaited page.
46
49
  */ export async function hasOtherPublishedBlockPlacement(req, options, slug, excluded) {
47
- const scanReq = req.payload.config.localization ? {
48
- ...req,
49
- locale: 'all'
50
- } : req;
50
+ const scanReq = localeRequest(req, {
51
+ locale: req.payload.config.localization ? 'all' : req.locale ?? '',
52
+ fallbackLocale: false
53
+ });
51
54
  const collections = new Set([
52
55
  ...normalisePuckCollections(options).map((entry)=>entry.slug),
53
56
  ...(options.blocks?.collections ?? []).map((entry)=>entry.slug)
54
57
  ]);
55
58
  for (const collection of collections){
56
59
  const drafts = req.payload.config.collections?.some((c)=>c.slug === collection && c.versions && c.versions.drafts);
60
+ const reader = placementReader(req, collection, options);
57
61
  let page = 1;
58
62
  while(true){
59
63
  const result = await placementDocuments(scanReq, {
@@ -75,15 +79,116 @@ function changedDocuments(docs, change, collection, draft, page, draftsEnabled)
75
79
  }
76
80
  } : {}
77
81
  }, true);
78
- if (result.docs.some((doc)=>doc._status !== 'draft' && doc.deletedAt == null && placementLocaleViews(req, collection).some((view)=>placementSlugs(options, collection, placementLocaleContent(localeRequest(req, view), collection, doc)).includes(slug) && !excluded(collection, doc.id, view, doc)))) return true;
82
+ if (result.docs.some((doc)=>doc._status !== 'draft' && doc.deletedAt == null && reader.views.some((view)=>reader.read(doc, 'all-locales', view).slugs.includes(slug) && !excluded(collection, doc.id, view, doc)))) return true;
79
83
  if (!result.hasNextPage) break;
80
84
  page++;
81
85
  }
82
86
  }
83
87
  return false;
84
88
  }
89
+ /** Visitor-impact scan: one published read per collection page. Resolve every
90
+ * visitor locale, including configured fallbacks; the endpoint groups by page.
91
+ */ export async function* publishedBlockPlacementPages(req, options, slug) {
92
+ const scanReq = localeRequest(req, {
93
+ locale: req.payload.config.localization ? 'all' : req.locale ?? '',
94
+ fallbackLocale: false
95
+ });
96
+ const puck = options.puck?.enabled ? normalisePuckCollections(options) : [], blocks = options.blocks?.collections ?? [];
97
+ for (const collection of new Set([
98
+ ...puck,
99
+ ...blocks
100
+ ].map((entry)=>entry.slug))){
101
+ const drafts = req.payload.config.collections?.some((c)=>c.slug === collection && c.versions && c.versions.drafts);
102
+ const reader = placementReader(req, collection, options);
103
+ let page = 1;
104
+ while(true){
105
+ req.signal?.throwIfAborted();
106
+ const result = await placementDocuments(scanReq, {
107
+ collection,
108
+ overrideAccess: false,
109
+ limit: 100,
110
+ page,
111
+ depth: 0,
112
+ draft: false,
113
+ ...req.payload.config.localization ? {
114
+ locale: 'all',
115
+ fallbackLocale: false
116
+ } : {},
117
+ ...drafts ? {
118
+ where: {
119
+ _status: {
120
+ equals: 'published'
121
+ }
122
+ }
123
+ } : {}
124
+ }, true);
125
+ req.signal?.throwIfAborted();
126
+ const placements = [];
127
+ for (const raw of result.docs){
128
+ if (raw._status === 'draft' || raw.deletedAt != null) continue;
129
+ for (const view of reader.views){
130
+ const placement = reader.read(raw, 'all-locales', view);
131
+ const doc = placement.doc;
132
+ const add = (contents, editor)=>placements.push({
133
+ id: raw.id,
134
+ editor,
135
+ slots: contents,
136
+ ...view.locale ? {
137
+ locale: view.locale
138
+ } : {}
139
+ });
140
+ if (placement.editor === 'puck') {
141
+ const registration = puck.find((entry)=>entry.slug === collection);
142
+ if (registration) walkVisiblePuck(doc[registration.field], (node, visibility)=>{
143
+ if (!visibility.hidden && node.type === 'ExperimentSlot' && puckExperimentSlug(node.props.experiment) === slug) {
144
+ add(Object.fromEntries(slots.map((slot)=>[
145
+ slot,
146
+ Array.isArray(node.props[slot]) ? node.props[slot].length : 0
147
+ ])), 'puck');
148
+ }
149
+ });
150
+ } else {
151
+ const registration = blocks.find((entry)=>entry.slug === collection);
152
+ if (registration) for (const block of hostBlocks(doc[registration.field])){
153
+ if (block.blockType === (registration.blockSlug ?? EXPERIMENT_BLOCK_SLUG) && block.experiment === slug) {
154
+ add(Object.fromEntries(BLOCK_SLOTS.map((slot)=>[
155
+ slot,
156
+ slotBlocks(block, slot).length
157
+ ])), 'blocks');
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
163
+ yield {
164
+ collection,
165
+ placements
166
+ };
167
+ if (!result.hasNextPage) break;
168
+ page++;
169
+ }
170
+ }
171
+ }
85
172
  export async function scanBlockPlacements(req, args) {
86
173
  const { puckCollections } = args;
174
+ const scanReq = localeRequest(req, {
175
+ locale: req.payload.config.localization ? 'all' : req.locale ?? '',
176
+ fallbackLocale: req.payload.config.localization ? false : req.fallbackLocale ?? false
177
+ });
178
+ const comparisons = new Map();
179
+ const comparison = (collection, id)=>{
180
+ if (!req.payload.config.localization) return comparisonContent(req, collection, id);
181
+ const key = JSON.stringify([
182
+ collection,
183
+ String(id)
184
+ ]);
185
+ let pending = comparisons.get(key);
186
+ if (!pending) {
187
+ pending = comparisonContent(req, collection, id);
188
+ comparisons.set(key, pending);
189
+ }
190
+ return pending;
191
+ };
87
192
  const slugs = args.slugs ?? new Set([
88
193
  args.slug
89
194
  ]);
@@ -99,17 +204,28 @@ export async function scanBlockPlacements(req, args) {
99
204
  collections: puckCollections
100
205
  }
101
206
  })){
207
+ const reader = placementReader(req, collection, {
208
+ puck: {
209
+ enabled: true,
210
+ collections: puckCollections
211
+ },
212
+ blocks: {
213
+ collections: args.blocksCollections
214
+ }
215
+ });
102
216
  const editors = new Map();
103
217
  const publishedIds = new Set();
104
218
  const publishedSlugs = new Map();
105
219
  const draftsEnabled = req.payload.config.collections?.some((config)=>config.slug === collection && config.versions && config.versions.drafts);
106
- for (const draft of [
220
+ for (const draft of args.publishedOnly ? [
221
+ false
222
+ ] : [
107
223
  false,
108
224
  true
109
225
  ]){
110
226
  let page = 1;
111
227
  while(true){
112
- const result = await placementDocuments(req, {
228
+ const result = await placementDocuments(scanReq, {
113
229
  collection,
114
230
  req,
115
231
  overrideAccess: false,
@@ -118,6 +234,10 @@ export async function scanBlockPlacements(req, args) {
118
234
  depth: 0,
119
235
  draft,
120
236
  includeLockStatus: true,
237
+ ...req.payload.config.localization ? {
238
+ locale: 'all',
239
+ fallbackLocale: false
240
+ } : {},
121
241
  ...!draft && draftsEnabled ? {
122
242
  where: {
123
243
  _status: {
@@ -126,52 +246,62 @@ export async function scanBlockPlacements(req, args) {
126
246
  }
127
247
  } : {}
128
248
  }, args.pageGuard);
129
- for (const doc of changedDocuments(result.docs, args.change, collection, draft, page, Boolean(draftsEnabled))){
249
+ for (const localized of changedDocuments(result.docs, args.change, collection, draft, page, Boolean(draftsEnabled)))for (const view of reader.views){
250
+ const doc = reader.read(localized, 'all-locales', view).doc;
251
+ const documentKey = JSON.stringify([
252
+ String(doc.id),
253
+ view.locale
254
+ ]);
130
255
  if (!draft && doc._status === 'draft') continue;
131
- if (draft && doc._status !== 'draft' && publishedIds.has(String(doc.id))) continue;
256
+ if (draft && doc._status !== 'draft' && publishedIds.has(documentKey)) continue;
132
257
  if (!draft) {
133
- editors.set(String(doc.id), documentEditor({
134
- puck: {
135
- enabled: true,
136
- collections: puckCollections
137
- },
138
- blocks: {
139
- collections: args.blocksCollections
140
- }
141
- }, collection, doc));
142
- publishedIds.add(String(doc.id));
143
- if (typeof doc.slug === 'string') publishedSlugs.set(String(doc.id), doc.slug);
258
+ editors.set(documentKey, reader.read(doc, 'localized').editor);
259
+ publishedIds.add(documentKey);
260
+ if (typeof doc.slug === 'string') publishedSlugs.set(documentKey, doc.slug);
144
261
  }
145
- const editor = editors.get(String(doc.id)) ?? documentEditor({
146
- puck: {
147
- enabled: true,
148
- collections: puckCollections
149
- },
150
- blocks: {
151
- collections: args.blocksCollections
152
- }
153
- }, collection, doc);
262
+ const editor = editors.get(documentKey) ?? reader.read(doc, 'localized').editor;
154
263
  if (editor !== 'puck') continue;
155
- function walk(value) {
156
- if (!value || typeof value !== 'object') return;
157
- if (Array.isArray(value)) {
158
- value.forEach(walk);
159
- return;
160
- }
161
- const node = value;
264
+ const nearest = new Map(nestedTests(doc[field]).map((pair)=>[
265
+ pair.inner,
266
+ pair
267
+ ]));
268
+ const nested = [
269
+ ...nearest.values()
270
+ ].map(({ outer, inner })=>({
271
+ outer: nestedTestLabel(outer),
272
+ inner: nestedTestLabel(inner),
273
+ outerSlug: nestedTestSlug(outer),
274
+ innerSlug: nestedTestSlug(inner)
275
+ }));
276
+ const offsets = new Map([
277
+ ...placements
278
+ ].map(([slug, list])=>[
279
+ slug,
280
+ list.length
281
+ ]));
282
+ walkVisiblePuck(doc[field], (node, visibility)=>{
283
+ if (visibility.hidden) return;
162
284
  const props = node.props;
163
- const ref = props?.experiment;
164
- const experiment = typeof ref === 'string' ? ref : ref && typeof ref === 'object' && 'slug' in ref ? ref.slug : null;
165
- if (node.type === 'ExperimentSlot' && props && typeof experiment === 'string' && slugs.has(experiment)) {
285
+ const experiment = puckExperimentSlug(props.experiment);
286
+ if (node.type === 'ExperimentSlot' && typeof experiment === 'string' && slugs.has(experiment)) {
166
287
  placements.get(experiment).push({
167
288
  collection,
168
289
  id: doc.id,
169
290
  editor: 'puck',
170
291
  doc,
292
+ nestedTests: nested.filter((pair)=>pair.outerSlug === experiment || pair.innerSlug === experiment).map(({ outer, inner, outerSlug, innerSlug })=>({
293
+ outer,
294
+ inner,
295
+ outerSlug: outerSlug ?? '',
296
+ innerSlug: innerSlug ?? ''
297
+ })),
298
+ ...view.locale ? {
299
+ locale: view.locale
300
+ } : {},
171
301
  title: String(doc.title ?? doc.name ?? doc.id),
172
302
  published: !draft || doc._status === 'published',
173
- ...publishedSlugs.has(String(doc.id)) ? {
174
- slug: publishedSlugs.get(String(doc.id))
303
+ ...publishedSlugs.has(documentKey) ? {
304
+ slug: publishedSlugs.get(documentKey)
175
305
  } : {},
176
306
  slots: Object.fromEntries(slots.map((slot)=>[
177
307
  slot,
@@ -183,36 +313,22 @@ export async function scanBlockPlacements(req, args) {
183
313
  locked: doc._isLocked === true || doc.locked === true
184
314
  });
185
315
  }
186
- Object.values(node).forEach(walk);
187
- }
188
- const offsets = new Map([
189
- ...placements
190
- ].map(([slug, list])=>[
191
- slug,
192
- list.length
193
- ]));
194
- walk(doc[field]);
316
+ });
195
317
  if (args.compareVersions && !draft && [
196
318
  ...placements
197
319
  ].some(([slug, list])=>list.length > offsets.get(slug))) {
198
- const raw = await comparisonContent(req, collection, doc.id);
320
+ const raw = reader.read(await comparison(collection, doc.id), 'all-locales', view).doc;
199
321
  const rawSlots = new Map();
200
- const collect = (value)=>{
201
- if (Array.isArray(value)) {
202
- value.forEach(collect);
203
- return;
204
- }
205
- if (!value || typeof value !== 'object') return;
206
- const node = value, props = node.props;
207
- const ref = props?.experiment;
208
- const slug = typeof ref === 'string' ? ref : ref && typeof ref === 'object' && 'slug' in ref ? ref.slug : undefined;
209
- if (node.type === 'ExperimentSlot' && props && typeof slug === 'string') {
210
- const list = rawSlots.get(slug) ?? [];
211
- list.push(props);
212
- rawSlots.set(slug, list);
213
- }
214
- Object.values(node).forEach(collect);
215
- };
322
+ const collect = (value)=>walkVisiblePuck(value, (node, visibility)=>{
323
+ if (visibility.hidden) return;
324
+ const props = node.props;
325
+ const slug = puckExperimentSlug(props?.experiment);
326
+ if (node.type === 'ExperimentSlot' && props && typeof slug === 'string') {
327
+ const list = rawSlots.get(slug) ?? [];
328
+ list.push(props);
329
+ rawSlots.set(slug, list);
330
+ }
331
+ });
216
332
  if (raw && raw._status !== 'draft') collect(raw[field]);
217
333
  for (const [slug, list] of placements)list.slice(offsets.get(slug)).forEach((placement, index)=>{
218
334
  const props = rawSlots.get(slug)?.[index];
@@ -227,15 +343,26 @@ export async function scanBlockPlacements(req, args) {
227
343
  }
228
344
  for (const registration of args.blocksCollections){
229
345
  const { slug: collection, field } = registration;
346
+ const reader = placementReader(req, collection, {
347
+ puck: {
348
+ enabled: true,
349
+ collections: puckCollections
350
+ },
351
+ blocks: {
352
+ collections: args.blocksCollections
353
+ }
354
+ });
230
355
  const published = new Map();
231
356
  const draftsEnabled = req.payload.config.collections?.some((c)=>c.slug === collection && c.versions && c.versions.drafts);
232
- for (const draft of [
357
+ for (const draft of args.publishedOnly ? [
358
+ false
359
+ ] : [
233
360
  false,
234
361
  true
235
362
  ]){
236
363
  let page = 1;
237
364
  while(true){
238
- const result = await placementDocuments(req, {
365
+ const result = await placementDocuments(scanReq, {
239
366
  collection,
240
367
  req,
241
368
  overrideAccess: false,
@@ -244,6 +371,10 @@ export async function scanBlockPlacements(req, args) {
244
371
  page,
245
372
  draft,
246
373
  includeLockStatus: true,
374
+ ...req.payload.config.localization ? {
375
+ locale: 'all',
376
+ fallbackLocale: false
377
+ } : {},
247
378
  ...!draft && draftsEnabled ? {
248
379
  where: {
249
380
  _status: {
@@ -252,21 +383,18 @@ export async function scanBlockPlacements(req, args) {
252
383
  }
253
384
  } : {}
254
385
  }, args.pageGuard);
255
- for (const doc of changedDocuments(result.docs, args.change, collection, draft, page, Boolean(draftsEnabled))){
386
+ for (const localized of changedDocuments(result.docs, args.change, collection, draft, page, Boolean(draftsEnabled)))for (const view of reader.views){
387
+ const doc = reader.read(localized, 'all-locales', view).doc;
388
+ const documentKey = JSON.stringify([
389
+ String(doc.id),
390
+ view.locale
391
+ ]);
256
392
  if (!draft && doc._status === 'draft') continue;
257
- if (!draft) published.set(String(doc.id), doc);
258
- else if (doc._status !== 'draft' && published.has(String(doc.id))) continue;
259
- const baseline = published.get(String(doc.id)) ?? doc;
260
- if (documentEditor({
261
- blocks: {
262
- collections: args.blocksCollections
263
- },
264
- puck: {
265
- enabled: true,
266
- collections: puckCollections
267
- }
268
- }, collection, baseline) !== 'blocks') continue;
269
- const raw = args.compareVersions && !draft && hostBlocks(doc[field]).some((block)=>block.blockType === (registration.blockSlug ?? EXPERIMENT_BLOCK_SLUG) && typeof block.experiment === 'string' && slugs.has(block.experiment)) ? await comparisonContent(req, collection, doc.id) : undefined;
393
+ if (!draft) published.set(documentKey, doc);
394
+ else if (doc._status !== 'draft' && published.has(documentKey)) continue;
395
+ const baseline = published.get(documentKey) ?? doc;
396
+ if (reader.read(baseline, 'localized').editor !== 'blocks') continue;
397
+ const raw = args.compareVersions && !draft && hostBlocks(doc[field]).some((block)=>block.blockType === (registration.blockSlug ?? EXPERIMENT_BLOCK_SLUG) && typeof block.experiment === 'string' && slugs.has(block.experiment)) ? reader.read(await comparison(collection, doc.id), 'all-locales', view).doc : undefined;
270
398
  for (const [index, block] of hostBlocks(doc[field]).entries()){
271
399
  if (block.blockType !== (registration.blockSlug ?? EXPERIMENT_BLOCK_SLUG) || typeof block.experiment !== 'string' || !slugs.has(block.experiment)) continue;
272
400
  const stored = raw ? hostBlocks(raw[field]).find((row)=>row.id === block.id) ?? hostBlocks(raw[field])[index] ?? block : block;
@@ -277,6 +405,9 @@ export async function scanBlockPlacements(req, args) {
277
405
  id: doc.id,
278
406
  editor: 'blocks',
279
407
  doc,
408
+ ...view.locale ? {
409
+ locale: view.locale
410
+ } : {},
280
411
  title: String(doc.title ?? doc.name ?? doc.id),
281
412
  published: !draft || doc._status === 'published',
282
413
  ...typeof baseline.slug === 'string' ? {
@@ -300,6 +431,27 @@ export async function scanBlockPlacements(req, args) {
300
431
  }
301
432
  }
302
433
  }
434
+ for (const list of placements.values()){
435
+ const groups = new Map();
436
+ for (const placement of list)if (placement.locale) {
437
+ const key = JSON.stringify([
438
+ placement.collection,
439
+ String(placement.id),
440
+ placement.published
441
+ ]);
442
+ const group = groups.get(key) ?? [];
443
+ group.push(placement);
444
+ groups.set(key, group);
445
+ }
446
+ for (const group of groups.values()){
447
+ const languages = placementLanguages(req, group.map((placement)=>placement.locale));
448
+ for (const placement of group)Object.assign(placement, languages, {
449
+ localeLabel: placementLanguages(req, [
450
+ placement.locale
451
+ ]).locales?.[0] ?? placement.locale
452
+ });
453
+ }
454
+ }
303
455
  return args.slugs ? placements : placements.get(args.slug);
304
456
  }
305
457
  export async function flagPlacements(options, experiment, req) {
@@ -29,7 +29,8 @@ export declare function loadStartInputsBatch<TUser extends {
29
29
  }>(req: PayloadRequest, options: ResolvedOptions<TUser>, experiments: StartExperiment[], tolerateErrors?: boolean): Promise<Map<string, CheckStartInput | null>>;
30
30
  export declare function preflightData<TUser extends {
31
31
  id: DocId;
32
- }>(req: PayloadRequest, { experimentId, options }: {
32
+ }>(req: PayloadRequest, { experimentId, options, placementLocale }: {
33
33
  experimentId: DocId;
34
34
  options: ResolvedOptions<TUser>;
35
+ placementLocale?: string;
35
36
  }, { now, estimate }?: PreflightDependencies): Promise<PreflightResult | null>;
@@ -1,3 +1,6 @@
1
+ import { nestedPlacementNames, nameNestedPlacements } from './nestedPlacementNames.js';
2
+ import { placementReader } from '../blocks/placementLocale.js';
3
+ import { placementLanguages } from '../blocks/placementLanguages.js';
1
4
  import { goalContent, OUTSIDE_GOAL } from '../goals/fairness.js';
2
5
  import { placementInteractions, placementPaths, uniquePlacementDocs } from './placementInteractions.js';
3
6
  import { Forbidden, NotFound } from 'payload';
@@ -96,6 +99,7 @@ const metricIds = (experiment)=>[
96
99
  // Lazy promises prevent unhandled rejections and share failed reads as well as successful ones.
97
100
  let pageTestsInUse;
98
101
  let blocks;
102
+ let nestedNames;
99
103
  let state;
100
104
  const busyPoints = new Map();
101
105
  const layers = new Map();
@@ -103,7 +107,11 @@ const metricIds = (experiment)=>[
103
107
  for (const experiment of experiments){
104
108
  try {
105
109
  const ownIds = new Set(metricIds(experiment).map(String));
110
+ const localization = req.payload.config.localization;
106
111
  const input = {
112
+ placementLocaleCodes: localization ? localization.localeCodes : undefined,
113
+ placementLocale: req.locale ?? undefined,
114
+ placementDefaultLocale: localization ? localization.defaultLocale : undefined,
107
115
  experiment,
108
116
  metrics: (await metrics).filter((metric)=>ownIds.has(String(metric.id))),
109
117
  metricSources: options.metricSources,
@@ -139,6 +147,12 @@ const metricIds = (experiment)=>[
139
147
  blocksCollections: options.blocks?.collections ?? []
140
148
  });
141
149
  input.placements = (await blocks).get(experiment.slug) ?? [];
150
+ if (input.placements.some((p)=>p.nestedTests?.length)) {
151
+ nestedNames ??= nestedPlacementNames(req, [
152
+ ...(await blocks).values()
153
+ ].flat());
154
+ input.placements = nameNestedPlacements(input.placements, await nestedNames);
155
+ }
142
156
  } catch (cause) {
143
157
  input.placementError = placementFailure(cause);
144
158
  }
@@ -292,7 +306,10 @@ async function placementUrls(options, placement, pendingPreviews, onFailure, pre
292
306
  const key = JSON.stringify([
293
307
  collection,
294
308
  String(id),
295
- published
309
+ published,
310
+ ...placement.locale ? [
311
+ placement.locale
312
+ ] : []
296
313
  ]);
297
314
  let preview = previews.get(key);
298
315
  if (!preview) previews.set(key, preview = previewLookup(options, placement, pendingPreviews, onFailure));
@@ -376,9 +393,11 @@ async function primaryHistory(source, now) {
376
393
  return null;
377
394
  }
378
395
  }
379
- export async function preflightData(req, { experimentId, options }, { now = new Date(), estimate = powerMetricWeeks } = {}) {
396
+ export async function preflightData(req, { experimentId, options, placementLocale }, { now = new Date(), estimate = powerMetricWeeks } = {}) {
380
397
  const input = await loadStartInputs(req, options, experimentId);
381
398
  if (!input) return null;
399
+ // Advisory title selection never changes the request used by scans or host adapters.
400
+ if (placementLocale !== undefined) input.placementLocale = placementLocale;
382
401
  const { experiment: e } = input;
383
402
  const primaryId = relationshipId(e.metrics?.primary);
384
403
  const metric = input.metrics.find((metric)=>String(metric.id) === String(primaryId));
@@ -458,7 +477,7 @@ export async function preflightData(req, { experimentId, options }, { now = new
458
477
  }
459
478
  const requiredSlots = e.arms.filter((arm)=>arm.kind !== 'holdout').map((arm)=>arm.slot);
460
479
  const blocks = input.placements ?? [];
461
- const placements = new Array(blocks.length);
480
+ let placements = new Array(blocks.length);
462
481
  let nextPlacement = 0;
463
482
  const pendingPreviews = {
464
483
  count: 0
@@ -492,6 +511,49 @@ export async function preflightData(req, { experimentId, options }, { now = new
492
511
  };
493
512
  }
494
513
  }));
514
+ if (blocks.some((placement)=>placement.locale)) {
515
+ // Summarize occurrences within one language and publication state first.
516
+ // Combine languages only when the complete displayed row agrees.
517
+ const languageRows = new Map();
518
+ blocks.forEach((placement, index)=>{
519
+ const key = JSON.stringify([
520
+ placement.collection,
521
+ String(placement.id),
522
+ placement.published,
523
+ placement.locale
524
+ ]);
525
+ const group = languageRows.get(key) ?? [];
526
+ group.push(index);
527
+ languageRows.set(key, group);
528
+ });
529
+ const groups = new Map();
530
+ for (const indices of languageRows.values()){
531
+ const rows = indices.map((index)=>placements[index]), first = rows[0];
532
+ const row = {
533
+ ...first,
534
+ slotCount: rows.length,
535
+ ok: rows.every((row)=>row.ok),
536
+ slots: Object.fromEntries(Object.keys(first.slots).map((slot)=>[
537
+ slot,
538
+ Math.min(...rows.map((row)=>row.slots[slot]))
539
+ ]))
540
+ };
541
+ const key = JSON.stringify(row);
542
+ const group = groups.get(key) ?? {
543
+ row,
544
+ codes: []
545
+ };
546
+ const locale = blocks[indices[0]].locale;
547
+ if (locale) group.codes.push(locale);
548
+ groups.set(key, group);
549
+ }
550
+ placements = [
551
+ ...groups.values()
552
+ ].map(({ row, codes })=>({
553
+ ...row,
554
+ ...placementLanguages(req, codes)
555
+ }));
556
+ }
495
557
  for (const placement of input.flagPlacements ?? [])placements.push({
496
558
  collection: e.flagPoint ?? '',
497
559
  id: placement.arm,
@@ -567,6 +629,7 @@ export async function preflightData(req, { experimentId, options }, { now = new
567
629
  });
568
630
  continue;
569
631
  }
632
+ doc = placementReader(req, collection.slug, options).read(doc, req.locale === 'all' ? 'all-locales' : 'localized').doc;
570
633
  const titleField = req.payload.config.collections?.find((entry)=>entry.slug === collection.slug)?.admin.useAsTitle ?? 'title';
571
634
  const placement = {
572
635
  collection: collection.slug,