@sanity/personalization-plugin 2.5.0 → 3.0.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.
Files changed (48) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +570 -144
  3. package/dist/growthbook/index.d.ts +12 -15
  4. package/dist/growthbook/index.d.ts.map +1 -0
  5. package/dist/growthbook/index.js +104 -68
  6. package/dist/growthbook/index.js.map +1 -1
  7. package/dist/index.d.ts +212 -252
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +398 -301
  10. package/dist/index.js.map +1 -1
  11. package/dist/launchDarkly/index.d.ts +9 -12
  12. package/dist/launchDarkly/index.d.ts.map +1 -0
  13. package/dist/launchDarkly/index.js +74 -46
  14. package/dist/launchDarkly/index.js.map +1 -1
  15. package/package.json +35 -77
  16. package/dist/growthbook/index.d.mts +0 -15
  17. package/dist/growthbook/index.mjs +0 -124
  18. package/dist/growthbook/index.mjs.map +0 -1
  19. package/dist/index.d.mts +0 -267
  20. package/dist/index.mjs +0 -472
  21. package/dist/index.mjs.map +0 -1
  22. package/dist/launchDarkly/index.d.mts +0 -12
  23. package/dist/launchDarkly/index.mjs +0 -107
  24. package/dist/launchDarkly/index.mjs.map +0 -1
  25. package/sanity.json +0 -8
  26. package/src/components/Array.tsx +0 -68
  27. package/src/components/ExperimentContext.tsx +0 -65
  28. package/src/components/ExperimentField.tsx +0 -138
  29. package/src/components/ExperimentInput.tsx +0 -75
  30. package/src/components/ExperimentItem.tsx +0 -10
  31. package/src/components/Select.tsx +0 -43
  32. package/src/components/VariantInput.tsx +0 -19
  33. package/src/components/VariantPreview.tsx +0 -75
  34. package/src/fieldExperiments.tsx +0 -266
  35. package/src/growthbook/Components/GrowthbookContext.tsx +0 -38
  36. package/src/growthbook/Components/Secrets.tsx +0 -47
  37. package/src/growthbook/index.ts +0 -54
  38. package/src/growthbook/types.ts +0 -15
  39. package/src/growthbook/utils.ts +0 -94
  40. package/src/index.ts +0 -3
  41. package/src/launchDarkly/components/LaunchDarklyContext.tsx +0 -36
  42. package/src/launchDarkly/components/Secrets.tsx +0 -46
  43. package/src/launchDarkly/index.ts +0 -52
  44. package/src/launchDarkly/types.ts +0 -193
  45. package/src/launchDarkly/utils.ts +0 -54
  46. package/src/types.ts +0 -245
  47. package/src/utils/flattenSchemaType.ts +0 -47
  48. package/v2-incompatible.js +0 -11
package/dist/index.js CHANGED
@@ -1,10 +1,13 @@
1
- "use strict";
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");
4
- function _interopDefaultCompat(e) {
5
- return e && typeof e == "object" && "default" in e ? e : { default: e };
6
- }
7
- var equal__default = /* @__PURE__ */ _interopDefaultCompat(equal);
1
+ import { jsx, jsxs } from "react/jsx-runtime";
2
+ import { useClient, useWorkspace, useFormValue, defineDocumentFieldAction, set, unset, getPublishedId, useDocumentOperation, isReference, isImage, isDocumentSchemaType, definePlugin, isObjectInputProps, defineType, defineField } from "sanity";
3
+ import { c } from "react/compiler-runtime";
4
+ import { Button, Stack, Inline, Select as Select$1, Card, Text } from "@sanity/ui";
5
+ import { uuid } from "@sanity/uuid";
6
+ import equal from "fast-deep-equal";
7
+ import { createContext, useContext, useState, useEffect } from "react";
8
+ import { suspend } from "suspend-react";
9
+ import { CloseIcon } from "@sanity/icons";
10
+ import { GiSoapExperiment } from "react-icons/gi";
8
11
  const CONFIG_DEFAULT = {
9
12
  fields: [],
10
13
  apiVersion: "2024-11-07",
@@ -13,97 +16,126 @@ const CONFIG_DEFAULT = {
13
16
  variantId: "variantId",
14
17
  variantArrayName: "variants",
15
18
  experimentId: "experimentId"
16
- }, ExperimentContext = react.createContext({
19
+ }, ExperimentContext = createContext({
17
20
  ...CONFIG_DEFAULT,
18
21
  experiments: []
19
22
  });
20
23
  function useExperimentContext() {
21
- return react.useContext(ExperimentContext);
24
+ return useContext(ExperimentContext);
22
25
  }
23
26
  function ExperimentProvider(props) {
24
- const { experimentFieldPluginConfig } = props, client = sanity.useClient({ apiVersion: experimentFieldPluginConfig.apiVersion }), workspace = sanity.useWorkspace(), experiments = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspendReact.suspend(
25
- // eslint-disable-next-line require-await
26
- async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments,
27
- [workspace],
28
- { equal: equal__default.default }
29
- ), context = react.useMemo(
30
- () => ({ ...experimentFieldPluginConfig, experiments }),
31
- [experimentFieldPluginConfig, experiments]
32
- );
33
- return /* @__PURE__ */ jsxRuntime.jsx(ExperimentContext.Provider, { value: context, children: props.renderDefault(props) });
27
+ const $ = c(14), {
28
+ experimentFieldPluginConfig
29
+ } = props;
30
+ let t0;
31
+ $[0] !== experimentFieldPluginConfig.apiVersion ? (t0 = {
32
+ apiVersion: experimentFieldPluginConfig.apiVersion
33
+ }, $[0] = experimentFieldPluginConfig.apiVersion, $[1] = t0) : t0 = $[1];
34
+ const client = useClient(t0), workspace = useWorkspace();
35
+ let t1;
36
+ $[2] !== client || $[3] !== experimentFieldPluginConfig || $[4] !== workspace ? (t1 = Array.isArray(experimentFieldPluginConfig.experiments) ? experimentFieldPluginConfig.experiments : suspend(async () => typeof experimentFieldPluginConfig.experiments == "function" ? experimentFieldPluginConfig.experiments(client) : experimentFieldPluginConfig.experiments, [workspace], {
37
+ equal
38
+ }), $[2] = client, $[3] = experimentFieldPluginConfig, $[4] = workspace, $[5] = t1) : t1 = $[5];
39
+ const experiments = t1;
40
+ let t2;
41
+ $[6] !== experimentFieldPluginConfig || $[7] !== experiments ? (t2 = {
42
+ ...experimentFieldPluginConfig,
43
+ experiments
44
+ }, $[6] = experimentFieldPluginConfig, $[7] = experiments, $[8] = t2) : t2 = $[8];
45
+ const context = t2;
46
+ let t3;
47
+ $[9] !== props ? (t3 = props.renderDefault(props), $[9] = props, $[10] = t3) : t3 = $[10];
48
+ let t4;
49
+ return $[11] !== context || $[12] !== t3 ? (t4 = /* @__PURE__ */ jsx(ExperimentContext.Provider, { value: context, children: t3 }), $[11] = context, $[12] = t3, $[13] = t4) : t4 = $[13], t4;
34
50
  }
35
51
  const ArrayInput = (props) => {
36
- const fieldPath = props.path.slice(0, -1), { onItemAppend, variantName, variantId, experimentId } = props, experimentValue = sanity.useFormValue([...fieldPath, experimentId]), { experiments } = useExperimentContext(), handleClick = react.useCallback(
37
- async (variant) => {
38
- const item = {
39
- _key: uuid.uuid(),
40
- [variantId]: variant.id,
41
- [experimentId]: experimentValue,
42
- _type: variantName
43
- };
44
- onItemAppend(item);
45
- },
46
- [variantId, experimentId, experimentValue, variantName, onItemAppend]
47
- ), filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant) => variant[variantId]);
48
- return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
49
- props.renderDefault({ ...props, arrayFunctions: () => null }),
50
- /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: filteredVariants.map((variant) => /* @__PURE__ */ jsxRuntime.jsx(
51
- ui.Button,
52
- {
53
- text: `Add ${variant.label}`,
54
- mode: "ghost",
55
- disabled: usedVariants?.includes(variant.id),
56
- onClick: () => handleClick(variant)
57
- },
58
- `${experimentValue}-${variant.id}`
59
- )) })
60
- ] });
61
- }, CloseIcon = /* @__PURE__ */ react.forwardRef(function(props, ref) {
62
- return /* @__PURE__ */ jsxRuntime.jsx(
63
- "svg",
64
- {
65
- "data-sanity-icon": "close",
66
- width: "1em",
67
- height: "1em",
68
- viewBox: "0 0 25 25",
69
- fill: "none",
70
- xmlns: "http://www.w3.org/2000/svg",
52
+ const $ = c(35);
53
+ let experimentId, onItemAppend, t0, t1, variantId, variantName;
54
+ if ($[0] !== props) {
55
+ const fieldPath = props.path.slice(0, -1);
56
+ ({
57
+ onItemAppend,
58
+ variantName,
59
+ variantId,
60
+ experimentId
61
+ } = props), t0 = useFormValue, t1 = [...fieldPath, experimentId], $[0] = props, $[1] = experimentId, $[2] = onItemAppend, $[3] = t0, $[4] = t1, $[5] = variantId, $[6] = variantName;
62
+ } else
63
+ experimentId = $[1], onItemAppend = $[2], t0 = $[3], t1 = $[4], variantId = $[5], variantName = $[6];
64
+ const experimentValue = t0(t1), {
65
+ experiments
66
+ } = useExperimentContext();
67
+ let t2;
68
+ $[7] !== experimentId || $[8] !== experimentValue || $[9] !== onItemAppend || $[10] !== variantId || $[11] !== variantName ? (t2 = async (variant) => {
69
+ const item = {
70
+ _key: uuid(),
71
+ [variantId]: variant.id,
72
+ [experimentId]: experimentValue,
73
+ _type: variantName
74
+ };
75
+ onItemAppend(item);
76
+ }, $[7] = experimentId, $[8] = experimentValue, $[9] = onItemAppend, $[10] = variantId, $[11] = variantName, $[12] = t2) : t2 = $[12];
77
+ const handleClick = t2;
78
+ let T0, T1, t3, t4, t5, t6;
79
+ if ($[13] !== experimentValue || $[14] !== experiments || $[15] !== handleClick || $[16] !== props || $[17] !== variantId) {
80
+ const filteredVariants = experiments.find((option) => option.id === experimentValue)?.variants || [], usedVariants = (props.value || [])?.map((variant_0) => variant_0[variantId]);
81
+ T1 = Stack, t5 = 3, $[24] !== props ? (t6 = props.renderDefault({
71
82
  ...props,
72
- ref,
73
- children: /* @__PURE__ */ jsxRuntime.jsx(
74
- "path",
75
- {
76
- d: "M18 7L7 18M7 7L18 18",
77
- stroke: "currentColor",
78
- strokeWidth: 1.2,
79
- strokeLinejoin: "round"
80
- }
81
- )
82
- }
83
- );
84
- }), useAddExperimentAction = (props) => {
85
- const { onChange, active, experimentNameOverride } = props, handleAddAction = react.useCallback(() => {
86
- onChange([sanity.set(!active, ["active"])]);
87
- }, [onChange, active]);
88
- return {
89
- title: `Add ${experimentNameOverride}`,
83
+ arrayFunctions: _temp$2
84
+ }), $[24] = props, $[25] = t6) : t6 = $[25], T0 = Inline, t3 = 1, t4 = filteredVariants.map((variant_1) => /* @__PURE__ */ jsx(Button, { text: `Add ${variant_1.label}`, mode: "ghost", disabled: usedVariants?.includes(variant_1.id), onClick: () => handleClick(variant_1) }, `${experimentValue}-${variant_1.id}`)), $[13] = experimentValue, $[14] = experiments, $[15] = handleClick, $[16] = props, $[17] = variantId, $[18] = T0, $[19] = T1, $[20] = t3, $[21] = t4, $[22] = t5, $[23] = t6;
85
+ } else
86
+ T0 = $[18], T1 = $[19], t3 = $[20], t4 = $[21], t5 = $[22], t6 = $[23];
87
+ let t7;
88
+ $[26] !== T0 || $[27] !== t3 || $[28] !== t4 ? (t7 = /* @__PURE__ */ jsx(T0, { gap: t3, children: t4 }), $[26] = T0, $[27] = t3, $[28] = t4, $[29] = t7) : t7 = $[29];
89
+ let t8;
90
+ return $[30] !== T1 || $[31] !== t5 || $[32] !== t6 || $[33] !== t7 ? (t8 = /* @__PURE__ */ jsxs(T1, { gap: t5, children: [
91
+ t6,
92
+ t7
93
+ ] }), $[30] = T1, $[31] = t5, $[32] = t6, $[33] = t7, $[34] = t8) : t8 = $[34], t8;
94
+ };
95
+ function _temp$2() {
96
+ return null;
97
+ }
98
+ const useAddExperimentAction = (props) => {
99
+ const $ = c(6), {
100
+ onChange,
101
+ active,
102
+ experimentNameOverride
103
+ } = props;
104
+ let t0;
105
+ $[0] !== active || $[1] !== onChange ? (t0 = () => {
106
+ onChange([set(!active, ["active"])]);
107
+ }, $[0] = active, $[1] = onChange, $[2] = t0) : t0 = $[2];
108
+ const handleAddAction = t0, t1 = `Add ${experimentNameOverride}`;
109
+ let t2;
110
+ return $[3] !== handleAddAction || $[4] !== t1 ? (t2 = {
111
+ title: t1,
90
112
  type: "action",
91
- icon: gi.GiSoapExperiment,
113
+ icon: GiSoapExperiment,
92
114
  onAction: handleAddAction,
93
115
  renderAsButton: !0
94
- };
116
+ }, $[3] = handleAddAction, $[4] = t1, $[5] = t2) : t2 = $[5], t2;
95
117
  }, useRemoveExperimentAction = (props) => {
96
- const { onChange, active, experimentId, experimentNameOverride, variantNameOverride } = props, handleClearAction = react.useCallback(() => {
118
+ const $ = c(8), {
119
+ onChange,
120
+ active,
121
+ experimentId,
122
+ experimentNameOverride,
123
+ variantNameOverride
124
+ } = props;
125
+ let t0;
126
+ $[0] !== active || $[1] !== experimentId || $[2] !== onChange || $[3] !== variantNameOverride ? (t0 = () => {
97
127
  const activeId = ["active"], experiment = [experimentId], variants = [`${variantNameOverride}s`];
98
- onChange([sanity.set(!active, activeId), sanity.unset(experiment), sanity.unset(variants)]);
99
- }, [onChange, active, experimentId, variantNameOverride]);
100
- return {
101
- title: `Remove ${experimentNameOverride}`,
128
+ onChange([set(!active, activeId), unset(experiment), unset(variants)]);
129
+ }, $[0] = active, $[1] = experimentId, $[2] = onChange, $[3] = variantNameOverride, $[4] = t0) : t0 = $[4];
130
+ const handleClearAction = t0, t1 = `Remove ${experimentNameOverride}`;
131
+ let t2;
132
+ return $[5] !== handleClearAction || $[6] !== t1 ? (t2 = {
133
+ title: t1,
102
134
  type: "action",
103
135
  icon: CloseIcon,
104
136
  onAction: handleClearAction,
105
137
  renderAsButton: !0
106
- };
138
+ }, $[5] = handleClearAction, $[6] = t1, $[7] = t2) : t2 = $[7], t2;
107
139
  }, createActions = ({
108
140
  onChange,
109
141
  inputId,
@@ -112,7 +144,7 @@ const ArrayInput = (props) => {
112
144
  experimentId,
113
145
  variantNameOverride
114
146
  }) => {
115
- const removeAction = sanity.defineDocumentFieldAction({
147
+ const removeAction = defineDocumentFieldAction({
116
148
  name: `Remove ${experimentNameOverride}`,
117
149
  useAction: (props) => useRemoveExperimentAction({
118
150
  active: !0,
@@ -121,7 +153,7 @@ const ArrayInput = (props) => {
121
153
  experimentId,
122
154
  variantNameOverride
123
155
  })
124
- }), addAction = sanity.defineDocumentFieldAction({
156
+ }), addAction = defineDocumentFieldAction({
125
157
  name: `Add ${experimentNameOverride}`,
126
158
  useAction: (props) => useAddExperimentAction({
127
159
  active: !1,
@@ -131,134 +163,221 @@ const ArrayInput = (props) => {
131
163
  });
132
164
  return active ? removeAction : addAction;
133
165
  }, ExperimentField = (props) => {
134
- const { onChange } = props.inputProps, { inputId, experimentNameOverride, experimentId, variantNameOverride } = props, active = props.value?.active, actionProps = react.useMemo(
135
- () => ({
136
- onChange,
137
- inputId,
138
- active,
139
- experimentNameOverride,
140
- experimentId,
141
- variantNameOverride
142
- }),
143
- [onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride]
144
- ), memoizedActions = react.useMemo(() => {
145
- const oldActions = props.actions || [];
146
- return [createActions(actionProps), ...oldActions];
147
- }, [actionProps, props.actions]), withActionProps = react.useMemo(
148
- () => ({
149
- ...props,
150
- actions: memoizedActions
151
- }),
152
- [props, memoizedActions]
153
- );
154
- return props.renderDefault(withActionProps);
166
+ const $ = c(20), {
167
+ onChange
168
+ } = props.inputProps, {
169
+ inputId,
170
+ experimentNameOverride,
171
+ experimentId,
172
+ variantNameOverride
173
+ } = props, active = props.value?.active;
174
+ let t0;
175
+ $[0] !== active || $[1] !== experimentId || $[2] !== experimentNameOverride || $[3] !== inputId || $[4] !== onChange || $[5] !== variantNameOverride ? (t0 = {
176
+ onChange,
177
+ inputId,
178
+ active,
179
+ experimentNameOverride,
180
+ experimentId,
181
+ variantNameOverride
182
+ }, $[0] = active, $[1] = experimentId, $[2] = experimentNameOverride, $[3] = inputId, $[4] = onChange, $[5] = variantNameOverride, $[6] = t0) : t0 = $[6];
183
+ const actionProps = t0;
184
+ let t1;
185
+ $[7] !== props.actions ? (t1 = props.actions || [], $[7] = props.actions, $[8] = t1) : t1 = $[8];
186
+ const oldActions = t1;
187
+ let t2;
188
+ $[9] !== actionProps ? (t2 = createActions(actionProps), $[9] = actionProps, $[10] = t2) : t2 = $[10];
189
+ let t3;
190
+ $[11] !== oldActions || $[12] !== t2 ? (t3 = [t2, ...oldActions], $[11] = oldActions, $[12] = t2, $[13] = t3) : t3 = $[13];
191
+ const memoizedActions = t3;
192
+ let t4;
193
+ $[14] !== memoizedActions || $[15] !== props ? (t4 = {
194
+ ...props,
195
+ actions: memoizedActions
196
+ }, $[14] = memoizedActions, $[15] = props, $[16] = t4) : t4 = $[16];
197
+ const withActionProps = t4;
198
+ let t5;
199
+ return $[17] !== props.renderDefault || $[18] !== withActionProps ? (t5 = props.renderDefault(withActionProps), $[17] = props.renderDefault, $[18] = withActionProps, $[19] = t5) : t5 = $[19], t5;
155
200
  }, Select = (props) => {
156
- const {
201
+ const $ = c(12), {
157
202
  value,
158
- // Current field value
159
203
  onChange,
160
- // Method to handle patch events,
161
204
  elementProps,
162
205
  listOptions,
163
206
  handleChange
164
207
  } = props;
165
- return /* @__PURE__ */ jsxRuntime.jsxs(
166
- ui.Select,
167
- {
168
- ...elementProps,
169
- fontSize: 2,
170
- padding: 3,
171
- space: [3, 3, 4],
172
- value: value || "",
173
- onChange: (event) => handleChange(event, onChange),
174
- children: [
175
- /* @__PURE__ */ jsxRuntime.jsx("option", { value: "", children: "Select an option..." }),
176
- listOptions.map(({ value: optionValue, title }) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: optionValue, children: title }, optionValue))
177
- ]
178
- }
179
- );
180
- }, formatlistOptions = (experiments) => experiments.map((experiment) => ({
208
+ let t0;
209
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t0 = [3, 3, 4], $[0] = t0) : t0 = $[0];
210
+ const t1 = value || "";
211
+ let t2;
212
+ $[1] !== handleChange || $[2] !== onChange ? (t2 = (event) => handleChange(event, onChange), $[1] = handleChange, $[2] = onChange, $[3] = t2) : t2 = $[3];
213
+ let t3;
214
+ $[4] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t3 = /* @__PURE__ */ jsx("option", { value: "", children: "Select an option..." }), $[4] = t3) : t3 = $[4];
215
+ let t4;
216
+ $[5] !== listOptions ? (t4 = listOptions.map(_temp$1), $[5] = listOptions, $[6] = t4) : t4 = $[6];
217
+ let t5;
218
+ return $[7] !== elementProps || $[8] !== t1 || $[9] !== t2 || $[10] !== t4 ? (t5 = /* @__PURE__ */ jsxs(Select$1, { ...elementProps, fontSize: 2, padding: 3, gap: t0, value: t1, onChange: t2, children: [
219
+ t3,
220
+ t4
221
+ ] }), $[7] = elementProps, $[8] = t1, $[9] = t2, $[10] = t4, $[11] = t5) : t5 = $[11], t5;
222
+ };
223
+ function _temp$1(t0) {
224
+ const {
225
+ value: optionValue,
226
+ title
227
+ } = t0;
228
+ return /* @__PURE__ */ jsx("option", { value: optionValue, children: title }, optionValue);
229
+ }
230
+ const formatlistOptions = (experiments) => experiments.map((experiment) => ({
181
231
  title: experiment.label,
182
232
  value: experiment.id
183
233
  })), ExperimentInput = (props) => {
184
- const { experiments } = useExperimentContext(), id = sanity.useFormValue(["_id"]), additionalChangePath = react.useMemo(
185
- () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
186
- [props.variantNameOverride, props.path]
187
- ), subValues = sanity.useFormValue(additionalChangePath), { patch } = sanity.useDocumentOperation(sanity.getPublishedId(id), props.schemaType.name), handleChange = react.useCallback(
188
- (event, onChange) => {
189
- const inputValue = event.currentTarget.value;
190
- if (onChange(inputValue ? sanity.set(inputValue) : sanity.unset()), subValues) {
191
- const patchEvent = {
192
- unset: [additionalChangePath.join(".")]
193
- };
194
- patch.execute([patchEvent]);
195
- }
196
- },
197
- [patch, subValues, additionalChangePath]
198
- );
199
- return experiments.length ? /* @__PURE__ */ jsxRuntime.jsx(Select, { ...props, listOptions: formatlistOptions(experiments), handleChange }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { padding: [3, 3, 4], radius: 2, shadow: 1, tone: "caution", children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { align: "center", size: [2, 2, 3], children: [
200
- "There are no defined ",
201
- props.experimentNameOverride,
202
- "s"
203
- ] }) });
234
+ const $ = c(20), {
235
+ experiments
236
+ } = useExperimentContext();
237
+ let t0;
238
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t0 = ["_id"], $[0] = t0) : t0 = $[0];
239
+ const id = useFormValue(t0);
240
+ let t1;
241
+ $[1] !== props.path || $[2] !== props.variantNameOverride ? (t1 = [...props.path.slice(0, -1), `${props.variantNameOverride}s`], $[1] = props.path, $[2] = props.variantNameOverride, $[3] = t1) : t1 = $[3];
242
+ const additionalChangePath = t1, subValues = useFormValue(additionalChangePath);
243
+ let t2;
244
+ $[4] !== id ? (t2 = getPublishedId(id), $[4] = id, $[5] = t2) : t2 = $[5];
245
+ const {
246
+ patch
247
+ } = useDocumentOperation(t2, props.schemaType.name);
248
+ let t3;
249
+ $[6] !== additionalChangePath || $[7] !== patch || $[8] !== subValues ? (t3 = (event, onChange) => {
250
+ const inputValue = event.currentTarget.value;
251
+ if (onChange(inputValue ? set(inputValue) : unset()), subValues) {
252
+ const patchEvent = {
253
+ unset: [additionalChangePath.join(".")]
254
+ };
255
+ patch.execute([patchEvent]);
256
+ }
257
+ }, $[6] = additionalChangePath, $[7] = patch, $[8] = subValues, $[9] = t3) : t3 = $[9];
258
+ const handleChange = t3;
259
+ if (!experiments.length) {
260
+ let t42;
261
+ $[10] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t42 = [3, 3, 4], $[10] = t42) : t42 = $[10];
262
+ let t52;
263
+ $[11] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t52 = [2, 2, 3], $[11] = t52) : t52 = $[11];
264
+ let t6;
265
+ return $[12] !== props.experimentNameOverride ? (t6 = /* @__PURE__ */ jsx(Card, { padding: t42, radius: 2, shadow: 1, tone: "caution", children: /* @__PURE__ */ jsxs(Text, { align: "center", size: t52, children: [
266
+ "There are no defined ",
267
+ props.experimentNameOverride,
268
+ "s"
269
+ ] }) }), $[12] = props.experimentNameOverride, $[13] = t6) : t6 = $[13], t6;
270
+ }
271
+ let t4;
272
+ $[14] !== experiments ? (t4 = formatlistOptions(experiments), $[14] = experiments, $[15] = t4) : t4 = $[15];
273
+ let t5;
274
+ return $[16] !== handleChange || $[17] !== props || $[18] !== t4 ? (t5 = /* @__PURE__ */ jsx(Select, { ...props, listOptions: t4, handleChange }), $[16] = handleChange, $[17] = props, $[18] = t4, $[19] = t5) : t5 = $[19], t5;
204
275
  }, ExperimentItem = (props) => {
205
- const { active } = props.value;
206
- return active || props.inputProps.onChange(sanity.set(!0, ["active"])), props.renderDefault(props);
276
+ const {
277
+ active
278
+ } = props.value;
279
+ return active || props.inputProps.onChange(set(!0, ["active"])), props.renderDefault(props);
207
280
  }, VariantInput = (props) => {
208
- const experimentPath = props.path.slice(0, -2), defaultValue = sanity.useFormValue([...experimentPath, "default"]), handleClick = () => {
209
- props.onChange(sanity.set(defaultValue, ["value"]));
210
- };
211
- return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
212
- props.renderDefault(props),
213
- /* @__PURE__ */ jsxRuntime.jsx(ui.Inline, { space: 1, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) })
214
- ] });
281
+ const $ = c(13);
282
+ let t0, t1;
283
+ if ($[0] !== props.path) {
284
+ const experimentPath = props.path.slice(0, -2);
285
+ t0 = useFormValue, t1 = [...experimentPath, "default"], $[0] = props.path, $[1] = t0, $[2] = t1;
286
+ } else
287
+ t0 = $[1], t1 = $[2];
288
+ const defaultValue = t0(t1);
289
+ let t2;
290
+ $[3] !== defaultValue || $[4] !== props ? (t2 = () => {
291
+ props.onChange(set(defaultValue, ["value"]));
292
+ }, $[3] = defaultValue, $[4] = props, $[5] = t2) : t2 = $[5];
293
+ const handleClick = t2;
294
+ let t3;
295
+ $[6] !== props ? (t3 = props.renderDefault(props), $[6] = props, $[7] = t3) : t3 = $[7];
296
+ let t4;
297
+ $[8] !== handleClick ? (t4 = /* @__PURE__ */ jsx(Inline, { gap: 1, children: /* @__PURE__ */ jsx(Button, { text: "Copy default", mode: "ghost", onClick: () => handleClick() }) }), $[8] = handleClick, $[9] = t4) : t4 = $[9];
298
+ let t5;
299
+ return $[10] !== t3 || $[11] !== t4 ? (t5 = /* @__PURE__ */ jsxs(Stack, { gap: 3, children: [
300
+ t3,
301
+ t4
302
+ ] }), $[10] = t3, $[11] = t4, $[12] = t5) : t5 = $[12], t5;
215
303
  }, VariantPreview = (props) => {
216
- 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);
217
- react.useEffect(() => {
304
+ const $ = c(25), [subtitle, setSubtitle] = useState(void 0), [title, setTitle] = useState(void 0), [media, setMedia] = useState(void 0);
305
+ let t0;
306
+ $[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t0 = {
307
+ apiVersion: "2025-01-01"
308
+ }, $[0] = t0) : t0 = $[0];
309
+ const client = useClient(t0), {
310
+ experiments
311
+ } = useExperimentContext(), {
312
+ experiment,
313
+ variant,
314
+ value
315
+ } = props;
316
+ let selectedExperiment, t1;
317
+ if ($[1] !== experiment || $[2] !== experiments || $[3] !== variant) {
318
+ let t22;
319
+ $[6] !== experiment ? (t22 = (experimentItem) => experimentItem.id === experiment, $[6] = experiment, $[7] = t22) : t22 = $[7], selectedExperiment = experiments.find(t22), t1 = selectedExperiment?.variants.find((variantItem) => variantItem.id === variant), $[1] = experiment, $[2] = experiments, $[3] = variant, $[4] = selectedExperiment, $[5] = t1;
320
+ } else
321
+ selectedExperiment = $[4], t1 = $[5];
322
+ const selectedVariant = t1;
323
+ let t2;
324
+ $[8] !== client || $[9] !== props.schemaType || $[10] !== selectedExperiment?.label || $[11] !== selectedVariant?.label || $[12] !== value ? (t2 = () => {
218
325
  (async () => {
219
326
  if (setTitle(`${selectedExperiment?.label} - ${selectedVariant?.label}`), typeof value == "string")
220
327
  return setSubtitle(value);
221
- if (sanity.isReference(value)) {
222
- const doc = await client.getDocument(value._ref), referenceType = (props.schemaType.fields.find((field) => field.name === "value")?.type).to.find((field) => field.type?.name === doc?._type), selectFields = {}, previewFields = referenceType?.preview?.select || {};
328
+ if (isReference(value)) {
329
+ const doc = await client.getDocument(value._ref), referenceType = (props.schemaType.fields.find(_temp)?.type).to.find((field_0) => field_0.type?.name === doc?._type), selectFields = {}, previewFields = referenceType?.preview?.select || {};
223
330
  Object.keys(previewFields).forEach((key) => {
224
331
  const valueKey = referenceType?.preview?.select?.[key];
225
- selectFields[key] = valueKey && doc ? valueKey?.split(".").reduce((acc, index) => acc[index], doc) : void 0;
332
+ selectFields[key] = valueKey && doc ? valueKey?.split(".").reduce(_temp2, doc) : void 0;
226
333
  });
227
334
  const previewContent = referenceType?.preview?.prepare?.(selectFields);
228
- return setMedia(previewContent?.media || selectFields.media), setSubtitle(previewContent?.title || selectFields?.title);
335
+ return setMedia(previewContent?.media || selectFields.media), setSubtitle(previewContent?.title || selectFields.title);
229
336
  }
230
- return sanity.isImage(value) && setMedia(value), "";
337
+ return isImage(value) && setMedia(value), "";
231
338
  })();
232
- }, [value, client, selectedExperiment?.label, selectedVariant?.label, props.schemaType]);
233
- const previewProps = {
234
- ...props,
235
- title,
236
- subtitle,
237
- media
238
- };
239
- return props.renderDefault(previewProps);
339
+ }, $[8] = client, $[9] = props.schemaType, $[10] = selectedExperiment?.label, $[11] = selectedVariant?.label, $[12] = value, $[13] = t2) : t2 = $[13];
340
+ const t3 = selectedExperiment?.label, t4 = selectedVariant?.label;
341
+ let t5;
342
+ $[14] !== client || $[15] !== props.schemaType || $[16] !== t3 || $[17] !== t4 || $[18] !== value ? (t5 = [value, client, t3, t4, props.schemaType], $[14] = client, $[15] = props.schemaType, $[16] = t3, $[17] = t4, $[18] = value, $[19] = t5) : t5 = $[19], useEffect(t2, t5);
343
+ let t6;
344
+ if ($[20] !== media || $[21] !== props || $[22] !== subtitle || $[23] !== title) {
345
+ const previewProps = {
346
+ ...props,
347
+ title,
348
+ subtitle,
349
+ media
350
+ };
351
+ t6 = props.renderDefault(previewProps), $[20] = media, $[21] = props, $[22] = subtitle, $[23] = title, $[24] = t6;
352
+ } else
353
+ t6 = $[24];
354
+ return t6;
240
355
  };
356
+ function _temp(field) {
357
+ return field.name === "value";
358
+ }
359
+ function _temp2(acc, index) {
360
+ return acc[index];
361
+ }
241
362
  function flattenSchemaType(schemaType) {
242
- return sanity.isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 5) : (console.error("Schema type is not a document"), []);
363
+ return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 5) : (console.error("Schema type is not a document"), []);
243
364
  }
244
365
  function extractInnerFields(fields, path, maxDepth) {
245
- return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
246
- const thisFieldWithPath = { path: [...path, field.name], ...field };
247
- if (field.type.jsonType === "object") {
248
- const innerFields = extractInnerFields(field.type.fields, [...path, field.name], maxDepth);
249
- return [...acc, thisFieldWithPath, ...innerFields];
250
- } else if (field.type.jsonType === "array") {
251
- const innerFields = (field.type.of || []).reduce((arrayAcc, arrayType) => {
252
- if ("fields" in arrayType) {
253
- const typeFields = extractInnerFields(arrayType.fields, [...path, field.name], maxDepth);
254
- return [...arrayAcc, ...typeFields];
255
- }
256
- return arrayAcc;
257
- }, []);
258
- return [...acc, thisFieldWithPath, ...innerFields];
366
+ if (path.length >= maxDepth)
367
+ return [];
368
+ const result = [];
369
+ for (const field of fields)
370
+ if (result.push({
371
+ path: [...path, field.name],
372
+ ...field
373
+ }), field.type.jsonType === "object")
374
+ result.push(...extractInnerFields(field.type.fields, [...path, field.name], maxDepth));
375
+ else if (field.type.jsonType === "array") {
376
+ const arrayTypes = field.type.of || [];
377
+ for (const arrayType of arrayTypes)
378
+ "fields" in arrayType && result.push(...extractInnerFields(arrayType.fields, [...path, field.name], maxDepth));
259
379
  }
260
- return [...acc, thisFieldWithPath];
261
- }, []);
380
+ return result;
262
381
  }
263
382
  const createExperimentType = ({
264
383
  field,
@@ -268,86 +387,63 @@ const createExperimentType = ({
268
387
  variantArrayName,
269
388
  experimentId
270
389
  }) => {
271
- const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1), variantName = `${variantNameOverride}${usedName}`;
272
- return sanity.defineType({
390
+ const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + typeName.slice(1), variantName = `${variantNameOverride}${usedName}`;
391
+ return defineType({
273
392
  name: `${experimentNameOverride}${usedName}`,
274
393
  type: "object",
275
394
  components: {
276
- field: (props) => /* @__PURE__ */ jsxRuntime.jsx(
277
- ExperimentField,
278
- {
279
- ...props,
280
- experimentId,
281
- experimentNameOverride,
282
- variantNameOverride
283
- }
284
- ),
395
+ field: (props) => /* @__PURE__ */ jsx(ExperimentField, { ...props, experimentId, experimentNameOverride, variantNameOverride }),
285
396
  item: ExperimentItem
286
397
  },
287
- fields: [
288
- typeof field == "string" ? (
289
- // Define a simple field if all we have is the name as a string
290
- sanity.defineField({
291
- name: "default",
292
- type: field
293
- })
294
- ) : (
295
- // Pass in the configured options, but overwrite the name
296
- {
297
- ...field,
298
- name: "default"
299
- }
300
- ),
301
- sanity.defineField({
302
- name: "active",
303
- type: "boolean",
304
- hidden: !0,
305
- initialValue: !1
306
- }),
307
- sanity.defineField({
308
- name: experimentId,
309
- type: "string",
310
- components: {
311
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
312
- ExperimentInput,
313
- {
314
- ...props,
315
- experimentNameOverride,
316
- variantNameOverride
317
- }
318
- )
319
- },
320
- hidden: ({ parent }) => !parent?.active
321
- }),
322
- sanity.defineField({
323
- name: variantArrayName,
324
- type: "array",
325
- hidden: ({ parent }) => !parent?.[experimentId],
326
- components: {
327
- input: (props) => /* @__PURE__ */ jsxRuntime.jsx(
328
- ArrayInput,
329
- {
330
- ...props,
331
- variantName,
332
- variantId,
333
- experimentId
334
- }
335
- )
336
- },
337
- of: [
338
- sanity.defineField({
339
- name: variantName,
340
- type: variantName
341
- })
342
- ]
398
+ fields: [typeof field == "string" ? (
399
+ // Define a simple field if all we have is the name as a string
400
+ defineField({
401
+ name: "default",
402
+ type: field
343
403
  })
344
- ],
404
+ ) : (
405
+ // Pass in the configured options, but overwrite the name
406
+ {
407
+ ...field,
408
+ name: "default"
409
+ }
410
+ ), defineField({
411
+ name: "active",
412
+ type: "boolean",
413
+ hidden: !0,
414
+ initialValue: !1
415
+ }), defineField({
416
+ name: experimentId,
417
+ type: "string",
418
+ components: {
419
+ input: (props) => /* @__PURE__ */ jsx(ExperimentInput, { ...props, experimentNameOverride, variantNameOverride })
420
+ },
421
+ hidden: ({
422
+ parent
423
+ }) => !parent?.active
424
+ }), defineField({
425
+ name: variantArrayName,
426
+ type: "array",
427
+ hidden: ({
428
+ parent
429
+ }) => !parent?.[experimentId],
430
+ components: {
431
+ input: (props) => /* @__PURE__ */ jsx(ArrayInput, { ...props, variantName, variantId, experimentId })
432
+ },
433
+ of: [defineField({
434
+ name: variantName,
435
+ type: variantName
436
+ })]
437
+ })],
345
438
  preview: {
346
439
  select: {
347
440
  base: "default",
348
441
  experiment: experimentId
349
442
  },
350
- prepare: ({ base, experiment }) => {
443
+ prepare: ({
444
+ base,
445
+ experiment
446
+ }) => {
351
447
  const title = base?.title || base?.name || typeof base == "string" ? base : "", experimentTitle = experiment ? `Experiment: ${experiment}` : "", media = base?.image || base?.photo || base?.media || "";
352
448
  return {
353
449
  title: title || experimentTitle,
@@ -363,8 +459,8 @@ const createExperimentType = ({
363
459
  variantId,
364
460
  experimentId
365
461
  }) => {
366
- const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + String(typeName).slice(1);
367
- return sanity.defineType({
462
+ const typeName = typeof field == "string" ? field : field.name, usedName = String(typeName[0]).toUpperCase() + typeName.slice(1);
463
+ return defineType({
368
464
  name: `${variantNameOverride}${usedName}`,
369
465
  title: `${variantNameOverride} array ${usedName}`,
370
466
  type: "object",
@@ -372,33 +468,29 @@ const createExperimentType = ({
372
468
  preview: VariantPreview,
373
469
  input: VariantInput
374
470
  },
375
- fields: [
376
- {
377
- type: "string",
378
- name: variantId,
379
- readOnly: !0
380
- },
471
+ fields: [{
472
+ type: "string",
473
+ name: variantId,
474
+ readOnly: !0
475
+ }, {
476
+ type: "string",
477
+ name: experimentId,
478
+ hidden: !0
479
+ }, typeof field == "string" ? (
480
+ // Define a simple field if all we have is the name as a string
481
+ defineField({
482
+ name: "value",
483
+ type: field
484
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
485
+ })
486
+ ) : (
487
+ // Pass in the configured options, but overwrite the name
381
488
  {
382
- type: "string",
383
- name: experimentId,
384
- hidden: !0
385
- },
386
- typeof field == "string" ? (
387
- // Define a simple field if all we have is the name as a string
388
- sanity.defineField({
389
- name: "value",
390
- type: field
391
- // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
392
- })
393
- ) : (
394
- // Pass in the configured options, but overwrite the name
395
- {
396
- ...field,
397
- name: "value"
398
- // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
399
- }
400
- )
401
- ],
489
+ ...field,
490
+ name: "value"
491
+ // hidden: ({parent}) => !parent?.[`${objectNameOverride}Id`],
492
+ }
493
+ )],
402
494
  preview: {
403
495
  select: {
404
496
  variant: variantId,
@@ -414,22 +506,27 @@ const createExperimentType = ({
414
506
  variantId,
415
507
  variantArrayName,
416
508
  experimentId
417
- }) => [
418
- ...fields.map(
419
- (field) => createVariantType({ field, variantNameOverride, variantId, experimentId })
420
- ),
421
- ...fields.map(
422
- (field) => createExperimentType({
423
- field,
424
- experimentNameOverride,
425
- variantNameOverride,
426
- variantId,
427
- variantArrayName,
428
- experimentId
429
- })
430
- )
431
- ], fieldLevelExperiments = sanity.definePlugin((config) => {
432
- const pluginConfig = { ...CONFIG_DEFAULT, ...config }, { fields, experimentNameOverride, variantNameOverride } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
509
+ }) => [...fields.map((field) => createVariantType({
510
+ field,
511
+ variantNameOverride,
512
+ variantId,
513
+ experimentId
514
+ })), ...fields.map((field) => createExperimentType({
515
+ field,
516
+ experimentNameOverride,
517
+ variantNameOverride,
518
+ variantId,
519
+ variantArrayName,
520
+ experimentId
521
+ }))], fieldLevelExperiments = definePlugin((config) => {
522
+ const pluginConfig = {
523
+ ...CONFIG_DEFAULT,
524
+ ...config
525
+ }, {
526
+ fields,
527
+ experimentNameOverride,
528
+ variantNameOverride
529
+ } = pluginConfig, experimentId = `${experimentNameOverride}Id`, variantArrayName = `${variantNameOverride}s`, variantId = `${variantNameOverride}Id`;
433
530
  return {
434
531
  name: "sanity-personalistaion-plugin-field-level-experiments",
435
532
  schema: {
@@ -445,9 +542,7 @@ const createExperimentType = ({
445
542
  form: {
446
543
  components: {
447
544
  input: (props) => {
448
- if (!(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).some(
449
- (field) => field.type.name.startsWith(experimentNameOverride) || field.name.startsWith(experimentNameOverride)
450
- ))
545
+ if (!(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).some((field) => field.type.name.startsWith(experimentNameOverride) || field.name.startsWith(experimentNameOverride)))
451
546
  return props.renderDefault(props);
452
547
  const providerProps = {
453
548
  ...props,
@@ -464,6 +559,8 @@ const createExperimentType = ({
464
559
  }
465
560
  };
466
561
  });
467
- exports.fieldLevelExperiments = fieldLevelExperiments;
468
- exports.flattenSchemaType = flattenSchemaType;
562
+ export {
563
+ fieldLevelExperiments,
564
+ flattenSchemaType
565
+ };
469
566
  //# sourceMappingURL=index.js.map