@sanity/personalization-plugin 2.2.0-launch-darkly.1 → 2.2.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,12 +1,11 @@
1
- import { jsx, jsxs, Fragment } from "react/jsx-runtime";
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, useDocumentOperation, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
3
- import { Stack, Inline, Button, Select as Select$1 } from "@sanity/ui";
3
+ import { Stack, Inline, Button, Select as Select$1, Card, Text } from "@sanity/ui";
4
4
  import { uuid } from "@sanity/uuid";
5
- import { createContext, useState, useMemo, useContext, useCallback, forwardRef, useEffect } from "react";
5
+ import { createContext, useMemo, useContext, useCallback, forwardRef, useState, useEffect } from "react";
6
6
  import equal from "fast-deep-equal";
7
7
  import { suspend } from "suspend-react";
8
8
  import { GiSoapExperiment } from "react-icons/gi";
9
- import { useSecrets, SettingsView } from "@sanity/studio-secrets";
10
9
  const CONFIG_DEFAULT = {
11
10
  fields: [],
12
11
  apiVersion: "2024-11-07",
@@ -17,23 +16,20 @@ const CONFIG_DEFAULT = {
17
16
  experimentId: "experimentId"
18
17
  }, ExperimentContext = createContext({
19
18
  ...CONFIG_DEFAULT,
20
- experiments: [],
21
- setSecret: () => {
22
- },
23
- secret: void 0
19
+ experiments: []
24
20
  });
25
21
  function useExperimentContext() {
26
22
  return useContext(ExperimentContext);
27
23
  }
28
24
  function ExperimentProvider(props) {
29
- const { experimentFieldPluginConfig } = props, [secret, setSecret] = useState(), client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
25
+ const { experimentFieldPluginConfig } = props, client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
30
26
  // eslint-disable-next-line require-await
31
27
  async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
32
- [workspace, secret],
28
+ [workspace],
33
29
  { equal }
34
30
  ), context = useMemo(
35
- () => ({ ...experimentFieldPluginConfig, experiments, secret, setSecret }),
36
- [experimentFieldPluginConfig, experiments, secret, setSecret]
31
+ () => ({ ...experimentFieldPluginConfig, experiments }),
32
+ [experimentFieldPluginConfig, experiments]
37
33
  );
38
34
  return /* @__PURE__ */ jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
39
35
  }
@@ -6510,7 +6506,11 @@ const useAddExperimentAction = (props) => {
6510
6506
  },
6511
6507
  [patch, subValues, aditionalChangePath]
6512
6508
  );
6513
- return experiments.length ? /* @__PURE__ */ jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsx(Fragment, {});
6509
+ 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: [
6510
+ "There are no defined ",
6511
+ props.experimentNameOverride,
6512
+ "s"
6513
+ ] }) });
6514
6514
  }, VariantInput = (props) => {
6515
6515
  const defaultValue = useFormValue([props.path[0], "default"]), handleClick = () => {
6516
6516
  props.onChange(set(defaultValue, ["value"]));
@@ -6611,7 +6611,14 @@ const createExperimentType = ({
6611
6611
  name: experimentId,
6612
6612
  type: "string",
6613
6613
  components: {
6614
- input: (props) => /* @__PURE__ */ jsx(ExperimentInput, { ...props, variantNameOverride })
6614
+ input: (props) => /* @__PURE__ */ jsx(
6615
+ ExperimentInput,
6616
+ {
6617
+ ...props,
6618
+ experimentNameOverride,
6619
+ variantNameOverride
6620
+ }
6621
+ )
6615
6622
  },
6616
6623
  hidden: ({ parent }) => !parent?.active
6617
6624
  }),
@@ -6747,81 +6754,9 @@ const createExperimentType = ({
6747
6754
  }
6748
6755
  }
6749
6756
  };
6750
- }), pluginConfigKeys = [
6751
- {
6752
- key: "apiKey",
6753
- title: "Your secret API key"
6754
- }
6755
- ], Secrets = (props, namespace) => {
6756
- const { secrets, loading } = useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = useState(!1);
6757
- return useEffect(() => {
6758
- if (!loading)
6759
- return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
6760
- }, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxs(Fragment, { children: [
6761
- /* @__PURE__ */ jsx(
6762
- SettingsView,
6763
- {
6764
- title: `${namespace} api key`,
6765
- namespace,
6766
- keys: pluginConfigKeys,
6767
- onClose: () => {
6768
- setShowSettings(!1);
6769
- }
6770
- }
6771
- ),
6772
- props.renderDefault(props)
6773
- ] }) : props.renderDefault(props);
6774
- }, getExperiments = async ({
6775
- client,
6776
- projectKey,
6777
- tags
6778
- }) => {
6779
- const secret = await client.fetch("*[_id == 'secrets.launchdarkly'][0].secrets.apiKey");
6780
- if (!secret) return [];
6781
- const url = new URL(`https://app.launchdarkly.com/api/v2/flags/${projectKey}`);
6782
- tags && url.searchParams.set("filter", `tags:${tags.join("+")}`);
6783
- const featureExperiments = [];
6784
- let hasMore = !0;
6785
- const offset = 0, limit = 10;
6786
- for (; hasMore; ) {
6787
- url.searchParams.set("offset", offset.toString()), url.searchParams.set("limit", limit.toString());
6788
- const responseFlags = await fetch(url, {
6789
- headers: {
6790
- Authorization: secret
6791
- }
6792
- }), { items } = await responseFlags.json(), experiments = items.map((flag) => ({
6793
- id: flag.key,
6794
- label: flag.name,
6795
- variants: flag.variations.map((variation) => ({
6796
- id: variation.value,
6797
- label: variation.name
6798
- }))
6799
- }));
6800
- featureExperiments.push(...experiments), items.length !== limit && (hasMore = !1);
6801
- }
6802
- return featureExperiments;
6803
- }, launchDarklyFieldLevel = definePlugin((config) => {
6804
- const { fields, projectKey, tags } = config;
6805
- return {
6806
- name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
6807
- plugins: [
6808
- fieldLevelExperiments({
6809
- fields,
6810
- experiments: (client) => getExperiments({ client, projectKey, tags })
6811
- })
6812
- ],
6813
- form: {
6814
- components: {
6815
- input: (props) => !(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
6816
- (field) => field.type.name
6817
- ).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props, "launchdarkly")
6818
- }
6819
- }
6820
- };
6821
6757
  });
6822
6758
  export {
6823
6759
  fieldLevelExperiments,
6824
- flattenSchemaType,
6825
- launchDarklyFieldLevel
6760
+ flattenSchemaType
6826
6761
  };
6827
6762
  //# sourceMappingURL=index.mjs.map