@sanity/personalization-plugin 2.0.0 → 2.1.0-field-names.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/dist/index.d.mts CHANGED
@@ -8,7 +8,9 @@ import {SanityClient} from 'sanity'
8
8
  import {SchemaType} from 'sanity'
9
9
 
10
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
11
- objectName: string
11
+ variantName: string
12
+ variantId: string
13
+ experimentId: string
12
14
  }
13
15
 
14
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
@@ -19,11 +21,15 @@ export declare type ExperimentGeneric<T> = {
19
21
  _type: string
20
22
  default?: T
21
23
  experimentValue?: string
22
- variants?: Array<
23
- {
24
- _key: string
25
- } & VariantGeneric<T>
26
- >
24
+ [key: string]:
25
+ | Array<
26
+ {
27
+ _key: string
28
+ } & VariantGeneric<T>
29
+ >
30
+ | string
31
+ | T
32
+ | undefined
27
33
  }
28
34
 
29
35
  export declare type ExperimentType = {
@@ -38,6 +44,11 @@ export declare type FieldPluginConfig = {
38
44
  fields: (string | FieldDefinition)[]
39
45
  experiments: ExperimentType[] | ((client: SanityClient) => Promise<ExperimentType[]>)
40
46
  apiVersion?: string
47
+ experimentNameOverride?: string
48
+ variantNameOverride?: string
49
+ variantId?: string
50
+ variantArrayName?: string
51
+ experimentId?: string
41
52
  }
42
53
 
43
54
  /**
@@ -50,15 +61,13 @@ export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
50
61
  }
51
62
 
52
63
  export declare type VariantGeneric<T> = {
64
+ [key: string]: string | T | undefined
53
65
  _type: string
54
- variantId?: string
55
- experimentId?: string
56
66
  value?: T
57
67
  }
58
68
 
59
69
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
60
- experiment: string
61
- variant: string
70
+ [key: string]: string
62
71
  value: any
63
72
  }
64
73
 
package/dist/index.d.ts CHANGED
@@ -8,7 +8,9 @@ import {SanityClient} from 'sanity'
8
8
  import {SchemaType} from 'sanity'
9
9
 
10
10
  export declare type ArrayInputProps = ArrayOfObjectsInputProps & {
11
- objectName: string
11
+ variantName: string
12
+ variantId: string
13
+ experimentId: string
12
14
  }
13
15
 
14
16
  export declare type ExperimentContextProps = Required<FieldPluginConfig> & {
@@ -19,11 +21,15 @@ export declare type ExperimentGeneric<T> = {
19
21
  _type: string
20
22
  default?: T
21
23
  experimentValue?: string
22
- variants?: Array<
23
- {
24
- _key: string
25
- } & VariantGeneric<T>
26
- >
24
+ [key: string]:
25
+ | Array<
26
+ {
27
+ _key: string
28
+ } & VariantGeneric<T>
29
+ >
30
+ | string
31
+ | T
32
+ | undefined
27
33
  }
28
34
 
29
35
  export declare type ExperimentType = {
@@ -38,6 +44,11 @@ export declare type FieldPluginConfig = {
38
44
  fields: (string | FieldDefinition)[]
39
45
  experiments: ExperimentType[] | ((client: SanityClient) => Promise<ExperimentType[]>)
40
46
  apiVersion?: string
47
+ experimentNameOverride?: string
48
+ variantNameOverride?: string
49
+ variantId?: string
50
+ variantArrayName?: string
51
+ experimentId?: string
41
52
  }
42
53
 
43
54
  /**
@@ -50,15 +61,13 @@ export declare type ObjectFieldWithPath = ObjectField<SchemaType> & {
50
61
  }
51
62
 
52
63
  export declare type VariantGeneric<T> = {
64
+ [key: string]: string | T | undefined
53
65
  _type: string
54
- variantId?: string
55
- experimentId?: string
56
66
  value?: T
57
67
  }
58
68
 
59
69
  export declare type VariantPreviewProps = Omit<PreviewProps, 'SchemaType'> & {
60
- experiment: string
61
- variant: string
70
+ [key: string]: string
62
71
  value: any
63
72
  }
64
73
 
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: []
@@ -28,18 +33,18 @@ function ExperimentProvider(props) {
28
33
  return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
29
34
  }
30
35
  const ArrayInput = (props) => {
31
- const fieldPath = props.path.slice(0, -1), experimentId = sanity.useFormValue([...fieldPath, "experimentId"]), { experiments } = useExperimentContext(), { onItemAppend, objectName } = props, handleClick = react.useCallback(
36
+ const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
32
37
  async (variant) => {
33
38
  const item = {
34
39
  _key: uuid.uuid(),
35
- variantId: variant.id,
36
- experimentId,
37
- _type: objectName
40
+ [variantId]: variant.id,
41
+ [experimentId]: experimentValue,
42
+ _type: variantName
38
43
  };
39
44
  onItemAppend(item);
40
45
  },
41
- [experimentId, objectName, onItemAppend]
42
- ), filteredVariants = experiments.find((option) => option.id === experimentId)?.variants || [], usedVariants = props.value?.map((variant) => variant.variantId);
46
+ [variantId, experimentId, experimentValue, variantName, onItemAppend]
47
+ ), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
43
48
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
44
49
  props.renderDefault({ ...props, arrayFunctions: () => null }),
45
50
  /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
@@ -50,7 +55,7 @@ const ArrayInput = (props) => {
50
55
  disabled: usedVariants?.includes(variant.id),
51
56
  onClick: () => handleClick(variant)
52
57
  },
53
- `${experimentId}-${variant.id}`
58
+ `${experimentValue}-${variant.id}`
54
59
  )) })
55
60
  ] });
56
61
  }, AccessDeniedIcon = react.forwardRef(function(props, ref) {
@@ -6400,40 +6405,60 @@ const icons = {
6400
6405
  });
6401
6406
  Icon.displayName = "ForwardRef(Icon)";
6402
6407
  const useAddExperimentAction = (props) => {
6403
- const patchActiveEvent = react.useMemo(() => sanity.set(!0, ["active"]), []), handleAction = react.useCallback(() => {
6404
- props.onChange([patchActiveEvent]);
6405
- }, [patchActiveEvent, props]);
6408
+ const { onChange, experimentNameOverride } = props, handleAddAction = () => {
6409
+ onChange([sanity.set(!0, ["active"])]);
6410
+ };
6406
6411
  return {
6407
- title: "Add experiment",
6412
+ title: `Add ${experimentNameOverride}`,
6408
6413
  type: "action",
6409
6414
  icon: gi.GiSoapExperiment,
6410
- onAction: handleAction,
6415
+ onAction: handleAddAction,
6411
6416
  renderAsButton: !0
6412
6417
  };
6413
6418
  }, useRemoveExperimentAction = (props) => {
6414
- const patchActiveEvent = react.useMemo(() => sanity.set(!1, ["active"]), []), patchClearEvent = react.useMemo(() => {
6415
- const experimentId = ["experimentId"], variants = ["variants"];
6416
- return [sanity.unset(experimentId), sanity.unset(variants)];
6417
- }, []), handleAction = react.useCallback(() => {
6418
- props.onChange([patchActiveEvent, ...patchClearEvent]);
6419
- }, [patchActiveEvent, patchClearEvent, props]);
6419
+ const { onChange, experimentId, experimentNameOverride } = props, patchActiveFalseEvent = () => sanity.set(!1, ["active"]), patchClearEvent = () => {
6420
+ const experiment = [experimentId], variants = [experimentNameOverride];
6421
+ return [sanity.unset(experiment), sanity.unset(variants)];
6422
+ }, handleClearAction = () => {
6423
+ const clearEvents = patchClearEvent(), activeEvent = patchActiveFalseEvent();
6424
+ onChange([activeEvent, ...clearEvents]);
6425
+ };
6420
6426
  return {
6421
- title: "Remove experiment",
6427
+ title: `Remove ${experimentNameOverride}`,
6422
6428
  type: "action",
6423
6429
  icon: CloseIcon,
6424
- onAction: handleAction,
6430
+ onAction: handleClearAction,
6425
6431
  renderAsButton: !0
6426
6432
  };
6427
- }, newActions = ({ onChange, inputId, active }) => active ? sanity.defineDocumentFieldAction({
6428
- name: "Experiment",
6429
- useAction: (props) => useRemoveExperimentAction({ ...props, onChange, inputId })
6430
- }) : sanity.defineDocumentFieldAction({
6431
- name: "Experiment",
6432
- useAction: (props) => useAddExperimentAction({ ...props, onChange, inputId })
6433
- }), ExperimentField = (props) => {
6434
- const { onChange } = props.inputProps, { inputId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
6433
+ }, newActions = ({
6434
+ onChange,
6435
+ inputId,
6436
+ active,
6437
+ experimentNameOverride,
6438
+ experimentId
6439
+ }) => {
6440
+ const removeAction = sanity.defineDocumentFieldAction({
6441
+ name: `Remove ${experimentNameOverride}`,
6442
+ useAction: (props) => useRemoveExperimentAction({
6443
+ onChange,
6444
+ experimentNameOverride,
6445
+ experimentId
6446
+ })
6447
+ }), addAction = sanity.defineDocumentFieldAction({
6448
+ name: `Add ${experimentNameOverride}`,
6449
+ useAction: (props) => useAddExperimentAction({
6450
+ onChange,
6451
+ experimentNameOverride
6452
+ })
6453
+ });
6454
+ return active ? removeAction : addAction;
6455
+ }, ExperimentField = (props) => {
6456
+ const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
6435
6457
  ...props,
6436
- actions: [newActions({ onChange, inputId, active }), ...oldActions]
6458
+ actions: [
6459
+ newActions({ onChange, inputId, active, experimentNameOverride, experimentId }),
6460
+ ...oldActions
6461
+ ]
6437
6462
  };
6438
6463
  return props.renderDefault(withActionProps);
6439
6464
  }, Select = (props) => {
@@ -6465,7 +6490,10 @@ const useAddExperimentAction = (props) => {
6465
6490
  title: experiment.label,
6466
6491
  value: experiment.id
6467
6492
  })), ExperimentInput = (props) => {
6468
- 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(
6493
+ const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
6494
+ () => [...props.path.slice(0, -1), props.variantNameOverride],
6495
+ [props.variantNameOverride, props.path]
6496
+ ), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
6469
6497
  (event, onChange) => {
6470
6498
  const inputValue = event.currentTarget.value;
6471
6499
  if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
@@ -6525,15 +6553,27 @@ function extractInnerFields(fields, path, maxDepth) {
6525
6553
  return [...acc, thisFieldWithPath];
6526
6554
  }, []);
6527
6555
  }
6528
- const createFieldType = ({
6529
- field
6556
+ const createExperimentType = ({
6557
+ field,
6558
+ experimentNameOverride,
6559
+ variantNameOverride,
6560
+ variantId,
6561
+ variantArrayName,
6562
+ experimentId
6530
6563
  }) => {
6531
- const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), objectName = `variant${usedName}`;
6564
+ const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
6532
6565
  return sanity.defineType({
6533
- name: `experiment${usedName}`,
6566
+ name: `${experimentNameOverride}${usedName}`,
6534
6567
  type: "object",
6535
6568
  components: {
6536
- field: ExperimentField
6569
+ field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
6570
+ ExperimentField,
6571
+ {
6572
+ ...props,
6573
+ experimentId,
6574
+ experimentNameOverride
6575
+ }
6576
+ )
6537
6577
  },
6538
6578
  fields: [
6539
6579
  typeof field == "string" ? (
@@ -6555,37 +6595,47 @@ const createFieldType = ({
6555
6595
  hidden: !0
6556
6596
  }),
6557
6597
  sanity.defineField({
6558
- title: "Experiment",
6559
- name: "experimentId",
6598
+ name: experimentId,
6560
6599
  type: "string",
6561
6600
  components: {
6562
- input: ExperimentInput
6601
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ExperimentInput, { ...props, variantNameOverride })
6563
6602
  },
6564
6603
  hidden: ({ parent }) => !parent?.active
6565
6604
  }),
6566
6605
  sanity.defineField({
6567
- name: "variants",
6606
+ name: variantArrayName,
6568
6607
  type: "array",
6569
- hidden: ({ parent }) => !parent?.experimentId,
6608
+ hidden: ({ parent }) => !parent?.[experimentId],
6570
6609
  components: {
6571
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ArrayInput, { ...props, objectName })
6610
+ input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
6611
+ ArrayInput,
6612
+ {
6613
+ ...props,
6614
+ variantName,
6615
+ variantId,
6616
+ experimentId
6617
+ }
6618
+ )
6572
6619
  },
6573
6620
  of: [
6574
6621
  sanity.defineField({
6575
- name: objectName,
6576
- type: objectName
6622
+ name: variantName,
6623
+ type: variantName
6577
6624
  })
6578
6625
  ]
6579
6626
  })
6580
6627
  ]
6581
6628
  });
6582
- }, createFieldObjectType = ({
6583
- field
6629
+ }, createVariantType = ({
6630
+ field,
6631
+ variantNameOverride,
6632
+ variantId,
6633
+ experimentId
6584
6634
  }) => {
6585
6635
  const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
6586
6636
  return sanity.defineType({
6587
- name: `variant${usedName}`,
6588
- title: `Experiment array ${usedName}`,
6637
+ name: `${variantNameOverride}${usedName}`,
6638
+ title: `${variantNameOverride} array ${usedName}`,
6589
6639
  type: "object",
6590
6640
  components: {
6591
6641
  preview: VariantPreview
@@ -6593,56 +6643,91 @@ const createFieldType = ({
6593
6643
  fields: [
6594
6644
  {
6595
6645
  type: "string",
6596
- name: "variantId",
6646
+ name: variantId,
6597
6647
  readOnly: !0
6598
6648
  },
6599
6649
  {
6600
6650
  type: "string",
6601
- name: "experimentId",
6651
+ name: experimentId,
6602
6652
  hidden: !0
6603
6653
  },
6604
6654
  typeof field == "string" ? (
6605
6655
  // Define a simple field if all we have is the name as a string
6606
6656
  sanity.defineField({
6607
6657
  name: "value",
6608
- type: field,
6609
- hidden: ({ parent }) => !parent?.variantId
6658
+ type: field
6659
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
6610
6660
  })
6611
6661
  ) : (
6612
6662
  // Pass in the configured options, but overwrite the name
6613
6663
  {
6614
6664
  ...field,
6615
- name: "value",
6616
- hidden: ({ parent }) => !parent?.variantId
6665
+ name: "value"
6666
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
6617
6667
  }
6618
6668
  )
6619
6669
  ],
6620
6670
  preview: {
6621
6671
  select: {
6622
- variant: "variantId",
6623
- experiment: "experimentId",
6672
+ variant: variantId,
6673
+ experiment: experimentId,
6624
6674
  value: "value"
6625
6675
  }
6626
6676
  }
6627
6677
  });
6628
- }, fieldSchema = ({ fields, experiments }) => [
6629
- ...fields.map((field) => createFieldObjectType({ field })),
6630
- ...fields.map((field) => createFieldType({ field }))
6678
+ }, fieldSchema = ({
6679
+ fields,
6680
+ experimentNameOverride,
6681
+ variantNameOverride,
6682
+ variantId,
6683
+ variantArrayName,
6684
+ experimentId
6685
+ }) => [
6686
+ ...fields.map(
6687
+ (field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
6688
+ ),
6689
+ ...fields.map(
6690
+ (field) => createExperimentType({
6691
+ field,
6692
+ experimentNameOverride,
6693
+ variantNameOverride,
6694
+ variantId,
6695
+ variantArrayName,
6696
+ experimentId
6697
+ })
6698
+ )
6631
6699
  ], fieldLevelExperiments = sanity.definePlugin((config) => {
6632
- const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experiments } = pluginConfig;
6700
+ const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
6633
6701
  return {
6634
6702
  name: "sanity-personalistaion-plugin-field-level-experiments",
6635
6703
  schema: {
6636
- types: fieldSchema({ fields, experiments })
6704
+ types: fieldSchema({
6705
+ fields,
6706
+ experimentNameOverride,
6707
+ variantNameOverride,
6708
+ variantId,
6709
+ variantArrayName,
6710
+ experimentId
6711
+ })
6637
6712
  },
6638
6713
  form: {
6639
6714
  components: {
6640
6715
  input: (props) => {
6641
6716
  if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
6642
6717
  (field) => field.type.name
6643
- ).some((name) => name.startsWith("experiment")))
6718
+ ).some(
6719
+ (name) => name.startsWith(experimentNameOverride)
6720
+ ))
6644
6721
  return props.renderDefault(props);
6645
- const providerProps = { ...props, experimentFieldPluginConfig: pluginConfig };
6722
+ const providerProps = {
6723
+ ...props,
6724
+ experimentFieldPluginConfig: {
6725
+ ...pluginConfig,
6726
+ variantId,
6727
+ variantArrayName,
6728
+ experimentId
6729
+ }
6730
+ };
6646
6731
  return ExperimentProvider(providerProps);
6647
6732
  }
6648
6733
  }