@sanity/personalization-plugin 2.4.2 → 2.5.0-field-level-personalization.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.
Files changed (34) hide show
  1. package/dist/_chunks-cjs/fieldExperiments.js +507 -0
  2. package/dist/_chunks-cjs/fieldExperiments.js.map +1 -0
  3. package/dist/_chunks-es/fieldExperiments.mjs +511 -0
  4. package/dist/_chunks-es/fieldExperiments.mjs.map +1 -0
  5. package/dist/growthbook/index.js +3 -3
  6. package/dist/growthbook/index.js.map +1 -1
  7. package/dist/growthbook/index.mjs +1 -1
  8. package/dist/index.d.mts +33 -12
  9. package/dist/index.d.ts +33 -12
  10. package/dist/index.js +157 -280
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +159 -280
  13. package/dist/index.mjs.map +1 -1
  14. package/package.json +1 -1
  15. package/src/components/{ExperimentItem.tsx → ArrayItem.tsx} +1 -2
  16. package/src/components/Select.tsx +1 -1
  17. package/src/components/{Array.tsx → experiment/Array.tsx} +2 -2
  18. package/src/components/{ExperimentContext.tsx → experiment/Context.tsx} +2 -2
  19. package/src/components/{ExperimentField.tsx → experiment/Field.tsx} +11 -8
  20. package/src/components/{ExperimentInput.tsx → experiment/Input.tsx} +4 -4
  21. package/src/components/{VariantPreview.tsx → experiment/VariantPreview.tsx} +2 -2
  22. package/src/components/experiment/index.ts +6 -0
  23. package/src/components/personalization/Array.tsx +59 -0
  24. package/src/components/personalization/Context.tsx +61 -0
  25. package/src/components/personalization/Field.tsx +134 -0
  26. package/src/components/personalization/SegmentInput.tsx +19 -0
  27. package/src/components/personalization/SegmentPreview.tsx +71 -0
  28. package/src/components/personalization/index.ts +5 -0
  29. package/src/fieldExperiments.tsx +43 -13
  30. package/src/fieldPersonalization.tsx +254 -0
  31. package/src/index.ts +1 -0
  32. package/src/types.ts +20 -2
  33. package/src/utils/clearChildGroups.ts +33 -0
  34. /package/src/components/{VariantInput.tsx → experiment/VariantInput.tsx} +0 -0
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), gi = require("react-icons/gi");
3
+ var fieldExperiments = require("./_chunks-cjs/fieldExperiments.js"), jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), io = require("react-icons/io");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -8,99 +8,74 @@ var equal__default = /* @__PURE__ */ _interopDefaultCompat(equal);
8
8
  const CONFIG_DEFAULT = {
9
9
  fields: [],
10
10
  apiVersion: "2024-11-07",
11
- experimentNameOverride: "experiment",
12
- variantNameOverride: "variant",
13
- variantId: "variantId",
14
- variantArrayName: "variants",
15
- experimentId: "experimentId"
16
- }, ExperimentContext = react.createContext({
11
+ personalizationNameOverride: "personalization",
12
+ segmentNameOverride: "segment",
13
+ segmentId: "segmentId",
14
+ segmentArrayName: "segments"
15
+ }, PersonalizationContext = react.createContext({
17
16
  ...CONFIG_DEFAULT,
18
- experiments: []
17
+ segments: []
19
18
  });
20
- function useExperimentContext() {
21
- return react.useContext(ExperimentContext);
19
+ function usePersonalizationContext() {
20
+ return react.useContext(PersonalizationContext);
22
21
  }
23
- function ExperimentProvider(props) {
24
- const { experimentFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
22
+ function PersonalizationProvider(props) {
23
+ const { personalizationFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: personalizationFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), segments = Array.isArray(personalizationFieldPluginConfig.segments) ? personalizationFieldPluginConfig.segments : suspendReact.suspend(
25
24
  // eslint-disable-next-line require-await
26
- async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
25
+ async () => typeof personalizationFieldPluginConfig.segments == "function" ? personalizationFieldPluginConfig.segments(client) : personalizationFieldPluginConfig.segments,
27
26
  [workspace],
28
27
  { equal: equal__default.default }
29
28
  ), context = react.useMemo(
30
- () => ({ ...experimentFieldPluginConfig, experiments }),
31
- [experimentFieldPluginConfig, experiments]
29
+ () => ({ ...personalizationFieldPluginConfig, segments }),
30
+ [personalizationFieldPluginConfig, segments]
32
31
  );
33
- return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
32
+ return /* @__PURE__ */ jsxRuntime.jsx(PersonalizationContext.Provider, { value: context, children: props.renderDefault(props) });
34
33
  }
35
34
  const ArrayInput = (props) => {
36
- const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
37
- async (variant) => {
35
+ const { onItemAppend, segmentName, segmentId } = props, { segments } = usePersonalizationContext(), handleClick = react.useCallback(
36
+ async (segment) => {
38
37
  const item = {
39
38
  _key: uuid.uuid(),
40
- [variantId]: variant.id,
41
- [experimentId]: experimentValue,
42
- _type: variantName
39
+ [segmentId]: segment.id,
40
+ _type: segmentName
43
41
  };
44
42
  onItemAppend(item);
45
43
  },
46
- [variantId, experimentId, experimentValue, variantName, onItemAppend]
47
- ), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
44
+ [segmentId, segmentName, onItemAppend]
45
+ ), usedSegments = (props.value || [])?.map((segment) => segment[segmentId]);
48
46
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
49
47
  props.renderDefault({ ...props, arrayFunctions: () => null }),
50
- /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
48
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: segments.map((segment) => /* @__PURE__ */ jsxRuntime.jsx(
51
49
  ui.Button,
52
50
  {
53
- text: `Add ${variant.label}`,
51
+ text: `Add ${segment.label}`,
54
52
  mode: "ghost",
55
- disabled: usedVariants?.includes(variant.id),
56
- onClick: () => handleClick(variant)
53
+ disabled: usedSegments?.includes(segment.id),
54
+ onClick: () => handleClick(segment)
57
55
  },
58
- `${experimentValue}-${variant.id}`
56
+ `${segment.id}`
59
57
  )) })
60
58
  ] });
61
- }, CloseIcon = /* @__PURE__ */ react.forwardRef(function(props, ref) {
62
- return /* @__PURE__ */ jsxRuntime.jsx(
63
- "svg",
64
- {
65
- "data-sanity-icon": "close",
66
- width: "1em",
67
- height: "1em",
68
- viewBox: "0 0 25 25",
69
- fill: "none",
70
- xmlns: "http://www.w3.org/2000/svg",
71
- ...props,
72
- ref,
73
- children: /* @__PURE__ */ jsxRuntime.jsx(
74
- "path",
75
- {
76
- d: "M18 7L7 18M7 7L18 18",
77
- stroke: "currentColor",
78
- strokeWidth: 1.2,
79
- strokeLinejoin: "round"
80
- }
81
- )
82
- }
83
- );
84
- }), useAddExperimentAction = (props) => {
85
- const { onChange, active, experimentNameOverride } = props, handleAddAction = react.useCallback(() => {
59
+ }, useAddExperimentAction = (props) => {
60
+ const { onChange, active, personalizationNameOverride } = props, handleAddAction = react.useCallback(() => {
86
61
  onChange([sanity.set(!active, ["active"])]);
87
62
  }, [onChange, active]);
88
63
  return {
89
- title: `Add ${experimentNameOverride}`,
64
+ title: `Add ${personalizationNameOverride}`,
90
65
  type: "action",
91
- icon: gi.GiSoapExperiment,
66
+ icon: io.IoMdPeople,
92
67
  onAction: handleAddAction,
93
68
  renderAsButton: !0
94
69
  };
95
70
  }, useRemoveExperimentAction = (props) => {
96
- const { onChange, active, experimentId, experimentNameOverride, variantNameOverride } = props, handleClearAction = react.useCallback(() => {
97
- const activeId = ["active"], experiment = [experimentId], variants = [`${variantNameOverride}s`];
98
- onChange([sanity.set(!active, activeId), sanity.unset(experiment), sanity.unset(variants)]);
99
- }, [onChange, active, experimentId, variantNameOverride]);
71
+ const { onChange, active, personalizationNameOverride, segmentNameOverride } = props, handleClearAction = react.useCallback(() => {
72
+ const activeId = ["active"], segments = [`${segmentNameOverride}s`];
73
+ onChange([sanity.set(!active, activeId), sanity.unset(segments)]);
74
+ }, [onChange, active, segmentNameOverride]);
100
75
  return {
101
- title: `Remove ${experimentNameOverride}`,
76
+ title: `Remove ${personalizationNameOverride}`,
102
77
  type: "action",
103
- icon: CloseIcon,
78
+ icon: fieldExperiments.CloseIcon,
104
79
  onAction: handleClearAction,
105
80
  renderAsButton: !0
106
81
  };
@@ -108,115 +83,57 @@ const ArrayInput = (props) => {
108
83
  onChange,
109
84
  inputId,
110
85
  active,
111
- experimentNameOverride,
112
- experimentId,
113
- variantNameOverride
86
+ personalizationNameOverride,
87
+ segmentNameOverride
114
88
  }) => {
115
89
  const removeAction = sanity.defineDocumentFieldAction({
116
- name: `Remove ${experimentNameOverride}`,
90
+ name: `Remove ${personalizationNameOverride}`,
117
91
  useAction: (props) => useRemoveExperimentAction({
118
92
  active: !0,
119
93
  onChange,
120
- experimentNameOverride,
121
- experimentId,
122
- variantNameOverride
94
+ personalizationNameOverride,
95
+ segmentNameOverride
123
96
  })
124
97
  }), addAction = sanity.defineDocumentFieldAction({
125
- name: `Add ${experimentNameOverride}`,
98
+ name: `Add ${personalizationNameOverride}`,
126
99
  useAction: (props) => useAddExperimentAction({
127
100
  active: !1,
128
101
  onChange,
129
- experimentNameOverride
102
+ personalizationNameOverride
130
103
  })
131
104
  });
132
105
  return active ? removeAction : addAction;
133
- }, ExperimentField = (props) => {
134
- const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId, variantNameOverride } = props, active = props.value?.active, actionProps = react.useMemo(
106
+ }, Field = (props) => {
107
+ const { onChange } = props.inputProps, { inputId, personalizationNameOverride, segmentNameOverride } = props, active = props.value?.active, actionProps = react.useMemo(
135
108
  () => ({
136
109
  onChange,
137
110
  inputId,
138
111
  active,
139
- experimentNameOverride,
140
- experimentId,
141
- variantNameOverride
112
+ personalizationNameOverride,
113
+ segmentNameOverride
142
114
  }),
143
- [onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
115
+ [onChange, inputId, active, personalizationNameOverride, segmentNameOverride]
144
116
  ), memoizedActions = react.useMemo(() => {
145
117
  const oldActions = props.actions || [];
146
118
  return [createActions(actionProps), ...oldActions];
147
- }, [actionProps, props.actions]), withActionProps = react.useMemo(
148
- () => ({
149
- ...props,
150
- actions: memoizedActions
151
- }),
152
- [props, memoizedActions]
153
- );
154
- return props.renderDefault(withActionProps);
155
- }, Select = (props) => {
156
- const {
157
- value,
158
- // Current field value
159
- onChange,
160
- // Method to handle patch events,
161
- elementProps,
162
- listOptions,
163
- handleChange
164
- } = props;
165
- return /* @__PURE__ */ jsxRuntime.jsxs(
166
- ui.Select,
167
- {
168
- ...elementProps,
169
- fontSize: 2,
170
- padding: 3,
171
- space: [3, 3, 4],
172
- value: value || "",
173
- onChange: (event) => handleChange(event, onChange),
174
- children: [
175
- /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", children: "Select an option..." }),
176
- listOptions.map(({ value: optionValue, title }) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: optionValue, children: title }, optionValue))
177
- ]
178
- }
179
- );
180
- }, formatlistOptions = (experiments) => experiments.map((experiment) => ({
181
- title: experiment.label,
182
- value: experiment.id
183
- })), ExperimentInput = (props) => {
184
- const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), additionalChangePath = react.useMemo(
185
- () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
186
- [props.variantNameOverride, props.path]
187
- ), subValues = sanity.useFormValue(additionalChangePath), { patch } = sanity.useDocumentOperation(sanity.getPublishedId(id), props.schemaType.name), handleChange = react.useCallback(
188
- (event, onChange) => {
189
- const inputValue = event.currentTarget.value;
190
- if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
191
- const patchEvent = {
192
- unset: [additionalChangePath.join(".")]
193
- };
194
- patch.execute([patchEvent]);
195
- }
196
- },
197
- [patch, subValues, additionalChangePath]
198
- );
199
- return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { padding: [3, 3, 4], radius: 2, shadow: 1, tone: "caution", children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { align: "center", size: [2, 2, 3], children: [
200
- "There are no defined ",
201
- props.experimentNameOverride,
202
- "s"
203
- ] }) });
204
- }, ExperimentItem = (props) => {
205
- const { active } = props.value;
206
- return active || props.inputProps.onChange(sanity.set(!0, ["active"])), props.renderDefault(props);
207
- }, VariantInput = (props) => {
208
- const experimentPath = props.path.slice(0, -2), defaultValue = sanity.useFormValue([...experimentPath, "default"]), handleClick = () => {
119
+ }, [actionProps, props.actions]), enhancedProps = react.useMemo(() => ({
120
+ ...fieldExperiments.clearChildrenGroups(props),
121
+ actions: memoizedActions
122
+ }), [props, memoizedActions]);
123
+ return props.renderDefault(enhancedProps);
124
+ }, SegmentInput = (props) => {
125
+ const personalizationPath = props.path.slice(0, -2), defaultValue = sanity.useFormValue([...personalizationPath, "default"]), handleClick = () => {
209
126
  props.onChange(sanity.set(defaultValue, ["value"]));
210
127
  };
211
128
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
212
129
  props.renderDefault(props),
213
130
  /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
214
131
  ] });
215
- }, VariantPreview = (props) => {
216
- const [subtitle, setSubtitle] = react.useState(void 0), [title, setTitle] = react.useState(void 0), [media, setMedia] = react.useState(void 0), client = sanity.useClient({ apiVersion: "2025-01-01" }), { experiments } = useExperimentContext(), { experiment, variant, value } = props, selectedExperiment = experiments.find((experimentItem) => experimentItem.id === experiment), selectedVariant = selectedExperiment?.variants.find((variantItem) => variantItem.id === variant);
132
+ }, SegmentPreview = (props) => {
133
+ const [subtitle, setSubtitle] = react.useState(void 0), [title, setTitle] = react.useState(void 0), [media, setMedia] = react.useState(void 0), client = sanity.useClient({ apiVersion: "2025-01-01" }), { segments } = usePersonalizationContext(), { segment, value } = props, selectedSegment = segments.find((segmentItem) => segmentItem.id === segment);
217
134
  react.useEffect(() => {
218
135
  (async () => {
219
- if (setTitle(`${selectedExperiment?.label} - ${selectedVariant?.label}`), typeof value == "string")
136
+ if (setTitle(`${selectedSegment?.label}`), typeof value == "string")
220
137
  return setSubtitle(value);
221
138
  if (sanity.isReference(value)) {
222
139
  const doc = await client.getDocument(value._ref), referenceType = (props.schemaType.fields.find((field) => field.name === "value")?.type).to.find((field) => field.type?.name === doc?._type), selectFields = {}, previewFields = referenceType?.preview?.select || {};
@@ -229,7 +146,7 @@ const ArrayInput = (props) => {
229
146
  }
230
147
  return sanity.isImage(value) && setMedia(value), "";
231
148
  })();
232
- }, [value, client, selectedExperiment?.label, selectedVariant?.label, props.schemaType]);
149
+ }, [value, client, selectedSegment?.label, props.schemaType]);
233
150
  const previewProps = {
234
151
  ...props,
235
152
  title,
@@ -237,65 +154,59 @@ const ArrayInput = (props) => {
237
154
  media
238
155
  };
239
156
  return props.renderDefault(previewProps);
240
- };
241
- function flattenSchemaType(schemaType) {
242
- return sanity.isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 5) : (console.error("Schema type is not a document"), []);
243
- }
244
- function extractInnerFields(fields, path, maxDepth) {
245
- return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
246
- const thisFieldWithPath = { path: [...path, field.name], ...field };
247
- if (field.type.jsonType === "object") {
248
- const innerFields = extractInnerFields(field.type.fields, [...path, field.name], maxDepth);
249
- return [...acc, thisFieldWithPath, ...innerFields];
250
- } else if (field.type.jsonType === "array") {
251
- const innerFields = (field.type.of || []).reduce((arrayAcc, arrayType) => {
252
- if ("fields" in arrayType) {
253
- const typeFields = extractInnerFields(arrayType.fields, [...path, field.name], maxDepth);
254
- return [...arrayAcc, ...typeFields];
255
- }
256
- return arrayAcc;
257
- }, []);
258
- return [...acc, thisFieldWithPath, ...innerFields];
259
- }
260
- return [...acc, thisFieldWithPath];
261
- }, []);
262
- }
263
- const createExperimentType = ({
157
+ }, createPersonalizationType = ({
264
158
  field,
265
- experimentNameOverride,
266
- variantNameOverride,
267
- variantId,
268
- variantArrayName,
269
- experimentId
159
+ personalizationNameOverride,
160
+ segmentNameOverride,
161
+ segmentId,
162
+ segmentArrayName
270
163
  }) => {
271
- const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
164
+ const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), segmentName = `${segmentNameOverride}${usedName}`;
272
165
  return sanity.defineType({
273
- name: `${experimentNameOverride}${usedName}`,
166
+ name: `${personalizationNameOverride}${usedName}`,
274
167
  type: "object",
168
+ groups: [
169
+ {
170
+ name: "default",
171
+ title: "Default",
172
+ hidden: ({ parent }) => !Array.isArray(parent)
173
+ },
174
+ {
175
+ name: "personalization",
176
+ title: "Personalization options",
177
+ hidden: ({ parent }) => !Array.isArray(parent)
178
+ },
179
+ {
180
+ name: "all-fields",
181
+ title: "All fields",
182
+ hidden: ({ parent }) => Array.isArray(parent)
183
+ }
184
+ ],
275
185
  components: {
276
186
  field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
277
- ExperimentField,
187
+ Field,
278
188
  {
279
189
  ...props,
280
- experimentId,
281
- experimentNameOverride,
282
- variantNameOverride
190
+ personalizationNameOverride,
191
+ segmentNameOverride
283
192
  }
284
193
  ),
285
- item: ExperimentItem
194
+ item: fieldExperiments.ArrayItem
286
195
  },
287
196
  fields: [
288
197
  typeof field == "string" ? (
289
198
  // Define a simple field if all we have is the name as a string
290
199
  sanity.defineField({
291
200
  name: "default",
292
- type: field
201
+ type: field,
202
+ group: "default"
293
203
  })
294
204
  ) : (
295
205
  // Pass in the configured options, but overwrite the name
296
206
  {
297
207
  ...field,
298
- name: "default"
208
+ name: "default",
209
+ group: "default"
299
210
  }
300
211
  ),
301
212
  sanity.defineField({
@@ -305,84 +216,54 @@ const createExperimentType = ({
305
216
  initialValue: !1
306
217
  }),
307
218
  sanity.defineField({
308
- name: experimentId,
309
- type: "string",
310
- components: {
311
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
312
- ExperimentInput,
313
- {
314
- ...props,
315
- experimentNameOverride,
316
- variantNameOverride
317
- }
318
- )
319
- },
320
- hidden: ({ parent }) => !parent?.active
321
- }),
322
- sanity.defineField({
323
- name: variantArrayName,
219
+ name: segmentArrayName,
324
220
  type: "array",
325
- hidden: ({ parent }) => !parent?.[experimentId],
221
+ hidden: ({ parent }) => !parent?.active,
222
+ group: "personalization",
326
223
  components: {
327
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
328
- ArrayInput,
329
- {
330
- ...props,
331
- variantName,
332
- variantId,
333
- experimentId
334
- }
335
- )
224
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ArrayInput, { ...props, segmentName, segmentId })
336
225
  },
337
226
  of: [
338
227
  sanity.defineField({
339
- name: variantName,
340
- type: variantName
228
+ name: segmentName,
229
+ type: segmentName
341
230
  })
342
231
  ]
343
232
  })
344
233
  ],
345
234
  preview: {
346
235
  select: {
347
- base: "default",
348
- experiment: experimentId
236
+ base: "default"
349
237
  },
350
- prepare: ({ base, experiment }) => {
351
- const title = base?.title || base?.name || typeof base == "string" ? base : "", experimentTitle = experiment ? `Experiment: ${experiment}` : "", media = base?.image || base?.photo || base?.media || "";
238
+ prepare: ({ base }) => {
239
+ const title = base?.title || base?.name || typeof base == "string" ? base : "", media = base?.image || base?.photo || base?.media || "";
352
240
  return {
353
- title: title || experimentTitle,
354
- subtitle: title ? experimentTitle : "",
241
+ title,
355
242
  media
356
243
  };
357
244
  }
358
245
  }
359
246
  });
360
- }, createVariantType = ({
247
+ }, createSegmentType = ({
361
248
  field,
362
- variantNameOverride,
363
- variantId,
364
- experimentId
249
+ segmentNameOverride,
250
+ segmentId
365
251
  }) => {
366
252
  const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
367
253
  return sanity.defineType({
368
- name: `${variantNameOverride}${usedName}`,
369
- title: `${variantNameOverride} array ${usedName}`,
254
+ name: `${segmentNameOverride}${usedName}`,
255
+ title: `${segmentNameOverride} array ${usedName}`,
370
256
  type: "object",
371
257
  components: {
372
- preview: VariantPreview,
373
- input: VariantInput
258
+ preview: SegmentPreview,
259
+ input: SegmentInput
374
260
  },
375
261
  fields: [
376
262
  {
377
263
  type: "string",
378
- name: variantId,
264
+ name: segmentId,
379
265
  readOnly: !0
380
266
  },
381
- {
382
- type: "string",
383
- name: experimentId,
384
- hidden: !0
385
- },
386
267
  typeof field == "string" ? (
387
268
  // Define a simple field if all we have is the name as a string
388
269
  sanity.defineField({
@@ -401,69 +282,65 @@ const createExperimentType = ({
401
282
  ],
402
283
  preview: {
403
284
  select: {
404
- variant: variantId,
405
- experiment: experimentId,
285
+ segment: segmentId,
406
286
  value: "value"
407
287
  }
408
288
  }
409
289
  });
410
290
  }, fieldSchema = ({
411
291
  fields,
412
- experimentNameOverride,
413
- variantNameOverride,
414
- variantId,
415
- variantArrayName,
416
- experimentId
292
+ personalizationNameOverride,
293
+ segmentNameOverride,
294
+ segmentId,
295
+ segmentArrayName
417
296
  }) => [
297
+ ...fields.map((field) => createSegmentType({ field, segmentNameOverride, segmentId })),
418
298
  ...fields.map(
419
- (field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
420
- ),
421
- ...fields.map(
422
- (field) => createExperimentType({
299
+ (field) => createPersonalizationType({
423
300
  field,
424
- experimentNameOverride,
425
- variantNameOverride,
426
- variantId,
427
- variantArrayName,
428
- experimentId
301
+ personalizationNameOverride,
302
+ segmentNameOverride,
303
+ segmentId,
304
+ segmentArrayName
429
305
  })
430
306
  )
431
- ], fieldLevelExperiments = sanity.definePlugin((config) => {
432
- const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
433
- return {
434
- name: "sanity-personalistaion-plugin-field-level-experiments",
435
- schema: {
436
- types: fieldSchema({
437
- fields,
438
- experimentNameOverride,
439
- variantNameOverride,
440
- variantId,
441
- variantArrayName,
442
- experimentId
443
- })
444
- },
445
- form: {
446
- components: {
447
- input: (props) => {
448
- if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).some(
449
- (field) => field.type.name.startsWith(experimentNameOverride) || field.name.startsWith(experimentNameOverride)
450
- ))
451
- return props.renderDefault(props);
452
- const providerProps = {
453
- ...props,
454
- experimentFieldPluginConfig: {
455
- ...pluginConfig,
456
- variantId,
457
- variantArrayName,
458
- experimentId
459
- }
460
- };
461
- return ExperimentProvider(providerProps);
307
+ ], fieldLevelPersonalization = sanity.definePlugin(
308
+ (config) => {
309
+ const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, personalizationNameOverride, segmentNameOverride } = pluginConfig, segmentArrayName = `${segmentNameOverride}s`, segmentId = `${segmentNameOverride}Id`;
310
+ return {
311
+ name: "sanity-personalistaion-plugin-field-level-personalization",
312
+ schema: {
313
+ types: fieldSchema({
314
+ fields,
315
+ personalizationNameOverride,
316
+ segmentNameOverride,
317
+ segmentId,
318
+ segmentArrayName
319
+ })
320
+ },
321
+ form: {
322
+ components: {
323
+ input: (props) => {
324
+ if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !fieldExperiments.flattenSchemaType(props.schemaType).some(
325
+ (field) => field.type.name.startsWith(personalizationNameOverride) || field.name.startsWith(personalizationNameOverride)
326
+ ))
327
+ return props.renderDefault(props);
328
+ const providerProps = {
329
+ ...props,
330
+ personalizationFieldPluginConfig: {
331
+ ...pluginConfig,
332
+ segmentId,
333
+ segmentArrayName
334
+ }
335
+ };
336
+ return PersonalizationProvider(providerProps);
337
+ }
462
338
  }
463
339
  }
464
- }
465
- };
466
- });
467
- exports.fieldLevelExperiments = fieldLevelExperiments;
468
- exports.flattenSchemaType = flattenSchemaType;
340
+ };
341
+ }
342
+ );
343
+ exports.fieldLevelExperiments = fieldExperiments.fieldLevelExperiments;
344
+ exports.flattenSchemaType = fieldExperiments.flattenSchemaType;
345
+ exports.fieldLevelPersonalization = fieldLevelPersonalization;
469
346
  //# sourceMappingURL=index.js.map