@sanity/personalization-plugin 2.1.0 → 2.2.0-launch-darkly.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
@@ -2,10 +2,11 @@ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
2
  import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, useDocumentOperation, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
3
3
  import { Stack, Inline, Button, Select as Select$1 } from "@sanity/ui";
4
4
  import { uuid } from "@sanity/uuid";
5
- import { createContext, useMemo, useContext, useCallback, forwardRef, useState, useEffect } from "react";
5
+ import { createContext, useState, useMemo, useContext, useCallback, forwardRef, 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";
9
10
  const CONFIG_DEFAULT = {
10
11
  fields: [],
11
12
  apiVersion: "2024-11-07",
@@ -16,20 +17,23 @@ const CONFIG_DEFAULT = {
16
17
  experimentId: "experimentId"
17
18
  }, ExperimentContext = createContext({
18
19
  ...CONFIG_DEFAULT,
19
- experiments: []
20
+ experiments: [],
21
+ setSecret: () => {
22
+ },
23
+ secret: void 0
20
24
  });
21
25
  function useExperimentContext() {
22
26
  return useContext(ExperimentContext);
23
27
  }
24
28
  function ExperimentProvider(props) {
25
- const { experimentFieldPluginConfig } = props, client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
29
+ const { experimentFieldPluginConfig } = props, [secret, setSecret] = useState(), client = useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(
26
30
  // eslint-disable-next-line require-await
27
31
  async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
28
- [workspace],
32
+ [workspace, secret],
29
33
  { equal }
30
34
  ), context = useMemo(
31
- () => ({ ...experimentFieldPluginConfig, experiments }),
32
- [experimentFieldPluginConfig, experiments]
35
+ () => ({ ...experimentFieldPluginConfig, experiments, secret, setSecret }),
36
+ [experimentFieldPluginConfig, experiments, secret, setSecret]
33
37
  );
34
38
  return /* @__PURE__ */ jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
35
39
  }
@@ -6743,9 +6747,81 @@ const createExperimentType = ({
6743
6747
  }
6744
6748
  }
6745
6749
  };
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
+ };
6746
6821
  });
6747
6822
  export {
6748
6823
  fieldLevelExperiments,
6749
- flattenSchemaType
6824
+ flattenSchemaType,
6825
+ launchDarklyFieldLevel
6750
6826
  };
6751
6827
  //# sourceMappingURL=index.mjs.map