-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTabBar.tsx
More file actions
172 lines (160 loc) · 5.71 KB
/
TabBar.tsx
File metadata and controls
172 lines (160 loc) · 5.71 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* eslint-disable react/prop-types */
// Third party dependencies.
import React, { useCallback, useRef } from 'react';
import { View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useSelector } from 'react-redux';
// External dependencies.
import TabBarItem from '../TabBarItem';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import {
Box,
BoxFlexDirection,
BoxAlignItems,
} from '@metamask/design-system-react-native';
import Routes from '../../../../constants/navigation/Routes';
import { IconName } from '../../Icons/Icon';
import { MetaMetricsEvents } from '../../../../core/Analytics';
import { getDecimalChainId } from '../../../../util/networks';
import { useAnalytics } from '../../../../components/hooks/useAnalytics/useAnalytics';
import { strings } from '../../../../../locales/i18n';
// Internal dependencies.
import { TabBarProps, TabBarIconKey } from './TabBar.types';
import {
ICON_BY_TAB_BAR_ICON_KEY,
LABEL_BY_TAB_BAR_ICON_KEY,
} from './TabBar.constants';
import { selectChainId } from '../../../../selectors/networkController';
const FILLED_ICONS: Partial<Record<TabBarIconKey, IconName>> = {
[TabBarIconKey.Wallet]: IconName.HomeFilled,
[TabBarIconKey.Activity]: IconName.ClockFilled,
[TabBarIconKey.Trending]: IconName.Search,
[TabBarIconKey.Rewards]: IconName.MetamaskFoxFilled,
[TabBarIconKey.Money]: IconName.MusdFilled,
};
const TabBar = ({ state, descriptors, navigation }: TabBarProps) => {
const { trackEvent, createEventBuilder } = useAnalytics();
const { bottom: bottomInset } = useSafeAreaInsets();
const chainId = useSelector(selectChainId);
const tabBarRef = useRef(null);
const previousTabIndexRef = useRef<number>(state.index);
const tw = useTailwind();
const renderTabBarItem = useCallback(
(route: { name: string; key: string }, index: number) => {
const descriptor = descriptors[route.key];
if (!descriptor) return null;
const { options } = descriptor;
const tabBarIconKey = options.tabBarIconKey;
//TODO: use another option on add it to the prop interface
const callback = options.callback;
const rootScreenName = options.rootScreenName;
const key = `tab-bar-item-${tabBarIconKey}`; // this key is also used to identify elements for e2e testing
const isSelected = options?.isSelected
? options.isSelected(state.routeNames[state.index])
: state.index === index;
const baseIcon = ICON_BY_TAB_BAR_ICON_KEY[tabBarIconKey];
const icon = isSelected
? (FILLED_ICONS[tabBarIconKey] ?? baseIcon)
: baseIcon;
const labelKey = LABEL_BY_TAB_BAR_ICON_KEY[tabBarIconKey];
const labelText = labelKey ? strings(labelKey) : '';
const onPress = () => {
// Call onLeave callback for the previous tab before switching
if (previousTabIndexRef.current !== index) {
const previousRoute = state.routes[previousTabIndexRef.current];
const previousOptions = descriptors[previousRoute?.key]?.options;
previousOptions?.onLeave?.();
previousTabIndexRef.current = index;
}
callback?.();
switch (rootScreenName) {
case Routes.WALLET_VIEW:
navigation.navigate(Routes.WALLET.HOME, {
screen: Routes.WALLET_VIEW,
});
break;
case Routes.MODAL.WALLET_ACTIONS:
navigation.navigate(Routes.MODAL.ROOT_MODAL_FLOW, {
screen: Routes.MODAL.WALLET_ACTIONS,
});
trackEvent(
createEventBuilder(MetaMetricsEvents.ACTIONS_BUTTON_CLICKED)
.addProperties({
text: '',
chain_id: getDecimalChainId(chainId),
})
.build(),
);
break;
case Routes.BROWSER.VIEW:
navigation.navigate(Routes.BROWSER.HOME, {
screen: Routes.BROWSER.VIEW,
});
break;
case Routes.TRANSACTIONS_VIEW:
navigation.navigate(Routes.TRANSACTIONS_VIEW);
break;
case Routes.REWARDS_VIEW:
navigation.navigate(Routes.REWARDS_VIEW);
break;
case Routes.SETTINGS_VIEW:
navigation.navigate(Routes.SETTINGS_VIEW, {
screen: Routes.ACCOUNTS_MENU_VIEW,
});
break;
case Routes.TRENDING_VIEW:
navigation.navigate(Routes.TRENDING_VIEW);
break;
case Routes.MONEY.HOME:
navigation.navigate(Routes.MONEY.ROOT, {
screen: Routes.MONEY.HOME,
});
break;
}
};
const isWalletAction =
rootScreenName === Routes.MODAL.TRADE_WALLET_ACTIONS;
if (options?.isHidden) {
return null;
}
return (
<View key={key} style={tw.style('flex-1 w-full')}>
<TabBarItem
label={labelText}
iconName={icon}
onPress={onPress}
isActive={isSelected}
isTradeButton={isWalletAction}
testID={key}
/>
</View>
);
},
[
state,
descriptors,
navigation,
chainId,
trackEvent,
createEventBuilder,
tw,
],
);
const renderTabBarItems = useCallback(
() => state.routes.map(renderTabBarItem),
[state, renderTabBarItem],
);
return (
<View ref={tabBarRef}>
<Box
flexDirection={BoxFlexDirection.Row}
alignItems={BoxAlignItems.End}
twClassName="w-full pt-3 mb-1 px-2 bg-default border-t border-muted"
style={[tw.style(`pb-[${bottomInset}px]`)]}
>
{renderTabBarItems()}
</Box>
</View>
);
};
export default TabBar;