@sanity/personalization-plugin 2.3.0 → 2.4.1

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.1",
4
4
  "description": "Plugin to help with personalization, a/b testing when using Sanity",
5
5
  "keywords": [
6
6
  "sanity",
@@ -85,7 +85,7 @@
85
85
  },
86
86
  "peerDependencies": {
87
87
  "react": "^18 || ^19",
88
- "sanity": "^3"
88
+ "sanity": "^3 || ^4.0.0-0"
89
89
  },
90
90
  "engines": {
91
91
  "node": ">=18"
@@ -64,6 +64,7 @@ const createExperimentType = ({
64
64
  name: 'active',
65
65
  type: 'boolean',
66
66
  hidden: true,
67
+ initialValue: false,
67
68
  }),
68
69
  defineField({
69
70
  name: experimentId,
@@ -105,6 +106,22 @@ const createExperimentType = ({
105
106
  ],
106
107
  }),
107
108
  ],
109
+ preview: {
110
+ select: {
111
+ base: 'default',
112
+ experiment: experimentId,
113
+ },
114
+ prepare: ({base, experiment}) => {
115
+ const title = base?.title || base?.name || ''
116
+ const experimentTitle = experiment ? `Experiment: ${experiment}` : ''
117
+ const media = base?.image || base?.photo || base?.media || ''
118
+ return {
119
+ title: title || experimentTitle,
120
+ subtitle: title ? experimentTitle : '',
121
+ media,
122
+ }
123
+ },
124
+ },
108
125
  })
109
126
  }
110
127
 
@@ -219,16 +236,17 @@ export const fieldLevelExperiments = definePlugin<FieldPluginConfig>((config) =>
219
236
  return props.renderDefault(props)
220
237
  }
221
238
 
222
- const flatFieldTypeNames = flattenSchemaType(props.schemaType).map(
223
- (field) => field.type.name,
224
- )
225
- const hasExperiment = flatFieldTypeNames.some((name) =>
226
- name.startsWith(experimentNameOverride),
239
+ const flatFields = flattenSchemaType(props.schemaType)
240
+ const hasExperiment = flatFields.some(
241
+ (field) =>
242
+ field.type.name.startsWith(experimentNameOverride) ||
243
+ field.name.startsWith(experimentNameOverride),
227
244
  )
228
245
 
229
246
  if (!hasExperiment) {
230
247
  return props.renderDefault(props)
231
248
  }
249
+
232
250
  const providerProps = {
233
251
  ...props,
234
252
  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