-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathuseTableViewValue.tsx
More file actions
46 lines (40 loc) · 1.41 KB
/
useTableViewValue.tsx
File metadata and controls
46 lines (40 loc) · 1.41 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
41
42
43
44
45
46
import React from 'react';
import * as cookies from 'lib/cookies';
import useFeatureValue from 'lib/growthbook/useFeatureValue';
import * as mixpanel from 'lib/mixpanel';
export default function useTableViewValue() {
const cookieValue = cookies.get(cookies.NAMES.TABLE_VIEW_ON_MOBILE);
const [ value, setValue ] = React.useState<boolean | undefined>(cookieValue ? cookieValue === 'true' : undefined);
const { value: featureFlag, isLoading: isFeatureLoading } = useFeatureValue('txns_view_exp', 'list_view');
const onToggle = React.useCallback(() => {
setValue((prev) => {
const nextValue = !prev;
cookies.set(cookies.NAMES.TABLE_VIEW_ON_MOBILE, nextValue ? 'true' : 'false');
mixpanel.logEvent(mixpanel.EventTypes.BUTTON_CLICK, {
Content: nextValue ? 'On' : 'Off',
Source: 'Table view',
});
return nextValue;
});
}, []);
React.useEffect(() => {
if (!isFeatureLoading) {
setValue((prev) => {
if (prev === undefined) {
return featureFlag === 'table_view';
}
return prev;
});
}
}, [ featureFlag, isFeatureLoading ]);
return React.useMemo(() => {
if (value !== undefined) {
return {
value,
isLoading: false,
onToggle,
};
}
return { value: featureFlag === 'table_view', isLoading: isFeatureLoading, onToggle };
}, [ featureFlag, isFeatureLoading, onToggle, value ]);
}