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