@sanity/personalization-plugin 2.3.0-launch-darkly.2 → 2.4.0

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
@@ -1,5 +1,5 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
- import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, useDocumentOperation, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
2
+ import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, useDocumentOperation, getPublishedId, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
3
3
  import { Stack, Inline, Button, Select as Select$1, Card, Text } from "@sanity/ui";
4
4
  import { uuid } from "@sanity/uuid";
5
5
  import { createContext, useMemo, useContext, useCallback, forwardRef, useState, useEffect } from "react";
@@ -6505,20 +6505,20 @@ const useAddExperimentAction = (props) => {
6505
6505
  title: experiment.label,
6506
6506
  value: experiment.id
6507
6507
  })), ExperimentInput = (props) => {
6508
- const { experiments } = useExperimentContext(), id = useFormValue(["_id"]), aditionalChangePath = useMemo(
6508
+ const { experiments } = useExperimentContext(), id = useFormValue(["_id"]), additionalChangePath = useMemo(
6509
6509
  () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
6510
6510
  [props.variantNameOverride, props.path]
6511
- ), subValues = useFormValue(aditionalChangePath), { patch } = useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = useCallback(
6511
+ ), subValues = useFormValue(additionalChangePath), { patch } = useDocumentOperation(getPublishedId(id), props.schemaType.name), handleChange = useCallback(
6512
6512
  (event, onChange) => {
6513
6513
  const inputValue = event.currentTarget.value;
6514
6514
  if (onChange(inputValue ? set(inputValue) : unset()), subValues) {
6515
6515
  const patchEvent = {
6516
- unset: [aditionalChangePath.join(".")]
6516
+ unset: [additionalChangePath.join(".")]
6517
6517
  };
6518
6518
  patch.execute([patchEvent]);
6519
6519
  }
6520
6520
  },
6521
- [patch, subValues, aditionalChangePath]
6521
+ [patch, subValues, additionalChangePath]
6522
6522
  );
6523
6523
  return experiments.length ? /* @__PURE__ */ jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsx(Card, { padding: [3, 3, 4], radius: 2, shadow: 1, tone: "caution", children: /* @__PURE__ */ jsxs(Text, { align: "center", size: [2, 2, 3], children: [
6524
6524
  "There are no defined ",
@@ -6560,7 +6560,7 @@ const useAddExperimentAction = (props) => {
6560
6560
  return props.renderDefault(previewProps);
6561
6561
  };
6562
6562
  function flattenSchemaType(schemaType) {
6563
- return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
6563
+ return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 5) : (console.error("Schema type is not a document"), []);
6564
6564
  }
6565
6565
  function extractInnerFields(fields, path, maxDepth) {
6566
6566
  return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
@@ -6568,13 +6568,14 @@ function extractInnerFields(fields, path, maxDepth) {
6568
6568
  if (field.type.jsonType === "object") {
6569
6569
  const innerFields = extractInnerFields(field.type.fields, [...path, field.name], maxDepth);
6570
6570
  return [...acc, thisFieldWithPath, ...innerFields];
6571
- } else if (field.type.jsonType === "array" && field.type.of.length && field.type.of.some((item) => "fields" in item)) {
6572
- const innerFields = extractInnerFields(
6573
- // @ts-expect-error - Fix TS assertion for array fields
6574
- field.type.of[0].fields,
6575
- [...path, field.name],
6576
- maxDepth
6577
- );
6571
+ } else if (field.type.jsonType === "array") {
6572
+ const innerFields = (field.type.of || []).reduce((arrayAcc, arrayType) => {
6573
+ if ("fields" in arrayType) {
6574
+ const typeFields = extractInnerFields(arrayType.fields, [...path, field.name], maxDepth);
6575
+ return [...arrayAcc, ...typeFields];
6576
+ }
6577
+ return arrayAcc;
6578
+ }, []);
6578
6579
  return [...acc, thisFieldWithPath, ...innerFields];
6579
6580
  }
6580
6581
  return [...acc, thisFieldWithPath];
@@ -6749,10 +6750,8 @@ const createExperimentType = ({
6749
6750
  form: {
6750
6751
  components: {
6751
6752
  input: (props) => {
6752
- if (!(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
6753
- (field) => field.type.name
6754
- ).some(
6755
- (name) => name.startsWith(experimentNameOverride)
6753
+ if (!(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).some(
6754
+ (field) => field.type.name.startsWith(experimentNameOverride) || field.name.startsWith(experimentNameOverride)
6756
6755
  ))
6757
6756
  return props.renderDefault(props);
6758
6757
  const providerProps = {