@sanity/personalization-plugin 2.2.0 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/personalization-plugin",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "description": "Plugin to help with personalization, a/b testing when using Sanity",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,4 +1,5 @@
1
1
  import {CloseIcon} from '@sanity/icons'
2
+ import {useCallback, useMemo} from 'react'
2
3
  import {GiSoapExperiment} from 'react-icons/gi'
3
4
  import {
4
5
  defineDocumentFieldAction,
@@ -18,9 +19,9 @@ const useAddExperimentAction = (
18
19
  ): DocumentFieldActionItem => {
19
20
  const {onChange, active, experimentNameOverride} = props
20
21
 
21
- const handleAddAction = () => {
22
+ const handleAddAction = useCallback(() => {
22
23
  onChange([set(!active, ['active'])])
23
- }
24
+ }, [onChange, active])
24
25
 
25
26
  return {
26
27
  title: `Add ${experimentNameOverride}`,
@@ -33,23 +34,21 @@ const useAddExperimentAction = (
33
34
 
34
35
  const useRemoveExperimentAction = (
35
36
  props: DocumentFieldActionProps &
36
- PatchStuff & {experimentNameOverride: string; experimentId: string; active: boolean},
37
+ PatchStuff & {
38
+ experimentNameOverride: string
39
+ experimentId: string
40
+ active: boolean
41
+ variantNameOverride: string
42
+ },
37
43
  ): DocumentFieldActionItem => {
38
- const {onChange, active, experimentId, experimentNameOverride} = props
39
- const patchActiveFalseEvent = () => {
44
+ const {onChange, active, experimentId, experimentNameOverride, variantNameOverride} = props
45
+ const handleClearAction = useCallback(() => {
40
46
  const activeId = ['active']
41
- return set(!active, activeId)
42
- }
43
- const patchClearEvent = () => {
44
47
  const experiment = [experimentId]
45
- const variants = [experimentNameOverride]
46
- return [unset(experiment), unset(variants)]
47
- }
48
- const handleClearAction = () => {
49
- const clearEvents = patchClearEvent()
50
- const activeEvent = patchActiveFalseEvent()
51
- onChange([activeEvent, ...clearEvents])
52
- }
48
+ const variants = [`${variantNameOverride}s`]
49
+ onChange([set(!active, activeId), unset(experiment), unset(variants)])
50
+ }, [onChange, active, experimentId, experimentNameOverride, variantNameOverride])
51
+
53
52
  return {
54
53
  title: `Remove ${experimentNameOverride}`,
55
54
  type: 'action',
@@ -59,13 +58,19 @@ const useRemoveExperimentAction = (
59
58
  }
60
59
  }
61
60
 
62
- const newActions = ({
61
+ const createActions = ({
63
62
  onChange,
64
63
  inputId,
65
64
  active,
66
65
  experimentNameOverride,
67
66
  experimentId,
68
- }: PatchStuff & {active?: boolean; experimentNameOverride: string; experimentId: string}) => {
67
+ variantNameOverride,
68
+ }: PatchStuff & {
69
+ active?: boolean
70
+ experimentNameOverride: string
71
+ experimentId: string
72
+ variantNameOverride: string
73
+ }) => {
69
74
  const removeAction = defineDocumentFieldAction({
70
75
  name: `Remove ${experimentNameOverride}`,
71
76
  useAction: (props) =>
@@ -76,6 +81,7 @@ const newActions = ({
76
81
  inputId,
77
82
  experimentNameOverride,
78
83
  experimentId,
84
+ variantNameOverride,
79
85
  }),
80
86
  })
81
87
  const addAction = defineDocumentFieldAction({
@@ -97,20 +103,39 @@ const newActions = ({
97
103
  }
98
104
 
99
105
  export const ExperimentField = (
100
- props: ObjectFieldProps & {experimentNameOverride: string; experimentId: string},
106
+ props: ObjectFieldProps & {
107
+ experimentNameOverride: string
108
+ experimentId: string
109
+ variantNameOverride: string
110
+ },
101
111
  ) => {
102
112
  const {onChange} = props.inputProps
103
- const {inputId, experimentNameOverride, experimentId} = props
113
+ const {inputId, experimentNameOverride, experimentId, variantNameOverride} = props
104
114
  const active = props.value?.active as boolean | undefined
105
115
 
106
- const oldActions = props.actions || []
116
+ const actionProps = useMemo(
117
+ () => ({
118
+ onChange,
119
+ inputId,
120
+ active,
121
+ experimentNameOverride,
122
+ experimentId,
123
+ variantNameOverride,
124
+ }),
125
+ [onChange, inputId, active, experimentNameOverride, experimentId, variantNameOverride],
126
+ )
107
127
 
108
- const withActionProps = {
109
- ...props,
110
- actions: [
111
- newActions({onChange, inputId, active, experimentNameOverride, experimentId}),
112
- ...oldActions,
113
- ],
114
- }
128
+ const memoizedActions = useMemo(() => {
129
+ const oldActions = props.actions || []
130
+ return [createActions(actionProps), ...oldActions]
131
+ }, [actionProps, props.actions])
132
+
133
+ const withActionProps = useMemo(
134
+ () => ({
135
+ ...props,
136
+ actions: memoizedActions,
137
+ }),
138
+ [props, memoizedActions],
139
+ )
115
140
  return props.renderDefault(withActionProps)
116
141
  }
@@ -2,6 +2,7 @@ import {Card, Text} from '@sanity/ui'
2
2
  import {FormEvent, useCallback, useMemo} from 'react'
3
3
  import {
4
4
  FormPatch,
5
+ getPublishedId,
5
6
  PatchEvent,
6
7
  set,
7
8
  StringInputProps,
@@ -27,13 +28,13 @@ export const ExperimentInput = (
27
28
  const {experiments} = useExperimentContext()
28
29
 
29
30
  const id = useFormValue(['_id']) as string
30
- const aditionalChangePath = useMemo(
31
- () => [...props.path.slice(0, -1), props.variantNameOverride],
31
+ const additionalChangePath = useMemo(
32
+ () => [...props.path.slice(0, -1), `${props.variantNameOverride}s`],
32
33
  [props.variantNameOverride, props.path],
33
34
  )
34
- const subValues = useFormValue(aditionalChangePath)
35
+ const subValues = useFormValue(additionalChangePath)
35
36
 
36
- const {patch} = useDocumentOperation(id.replace('drafts.', ''), props.schemaType.name)
37
+ const {patch} = useDocumentOperation(getPublishedId(id), props.schemaType.name)
37
38
 
38
39
  const handleChange = useCallback(
39
40
  (
@@ -51,12 +52,12 @@ export const ExperimentInput = (
51
52
 
52
53
  if (subValues) {
53
54
  const patchEvent = {
54
- unset: [aditionalChangePath.join('.')],
55
+ unset: [additionalChangePath.join('.')],
55
56
  }
56
57
  patch.execute([patchEvent])
57
58
  }
58
59
  },
59
- [patch, subValues, aditionalChangePath],
60
+ [patch, subValues, additionalChangePath],
60
61
  )
61
62
 
62
63
  if (!experiments.length)
@@ -44,6 +44,7 @@ const createExperimentType = ({
44
44
  {...props}
45
45
  experimentId={experimentId}
46
46
  experimentNameOverride={experimentNameOverride}
47
+ variantNameOverride={variantNameOverride}
47
48
  />
48
49
  ),
49
50
  },