-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathBatchSellReview.tsx
More file actions
300 lines (276 loc) · 9.63 KB
/
BatchSellReview.tsx
File metadata and controls
300 lines (276 loc) · 9.63 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { useNavigation } from '@react-navigation/native';
import React, { useCallback, useEffect, useState } from 'react';
import { ScrollView } from 'react-native-gesture-handler';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useDispatch, useSelector } from 'react-redux';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import {
AvatarToken,
AvatarTokenSize,
Box,
BoxAlignItems,
BoxFlexDirection,
BoxJustifyContent,
Button,
ButtonSize,
ButtonVariant,
FontWeight,
HeaderStandard,
Icon,
IconColor,
IconName,
IconSize,
Text,
TextColor,
TextVariant,
} from '@metamask/design-system-react-native';
import { strings } from '../../../../../../locales/i18n';
import Routes from '../../../../../constants/navigation/Routes';
import { Skeleton } from '../../../../../component-library/components-temp/Skeleton';
import {
resetBridgeState,
selectBatchSellSlippages,
selectBatchSellDestToken,
selectBatchSellDestStablecoins,
selectBatchSellSourceTokens,
setBatchSellDestToken,
setBatchSellSourceTokens,
setBatchSellTokenSlippages,
} from '../../../../../core/redux/slices/bridge';
import { RootState } from '../../../../../reducers';
import { BridgeToken } from '../../types';
import { getBridgeTokenAssetId } from '../../utils/tokenUtils';
import { getBatchSellInitialSlippage } from '../../components/SlippageModal/utils';
import { BatchSellReviewSelectorsIDs } from './BatchSellReview.testIds';
import { BatchSellReviewTokenRow } from './BatchSellReviewTokenRow';
const DEFAULT_PERCENT = 100;
const UNKNOWN_DESTINATION_TOKEN_SYMBOL = 'UNKNOWN';
// TODO(SWAPS-4439): When Batch Sell quote fetching is wired, pass
// batchSellSlippages[assetId] into each token's BridgeController quote request.
const HAS_QUOTES = false;
const getTokenKey = (token: BridgeToken) => `${token.chainId}:${token.address}`;
function areBatchSellSlippageMapsEqual(
first: Record<string, string | undefined>,
second: Record<string, string | undefined>,
) {
const firstKeys = Object.keys(first);
const secondKeys = Object.keys(second);
return (
firstKeys.length === secondKeys.length &&
firstKeys.every(
(assetId) =>
Object.prototype.hasOwnProperty.call(second, assetId) &&
first[assetId] === second[assetId],
)
);
}
export function BatchSellReview() {
const navigation = useNavigation();
const dispatch = useDispatch();
const tw = useTailwind();
const selectedTokens = useSelector(selectBatchSellSourceTokens);
const sourceChainId = selectedTokens[0]?.chainId;
const destinationTokens = useSelector((state: RootState) =>
selectBatchSellDestStablecoins(state, sourceChainId),
);
const selectedDestinationToken = useSelector(selectBatchSellDestToken);
const batchSellSlippages = useSelector(selectBatchSellSlippages);
const isRemoveTokenDisabled = selectedTokens.length <= 2;
const [percentsByTokenKey, setPercentsByTokenKey] = useState<
Record<string, number>
>({});
// Seed the selected destination token on entry so the pill always reads from Redux.
useEffect(() => {
if (destinationTokens[0] && !selectedDestinationToken) {
dispatch(setBatchSellDestToken(destinationTokens[0]));
}
}, [destinationTokens, dispatch, selectedDestinationToken]);
// Keep local percents aligned with selected tokens while defaulting new tokens to 100%.
useEffect(() => {
setPercentsByTokenKey((currentPercents) =>
selectedTokens.reduce<Record<string, number>>((nextPercents, token) => {
const tokenKey = getTokenKey(token);
nextPercents[tokenKey] = currentPercents[tokenKey] ?? DEFAULT_PERCENT;
return nextPercents;
}, {}),
);
}, [selectedTokens]);
// Reset bridge state when component unmounts.
useEffect(
() => () => {
dispatch(resetBridgeState());
},
[dispatch],
);
useEffect(() => {
// Keep Redux slippages aligned with selected tokens when the user removes tokens.
const nextSlippage = selectedTokens.reduce<
Record<string, string | undefined>
>((slippageByAssetId, token) => {
const assetId = getBridgeTokenAssetId(token);
if (!assetId) return slippageByAssetId;
slippageByAssetId[assetId] = getBatchSellInitialSlippage(
batchSellSlippages,
assetId,
);
return slippageByAssetId;
}, {});
if (!areBatchSellSlippageMapsEqual(batchSellSlippages, nextSlippage)) {
dispatch(setBatchSellTokenSlippages(nextSlippage));
}
}, [batchSellSlippages, dispatch, selectedTokens]);
const handlePercentChange = useCallback(
(tokenKey: string, percent: number) => {
setPercentsByTokenKey((currentPercents) => ({
...currentPercents,
[tokenKey]: percent,
}));
},
[],
);
const handleBackPress = useCallback(() => {
navigation.goBack();
}, [navigation]);
const handleOpenDestinationTokenSelector = useCallback(() => {
navigation.navigate(Routes.BRIDGE.MODALS.ROOT, {
screen: Routes.BRIDGE.MODALS.BATCH_SELL_DESTINATION_TOKEN_SELECTOR_MODAL,
});
}, [navigation]);
const handleSlippagePress = useCallback(
(token: BridgeToken) => {
const assetId = getBridgeTokenAssetId(token);
if (!assetId) return;
navigation.navigate(Routes.BRIDGE.MODALS.ROOT, {
screen: Routes.BRIDGE.MODALS.BATCH_SELL_DEFAULT_SLIPPAGE_MODAL,
params: {
sourceChainId: token.chainId,
destChainId: selectedDestinationToken?.chainId,
batchSellAssetId: assetId,
},
});
},
[navigation, selectedDestinationToken?.chainId],
);
const handleRemoveToken = useCallback(
(tokenToRemove: BridgeToken) => {
if (isRemoveTokenDisabled) return;
const tokenKeyToRemove = getTokenKey(tokenToRemove);
const remainingTokens = selectedTokens.filter(
(token) => getTokenKey(token) !== tokenKeyToRemove,
);
dispatch(setBatchSellSourceTokens(remainingTokens));
},
[dispatch, isRemoveTokenDisabled, selectedTokens],
);
return (
<SafeAreaView
style={tw.style('flex-1 bg-default')}
edges={['bottom', 'left', 'right']}
>
<Box
testID={BatchSellReviewSelectorsIDs.CONTAINER}
twClassName="flex-1 bg-default"
>
<HeaderStandard title="" onBack={handleBackPress} includesTopInset />
<Box twClassName="px-4 pb-6">
<Box
flexDirection={BoxFlexDirection.Row}
alignItems={BoxAlignItems.Center}
gap={1}
>
<Text
variant={TextVariant.BodyMd}
fontWeight={FontWeight.Medium}
color={TextColor.TextDefault}
>
{strings('bridge.batch_sell_total_received')}
</Text>
<Icon
name={IconName.Info}
size={IconSize.Sm}
color={IconColor.IconDefault}
/>
</Box>
<Box
flexDirection={BoxFlexDirection.Row}
alignItems={BoxAlignItems.Center}
justifyContent={BoxJustifyContent.Between}
twClassName="mt-2"
>
<Skeleton
width={195}
height={50}
style={tw.style('rounded-lg')}
testID={BatchSellReviewSelectorsIDs.TOTAL_RECEIVED_SKELETON}
/>
<Button
variant={ButtonVariant.Secondary}
size={ButtonSize.Md}
testID={BatchSellReviewSelectorsIDs.DESTINATION_TOKEN_PILL}
onPress={handleOpenDestinationTokenSelector}
style={tw.style('rounded-xl px-3 py-3')}
>
<Box
flexDirection={BoxFlexDirection.Row}
alignItems={BoxAlignItems.Center}
gap={2}
>
<AvatarToken
name={
selectedDestinationToken?.symbol ??
UNKNOWN_DESTINATION_TOKEN_SYMBOL
}
src={
selectedDestinationToken?.image
? { uri: selectedDestinationToken.image }
: undefined
}
size={AvatarTokenSize.Sm}
/>
<Text
variant={TextVariant.BodyMd}
fontWeight={FontWeight.Medium}
color={TextColor.TextDefault}
>
{selectedDestinationToken?.symbol ??
UNKNOWN_DESTINATION_TOKEN_SYMBOL}
</Text>
</Box>
</Button>
</Box>
</Box>
<ScrollView
showsVerticalScrollIndicator={false}
contentContainerStyle={tw.style('pb-4')}
>
{selectedTokens.map((token) => {
const tokenKey = getTokenKey(token);
return (
<BatchSellReviewTokenRow
key={tokenKey}
token={token}
tokenKey={tokenKey}
percent={percentsByTokenKey[tokenKey] ?? DEFAULT_PERCENT}
onPercentChange={handlePercentChange}
onSlippagePress={handleSlippagePress}
onRemovePress={handleRemoveToken}
isRemoveTokenDisabled={isRemoveTokenDisabled}
/>
);
})}
</ScrollView>
<Box twClassName="border-t border-muted px-4 pb-4 pt-3">
<Button
variant={ButtonVariant.Primary}
size={ButtonSize.Lg}
isFullWidth
isDisabled={!HAS_QUOTES}
testID={BatchSellReviewSelectorsIDs.REVIEW_BUTTON}
>
{strings('bridge.batch_sell_review')}
</Button>
</Box>
</Box>
</SafeAreaView>
);
}