@sanity/personalization-plugin 2.1.0-growthbook.1 → 2.1.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 +60 -6
- package/dist/index.d.mts +20 -215
- package/dist/index.d.ts +20 -215
- package/dist/index.js +165 -158
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +166 -160
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -2
- package/src/components/Array.tsx +11 -12
- package/src/components/ExperimentContext.tsx +10 -8
- package/src/components/ExperimentField.tsx +71 -39
- package/src/components/ExperimentInput.tsx +5 -2
- package/src/components/VariantInput.tsx +15 -68
- package/src/fieldExperiments.tsx +99 -34
- package/src/index.ts +0 -1
- package/src/types.ts +19 -191
- package/src/components/Secrets.tsx +0 -47
- package/src/growthbookFieldExperiments.tsx +0 -51
- package/src/utils/growthbook.ts +0 -78
package/dist/index.js
CHANGED
|
@@ -1,48 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), gi = require("react-icons/gi")
|
|
3
|
+
var jsxRuntime = require("react/jsx-runtime"), sanity = require("sanity"), ui = require("@sanity/ui"), uuid = require("@sanity/uuid"), react = require("react"), equal = require("fast-deep-equal"), suspendReact = require("suspend-react"), gi = require("react-icons/gi");
|
|
4
4
|
function _interopDefaultCompat(e) {
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
7
|
var equal__default = /* @__PURE__ */ _interopDefaultCompat(equal);
|
|
8
8
|
const CONFIG_DEFAULT = {
|
|
9
9
|
fields: [],
|
|
10
|
-
apiVersion: "2024-11-07"
|
|
10
|
+
apiVersion: "2024-11-07",
|
|
11
|
+
experimentNameOverride: "experiment",
|
|
12
|
+
variantNameOverride: "variant",
|
|
13
|
+
variantId: "variantId",
|
|
14
|
+
variantArrayName: "variants",
|
|
15
|
+
experimentId: "experimentId"
|
|
11
16
|
}, ExperimentContext = react.createContext({
|
|
12
17
|
...CONFIG_DEFAULT,
|
|
13
|
-
experiments: []
|
|
14
|
-
setSecret: () => {
|
|
15
|
-
},
|
|
16
|
-
secret: void 0
|
|
18
|
+
experiments: []
|
|
17
19
|
});
|
|
18
20
|
function useExperimentContext() {
|
|
19
21
|
return react.useContext(ExperimentContext);
|
|
20
22
|
}
|
|
21
23
|
function ExperimentProvider(props) {
|
|
22
|
-
const { experimentFieldPluginConfig } = props,
|
|
24
|
+
const { experimentFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
|
|
23
25
|
// eslint-disable-next-line require-await
|
|
24
|
-
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client
|
|
25
|
-
[workspace
|
|
26
|
+
async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
|
|
27
|
+
[workspace],
|
|
26
28
|
{ equal: equal__default.default }
|
|
27
29
|
), context = react.useMemo(
|
|
28
|
-
() => ({ ...experimentFieldPluginConfig, experiments
|
|
29
|
-
[experimentFieldPluginConfig, experiments
|
|
30
|
+
() => ({ ...experimentFieldPluginConfig, experiments }),
|
|
31
|
+
[experimentFieldPluginConfig, experiments]
|
|
30
32
|
);
|
|
31
33
|
return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
|
|
32
34
|
}
|
|
33
35
|
const ArrayInput = (props) => {
|
|
34
|
-
const fieldPath = props.path.slice(0, -1), experimentId = sanity.useFormValue([...fieldPath,
|
|
36
|
+
const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
|
|
35
37
|
async (variant) => {
|
|
36
38
|
const item = {
|
|
37
39
|
_key: uuid.uuid(),
|
|
38
|
-
variantId: variant.id,
|
|
39
|
-
experimentId,
|
|
40
|
-
_type:
|
|
40
|
+
[variantId]: variant.id,
|
|
41
|
+
[experimentId]: experimentValue,
|
|
42
|
+
_type: variantName
|
|
41
43
|
};
|
|
42
44
|
onItemAppend(item);
|
|
43
45
|
},
|
|
44
|
-
[experimentId,
|
|
45
|
-
), filteredVariants = experiments.find((option) => option.id ===
|
|
46
|
+
[variantId, experimentId, experimentValue, variantName, onItemAppend]
|
|
47
|
+
), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
|
|
46
48
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
47
49
|
props.renderDefault({ ...props, arrayFunctions: () => null }),
|
|
48
50
|
/* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -53,7 +55,7 @@ const ArrayInput = (props) => {
|
|
|
53
55
|
disabled: usedVariants?.includes(variant.id),
|
|
54
56
|
onClick: () => handleClick(variant)
|
|
55
57
|
},
|
|
56
|
-
`${
|
|
58
|
+
`${experimentValue}-${variant.id}`
|
|
57
59
|
)) })
|
|
58
60
|
] });
|
|
59
61
|
}, AccessDeniedIcon = react.forwardRef(function(props, ref) {
|
|
@@ -6403,40 +6405,60 @@ const icons = {
|
|
|
6403
6405
|
});
|
|
6404
6406
|
Icon.displayName = "ForwardRef(Icon)";
|
|
6405
6407
|
const useAddExperimentAction = (props) => {
|
|
6406
|
-
const
|
|
6407
|
-
|
|
6408
|
-
}
|
|
6408
|
+
const { onChange, experimentNameOverride } = props, handleAddAction = () => {
|
|
6409
|
+
onChange([sanity.set(!0, ["active"])]);
|
|
6410
|
+
};
|
|
6409
6411
|
return {
|
|
6410
|
-
title:
|
|
6412
|
+
title: `Add ${experimentNameOverride}`,
|
|
6411
6413
|
type: "action",
|
|
6412
6414
|
icon: gi.GiSoapExperiment,
|
|
6413
|
-
onAction:
|
|
6415
|
+
onAction: handleAddAction,
|
|
6414
6416
|
renderAsButton: !0
|
|
6415
6417
|
};
|
|
6416
6418
|
}, useRemoveExperimentAction = (props) => {
|
|
6417
|
-
const
|
|
6418
|
-
const
|
|
6419
|
-
return [sanity.unset(
|
|
6420
|
-
},
|
|
6421
|
-
|
|
6422
|
-
|
|
6419
|
+
const { onChange, experimentId, experimentNameOverride } = props, patchActiveFalseEvent = () => sanity.set(!1, ["active"]), patchClearEvent = () => {
|
|
6420
|
+
const experiment = [experimentId], variants = [experimentNameOverride];
|
|
6421
|
+
return [sanity.unset(experiment), sanity.unset(variants)];
|
|
6422
|
+
}, handleClearAction = () => {
|
|
6423
|
+
const clearEvents = patchClearEvent(), activeEvent = patchActiveFalseEvent();
|
|
6424
|
+
onChange([activeEvent, ...clearEvents]);
|
|
6425
|
+
};
|
|
6423
6426
|
return {
|
|
6424
|
-
title:
|
|
6427
|
+
title: `Remove ${experimentNameOverride}`,
|
|
6425
6428
|
type: "action",
|
|
6426
6429
|
icon: CloseIcon,
|
|
6427
|
-
onAction:
|
|
6430
|
+
onAction: handleClearAction,
|
|
6428
6431
|
renderAsButton: !0
|
|
6429
6432
|
};
|
|
6430
|
-
}, newActions = ({
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
})
|
|
6437
|
-
const
|
|
6433
|
+
}, newActions = ({
|
|
6434
|
+
onChange,
|
|
6435
|
+
inputId,
|
|
6436
|
+
active,
|
|
6437
|
+
experimentNameOverride,
|
|
6438
|
+
experimentId
|
|
6439
|
+
}) => {
|
|
6440
|
+
const removeAction = sanity.defineDocumentFieldAction({
|
|
6441
|
+
name: `Remove ${experimentNameOverride}`,
|
|
6442
|
+
useAction: (props) => useRemoveExperimentAction({
|
|
6443
|
+
onChange,
|
|
6444
|
+
experimentNameOverride,
|
|
6445
|
+
experimentId
|
|
6446
|
+
})
|
|
6447
|
+
}), addAction = sanity.defineDocumentFieldAction({
|
|
6448
|
+
name: `Add ${experimentNameOverride}`,
|
|
6449
|
+
useAction: (props) => useAddExperimentAction({
|
|
6450
|
+
onChange,
|
|
6451
|
+
experimentNameOverride
|
|
6452
|
+
})
|
|
6453
|
+
});
|
|
6454
|
+
return active ? removeAction : addAction;
|
|
6455
|
+
}, ExperimentField = (props) => {
|
|
6456
|
+
const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId } = props, active = props.value?.active, oldActions = props.actions || [], withActionProps = {
|
|
6438
6457
|
...props,
|
|
6439
|
-
actions: [
|
|
6458
|
+
actions: [
|
|
6459
|
+
newActions({ onChange, inputId, active, experimentNameOverride, experimentId }),
|
|
6460
|
+
...oldActions
|
|
6461
|
+
]
|
|
6440
6462
|
};
|
|
6441
6463
|
return props.renderDefault(withActionProps);
|
|
6442
6464
|
}, Select = (props) => {
|
|
@@ -6468,7 +6490,10 @@ const useAddExperimentAction = (props) => {
|
|
|
6468
6490
|
title: experiment.label,
|
|
6469
6491
|
value: experiment.id
|
|
6470
6492
|
})), ExperimentInput = (props) => {
|
|
6471
|
-
const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
|
|
6493
|
+
const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), aditionalChangePath = react.useMemo(
|
|
6494
|
+
() => [...props.path.slice(0, -1), props.variantNameOverride],
|
|
6495
|
+
[props.variantNameOverride, props.path]
|
|
6496
|
+
), subValues = sanity.useFormValue(aditionalChangePath), { patch } = sanity.useDocumentOperation(id.replace("drafts.", ""), props.schemaType.name), handleChange = react.useCallback(
|
|
6472
6497
|
(event, onChange) => {
|
|
6473
6498
|
const inputValue = event.currentTarget.value;
|
|
6474
6499
|
if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
|
|
@@ -6481,6 +6506,14 @@ const useAddExperimentAction = (props) => {
|
|
|
6481
6506
|
[patch, subValues, aditionalChangePath]
|
|
6482
6507
|
);
|
|
6483
6508
|
return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, {});
|
|
6509
|
+
}, VariantInput = (props) => {
|
|
6510
|
+
const defaultValue = sanity.useFormValue([props.path[0], "default"]), handleClick = () => {
|
|
6511
|
+
props.onChange(sanity.set(defaultValue, ["value"]));
|
|
6512
|
+
};
|
|
6513
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
6514
|
+
props.renderDefault(props),
|
|
6515
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
|
|
6516
|
+
] });
|
|
6484
6517
|
}, VariantPreview = (props) => {
|
|
6485
6518
|
const [subtitle, setSubtitle] = react.useState(void 0), [title, setTitle] = react.useState(void 0), [media, setMedia] = react.useState(void 0), client = sanity.useClient({ apiVersion: "2025-01-01" }), { experiments } = useExperimentContext(), { experiment, variant, value } = props, selectedExperiment = experiments.find((experimentItem) => experimentItem.id === experiment), selectedVariant = selectedExperiment?.variants.find((variantItem) => variantItem.id === variant);
|
|
6486
6519
|
react.useEffect(() => {
|
|
@@ -6528,15 +6561,27 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
6528
6561
|
return [...acc, thisFieldWithPath];
|
|
6529
6562
|
}, []);
|
|
6530
6563
|
}
|
|
6531
|
-
const
|
|
6532
|
-
field
|
|
6564
|
+
const createExperimentType = ({
|
|
6565
|
+
field,
|
|
6566
|
+
experimentNameOverride,
|
|
6567
|
+
variantNameOverride,
|
|
6568
|
+
variantId,
|
|
6569
|
+
variantArrayName,
|
|
6570
|
+
experimentId
|
|
6533
6571
|
}) => {
|
|
6534
|
-
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1),
|
|
6572
|
+
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
|
|
6535
6573
|
return sanity.defineType({
|
|
6536
|
-
name:
|
|
6574
|
+
name: `${experimentNameOverride}${usedName}`,
|
|
6537
6575
|
type: "object",
|
|
6538
6576
|
components: {
|
|
6539
|
-
field:
|
|
6577
|
+
field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6578
|
+
ExperimentField,
|
|
6579
|
+
{
|
|
6580
|
+
...props,
|
|
6581
|
+
experimentId,
|
|
6582
|
+
experimentNameOverride
|
|
6583
|
+
}
|
|
6584
|
+
)
|
|
6540
6585
|
},
|
|
6541
6586
|
fields: [
|
|
6542
6587
|
typeof field == "string" ? (
|
|
@@ -6558,184 +6603,146 @@ const createFieldType = ({
|
|
|
6558
6603
|
hidden: !0
|
|
6559
6604
|
}),
|
|
6560
6605
|
sanity.defineField({
|
|
6561
|
-
|
|
6562
|
-
name: "experimentId",
|
|
6606
|
+
name: experimentId,
|
|
6563
6607
|
type: "string",
|
|
6564
6608
|
components: {
|
|
6565
|
-
input: ExperimentInput
|
|
6609
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(ExperimentInput, { ...props, variantNameOverride })
|
|
6566
6610
|
},
|
|
6567
6611
|
hidden: ({ parent }) => !parent?.active
|
|
6568
6612
|
}),
|
|
6569
6613
|
sanity.defineField({
|
|
6570
|
-
name:
|
|
6614
|
+
name: variantArrayName,
|
|
6571
6615
|
type: "array",
|
|
6572
|
-
hidden: ({ parent }) => !parent?.experimentId,
|
|
6616
|
+
hidden: ({ parent }) => !parent?.[experimentId],
|
|
6573
6617
|
components: {
|
|
6574
|
-
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6618
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
6619
|
+
ArrayInput,
|
|
6620
|
+
{
|
|
6621
|
+
...props,
|
|
6622
|
+
variantName,
|
|
6623
|
+
variantId,
|
|
6624
|
+
experimentId
|
|
6625
|
+
}
|
|
6626
|
+
)
|
|
6575
6627
|
},
|
|
6576
6628
|
of: [
|
|
6577
6629
|
sanity.defineField({
|
|
6578
|
-
name:
|
|
6579
|
-
type:
|
|
6630
|
+
name: variantName,
|
|
6631
|
+
type: variantName
|
|
6580
6632
|
})
|
|
6581
6633
|
]
|
|
6582
6634
|
})
|
|
6583
6635
|
]
|
|
6584
6636
|
});
|
|
6585
|
-
},
|
|
6586
|
-
field
|
|
6637
|
+
}, createVariantType = ({
|
|
6638
|
+
field,
|
|
6639
|
+
variantNameOverride,
|
|
6640
|
+
variantId,
|
|
6641
|
+
experimentId
|
|
6587
6642
|
}) => {
|
|
6588
6643
|
const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
|
|
6589
6644
|
return sanity.defineType({
|
|
6590
|
-
name:
|
|
6591
|
-
title:
|
|
6645
|
+
name: `${variantNameOverride}${usedName}`,
|
|
6646
|
+
title: `${variantNameOverride} array ${usedName}`,
|
|
6592
6647
|
type: "object",
|
|
6593
6648
|
components: {
|
|
6594
|
-
preview: VariantPreview
|
|
6649
|
+
preview: VariantPreview,
|
|
6650
|
+
input: VariantInput
|
|
6595
6651
|
},
|
|
6596
6652
|
fields: [
|
|
6597
6653
|
{
|
|
6598
6654
|
type: "string",
|
|
6599
|
-
name:
|
|
6655
|
+
name: variantId,
|
|
6600
6656
|
readOnly: !0
|
|
6601
6657
|
},
|
|
6602
6658
|
{
|
|
6603
6659
|
type: "string",
|
|
6604
|
-
name:
|
|
6660
|
+
name: experimentId,
|
|
6605
6661
|
hidden: !0
|
|
6606
6662
|
},
|
|
6607
6663
|
typeof field == "string" ? (
|
|
6608
6664
|
// Define a simple field if all we have is the name as a string
|
|
6609
6665
|
sanity.defineField({
|
|
6610
6666
|
name: "value",
|
|
6611
|
-
type: field
|
|
6612
|
-
hidden: ({
|
|
6667
|
+
type: field
|
|
6668
|
+
// hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
|
|
6613
6669
|
})
|
|
6614
6670
|
) : (
|
|
6615
6671
|
// Pass in the configured options, but overwrite the name
|
|
6616
6672
|
{
|
|
6617
6673
|
...field,
|
|
6618
|
-
name: "value"
|
|
6619
|
-
hidden: ({
|
|
6674
|
+
name: "value"
|
|
6675
|
+
// hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
|
|
6620
6676
|
}
|
|
6621
6677
|
)
|
|
6622
6678
|
],
|
|
6623
6679
|
preview: {
|
|
6624
6680
|
select: {
|
|
6625
|
-
variant:
|
|
6626
|
-
experiment:
|
|
6681
|
+
variant: variantId,
|
|
6682
|
+
experiment: experimentId,
|
|
6627
6683
|
value: "value"
|
|
6628
6684
|
}
|
|
6629
6685
|
}
|
|
6630
6686
|
});
|
|
6631
|
-
}, fieldSchema = ({
|
|
6632
|
-
|
|
6633
|
-
|
|
6687
|
+
}, fieldSchema = ({
|
|
6688
|
+
fields,
|
|
6689
|
+
experimentNameOverride,
|
|
6690
|
+
variantNameOverride,
|
|
6691
|
+
variantId,
|
|
6692
|
+
variantArrayName,
|
|
6693
|
+
experimentId
|
|
6694
|
+
}) => [
|
|
6695
|
+
...fields.map(
|
|
6696
|
+
(field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
|
|
6697
|
+
),
|
|
6698
|
+
...fields.map(
|
|
6699
|
+
(field) => createExperimentType({
|
|
6700
|
+
field,
|
|
6701
|
+
experimentNameOverride,
|
|
6702
|
+
variantNameOverride,
|
|
6703
|
+
variantId,
|
|
6704
|
+
variantArrayName,
|
|
6705
|
+
experimentId
|
|
6706
|
+
})
|
|
6707
|
+
)
|
|
6634
6708
|
], fieldLevelExperiments = sanity.definePlugin((config) => {
|
|
6635
|
-
const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields,
|
|
6709
|
+
const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
|
|
6636
6710
|
return {
|
|
6637
6711
|
name: "sanity-personalistaion-plugin-field-level-experiments",
|
|
6638
6712
|
schema: {
|
|
6639
|
-
types: fieldSchema({
|
|
6713
|
+
types: fieldSchema({
|
|
6714
|
+
fields,
|
|
6715
|
+
experimentNameOverride,
|
|
6716
|
+
variantNameOverride,
|
|
6717
|
+
variantId,
|
|
6718
|
+
variantArrayName,
|
|
6719
|
+
experimentId
|
|
6720
|
+
})
|
|
6640
6721
|
},
|
|
6641
6722
|
form: {
|
|
6642
6723
|
components: {
|
|
6643
6724
|
input: (props) => {
|
|
6644
6725
|
if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6645
6726
|
(field) => field.type.name
|
|
6646
|
-
).some(
|
|
6727
|
+
).some(
|
|
6728
|
+
(name) => name.startsWith(experimentNameOverride)
|
|
6729
|
+
))
|
|
6647
6730
|
return props.renderDefault(props);
|
|
6648
|
-
const providerProps = {
|
|
6731
|
+
const providerProps = {
|
|
6732
|
+
...props,
|
|
6733
|
+
experimentFieldPluginConfig: {
|
|
6734
|
+
...pluginConfig,
|
|
6735
|
+
variantId,
|
|
6736
|
+
variantArrayName,
|
|
6737
|
+
experimentId
|
|
6738
|
+
}
|
|
6739
|
+
};
|
|
6649
6740
|
return ExperimentProvider(providerProps);
|
|
6650
6741
|
}
|
|
6651
6742
|
}
|
|
6652
6743
|
}
|
|
6653
6744
|
};
|
|
6654
|
-
}), namespace = "growthbook", pluginConfigKeys = [
|
|
6655
|
-
{
|
|
6656
|
-
key: "apiKey",
|
|
6657
|
-
title: "Your secret API key"
|
|
6658
|
-
}
|
|
6659
|
-
], Secrets = (props) => {
|
|
6660
|
-
const { secrets, loading } = studioSecrets.useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = react.useState(!1);
|
|
6661
|
-
return react.useEffect(() => {
|
|
6662
|
-
if (!loading)
|
|
6663
|
-
return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
|
|
6664
|
-
}, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
6665
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
6666
|
-
studioSecrets.SettingsView,
|
|
6667
|
-
{
|
|
6668
|
-
title: "Growthbook secret",
|
|
6669
|
-
namespace,
|
|
6670
|
-
keys: pluginConfigKeys,
|
|
6671
|
-
onClose: () => {
|
|
6672
|
-
setShowSettings(!1);
|
|
6673
|
-
}
|
|
6674
|
-
}
|
|
6675
|
-
),
|
|
6676
|
-
props.renderDefault(props)
|
|
6677
|
-
] }) : props.renderDefault(props);
|
|
6678
|
-
}, getBooleanConversion = (value) => value === "true" ? "variant" : value === "false" ? "control" : value, getExperiments = async ({
|
|
6679
|
-
client,
|
|
6680
|
-
environment,
|
|
6681
|
-
baseUrl,
|
|
6682
|
-
project,
|
|
6683
|
-
convertBooleans
|
|
6684
|
-
}) => {
|
|
6685
|
-
const secret = await client.fetch("*[_id == 'secrets.growthbook'][0].secrets.apiKey");
|
|
6686
|
-
if (!secret) return [];
|
|
6687
|
-
const featureExperiments = [];
|
|
6688
|
-
let hasMore = !0, offset = 0;
|
|
6689
|
-
const url = new URL(baseUrl ?? "https://api.growthbook.io/api/v1/features");
|
|
6690
|
-
for (project && url.searchParams.set("projectId", project); hasMore; ) {
|
|
6691
|
-
url.searchParams.set("offset", offset.toString());
|
|
6692
|
-
const response = await fetch(url, {
|
|
6693
|
-
headers: {
|
|
6694
|
-
Authorization: `Bearer ${secret}`
|
|
6695
|
-
}
|
|
6696
|
-
}), { features, hasMore: responseHasMore, nextOffset } = await response.json();
|
|
6697
|
-
hasMore = responseHasMore, offset = nextOffset, features && features.forEach((feature) => {
|
|
6698
|
-
if (feature.archived)
|
|
6699
|
-
return;
|
|
6700
|
-
const experiments = feature.environments[environment]?.rules.filter(
|
|
6701
|
-
(experiment) => experiment.type === "experiment-ref" || experiment.type === "experiment"
|
|
6702
|
-
);
|
|
6703
|
-
if (!experiments)
|
|
6704
|
-
return;
|
|
6705
|
-
const variations = /* @__PURE__ */ new Set();
|
|
6706
|
-
experiments.forEach((experiment) => {
|
|
6707
|
-
experiment?.variations.forEach((variant) => {
|
|
6708
|
-
variations.add({
|
|
6709
|
-
id: convertBooleans ? getBooleanConversion(variant.value) : variant.value,
|
|
6710
|
-
label: convertBooleans ? getBooleanConversion(variant.value) : variant.value
|
|
6711
|
-
});
|
|
6712
|
-
});
|
|
6713
|
-
});
|
|
6714
|
-
const value = { id: feature.id, label: feature.id, variants: [...variations] };
|
|
6715
|
-
featureExperiments.push(value);
|
|
6716
|
-
});
|
|
6717
|
-
}
|
|
6718
|
-
return featureExperiments;
|
|
6719
|
-
}, growthbookFieldLevel = sanity.definePlugin((config) => {
|
|
6720
|
-
const { fields, environment, project, convertBooleans, baseUrl } = config;
|
|
6721
|
-
return {
|
|
6722
|
-
name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
|
|
6723
|
-
plugins: [
|
|
6724
|
-
fieldLevelExperiments({
|
|
6725
|
-
fields,
|
|
6726
|
-
experiments: (client) => getExperiments({ client, environment, baseUrl, project, convertBooleans })
|
|
6727
|
-
})
|
|
6728
|
-
],
|
|
6729
|
-
form: {
|
|
6730
|
-
components: {
|
|
6731
|
-
input: (props) => !(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6732
|
-
(field) => field.type.name
|
|
6733
|
-
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props)
|
|
6734
|
-
}
|
|
6735
|
-
}
|
|
6736
|
-
};
|
|
6737
6745
|
});
|
|
6738
6746
|
exports.fieldLevelExperiments = fieldLevelExperiments;
|
|
6739
6747
|
exports.flattenSchemaType = flattenSchemaType;
|
|
6740
|
-
exports.growthbookFieldLevel = growthbookFieldLevel;
|
|
6741
6748
|
//# sourceMappingURL=index.js.map
|