-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathExperimentsProvider.tsx
115 lines (102 loc) · 3.31 KB
/
ExperimentsProvider.tsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React from 'react';
import {
GrowthBook,
GrowthBookProvider,
useFeature as useGrowthbookFeature,
} from '@growthbook/growthbook-react';
import DeviceInfo from 'react-native-device-info';
import analyticsService from '~/common/services/analytics.service';
import mindsConfigService from '~/common/services/minds-config.service';
import sessionService from '~/common/services/session.service';
import { storages } from '~/common/services/storage/storages.service';
import { IS_IOS, IS_REVIEW } from '~/config/Config';
export const growthbook = new GrowthBook({
trackingCallback: (experiment, result) => {
const CACHE_KEY = `experiment:${experiment.key}`;
const date = storages.user?.getInt(CACHE_KEY);
if (date && date > Date.now() - 86400000) {
return; // Do not emit event
} else {
storages.user?.setInt(CACHE_KEY, Date.now());
}
if (!IS_REVIEW) {
analyticsService.addExperiment(experiment.key, result.variationId);
}
},
});
/**
* Return whether a feature has a given variation state.
* @param { string } featureKey - growthbook feature key.
* @param { string|number|boolean } variation - variation to check, e.g. 'on' or 'off'.
* @returns { boolean } - true if params reflect current variation.
*/
export function hasVariation(
featureKey: FeatureID | FeatureID[],
variation: string = 'on',
) {
return Array.isArray(featureKey)
? featureKey.every(key => growthbook.feature(key)[variation])
: growthbook.feature(featureKey)[variation];
}
export function IfHasVariation({
featureKey,
children,
}: React.PropsWithChildren<{
featureKey: FeatureID;
}>) {
const on = useIsFeatureOn(featureKey);
if (!on) {
return null;
}
return <>{children}</>;
}
/**
* Update growthbook's attributes and features
*/
export function updateGrowthBookAttributes() {
const user = sessionService.getUser();
const config = mindsConfigService.getSettings();
const userId = sessionService.token ? user?.guid : DeviceInfo.getUniqueId();
if (config.growthbook) {
growthbook.setFeatures(config.growthbook?.features);
growthbook.setAttributes({
...config.growthbook?.attributes,
...growthbook.getAttributes(),
loggedIn: Boolean(sessionService.token),
id: userId,
appVersion: DeviceInfo.getVersion(),
buildNumber: DeviceInfo.getBuildNumber(),
platform: IS_IOS ? 'ios' : 'android',
user: {
id: userId,
},
});
}
}
export function useIsFeatureOn(feature: FeatureID) {
return useGrowthbookFeature(feature).on;
}
export function useFeature(feature: FeatureID) {
return useGrowthbookFeature(feature);
}
export default function ExperimentsProvider({ children }) {
return (
<GrowthBookProvider growthbook={growthbook}>{children}</GrowthBookProvider>
);
}
export const useIsIOSFeatureOn = (feature: FeatureID) =>
useGrowthbookFeature(feature).on && IS_IOS;
export const useIsAndroidFeatureOn = (feature: FeatureID) =>
useGrowthbookFeature(feature).on && !IS_IOS;
export type FeatureID =
| 'minds-3055-email-codes'
| 'mob-discovery-redirect'
| 'mob-4424-sockets'
| 'mob-4472-in-app-verification'
| 'mob-4637-ios-hide-minds-superminds'
| 'mob-4638-boost-v3'
| 'mob-4722-track-code-push'
| 'mob-twitter-oauth-4715'
| 'engine-2503-twitter-feats'
| 'minds-3639-plus-notice'
| 'epic-303-boost-partners';