forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABTestWrapper.js
More file actions
40 lines (36 loc) · 1.02 KB
/
ABTestWrapper.js
File metadata and controls
40 lines (36 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React from 'react'
import { any, string } from 'prop-types'
import { useUserVar } from '../utilities/userState'
export function ABTestVariant({ children }) {
return <>{children}</>
}
export function ABTestWrapper({ children, insiderVariantName = 'B' }) {
const {
currentUser: { insideUser },
} = useUserVar()
let childIndex = 0
// only one variant
if (!children.length) {
if (insideUser) {
if (children.props.name === insiderVariantName) return <>{children}</>
else return <></>
} else {
if (children.props.name !== insiderVariantName) return <>{children}</>
else return <></>
}
}
// A + B variants
if (insideUser) {
childIndex = children.findIndex((variant) => variant.props.name === insiderVariantName)
} else {
childIndex = children.findIndex((variant) => variant.props.name !== insiderVariantName)
}
return <>{children[childIndex]}</>
}
ABTestVariant.propTypes = {
children: any,
}
ABTestWrapper.propTypes = {
insiderVariantName: string,
children: any,
}