2022-05-09 19:47:06 +02:00
|
|
|
import React, { useContext, useState } from 'react';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import TokenList from '../token-list';
|
|
|
|
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
|
|
|
|
import { useUserPreferencedCurrency } from '../../../hooks/useUserPreferencedCurrency';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
2022-06-13 21:39:48 +02:00
|
|
|
getSelectedAccountCachedBalance,
|
2020-11-03 00:41:28 +01:00
|
|
|
getShouldShowFiat,
|
2021-04-02 00:57:00 +02:00
|
|
|
getNativeCurrencyImage,
|
2022-05-09 19:47:06 +02:00
|
|
|
getDetectedTokensInCurrentNetwork,
|
2022-09-19 20:11:48 +02:00
|
|
|
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
|
2023-03-23 11:08:33 +01:00
|
|
|
getTokenList,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../../selectors';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useCurrencyDisplay } from '../../../hooks/useCurrencyDisplay';
|
2021-09-10 20:03:42 +02:00
|
|
|
import Box from '../../ui/box/box';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2022-05-09 19:47:06 +02:00
|
|
|
import DetectedToken from '../detected-token/detected-token';
|
2023-03-23 11:08:33 +01:00
|
|
|
import {
|
|
|
|
DetectedTokensBanner,
|
2023-06-14 17:51:19 +02:00
|
|
|
TokenListItem,
|
|
|
|
ImportTokenLink,
|
2023-03-23 11:08:33 +01:00
|
|
|
} from '../../multichain';
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
const AssetList = ({ onClickAsset }) => {
|
2022-05-09 19:47:06 +02:00
|
|
|
const [showDetectedTokens, setShowDetectedTokens] = useState(false);
|
|
|
|
|
2022-06-13 21:39:48 +02:00
|
|
|
const selectedAccountBalance = useSelector(getSelectedAccountCachedBalance);
|
2021-02-04 19:15:23 +01:00
|
|
|
const nativeCurrency = useSelector(getNativeCurrency);
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2022-07-01 15:08:42 +02:00
|
|
|
const balance = useSelector(getSelectedAccountCachedBalance);
|
|
|
|
const balanceIsLoading = !balance;
|
2020-05-14 05:16:30 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
currency: primaryCurrency,
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useUserPreferencedCurrency(PRIMARY, { ethNumberOfDecimals: 4 });
|
2020-05-14 05:16:30 +02:00
|
|
|
const {
|
|
|
|
currency: secondaryCurrency,
|
|
|
|
numberOfDecimals: secondaryNumberOfDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = useUserPreferencedCurrency(SECONDARY, { ethNumberOfDecimals: 4 });
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-11-05 23:16:24 +01:00
|
|
|
const [, primaryCurrencyProperties] = useCurrencyDisplay(
|
|
|
|
selectedAccountBalance,
|
|
|
|
{
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
|
|
|
currency: primaryCurrency,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-03 19:50:12 +02:00
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const [secondaryCurrencyDisplay, secondaryCurrencyProperties] =
|
|
|
|
useCurrencyDisplay(selectedAccountBalance, {
|
|
|
|
numberOfDecimals: secondaryNumberOfDecimals,
|
|
|
|
currency: secondaryCurrency,
|
|
|
|
});
|
2020-06-03 19:50:12 +02:00
|
|
|
|
2021-04-02 00:57:00 +02:00
|
|
|
const primaryTokenImage = useSelector(getNativeCurrencyImage);
|
2022-05-09 19:47:06 +02:00
|
|
|
const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork) || [];
|
2022-09-19 20:11:48 +02:00
|
|
|
const istokenDetectionInactiveOnNonMainnetSupportedNetwork = useSelector(
|
|
|
|
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
|
|
|
|
);
|
2023-03-23 11:08:33 +01:00
|
|
|
const tokenList = useSelector(getTokenList);
|
|
|
|
const tokenData = Object.values(tokenList).find(
|
|
|
|
(token) => token.symbol === primaryCurrencyProperties.suffix,
|
|
|
|
);
|
|
|
|
const title = tokenData?.name || primaryCurrencyProperties.suffix;
|
2020-05-14 05:16:30 +02:00
|
|
|
return (
|
|
|
|
<>
|
2023-06-14 17:51:19 +02:00
|
|
|
<TokenListItem
|
2023-06-01 23:14:38 +02:00
|
|
|
onClick={() => onClickAsset(nativeCurrency)}
|
|
|
|
title={title}
|
|
|
|
primary={
|
|
|
|
primaryCurrencyProperties.value ?? secondaryCurrencyProperties.value
|
|
|
|
}
|
|
|
|
tokenSymbol={primaryCurrencyProperties.suffix}
|
|
|
|
secondary={showFiat ? secondaryCurrencyDisplay : undefined}
|
|
|
|
tokenImage={balanceIsLoading ? null : primaryTokenImage}
|
|
|
|
/>
|
2020-05-14 05:16:30 +02:00
|
|
|
<TokenList
|
|
|
|
onTokenClick={(tokenAddress) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
onClickAsset(tokenAddress);
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.TokenScreenOpened,
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
2022-08-16 18:39:23 +02:00
|
|
|
token_symbol: primaryCurrencyProperties.suffix,
|
|
|
|
location: 'Home',
|
2022-03-14 19:12:38 +01:00
|
|
|
},
|
|
|
|
});
|
2020-05-14 05:16:30 +02:00
|
|
|
}}
|
|
|
|
/>
|
2022-09-19 20:11:48 +02:00
|
|
|
{detectedTokens.length > 0 &&
|
2023-06-01 23:14:38 +02:00
|
|
|
!istokenDetectionInactiveOnNonMainnetSupportedNetwork ? (
|
|
|
|
<DetectedTokensBanner
|
|
|
|
actionButtonOnClick={() => setShowDetectedTokens(true)}
|
|
|
|
margin={4}
|
|
|
|
/>
|
|
|
|
) : null}
|
2022-05-09 19:47:06 +02:00
|
|
|
<Box marginTop={detectedTokens.length > 0 ? 0 : 4}>
|
2023-06-14 17:51:19 +02:00
|
|
|
<ImportTokenLink margin={4} />
|
2021-09-10 20:03:42 +02:00
|
|
|
</Box>
|
2022-05-09 19:47:06 +02:00
|
|
|
{showDetectedTokens && (
|
|
|
|
<DetectedToken setShowDetectedTokens={setShowDetectedTokens} />
|
|
|
|
)}
|
2020-05-14 05:16:30 +02:00
|
|
|
</>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2020-05-14 05:16:30 +02:00
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
AssetList.propTypes = {
|
|
|
|
onClickAsset: PropTypes.func.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-06-01 19:54:32 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default AssetList;
|