-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAddAsset.tsx
More file actions
108 lines (97 loc) · 3.86 KB
/
AddAsset.tsx
File metadata and controls
108 lines (97 loc) · 3.86 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
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { HeaderStandard } from '@metamask/design-system-react-native';
import {
SafeAreaView,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
import { useSelector } from 'react-redux';
import { strings } from '../../../../locales/i18n';
import { selectNetworkConfigurations } from '../../../selectors/networkController';
import { useNavigation } from '@react-navigation/native';
import { useParams } from '../../../util/navigation/navUtils';
import { BottomSheetRef } from '../../../component-library/components/BottomSheets/BottomSheet';
import { Hex } from '@metamask/utils';
import Engine from '../../../core/Engine';
import { SupportedCaipChainId } from '@metamask/multichain-network-controller';
import { isNonEvmChainId } from '../../../core/Multichain/utils';
import { useNetworkEnablement } from '../../hooks/useNetworkEnablement/useNetworkEnablement';
import NetworkListBottomSheet from './components/NetworkListBottomSheet/NetworkListBottomSheet';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import TokenView from './Views/TokenView/TokenView';
import NFTView from './Views/NFTView/NFTView';
export interface AddAssetParams {
assetType: 'token' | 'collectible';
collectibleContract?: {
address: string;
};
}
const AddAsset = () => {
const navigation = useNavigation();
const { assetType, collectibleContract } = useParams<AddAssetParams>();
const tw = useTailwind();
const insets = useSafeAreaInsets();
const networkConfigurations = useSelector(selectNetworkConfigurations);
const [openNetworkSelector, setOpenNetworkSelector] = useState(false);
const { enabledNetworksForAllNamespaces } = useNetworkEnablement();
const enabledChainId = useMemo(
() =>
Object.keys(enabledNetworksForAllNamespaces).find(
(chainId) => enabledNetworksForAllNamespaces[chainId as Hex] === true,
) ?? '0x1', // Fallback to Ethereum Mainnet if no networks are enabled
[enabledNetworksForAllNamespaces],
);
const [selectedNetwork, setSelectedNetwork] = useState<
SupportedCaipChainId | Hex | null
>(enabledChainId as Hex);
const sheetRef = useRef<BottomSheetRef>(null);
const renderNetworkSelector = useCallback(
() => (
<NetworkListBottomSheet
selectedNetwork={selectedNetwork}
setSelectedNetwork={async (network) => {
setSelectedNetwork(network);
if (!isNonEvmChainId(network)) {
Engine.context.TokenListController.fetchTokenList(network as Hex);
}
}}
setOpenNetworkSelector={setOpenNetworkSelector}
sheetRef={sheetRef}
displayEvmNetworksOnly={assetType === 'collectible'}
/>
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[openNetworkSelector, networkConfigurations, selectedNetwork, assetType],
);
return (
<SafeAreaView
edges={['left', 'right', 'bottom']}
style={tw.style('flex-1 bg-default', { paddingTop: insets.top })}
testID={`add-${assetType}-screen`}
>
{/* Header */}
<HeaderStandard
title={strings(
`add_asset.${assetType === 'token' ? 'title' : 'title_nft'}`,
)}
onBack={() => navigation.goBack()}
/>
{assetType === 'token' && (
<TokenView
openNetworkSelector={() => setOpenNetworkSelector(true)}
selectedNetwork={selectedNetwork}
networkConfigurations={networkConfigurations}
/>
)}
{assetType === 'collectible' && (
<NFTView
collectibleContract={collectibleContract}
selectedNetwork={selectedNetwork}
openNetworkSelector={() => setOpenNetworkSelector(true)}
networkConfigurations={networkConfigurations}
/>
)}
{openNetworkSelector ? renderNetworkSelector() : null}
</SafeAreaView>
);
};
export default AddAsset;