@sanity/personalization-plugin 2.1.0-growthbook.1 → 2.2.0-growthbook.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/README.md CHANGED
@@ -85,8 +85,8 @@ export default defineConfig({
85
85
 
86
86
  This will register two new fields to the schema., based on the setting passed intto `fields:`
87
87
 
88
- - `experimentString` an Object field with `string` field called `default`, a `string` field called `experimentId` and an array field of type:
89
- - `varirantsString` an object field with a `string` field called `value`, a string field called `variantId`, a `string` field called `experimentId`.
88
+ - `experimentString` an Object field with `string` field called `default`, a `string` field called `experimentId` and an array field called `variants` of type:
89
+ - `variantString` an object field with a `string` field called `value`, a string field called `variantId`, a `string` field called `experimentId`.
90
90
 
91
91
  Use the experiment field in your schema like this:
92
92
 
@@ -202,7 +202,7 @@ export default defineConfig({
202
202
 
203
203
  This would also create two new fields in your schema.
204
204
 
205
- - `experimentFeaturedProduct` an Object field with `reference` field called `default`, a `string` field called `experimentId` and an array field of type:
205
+ - `experimentFeaturedProduct` an Object field with `reference` field called `default`, a `string` field called `experimentId` and an array field called `variants` of type:
206
206
  - `variantFeaturedProduct` an object field with a `reference` field called `value`, a string field called `variandId`, a `string` field called `experimentId`.
207
207
 
208
208
  Note that the name key in the field gets rewritten to value and is instead used to name the object field.
@@ -242,7 +242,7 @@ The custom input contains buttons which will add new array items with the experi
242
242
  ```json
243
243
  "title": {
244
244
  "default": "asdf",
245
- "experimentValue": "test-1",
245
+ "experimentId": "test-1",
246
246
  "variants": [
247
247
  {
248
248
  "experimentId": "test-1",
@@ -267,6 +267,62 @@ Using GROQ filters you can query for a specific experitment, with a fallback to
267
267
  }
268
268
  ```
269
269
 
270
+ ## Overwriting the experiment and variant field names
271
+
272
+ If your use case does not match exactly with experiments you can overwrite the name field names for experiment and variant in the config.
273
+
274
+ ```ts
275
+ import {defineConfig} from 'sanity'
276
+ import {fieldLevelExperiments} from '@sanity/personalization-plugin'
277
+
278
+ export default defineConfig({
279
+ //...
280
+ plugins: [
281
+ //...
282
+ fieldLevelExperiments({
283
+ fields: ['string'],
284
+ experiments: [experiment1, experiment2],
285
+ experimentNameOverride: 'audience',
286
+ variantNameOverride: 'segment',
287
+ }),
288
+ ],
289
+ })
290
+ ```
291
+
292
+ This would also create two new fields in your schema.
293
+
294
+ - `audienceString` an Object field with `string` field called `default`, a `string` field called `audienceId` and an array field called `segments` of type:
295
+ - `segmentString` an object field with a `string` field called `value`, a string field called `segmentId`, a `string` field called `audienceId`.
296
+
297
+ the data will be stored as
298
+
299
+ ```json
300
+ "title": {
301
+ "default": "asdf",
302
+ "audienceId": "test-1",
303
+ "segments": [
304
+ {
305
+ "audienceId": "test-1",
306
+ "value": "asdf",
307
+ "segmentId": "test-1-a"
308
+ },
309
+ {
310
+ "audienceId": "test-1",
311
+ "segmentId": "test-1-b",
312
+ "value": "qwer"
313
+ }
314
+ ]
315
+ }
316
+ ```
317
+
318
+ This will also affect the query you write to fetch data to be:
319
+
320
+ ```ts
321
+ *[_type == "post"] {
322
+ "title":coalesce(title.segments[audienceId == $audience && segmentId == $segment][0].value, title.default),
323
+ }
324
+ ```
325
+
270
326
  ## License
271
327
 
272
328
  [MIT](LICENSE) © Jon Burbridge
package/dist/index.d.mts CHANGED
@@ -10,7 +10,9 @@ import {SchemaType} from 'sanity'
10
10
  import {SetStateAction} from 'react'
11
11
 
12
12
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
- objectName: string
13
+ variantName: string
14
+ variantId: string
15
+ experimentId: string
14
16
  }
15
17
 
16
18
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
@@ -23,11 +25,15 @@ export declare type ExperimentGeneric<T> = {
23
25
  _type: string
24
26
  default?: T
25
27
  experimentValue?: string
26
- variants?: Array<
27
- {
28
- _key: string
29
- } & VariantGeneric<T>
30
- >
28
+ [key: string]:
29
+ | Array<
30
+ {
31
+ _key: string
32
+ } & VariantGeneric<T>
33
+ >
34
+ | string
35
+ | T
36
+ | undefined
31
37
  }
32
38
 
33
39
  export declare type ExperimentType = {
@@ -44,6 +50,11 @@ export declare type FieldPluginConfig = {
44
50
  | ExperimentType[]
45
51
  | ((client: SanityClient, secret?: string) => Promise<ExperimentType[]>)
46
52
  apiVersion?: string
53
+ experimentNameOverride?: string
54
+ variantNameOverride?: string
55
+ variantId?: string
56
+ variantArrayName?: string
57
+ experimentId?: string
47
58
  }
48
59
 
49
60
  /**
@@ -254,15 +265,13 @@ export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
254
265
  }
255
266
 
256
267
  export declare type VariantGeneric<T> = {
268
+ [key: string]: string | T | undefined
257
269
  _type: string
258
- variantId?: string
259
- experimentId?: string
260
270
  value?: T
261
271
  }
262
272
 
263
273
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
264
- experiment: string
265
- variant: string
274
+ [key: string]: string
266
275
  value: any
267
276
  }
268
277
 
package/dist/index.d.ts CHANGED
@@ -10,7 +10,9 @@ import {SchemaType} from 'sanity'
10
10
  import {SetStateAction} from 'react'
11
11
 
12
12
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
13
- objectName: string
13
+ variantName: string
14
+ variantId: string
15
+ experimentId: string
14
16
  }
15
17
 
16
18
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
@@ -23,11 +25,15 @@ export declare type ExperimentGeneric<T> = {
23
25
  _type: string
24
26
  default?: T
25
27
  experimentValue?: string
26
- variants?: Array<
27
- {
28
- _key: string
29
- } & VariantGeneric<T>
30
- >
28
+ [key: string]:
29
+ | Array<
30
+ {
31
+ _key: string
32
+ } & VariantGeneric<T>
33
+ >
34
+ | string
35
+ | T
36
+ | undefined
31
37
  }
32
38
 
33
39
  export declare type ExperimentType = {
@@ -44,6 +50,11 @@ export declare type FieldPluginConfig = {
44
50
  | ExperimentType[]
45
51
  | ((client: SanityClient, secret?: string) => Promise<ExperimentType[]>)
46
52
  apiVersion?: string
53
+ experimentNameOverride?: string
54
+ variantNameOverride?: string
55
+ variantId?: string
56
+ variantArrayName?: string
57
+ experimentId?: string
47
58
  }
48
59
 
49
60
  /**
@@ -254,15 +265,13 @@ export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
254
265
  }
255
266
 
256
267
  export declare type VariantGeneric<T> = {
268
+ [key: string]: string | T | undefined
257
269
  _type: string
258
- variantId?: string
259
- experimentId?: string
260
270
  value?: T
261
271
  }
262
272
 
263
273
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
264
- experiment: string
265
- variant: string
274
+ [key: string]: string
266
275
  value: any
267
276
  }
268
277
 
package/dist/index.js CHANGED
@@ -7,7 +7,12 @@ function _interopDefaultCompat(e) {
7
7
  var equal__default = /* @__PURE__ */ _interopDefaultCompat(equal);
8
8
  const CONFIG_DEFAULT = {
9
9
  fields: [],
10
- apiVersion: "2024-11-07"
10
+ apiVersion: "2024-11-07",
11
+ experimentNameOverride: "experiment",
12
+ variantNameOverride: "variant",
13
+ variantId: "variantId",
14
+ variantArrayName: "variants",
15
+ experimentId: "experimentId"
11
16
  }, ExperimentContext = react.createContext({
12
17
  ...CONFIG_DEFAULT,
13
18
  experiments: [],
@@ -31,18 +36,18 @@ function ExperimentProvider(props) {
31
36
  return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
32
37
  }
33
38
  const ArrayInput = (props) => {
34
- const fieldPath = props.path.slice(0, -1), experimentId = sanity.useFormValue([...fieldPath, "experimentId"]), { experiments } = useExperimentContext(), { onItemAppend, objectName } = props, handleClick = react.useCallback(
39
+ const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
35
40
  async (variant) => {
36
41
  const item = {
37
42
  _key: uuid.uuid(),
38
- variantId: variant.id,
39
- experimentId,
40
- _type: objectName
43
+ [variantId]: variant.id,
44
+ [experimentId]: experimentValue,
45
+ _type: variantName
41
46
  };
42
47
  onItemAppend(item);
43
48
  },
44
- [experimentId, objectName, onItemAppend]
45
- ), filteredVariants = experiments.find((option) => option.id === experimentId)?.variants || [], usedVariants = props.value?.map((variant) => variant.variantId);
49
+ [variantId, experimentId, experimentValue, variantName, onItemAppend]
50
+ ), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
46
51
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
47
52
  props.renderDefault({ ...props, arrayFunctions: () => null }),
48
53
  /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
@@ -53,7 +58,7 @@ const ArrayInput = (props) => {
53
58
  disabled: usedVariants?.includes(variant.id),
54
59
  onClick: () => handleClick(variant)
55
60
  },
56
- `${experimentId}-${variant.id}`
61
+ `${experimentValue}-${variant.id}`
57
62
  )) })
58
63
  ] });
59
64
  }, AccessDeniedIcon = react.forwardRef(function(props, ref) {
@@ -6403,41 +6408,75 @@ const icons = {
6403
6408
  });
6404
6409
  Icon.displayName = "ForwardRef(Icon)";
6405
6410
  const useAddExperimentAction = (props) => {
6406
- const patchActiveEvent = react.useMemo(() => sanity.set(!0, ["active"]), []), handleAction = react.useCallback(() => {
6407
- props.onChange([patchActiveEvent]);
6408
- }, [patchActiveEvent, props]);
6411
+ const { onChange, active, experimentNameOverride } = props, handleAddAction = react.useCallback(() => {
6412
+ onChange([sanity.set(!active, ["active"])]);
6413
+ }, [onChange, active]);
6409
6414
  return {
6410
- title: "Add experiment",
6415
+ title: `Add ${experimentNameOverride}`,
6411
6416
  type: "action",
6412
6417
  icon: gi.GiSoapExperiment,
6413
- onAction: handleAction,
6418
+ onAction: handleAddAction,
6414
6419
  renderAsButton: !0
6415
6420
  };
6416
6421
  }, useRemoveExperimentAction = (props) => {
6417
- const patchActiveEvent = react.useMemo(() => sanity.set(!1, ["active"]), []), patchClearEvent = react.useMemo(() => {
6418
- const experimentId = ["experimentId"], variants = ["variants"];
6419
- return [sanity.unset(experimentId), sanity.unset(variants)];
6420
- }, []), handleAction = react.useCallback(() => {
6421
- props.onChange([patchActiveEvent, ...patchClearEvent]);
6422
- }, [patchActiveEvent, patchClearEvent, props]);
6422
+ const { onChange, active, experimentId, experimentNameOverride, variantNameOverride } = props, handleClearAction = react.useCallback(() => {
6423
+ const activeId = ["active"], experiment = [experimentId], variants = [`${variantNameOverride}s`];
6424
+ onChange([sanity.set(!active, activeId), sanity.unset(experiment), sanity.unset(variants)]);
6425
+ }, [onChange, active, experimentId, variantNameOverride]);
6423
6426
  return {
6424
- title: "Remove experiment",
6427
+ title: `Remove ${experimentNameOverride}`,
6425
6428
  type: "action",
6426
6429
  icon: CloseIcon,
6427
- onAction: handleAction,
6430
+ onAction: handleClearAction,
6428
6431
  renderAsButton: !0
6429
6432
  };
6430
- }, newActions = ({ onChange, inputId, active }) => active ? sanity.defineDocumentFieldAction({
6431
- name: "Experiment",
6432
- useAction: (props) => useRemoveExperimentAction({ ...props, onChange, inputId })
6433
- }) : sanity.defineDocumentFieldAction({
6434
- name: "Experiment",
6435
- useAction: (props) => useAddExperimentAction({ ...props, onChange, inputId })
6436
- }), ExperimentField = (props) => {
6437
- const { onChange } = props.inputProps, { inputId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
6438
- ...props,
6439
- actions: [newActions({ onChange, inputId, active }), ...oldActions]
6440
- };
6433
+ }, createActions = ({
6434
+ onChange,
6435
+ inputId,
6436
+ active,
6437
+ experimentNameOverride,
6438
+ experimentId,
6439
+ variantNameOverride
6440
+ }) => {
6441
+ const removeAction = sanity.defineDocumentFieldAction({
6442
+ name: `Remove ${experimentNameOverride}`,
6443
+ useAction: (props) => useRemoveExperimentAction({
6444
+ active: !0,
6445
+ onChange,
6446
+ experimentNameOverride,
6447
+ experimentId,
6448
+ variantNameOverride
6449
+ })
6450
+ }), addAction = sanity.defineDocumentFieldAction({
6451
+ name: `Add ${experimentNameOverride}`,
6452
+ useAction: (props) => useAddExperimentAction({
6453
+ active: !1,
6454
+ onChange,
6455
+ experimentNameOverride
6456
+ })
6457
+ });
6458
+ return active ? removeAction : addAction;
6459
+ }, ExperimentField = (props) => {
6460
+ const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId, variantNameOverride } = props, active = props.value?.active, actionProps = react.useMemo(
6461
+ () => ({
6462
+ onChange,
6463
+ inputId,
6464
+ active,
6465
+ experimentNameOverride,
6466
+ experimentId,
6467
+ variantNameOverride
6468
+ }),
6469
+ [onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
6470
+ ), memoizedActions = react.useMemo(() => {
6471
+ const oldActions = props.actions || [];
6472
+ return [createActions(actionProps), ...oldActions];
6473
+ }, [actionProps, props.actions]), withActionProps = react.useMemo(
6474
+ () => ({
6475
+ ...props,
6476
+ actions: memoizedActions
6477
+ }),
6478
+ [props, memoizedActions]
6479
+ );
6441
6480
  return props.renderDefault(withActionProps);
6442
6481
  }, Select = (props) => {
6443
6482
  const {
@@ -6468,7 +6507,10 @@ const useAddExperimentAction = (props) => {
6468
6507
  title: experiment.label,
6469
6508
  value: experiment.id
6470
6509
  })), ExperimentInput = (props) => {
6471
- const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(() => [...props.path.slice(0, -1), "variants"], [props.path]), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
6510
+ const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
6511
+ () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
6512
+ [props.variantNameOverride, props.path]
6513
+ ), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
6472
6514
  (event, onChange) => {
6473
6515
  const inputValue = event.currentTarget.value;
6474
6516
  if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
@@ -6481,6 +6523,14 @@ const useAddExperimentAction = (props) => {
6481
6523
  [patch, subValues, aditionalChangePath]
6482
6524
  );
6483
6525
  return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
6526
+ }, VariantInput = (props) => {
6527
+ const defaultValue = sanity.useFormValue([props.path[0], "default"]), handleClick = () => {
6528
+ props.onChange(sanity.set(defaultValue, ["value"]));
6529
+ };
6530
+ return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
6531
+ props.renderDefault(props),
6532
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
6533
+ ] });
6484
6534
  }, VariantPreview = (props) => {
6485
6535
  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);
6486
6536
  react.useEffect(() => {
@@ -6528,15 +6578,28 @@ function extractInnerFields(fields, path, maxDepth) {
6528
6578
  return [...acc, thisFieldWithPath];
6529
6579
  }, []);
6530
6580
  }
6531
- const createFieldType = ({
6532
- field
6581
+ const createExperimentType = ({
6582
+ field,
6583
+ experimentNameOverride,
6584
+ variantNameOverride,
6585
+ variantId,
6586
+ variantArrayName,
6587
+ experimentId
6533
6588
  }) => {
6534
- const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), objectName = `variant${usedName}`;
6589
+ const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
6535
6590
  return sanity.defineType({
6536
- name: `experiment${usedName}`,
6591
+ name: `${experimentNameOverride}${usedName}`,
6537
6592
  type: "object",
6538
6593
  components: {
6539
- field: ExperimentField
6594
+ field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
6595
+ ExperimentField,
6596
+ {
6597
+ ...props,
6598
+ experimentId,
6599
+ experimentNameOverride,
6600
+ variantNameOverride
6601
+ }
6602
+ )
6540
6603
  },
6541
6604
  fields: [
6542
6605
  typeof field == "string" ? (
@@ -6558,94 +6621,140 @@ const createFieldType = ({
6558
6621
  hidden: !0
6559
6622
  }),
6560
6623
  sanity.defineField({
6561
- title: "Experiment",
6562
- name: "experimentId",
6624
+ name: experimentId,
6563
6625
  type: "string",
6564
6626
  components: {
6565
- input: ExperimentInput
6627
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ExperimentInput, { ...props, variantNameOverride })
6566
6628
  },
6567
6629
  hidden: ({ parent }) => !parent?.active
6568
6630
  }),
6569
6631
  sanity.defineField({
6570
- name: "variants",
6632
+ name: variantArrayName,
6571
6633
  type: "array",
6572
- hidden: ({ parent }) => !parent?.experimentId,
6634
+ hidden: ({ parent }) => !parent?.[experimentId],
6573
6635
  components: {
6574
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ArrayInput, { ...props, objectName })
6636
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
6637
+ ArrayInput,
6638
+ {
6639
+ ...props,
6640
+ variantName,
6641
+ variantId,
6642
+ experimentId
6643
+ }
6644
+ )
6575
6645
  },
6576
6646
  of: [
6577
6647
  sanity.defineField({
6578
- name: objectName,
6579
- type: objectName
6648
+ name: variantName,
6649
+ type: variantName
6580
6650
  })
6581
6651
  ]
6582
6652
  })
6583
6653
  ]
6584
6654
  });
6585
- }, createFieldObjectType = ({
6586
- field
6655
+ }, createVariantType = ({
6656
+ field,
6657
+ variantNameOverride,
6658
+ variantId,
6659
+ experimentId
6587
6660
  }) => {
6588
6661
  const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
6589
6662
  return sanity.defineType({
6590
- name: `variant${usedName}`,
6591
- title: `Experiment array ${usedName}`,
6663
+ name: `${variantNameOverride}${usedName}`,
6664
+ title: `${variantNameOverride} array ${usedName}`,
6592
6665
  type: "object",
6593
6666
  components: {
6594
- preview: VariantPreview
6667
+ preview: VariantPreview,
6668
+ input: VariantInput
6595
6669
  },
6596
6670
  fields: [
6597
6671
  {
6598
6672
  type: "string",
6599
- name: "variantId",
6673
+ name: variantId,
6600
6674
  readOnly: !0
6601
6675
  },
6602
6676
  {
6603
6677
  type: "string",
6604
- name: "experimentId",
6678
+ name: experimentId,
6605
6679
  hidden: !0
6606
6680
  },
6607
6681
  typeof field == "string" ? (
6608
6682
  // Define a simple field if all we have is the name as a string
6609
6683
  sanity.defineField({
6610
6684
  name: "value",
6611
- type: field,
6612
- hidden: ({ parent }) => !parent?.variantId
6685
+ type: field
6686
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
6613
6687
  })
6614
6688
  ) : (
6615
6689
  // Pass in the configured options, but overwrite the name
6616
6690
  {
6617
6691
  ...field,
6618
- name: "value",
6619
- hidden: ({ parent }) => !parent?.variantId
6692
+ name: "value"
6693
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
6620
6694
  }
6621
6695
  )
6622
6696
  ],
6623
6697
  preview: {
6624
6698
  select: {
6625
- variant: "variantId",
6626
- experiment: "experimentId",
6699
+ variant: variantId,
6700
+ experiment: experimentId,
6627
6701
  value: "value"
6628
6702
  }
6629
6703
  }
6630
6704
  });
6631
- }, fieldSchema = ({ fields, experiments }) => [
6632
- ...fields.map((field) => createFieldObjectType({ field })),
6633
- ...fields.map((field) => createFieldType({ field }))
6705
+ }, fieldSchema = ({
6706
+ fields,
6707
+ experimentNameOverride,
6708
+ variantNameOverride,
6709
+ variantId,
6710
+ variantArrayName,
6711
+ experimentId
6712
+ }) => [
6713
+ ...fields.map(
6714
+ (field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
6715
+ ),
6716
+ ...fields.map(
6717
+ (field) => createExperimentType({
6718
+ field,
6719
+ experimentNameOverride,
6720
+ variantNameOverride,
6721
+ variantId,
6722
+ variantArrayName,
6723
+ experimentId
6724
+ })
6725
+ )
6634
6726
  ], fieldLevelExperiments = sanity.definePlugin((config) => {
6635
- const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experiments } = pluginConfig;
6727
+ const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
6636
6728
  return {
6637
6729
  name: "sanity-personalistaion-plugin-field-level-experiments",
6638
6730
  schema: {
6639
- types: fieldSchema({ fields, experiments })
6731
+ types: fieldSchema({
6732
+ fields,
6733
+ experimentNameOverride,
6734
+ variantNameOverride,
6735
+ variantId,
6736
+ variantArrayName,
6737
+ experimentId
6738
+ })
6640
6739
  },
6641
6740
  form: {
6642
6741
  components: {
6643
6742
  input: (props) => {
6644
6743
  if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
6645
6744
  (field) => field.type.name
6646
- ).some((name) => name.startsWith("experiment")))
6745
+ ).some(
6746
+ (name) => name.startsWith(experimentNameOverride)
6747
+ ))
6647
6748
  return props.renderDefault(props);
6648
- const providerProps = { ...props, experimentFieldPluginConfig: pluginConfig };
6749
+ const providerProps = {
6750
+ ...props,
6751
+ experimentFieldPluginConfig: {
6752
+ ...pluginConfig,
6753
+ variantId,
6754
+ variantArrayName,
6755
+ experimentId
6756
+ }
6757
+ };
6649
6758
  return ExperimentProvider(providerProps);
6650
6759
  }
6651
6760
  }