@sanity/personalization-plugin 2.2.0 → 2.2.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.js CHANGED
@@ -6405,9 +6405,9 @@ const icons = {
6405
6405
  });
6406
6406
  Icon.displayName = "ForwardRef(Icon)";
6407
6407
  const useAddExperimentAction = (props) => {
6408
- const { onChange, experimentNameOverride } = props, handleAddAction = () => {
6409
- onChange([sanity.set(!0, ["active"])]);
6410
- };
6408
+ const { onChange, active, experimentNameOverride } = props, handleAddAction = react.useCallback(() => {
6409
+ onChange([sanity.set(!active, ["active"])]);
6410
+ }, [onChange, active]);
6411
6411
  return {
6412
6412
  title: `Add ${experimentNameOverride}`,
6413
6413
  type: "action",
@@ -6416,13 +6416,10 @@ const useAddExperimentAction = (props) => {
6416
6416
  renderAsButton: !0
6417
6417
  };
6418
6418
  }, useRemoveExperimentAction = (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
- };
6419
+ const { onChange, active, experimentId, experimentNameOverride, variantNameOverride } = props, handleClearAction = react.useCallback(() => {
6420
+ const activeId = ["active"], experiment = [experimentId], variants = [`${variantNameOverride}s`];
6421
+ onChange([sanity.set(!active, activeId), sanity.unset(experiment), sanity.unset(variants)]);
6422
+ }, [onChange, active, experimentId, experimentNameOverride, variantNameOverride]);
6426
6423
  return {
6427
6424
  title: `Remove ${experimentNameOverride}`,
6428
6425
  type: "action",
@@ -6430,36 +6427,53 @@ const useAddExperimentAction = (props) => {
6430
6427
  onAction: handleClearAction,
6431
6428
  renderAsButton: !0
6432
6429
  };
6433
- }, newActions = ({
6430
+ }, createActions = ({
6434
6431
  onChange,
6435
6432
  inputId,
6436
6433
  active,
6437
6434
  experimentNameOverride,
6438
- experimentId
6435
+ experimentId,
6436
+ variantNameOverride
6439
6437
  }) => {
6440
6438
  const removeAction = sanity.defineDocumentFieldAction({
6441
6439
  name: `Remove ${experimentNameOverride}`,
6442
6440
  useAction: (props) => useRemoveExperimentAction({
6441
+ active: !0,
6443
6442
  onChange,
6444
6443
  experimentNameOverride,
6445
- experimentId
6444
+ experimentId,
6445
+ variantNameOverride
6446
6446
  })
6447
6447
  }), addAction = sanity.defineDocumentFieldAction({
6448
6448
  name: `Add ${experimentNameOverride}`,
6449
6449
  useAction: (props) => useAddExperimentAction({
6450
+ active: !1,
6450
6451
  onChange,
6451
6452
  experimentNameOverride
6452
6453
  })
6453
6454
  });
6454
6455
  return active ? removeAction : addAction;
6455
6456
  }, ExperimentField = (props) => {
6456
- const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
6457
- ...props,
6458
- actions: [
6459
- newActions({ onChange, inputId, active, experimentNameOverride, experimentId }),
6460
- ...oldActions
6461
- ]
6462
- };
6457
+ const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId, variantNameOverride } = props, active = props.value?.active, actionProps = react.useMemo(
6458
+ () => ({
6459
+ onChange,
6460
+ inputId,
6461
+ active,
6462
+ experimentNameOverride,
6463
+ experimentId,
6464
+ variantNameOverride
6465
+ }),
6466
+ [onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
6467
+ ), memoizedActions = react.useMemo(() => {
6468
+ const oldActions = props.actions || [];
6469
+ return [createActions(actionProps), ...oldActions];
6470
+ }, [actionProps, props.actions]), withActionProps = react.useMemo(
6471
+ () => ({
6472
+ ...props,
6473
+ actions: memoizedActions
6474
+ }),
6475
+ [props, memoizedActions]
6476
+ );
6463
6477
  return props.renderDefault(withActionProps);
6464
6478
  }, Select = (props) => {
6465
6479
  const {
@@ -6490,20 +6504,20 @@ const useAddExperimentAction = (props) => {
6490
6504
  title: experiment.label,
6491
6505
  value: experiment.id
6492
6506
  })), ExperimentInput = (props) => {
6493
- const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
6494
- () => [...props.path.slice(0, -1), props.variantNameOverride],
6507
+ const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), additionalChangePath = react.useMemo(
6508
+ () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
6495
6509
  [props.variantNameOverride, props.path]
6496
- ), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
6510
+ ), subValues = sanity.useFormValue(additionalChangePath), { patch } = sanity.useDocumentOperation(sanity.getPublishedId(id), props.schemaType.name), handleChange = react.useCallback(
6497
6511
  (event, onChange) => {
6498
6512
  const inputValue = event.currentTarget.value;
6499
6513
  if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
6500
6514
  const patchEvent = {
6501
- unset: [aditionalChangePath.join(".")]
6515
+ unset: [additionalChangePath.join(".")]
6502
6516
  };
6503
6517
  patch.execute([patchEvent]);
6504
6518
  }
6505
6519
  },
6506
- [patch, subValues, aditionalChangePath]
6520
+ [patch, subValues, additionalChangePath]
6507
6521
  );
6508
6522
  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: [
6509
6523
  "There are no defined ",
@@ -6583,7 +6597,8 @@ const createExperimentType = ({
6583
6597
  {
6584
6598
  ...props,
6585
6599
  experimentId,
6586
- experimentNameOverride
6600
+ experimentNameOverride,
6601
+ variantNameOverride
6587
6602
  }
6588
6603
  )
6589
6604
  },