@sanity/personalization-plugin 2.2.0-growthbook.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/README.md +0 -2
- package/dist/index.d.mts +1 -205
- package/dist/index.d.ts +1 -205
- package/dist/index.js +42 -133
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +45 -137
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -3
- package/src/components/ExperimentContext.tsx +5 -8
- package/src/components/ExperimentField.tsx +32 -56
- package/src/components/ExperimentInput.tsx +14 -4
- package/src/fieldExperiments.tsx +5 -2
- package/src/index.ts +0 -1
- package/src/types.ts +1 -182
- package/src/components/Secrets.tsx +0 -47
- package/src/growthbookFieldExperiments.tsx +0 -51
- package/src/utils/growthbook.ts +0 -78
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
|
-
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client
|
|
32
|
-
[workspace
|
|
27
|
+
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
|
|
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,
|
|
6414
|
-
onChange([set(!
|
|
6415
|
-
}
|
|
6409
|
+
const { onChange, experimentNameOverride } = props, handleAddAction = () => {
|
|
6410
|
+
onChange([set(!0, ["active"])]);
|
|
6411
|
+
};
|
|
6416
6412
|
return {
|
|
6417
6413
|
title: `Add ${experimentNameOverride}`,
|
|
6418
6414
|
type: "action",
|
|
@@ -6421,10 +6417,13 @@ const useAddExperimentAction = (props) => {
|
|
|
6421
6417
|
renderAsButton: !0
|
|
6422
6418
|
};
|
|
6423
6419
|
}, useRemoveExperimentAction = (props) => {
|
|
6424
|
-
const { onChange,
|
|
6425
|
-
const
|
|
6426
|
-
|
|
6427
|
-
},
|
|
6420
|
+
const { onChange, experimentId, experimentNameOverride } = props, patchActiveFalseEvent = () => set(!1, ["active"]), patchClearEvent = () => {
|
|
6421
|
+
const experiment = [experimentId], variants = [experimentNameOverride];
|
|
6422
|
+
return [unset(experiment), unset(variants)];
|
|
6423
|
+
}, handleClearAction = () => {
|
|
6424
|
+
const clearEvents = patchClearEvent(), activeEvent = patchActiveFalseEvent();
|
|
6425
|
+
onChange([activeEvent, ...clearEvents]);
|
|
6426
|
+
};
|
|
6428
6427
|
return {
|
|
6429
6428
|
title: `Remove ${experimentNameOverride}`,
|
|
6430
6429
|
type: "action",
|
|
@@ -6432,53 +6431,36 @@ const useAddExperimentAction = (props) => {
|
|
|
6432
6431
|
onAction: handleClearAction,
|
|
6433
6432
|
renderAsButton: !0
|
|
6434
6433
|
};
|
|
6435
|
-
},
|
|
6434
|
+
}, newActions = ({
|
|
6436
6435
|
onChange,
|
|
6437
6436
|
inputId,
|
|
6438
6437
|
active,
|
|
6439
6438
|
experimentNameOverride,
|
|
6440
|
-
experimentId
|
|
6441
|
-
variantNameOverride
|
|
6439
|
+
experimentId
|
|
6442
6440
|
}) => {
|
|
6443
6441
|
const removeAction = defineDocumentFieldAction({
|
|
6444
6442
|
name: `Remove ${experimentNameOverride}`,
|
|
6445
6443
|
useAction: (props) => useRemoveExperimentAction({
|
|
6446
|
-
active: !0,
|
|
6447
6444
|
onChange,
|
|
6448
6445
|
experimentNameOverride,
|
|
6449
|
-
experimentId
|
|
6450
|
-
variantNameOverride
|
|
6446
|
+
experimentId
|
|
6451
6447
|
})
|
|
6452
6448
|
}), addAction = defineDocumentFieldAction({
|
|
6453
6449
|
name: `Add ${experimentNameOverride}`,
|
|
6454
6450
|
useAction: (props) => useAddExperimentAction({
|
|
6455
|
-
active: !1,
|
|
6456
6451
|
onChange,
|
|
6457
6452
|
experimentNameOverride
|
|
6458
6453
|
})
|
|
6459
6454
|
});
|
|
6460
6455
|
return active ? removeAction : addAction;
|
|
6461
6456
|
}, ExperimentField = (props) => {
|
|
6462
|
-
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
inputId,
|
|
6466
|
-
|
|
6467
|
-
|
|
6468
|
-
|
|
6469
|
-
variantNameOverride
|
|
6470
|
-
}),
|
|
6471
|
-
[onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
|
|
6472
|
-
), memoizedActions = useMemo(() => {
|
|
6473
|
-
const oldActions = props.actions || [];
|
|
6474
|
-
return [createActions(actionProps), ...oldActions];
|
|
6475
|
-
}, [actionProps, props.actions]), withActionProps = useMemo(
|
|
6476
|
-
() => ({
|
|
6477
|
-
...props,
|
|
6478
|
-
actions: memoizedActions
|
|
6479
|
-
}),
|
|
6480
|
-
[props, memoizedActions]
|
|
6481
|
-
);
|
|
6457
|
+
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
|
|
6458
|
+
...props,
|
|
6459
|
+
actions: [
|
|
6460
|
+
newActions({ onChange, inputId, active, experimentNameOverride, experimentId }),
|
|
6461
|
+
...oldActions
|
|
6462
|
+
]
|
|
6463
|
+
};
|
|
6482
6464
|
return props.renderDefault(withActionProps);
|
|
6483
6465
|
}, Select = (props) => {
|
|
6484
6466
|
const {
|
|
@@ -6510,7 +6492,7 @@ const useAddExperimentAction = (props) => {
|
|
|
6510
6492
|
value: experiment.id
|
|
6511
6493
|
})), ExperimentInput = (props) => {
|
|
6512
6494
|
const { experiments } = useExperimentContext(), id = useFormValue(["_id"]), aditionalChangePath = useMemo(
|
|
6513
|
-
() => [...props.path.slice(0, -1),
|
|
6495
|
+
() => [...props.path.slice(0, -1), props.variantNameOverride],
|
|
6514
6496
|
[props.variantNameOverride, props.path]
|
|
6515
6497
|
), subValues = useFormValue(aditionalChangePath), { patch } = useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = useCallback(
|
|
6516
6498
|
(event, onChange) => {
|
|
@@ -6524,7 +6506,11 @@ const useAddExperimentAction = (props) => {
|
|
|
6524
6506
|
},
|
|
6525
6507
|
[patch, subValues, aditionalChangePath]
|
|
6526
6508
|
);
|
|
6527
|
-
return experiments.length ? /* @__PURE__ */ jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsx(
|
|
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
|
+
] }) });
|
|
6528
6514
|
}, VariantInput = (props) => {
|
|
6529
6515
|
const defaultValue = useFormValue([props.path[0], "default"]), handleClick = () => {
|
|
6530
6516
|
props.onChange(set(defaultValue, ["value"]));
|
|
@@ -6598,8 +6584,7 @@ const createExperimentType = ({
|
|
|
6598
6584
|
{
|
|
6599
6585
|
...props,
|
|
6600
6586
|
experimentId,
|
|
6601
|
-
experimentNameOverride
|
|
6602
|
-
variantNameOverride
|
|
6587
|
+
experimentNameOverride
|
|
6603
6588
|
}
|
|
6604
6589
|
)
|
|
6605
6590
|
},
|
|
@@ -6626,7 +6611,14 @@ const createExperimentType = ({
|
|
|
6626
6611
|
name: experimentId,
|
|
6627
6612
|
type: "string",
|
|
6628
6613
|
components: {
|
|
6629
|
-
input: (props) => /* @__PURE__ */ jsx(
|
|
6614
|
+
input: (props) => /* @__PURE__ */ jsx(
|
|
6615
|
+
ExperimentInput,
|
|
6616
|
+
{
|
|
6617
|
+
...props,
|
|
6618
|
+
experimentNameOverride,
|
|
6619
|
+
variantNameOverride
|
|
6620
|
+
}
|
|
6621
|
+
)
|
|
6630
6622
|
},
|
|
6631
6623
|
hidden: ({ parent }) => !parent?.active
|
|
6632
6624
|
}),
|
|
@@ -6762,93 +6754,9 @@ const createExperimentType = ({
|
|
|
6762
6754
|
}
|
|
6763
6755
|
}
|
|
6764
6756
|
};
|
|
6765
|
-
}), namespace = "growthbook", pluginConfigKeys = [
|
|
6766
|
-
{
|
|
6767
|
-
key: "apiKey",
|
|
6768
|
-
title: "Your secret API key"
|
|
6769
|
-
}
|
|
6770
|
-
], Secrets = (props) => {
|
|
6771
|
-
const { secrets, loading } = useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = useState(!1);
|
|
6772
|
-
return useEffect(() => {
|
|
6773
|
-
if (!loading)
|
|
6774
|
-
return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
|
|
6775
|
-
}, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
6776
|
-
/* @__PURE__ */ jsx(
|
|
6777
|
-
SettingsView,
|
|
6778
|
-
{
|
|
6779
|
-
title: "Growthbook secret",
|
|
6780
|
-
namespace,
|
|
6781
|
-
keys: pluginConfigKeys,
|
|
6782
|
-
onClose: () => {
|
|
6783
|
-
setShowSettings(!1);
|
|
6784
|
-
}
|
|
6785
|
-
}
|
|
6786
|
-
),
|
|
6787
|
-
props.renderDefault(props)
|
|
6788
|
-
] }) : props.renderDefault(props);
|
|
6789
|
-
}, getBooleanConversion = (value) => value === "true" ? "variant" : value === "false" ? "control" : value, getExperiments = async ({
|
|
6790
|
-
client,
|
|
6791
|
-
environment,
|
|
6792
|
-
baseUrl,
|
|
6793
|
-
project,
|
|
6794
|
-
convertBooleans
|
|
6795
|
-
}) => {
|
|
6796
|
-
const secret = await client.fetch("*[_id == 'secrets.growthbook'][0].secrets.apiKey");
|
|
6797
|
-
if (!secret) return [];
|
|
6798
|
-
const featureExperiments = [];
|
|
6799
|
-
let hasMore = !0, offset = 0;
|
|
6800
|
-
const url = new URL(baseUrl ?? "https://api.growthbook.io/api/v1/features");
|
|
6801
|
-
for (project && url.searchParams.set("projectId", project); hasMore; ) {
|
|
6802
|
-
url.searchParams.set("offset", offset.toString());
|
|
6803
|
-
const response = await fetch(url, {
|
|
6804
|
-
headers: {
|
|
6805
|
-
Authorization: `Bearer ${secret}`
|
|
6806
|
-
}
|
|
6807
|
-
}), { features, hasMore: responseHasMore, nextOffset } = await response.json();
|
|
6808
|
-
hasMore = responseHasMore, offset = nextOffset, features && features.forEach((feature) => {
|
|
6809
|
-
if (feature.archived)
|
|
6810
|
-
return;
|
|
6811
|
-
const experiments = feature.environments[environment]?.rules.filter(
|
|
6812
|
-
(experiment) => experiment.type === "experiment-ref" || experiment.type === "experiment"
|
|
6813
|
-
);
|
|
6814
|
-
if (!experiments)
|
|
6815
|
-
return;
|
|
6816
|
-
const variations = /* @__PURE__ */ new Set();
|
|
6817
|
-
experiments.forEach((experiment) => {
|
|
6818
|
-
experiment?.variations.forEach((variant) => {
|
|
6819
|
-
variations.add({
|
|
6820
|
-
id: convertBooleans ? getBooleanConversion(variant.value) : variant.value,
|
|
6821
|
-
label: convertBooleans ? getBooleanConversion(variant.value) : variant.value
|
|
6822
|
-
});
|
|
6823
|
-
});
|
|
6824
|
-
});
|
|
6825
|
-
const value = { id: feature.id, label: feature.id, variants: [...variations] };
|
|
6826
|
-
featureExperiments.push(value);
|
|
6827
|
-
});
|
|
6828
|
-
}
|
|
6829
|
-
return featureExperiments;
|
|
6830
|
-
}, growthbookFieldLevel = definePlugin((config) => {
|
|
6831
|
-
const { fields, environment, project, convertBooleans, baseUrl } = config;
|
|
6832
|
-
return {
|
|
6833
|
-
name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
|
|
6834
|
-
plugins: [
|
|
6835
|
-
fieldLevelExperiments({
|
|
6836
|
-
fields,
|
|
6837
|
-
experiments: (client) => getExperiments({ client, environment, baseUrl, project, convertBooleans })
|
|
6838
|
-
})
|
|
6839
|
-
],
|
|
6840
|
-
form: {
|
|
6841
|
-
components: {
|
|
6842
|
-
input: (props) => !(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6843
|
-
(field) => field.type.name
|
|
6844
|
-
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props)
|
|
6845
|
-
}
|
|
6846
|
-
}
|
|
6847
|
-
};
|
|
6848
6757
|
});
|
|
6849
6758
|
export {
|
|
6850
6759
|
fieldLevelExperiments,
|
|
6851
|
-
flattenSchemaType
|
|
6852
|
-
growthbookFieldLevel
|
|
6760
|
+
flattenSchemaType
|
|
6853
6761
|
};
|
|
6854
6762
|
//# sourceMappingURL=index.mjs.map
|