@sanity/personalization-plugin 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/personalization-plugin",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "Plugin to help with personalization, a/b testing when using Sanity",
5
5
  "keywords": [
6
6
  "sanity",
@@ -219,16 +219,17 @@ export const fieldLevelExperiments = definePlugin<FieldPluginConfig>((config) =>
219
219
  return props.renderDefault(props)
220
220
  }
221
221
 
222
- const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(
223
- (field) => field.type.name,
224
- )
225
- const hasExperiment = flatFieldTypeNames.some((name) =>
226
- name.startsWith(experimentNameOverride),
222
+ const flatFields = flattenSchemaType(props.schemaType)
223
+ const hasExperiment = flatFields.some(
224
+ (field) =>
225
+ field.type.name.startsWith(experimentNameOverride) ||
226
+ field.name.startsWith(experimentNameOverride),
227
227
  )
228
228
 
229
229
  if (!hasExperiment) {
230
230
  return props.renderDefault(props)
231
231
  }
232
+
232
233
  const providerProps = {
233
234
  ...props,
234
235
  experimentFieldPluginConfig: {
@@ -11,7 +11,7 @@ export function flattenSchemaType(schemaType: SchemaType): ObjectFieldWithPath[]
11
11
  return []
12
12
  }
13
13
 
14
- return extractInnerFields(schemaType.fields, [], 3)
14
+ return extractInnerFields(schemaType.fields, [], 5)
15
15
  }
16
16
 
17
17
  function extractInnerFields(
@@ -28,20 +28,17 @@ function extractInnerFields(
28
28
 
29
29
  if (field.type.jsonType === 'object') {
30
30
  const innerFields = extractInnerFields(field.type.fields, [...path, field.name], maxDepth)
31
-
32
31
  return [...acc, thisFieldWithPath, ...innerFields]
33
- } else if (
34
- field.type.jsonType === 'array' &&
35
- field.type.of.length &&
36
- field.type.of.some((item) => 'fields' in item)
37
- ) {
38
- const innerFields = extractInnerFields(
39
- // @ts-expect-error - Fix TS assertion for array fields
40
- field.type.of[0].fields,
41
- [...path, field.name],
42
- maxDepth,
43
- )
44
-
32
+ } else if (field.type.jsonType === 'array') {
33
+ // Handle array types by checking each possible type in the array
34
+ const arrayTypes = field.type.of || []
35
+ const innerFields = arrayTypes.reduce<ObjectFieldWithPath[]>((arrayAcc, arrayType) => {
36
+ if ('fields' in arrayType) {
37
+ const typeFields = extractInnerFields(arrayType.fields, [...path, field.name], maxDepth)
38
+ return [...arrayAcc, ...typeFields]
39
+ }
40
+ return arrayAcc
41
+ }, [])
45
42
  return [...acc, thisFieldWithPath, ...innerFields]
46
43
  }
47
44