@sanity/personalization-plugin 2.2.0-launch-darkly.1 → 2.3.0-growthbook.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/README.md +2 -0
- package/dist/growthbook/index.d.mts +15 -0
- package/dist/growthbook/index.d.ts +15 -0
- package/dist/growthbook/index.js +118 -0
- package/dist/growthbook/index.js.map +1 -0
- package/dist/growthbook/index.mjs +122 -0
- package/dist/growthbook/index.mjs.map +1 -0
- package/dist/index.d.mts +174 -178
- package/dist/index.d.ts +174 -178
- package/dist/index.js +56 -105
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +59 -109
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -3
- package/src/components/ExperimentContext.tsx +4 -7
- package/src/components/ExperimentField.tsx +56 -32
- package/src/components/ExperimentInput.tsx +14 -4
- package/src/fieldExperiments.tsx +6 -1
- package/src/growthbook/Components/GrowthbookContext.tsx +38 -0
- package/src/{components → growthbook/Components}/Secrets.tsx +7 -5
- package/src/growthbook/index.ts +52 -0
- package/src/growthbook/types.ts +15 -0
- package/src/growthbook/utils.ts +94 -0
- package/src/index.ts +0 -1
- package/src/types.ts +163 -170
- package/src/launchDarklyExperiments.tsx +0 -48
- package/src/utils/launchDarkly.ts +0 -54
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { jsx, jsxs
|
|
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,
|
|
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,
|
|
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
|
|
28
|
+
[workspace],
|
|
33
29
|
{ equal }
|
|
34
30
|
), context = useMemo(
|
|
35
|
-
() => ({ ...experimentFieldPluginConfig, experiments
|
|
36
|
-
[experimentFieldPluginConfig, experiments
|
|
31
|
+
() => ({ ...experimentFieldPluginConfig, experiments }),
|
|
32
|
+
[experimentFieldPluginConfig, experiments]
|
|
37
33
|
);
|
|
38
34
|
return /* @__PURE__ */ jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
|
|
39
35
|
}
|
|
@@ -6410,9 +6406,9 @@ const icons = {
|
|
|
6410
6406
|
});
|
|
6411
6407
|
Icon.displayName = "ForwardRef(Icon)";
|
|
6412
6408
|
const useAddExperimentAction = (props) => {
|
|
6413
|
-
const { onChange, experimentNameOverride } = props, handleAddAction = () => {
|
|
6414
|
-
onChange([set(!
|
|
6415
|
-
};
|
|
6409
|
+
const { onChange, active, experimentNameOverride } = props, handleAddAction = useCallback(() => {
|
|
6410
|
+
onChange([set(!active, ["active"])]);
|
|
6411
|
+
}, [onChange, active]);
|
|
6416
6412
|
return {
|
|
6417
6413
|
title: `Add ${experimentNameOverride}`,
|
|
6418
6414
|
type: "action",
|
|
@@ -6421,13 +6417,10 @@ const useAddExperimentAction = (props) => {
|
|
|
6421
6417
|
renderAsButton: !0
|
|
6422
6418
|
};
|
|
6423
6419
|
}, useRemoveExperimentAction = (props) => {
|
|
6424
|
-
const { onChange, experimentId, experimentNameOverride } = props,
|
|
6425
|
-
const experiment = [experimentId], variants = [
|
|
6426
|
-
|
|
6427
|
-
},
|
|
6428
|
-
const clearEvents = patchClearEvent(), activeEvent = patchActiveFalseEvent();
|
|
6429
|
-
onChange([activeEvent, ...clearEvents]);
|
|
6430
|
-
};
|
|
6420
|
+
const { onChange, active, experimentId, experimentNameOverride, variantNameOverride } = props, handleClearAction = useCallback(() => {
|
|
6421
|
+
const activeId = ["active"], experiment = [experimentId], variants = [`${variantNameOverride}s`];
|
|
6422
|
+
onChange([set(!active, activeId), unset(experiment), unset(variants)]);
|
|
6423
|
+
}, [onChange, active, experimentId, variantNameOverride]);
|
|
6431
6424
|
return {
|
|
6432
6425
|
title: `Remove ${experimentNameOverride}`,
|
|
6433
6426
|
type: "action",
|
|
@@ -6435,36 +6428,53 @@ const useAddExperimentAction = (props) => {
|
|
|
6435
6428
|
onAction: handleClearAction,
|
|
6436
6429
|
renderAsButton: !0
|
|
6437
6430
|
};
|
|
6438
|
-
},
|
|
6431
|
+
}, createActions = ({
|
|
6439
6432
|
onChange,
|
|
6440
6433
|
inputId,
|
|
6441
6434
|
active,
|
|
6442
6435
|
experimentNameOverride,
|
|
6443
|
-
experimentId
|
|
6436
|
+
experimentId,
|
|
6437
|
+
variantNameOverride
|
|
6444
6438
|
}) => {
|
|
6445
6439
|
const removeAction = defineDocumentFieldAction({
|
|
6446
6440
|
name: `Remove ${experimentNameOverride}`,
|
|
6447
6441
|
useAction: (props) => useRemoveExperimentAction({
|
|
6442
|
+
active: !0,
|
|
6448
6443
|
onChange,
|
|
6449
6444
|
experimentNameOverride,
|
|
6450
|
-
experimentId
|
|
6445
|
+
experimentId,
|
|
6446
|
+
variantNameOverride
|
|
6451
6447
|
})
|
|
6452
6448
|
}), addAction = defineDocumentFieldAction({
|
|
6453
6449
|
name: `Add ${experimentNameOverride}`,
|
|
6454
6450
|
useAction: (props) => useAddExperimentAction({
|
|
6451
|
+
active: !1,
|
|
6455
6452
|
onChange,
|
|
6456
6453
|
experimentNameOverride
|
|
6457
6454
|
})
|
|
6458
6455
|
});
|
|
6459
6456
|
return active ? removeAction : addAction;
|
|
6460
6457
|
}, ExperimentField = (props) => {
|
|
6461
|
-
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active,
|
|
6462
|
-
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
6466
|
-
|
|
6467
|
-
|
|
6458
|
+
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId, variantNameOverride } = props, active = props.value?.active, actionProps = useMemo(
|
|
6459
|
+
() => ({
|
|
6460
|
+
onChange,
|
|
6461
|
+
inputId,
|
|
6462
|
+
active,
|
|
6463
|
+
experimentNameOverride,
|
|
6464
|
+
experimentId,
|
|
6465
|
+
variantNameOverride
|
|
6466
|
+
}),
|
|
6467
|
+
[onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
|
|
6468
|
+
), memoizedActions = useMemo(() => {
|
|
6469
|
+
const oldActions = props.actions || [];
|
|
6470
|
+
return [createActions(actionProps), ...oldActions];
|
|
6471
|
+
}, [actionProps, props.actions]), withActionProps = useMemo(
|
|
6472
|
+
() => ({
|
|
6473
|
+
...props,
|
|
6474
|
+
actions: memoizedActions
|
|
6475
|
+
}),
|
|
6476
|
+
[props, memoizedActions]
|
|
6477
|
+
);
|
|
6468
6478
|
return props.renderDefault(withActionProps);
|
|
6469
6479
|
}, Select = (props) => {
|
|
6470
6480
|
const {
|
|
@@ -6496,7 +6506,7 @@ const useAddExperimentAction = (props) => {
|
|
|
6496
6506
|
value: experiment.id
|
|
6497
6507
|
})), ExperimentInput = (props) => {
|
|
6498
6508
|
const { experiments } = useExperimentContext(), id = useFormValue(["_id"]), aditionalChangePath = useMemo(
|
|
6499
|
-
() => [...props.path.slice(0, -1), props.variantNameOverride],
|
|
6509
|
+
() => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
|
|
6500
6510
|
[props.variantNameOverride, props.path]
|
|
6501
6511
|
), subValues = useFormValue(aditionalChangePath), { patch } = useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = useCallback(
|
|
6502
6512
|
(event, onChange) => {
|
|
@@ -6510,7 +6520,11 @@ const useAddExperimentAction = (props) => {
|
|
|
6510
6520
|
},
|
|
6511
6521
|
[patch, subValues, aditionalChangePath]
|
|
6512
6522
|
);
|
|
6513
|
-
return experiments.length ? /* @__PURE__ */ jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsx(
|
|
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
|
+
"There are no defined ",
|
|
6525
|
+
props.experimentNameOverride,
|
|
6526
|
+
"s"
|
|
6527
|
+
] }) });
|
|
6514
6528
|
}, VariantInput = (props) => {
|
|
6515
6529
|
const defaultValue = useFormValue([props.path[0], "default"]), handleClick = () => {
|
|
6516
6530
|
props.onChange(set(defaultValue, ["value"]));
|
|
@@ -6584,7 +6598,8 @@ const createExperimentType = ({
|
|
|
6584
6598
|
{
|
|
6585
6599
|
...props,
|
|
6586
6600
|
experimentId,
|
|
6587
|
-
experimentNameOverride
|
|
6601
|
+
experimentNameOverride,
|
|
6602
|
+
variantNameOverride
|
|
6588
6603
|
}
|
|
6589
6604
|
)
|
|
6590
6605
|
},
|
|
@@ -6611,7 +6626,14 @@ const createExperimentType = ({
|
|
|
6611
6626
|
name: experimentId,
|
|
6612
6627
|
type: "string",
|
|
6613
6628
|
components: {
|
|
6614
|
-
input: (props) => /* @__PURE__ */ jsx(
|
|
6629
|
+
input: (props) => /* @__PURE__ */ jsx(
|
|
6630
|
+
ExperimentInput,
|
|
6631
|
+
{
|
|
6632
|
+
...props,
|
|
6633
|
+
experimentNameOverride,
|
|
6634
|
+
variantNameOverride
|
|
6635
|
+
}
|
|
6636
|
+
)
|
|
6615
6637
|
},
|
|
6616
6638
|
hidden: ({ parent }) => !parent?.active
|
|
6617
6639
|
}),
|
|
@@ -6747,81 +6769,9 @@ const createExperimentType = ({
|
|
|
6747
6769
|
}
|
|
6748
6770
|
}
|
|
6749
6771
|
};
|
|
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
6772
|
});
|
|
6822
6773
|
export {
|
|
6823
6774
|
fieldLevelExperiments,
|
|
6824
|
-
flattenSchemaType
|
|
6825
|
-
launchDarklyFieldLevel
|
|
6775
|
+
flattenSchemaType
|
|
6826
6776
|
};
|
|
6827
6777
|
//# sourceMappingURL=index.mjs.map
|