@sanity/personalization-plugin 2.3.0-launch-darkly.1 → 2.3.0-launch-darkly.2
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.d.mts +0 -194
- package/dist/index.d.ts +0 -194
- package/dist/index.js +43 -103
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +45 -106
- package/dist/index.mjs.map +1 -1
- package/dist/launchDarkly/index.d.mts +12 -0
- package/dist/launchDarkly/index.d.ts +12 -0
- package/dist/launchDarkly/index.js +103 -0
- package/dist/launchDarkly/index.js.map +1 -0
- package/dist/launchDarkly/index.mjs +107 -0
- package/dist/launchDarkly/index.mjs.map +1 -0
- package/package.json +6 -1
- package/src/components/ExperimentContext.tsx +4 -7
- package/src/components/ExperimentField.tsx +54 -28
- package/src/components/ExperimentInput.tsx +2 -2
- package/src/fieldExperiments.tsx +1 -0
- package/src/index.ts +0 -1
- package/src/launchDarkly/components/LaunchDarklyContext.tsx +36 -0
- package/src/{components → launchDarkly/components}/Secrets.tsx +4 -3
- package/src/launchDarkly/index.ts +52 -0
- package/src/launchDarkly/types.ts +193 -0
- package/src/{utils/launchDarkly.ts → launchDarkly/utils.ts} +2 -2
- package/src/types.ts +0 -185
- package/src/launchDarklyExperiments.tsx +0 -48
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
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) => {
|
|
@@ -6588,7 +6598,8 @@ const createExperimentType = ({
|
|
|
6588
6598
|
{
|
|
6589
6599
|
...props,
|
|
6590
6600
|
experimentId,
|
|
6591
|
-
experimentNameOverride
|
|
6601
|
+
experimentNameOverride,
|
|
6602
|
+
variantNameOverride
|
|
6592
6603
|
}
|
|
6593
6604
|
)
|
|
6594
6605
|
},
|
|
@@ -6758,81 +6769,9 @@ const createExperimentType = ({
|
|
|
6758
6769
|
}
|
|
6759
6770
|
}
|
|
6760
6771
|
};
|
|
6761
|
-
}), pluginConfigKeys = [
|
|
6762
|
-
{
|
|
6763
|
-
key: "apiKey",
|
|
6764
|
-
title: "Your secret API key"
|
|
6765
|
-
}
|
|
6766
|
-
], Secrets = (props, namespace) => {
|
|
6767
|
-
const { secrets, loading } = useSecrets(namespace), { setSecret } = useExperimentContext(), [showSettings, setShowSettings] = useState(!1);
|
|
6768
|
-
return useEffect(() => {
|
|
6769
|
-
if (!loading)
|
|
6770
|
-
return !secrets && !loading ? (setSecret(void 0), setShowSettings(!0)) : (setSecret(secrets.apiKey), setShowSettings(!1));
|
|
6771
|
-
}, [secrets, loading, setSecret]), showSettings ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
6772
|
-
/* @__PURE__ */ jsx(
|
|
6773
|
-
SettingsView,
|
|
6774
|
-
{
|
|
6775
|
-
title: `${namespace} api key`,
|
|
6776
|
-
namespace,
|
|
6777
|
-
keys: pluginConfigKeys,
|
|
6778
|
-
onClose: () => {
|
|
6779
|
-
setShowSettings(!1);
|
|
6780
|
-
}
|
|
6781
|
-
}
|
|
6782
|
-
),
|
|
6783
|
-
props.renderDefault(props)
|
|
6784
|
-
] }) : props.renderDefault(props);
|
|
6785
|
-
}, getExperiments = async ({
|
|
6786
|
-
client,
|
|
6787
|
-
projectKey,
|
|
6788
|
-
tags
|
|
6789
|
-
}) => {
|
|
6790
|
-
const secret = await client.fetch("*[_id == 'secrets.launchdarkly'][0].secrets.apiKey");
|
|
6791
|
-
if (!secret) return [];
|
|
6792
|
-
const url = new URL(`https://app.launchdarkly.com/api/v2/flags/${projectKey}`);
|
|
6793
|
-
tags && url.searchParams.set("filter", `tags:${tags.join("+")}`);
|
|
6794
|
-
const featureExperiments = [];
|
|
6795
|
-
let hasMore = !0;
|
|
6796
|
-
const offset = 0, limit = 10;
|
|
6797
|
-
for (; hasMore; ) {
|
|
6798
|
-
url.searchParams.set("offset", offset.toString()), url.searchParams.set("limit", limit.toString());
|
|
6799
|
-
const responseFlags = await fetch(url, {
|
|
6800
|
-
headers: {
|
|
6801
|
-
Authorization: secret
|
|
6802
|
-
}
|
|
6803
|
-
}), { items } = await responseFlags.json(), experiments = items.map((flag) => ({
|
|
6804
|
-
id: flag.key,
|
|
6805
|
-
label: flag.name,
|
|
6806
|
-
variants: flag.variations.map((variation) => ({
|
|
6807
|
-
id: variation.value,
|
|
6808
|
-
label: variation.name ?? variation.value
|
|
6809
|
-
}))
|
|
6810
|
-
}));
|
|
6811
|
-
featureExperiments.push(...experiments), items.length !== limit && (hasMore = !1);
|
|
6812
|
-
}
|
|
6813
|
-
return featureExperiments;
|
|
6814
|
-
}, launchDarklyFieldLevel = definePlugin((config) => {
|
|
6815
|
-
const { fields, projectKey, tags } = config;
|
|
6816
|
-
return {
|
|
6817
|
-
name: "sanity-growthbook-personalistaion-plugin-field-level-experiments",
|
|
6818
|
-
plugins: [
|
|
6819
|
-
fieldLevelExperiments({
|
|
6820
|
-
fields,
|
|
6821
|
-
experiments: (client) => getExperiments({ client, projectKey, tags })
|
|
6822
|
-
})
|
|
6823
|
-
],
|
|
6824
|
-
form: {
|
|
6825
|
-
components: {
|
|
6826
|
-
input: (props) => !(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
6827
|
-
(field) => field.type.name
|
|
6828
|
-
).some((name) => name.startsWith("experiment")) ? props.renderDefault(props) : Secrets(props, "launchdarkly")
|
|
6829
|
-
}
|
|
6830
|
-
}
|
|
6831
|
-
};
|
|
6832
6772
|
});
|
|
6833
6773
|
export {
|
|
6834
6774
|
fieldLevelExperiments,
|
|
6835
|
-
flattenSchemaType
|
|
6836
|
-
launchDarklyFieldLevel
|
|
6775
|
+
flattenSchemaType
|
|
6837
6776
|
};
|
|
6838
6777
|
//# sourceMappingURL=index.mjs.map
|