2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import SendRowWrapper from '../send-row-wrapper';
|
2022-01-10 17:23:53 +01:00
|
|
|
import Identicon from '../../../../components/ui/identicon';
|
2021-02-04 19:15:23 +01:00
|
|
|
import TokenBalance from '../../../../components/ui/token-balance';
|
2022-02-02 12:32:16 +01:00
|
|
|
import TokenListDisplay from '../../../../components/app/token-list-display';
|
2021-02-04 19:15:23 +01:00
|
|
|
import UserPreferencedCurrencyDisplay from '../../../../components/app/user-preferenced-currency-display';
|
2022-07-19 18:07:13 +02:00
|
|
|
import { PRIMARY } from '../../../../helpers/constants/common';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../../../shared/modules/string-utils';
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsEventCategory } from '../../../../../shared/constants/metametrics';
|
2022-07-19 18:07:13 +02:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
AssetType,
|
|
|
|
TokenStandard,
|
2022-07-19 18:07:13 +02:00
|
|
|
} from '../../../../../shared/constants/transaction';
|
2019-04-17 21:15:13 +02:00
|
|
|
|
|
|
|
export default class SendAssetRow extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
tokens: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
address: PropTypes.string,
|
2019-04-18 01:56:25 +02:00
|
|
|
decimals: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2019-04-17 21:15:13 +02:00
|
|
|
symbol: PropTypes.string,
|
2021-09-10 19:37:19 +02:00
|
|
|
image: PropTypes.string,
|
2020-07-14 17:20:41 +02:00
|
|
|
}),
|
2019-04-17 21:15:13 +02:00
|
|
|
).isRequired,
|
|
|
|
accounts: PropTypes.object.isRequired,
|
|
|
|
selectedAddress: PropTypes.string.isRequired,
|
2022-01-10 17:23:53 +01:00
|
|
|
sendAsset: PropTypes.object,
|
2021-06-23 23:35:25 +02:00
|
|
|
updateSendAsset: PropTypes.func.isRequired,
|
2021-02-11 19:20:08 +01:00
|
|
|
nativeCurrency: PropTypes.string,
|
2021-04-02 00:57:00 +02:00
|
|
|
nativeCurrencyImage: PropTypes.string,
|
2023-02-16 20:23:29 +01:00
|
|
|
nfts: PropTypes.arrayOf(
|
2022-01-10 17:23:53 +01:00
|
|
|
PropTypes.shape({
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
tokenId: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string,
|
|
|
|
description: PropTypes.string,
|
|
|
|
image: PropTypes.string,
|
|
|
|
standard: PropTypes.string,
|
|
|
|
imageThumbnail: PropTypes.string,
|
|
|
|
imagePreview: PropTypes.string,
|
|
|
|
creator: PropTypes.shape({
|
|
|
|
address: PropTypes.string,
|
|
|
|
config: PropTypes.string,
|
|
|
|
profile_img_url: PropTypes.string,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
),
|
2023-01-19 18:30:35 +01:00
|
|
|
collections: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
address: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string,
|
|
|
|
}),
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-04-17 21:15:13 +02:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2022-03-29 15:46:24 +02:00
|
|
|
trackEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-04-17 21:15:13 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
isShowingDropdown: false,
|
2021-06-22 19:39:44 +02:00
|
|
|
sendableTokens: [],
|
2023-02-16 20:23:29 +01:00
|
|
|
sendableNfts: [],
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2021-06-22 19:39:44 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
const sendableTokens = this.props.tokens.filter((token) => !token.isERC721);
|
2023-02-16 20:23:29 +01:00
|
|
|
const sendableNfts = this.props.nfts.filter(
|
|
|
|
(nft) => nft.isCurrentlyOwned && nft.standard === TokenStandard.ERC721,
|
2022-01-10 17:23:53 +01:00
|
|
|
);
|
2023-02-16 20:23:29 +01:00
|
|
|
this.setState({ sendableTokens, sendableNfts });
|
2021-06-22 19:39:44 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
openDropdown = () => this.setState({ isShowingDropdown: true });
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
closeDropdown = () => this.setState({ isShowingDropdown: false });
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
getAssetSelected = (type, token) => {
|
|
|
|
switch (type) {
|
2023-01-18 15:47:29 +01:00
|
|
|
case AssetType.native:
|
2022-01-10 17:23:53 +01:00
|
|
|
return this.props.nativeCurrency;
|
2023-01-18 15:47:29 +01:00
|
|
|
case AssetType.token:
|
|
|
|
return TokenStandard.ERC20;
|
|
|
|
case AssetType.NFT:
|
2022-01-10 17:23:53 +01:00
|
|
|
return token?.standard;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
selectToken = (type, token) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
this.setState(
|
|
|
|
{
|
|
|
|
isShowingDropdown: false,
|
|
|
|
},
|
|
|
|
() => {
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Transactions,
|
2022-03-29 15:46:24 +02:00
|
|
|
event: 'User clicks "Assets" dropdown',
|
|
|
|
properties: {
|
2020-11-03 00:41:28 +01:00
|
|
|
action: 'Send Screen',
|
2022-03-29 15:46:24 +02:00
|
|
|
legacy_event: true,
|
2022-01-10 17:23:53 +01:00
|
|
|
assetSelected: this.getAssetSelected(type, token),
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-06-23 23:35:25 +02:00
|
|
|
this.props.updateSendAsset({
|
|
|
|
type,
|
2023-01-18 15:47:29 +01:00
|
|
|
details: type === AssetType.native ? null : token,
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
};
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2019-04-17 21:15:13 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SendRowWrapper label={`${t('asset')}:`}>
|
|
|
|
<div className="send-v2__asset-dropdown">
|
2022-01-10 17:23:53 +01:00
|
|
|
<div
|
|
|
|
className="send-v2__asset-dropdown__input-wrapper"
|
|
|
|
onClick={this.openDropdown}
|
|
|
|
>
|
|
|
|
{this.renderSendAsset()}
|
|
|
|
</div>
|
2023-02-16 20:23:29 +01:00
|
|
|
{[...this.state.sendableTokens, ...this.state.sendableNfts].length > 0
|
2021-06-22 19:39:44 +02:00
|
|
|
? this.renderAssetDropdown()
|
|
|
|
: null}
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
|
|
|
</SendRowWrapper>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
renderSendAsset() {
|
|
|
|
const {
|
|
|
|
sendAsset: { details, type },
|
|
|
|
tokens,
|
2023-02-16 20:23:29 +01:00
|
|
|
nfts,
|
2022-01-10 17:23:53 +01:00
|
|
|
} = this.props;
|
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
if (type === AssetType.token) {
|
2022-01-10 17:23:53 +01:00
|
|
|
const token = tokens.find(({ address }) =>
|
|
|
|
isEqualCaseInsensitive(address, details.address),
|
|
|
|
);
|
|
|
|
if (token) {
|
|
|
|
return this.renderToken(token);
|
|
|
|
}
|
2023-03-03 17:52:32 +01:00
|
|
|
return this.renderToken(details);
|
2023-01-18 15:47:29 +01:00
|
|
|
} else if (type === AssetType.NFT) {
|
2023-02-16 20:23:29 +01:00
|
|
|
const nft = nfts.find(
|
2022-01-10 17:23:53 +01:00
|
|
|
({ address, tokenId }) =>
|
|
|
|
isEqualCaseInsensitive(address, details.address) &&
|
|
|
|
tokenId === details.tokenId,
|
|
|
|
);
|
2023-02-16 20:23:29 +01:00
|
|
|
if (nft) {
|
|
|
|
return this.renderNft(nft);
|
2022-01-10 17:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.renderNativeCurrency();
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderAssetDropdown() {
|
|
|
|
return (
|
|
|
|
this.state.isShowingDropdown && (
|
|
|
|
<div>
|
|
|
|
<div
|
|
|
|
className="send-v2__asset-dropdown__close-area"
|
|
|
|
onClick={this.closeDropdown}
|
|
|
|
/>
|
|
|
|
<div className="send-v2__asset-dropdown__list">
|
2021-02-11 19:20:08 +01:00
|
|
|
{this.renderNativeCurrency(true)}
|
2022-02-02 12:32:16 +01:00
|
|
|
<TokenListDisplay
|
2023-01-18 15:47:29 +01:00
|
|
|
clickHandler={(token) => this.selectToken(AssetType.token, token)}
|
2022-02-02 12:32:16 +01:00
|
|
|
/>
|
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
{this.state.sendableNfts.map((nft) => this.renderNft(nft, true))}
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
)
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
|
|
|
|
2021-02-11 19:20:08 +01:00
|
|
|
renderNativeCurrency(insideDropdown = false) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2022-07-31 20:26:40 +02:00
|
|
|
const { accounts, selectedAddress, nativeCurrency, nativeCurrencyImage } =
|
|
|
|
this.props;
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const { sendableTokens, sendableNfts } = this.state;
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const balanceValue = accounts[selectedAddress]
|
|
|
|
? accounts[selectedAddress].balance
|
2021-02-04 19:15:23 +01:00
|
|
|
: '';
|
2019-04-17 21:15:13 +02:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
const sendableAssets = [...sendableTokens, ...sendableNfts];
|
2019-04-17 21:15:13 +02:00
|
|
|
return (
|
|
|
|
<div
|
2020-11-03 00:41:28 +01:00
|
|
|
className={
|
2022-01-10 17:23:53 +01:00
|
|
|
sendableAssets.length > 0
|
2020-11-03 00:41:28 +01:00
|
|
|
? 'send-v2__asset-dropdown__asset'
|
|
|
|
: 'send-v2__asset-dropdown__single-asset'
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
onClick={() => this.selectToken(AssetType.native)}
|
2019-04-17 21:15:13 +02:00
|
|
|
>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-icon">
|
2021-02-11 19:20:08 +01:00
|
|
|
<Identicon
|
|
|
|
diameter={36}
|
2021-04-02 00:57:00 +02:00
|
|
|
image={nativeCurrencyImage}
|
|
|
|
address={nativeCurrency}
|
2021-02-11 19:20:08 +01:00
|
|
|
/>
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-data">
|
2021-02-11 19:20:08 +01:00
|
|
|
<div className="send-v2__asset-dropdown__symbol">
|
|
|
|
{nativeCurrency}
|
|
|
|
</div>
|
2019-04-17 21:15:13 +02:00
|
|
|
<div className="send-v2__asset-dropdown__name">
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="send-v2__asset-dropdown__name__label">
|
|
|
|
{`${t('balance')}:`}
|
|
|
|
</span>
|
2019-04-17 21:15:13 +02:00
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
value={balanceValue}
|
|
|
|
type={PRIMARY}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-01-10 17:23:53 +01:00
|
|
|
{!insideDropdown && sendableAssets.length > 0 && (
|
2021-02-19 20:03:44 +01:00
|
|
|
<i className="fa fa-caret-down fa-lg send-v2__asset-dropdown__caret" />
|
2020-03-16 23:50:07 +01:00
|
|
|
)}
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 17:23:53 +01:00
|
|
|
renderToken(token, insideDropdown = false) {
|
2021-09-10 19:37:19 +02:00
|
|
|
const { address, symbol, image } = token;
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
2019-04-17 21:15:13 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2019-12-03 17:35:44 +01:00
|
|
|
key={address}
|
|
|
|
className="send-v2__asset-dropdown__asset"
|
2023-01-18 15:47:29 +01:00
|
|
|
onClick={() => this.selectToken(AssetType.token, token)}
|
2019-04-17 21:15:13 +02:00
|
|
|
>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-icon">
|
2021-09-10 19:37:19 +02:00
|
|
|
<Identicon address={address} diameter={36} image={image} />
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-data">
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="send-v2__asset-dropdown__symbol">{symbol}</div>
|
2019-04-17 21:15:13 +02:00
|
|
|
<div className="send-v2__asset-dropdown__name">
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="send-v2__asset-dropdown__name__label">
|
|
|
|
{`${t('balance')}:`}
|
|
|
|
</span>
|
|
|
|
<TokenBalance token={token} />
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{!insideDropdown && (
|
2021-02-19 20:03:44 +01:00
|
|
|
<i className="fa fa-caret-down fa-lg send-v2__asset-dropdown__caret" />
|
2020-03-16 23:50:07 +01:00
|
|
|
)}
|
2019-04-17 21:15:13 +02:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2023-02-16 20:23:29 +01:00
|
|
|
renderNft(nft, insideDropdown = false) {
|
|
|
|
const { address, name, image, tokenId } = nft;
|
2022-01-10 17:23:53 +01:00
|
|
|
const { t } = this.context;
|
2023-02-16 20:23:29 +01:00
|
|
|
const nftCollection = this.props.collections.find(
|
2023-01-19 18:30:35 +01:00
|
|
|
(collection) => collection.address === address,
|
|
|
|
);
|
2022-01-10 17:23:53 +01:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key={address}
|
|
|
|
className="send-v2__asset-dropdown__asset"
|
2023-02-16 20:23:29 +01:00
|
|
|
onClick={() => this.selectToken(AssetType.NFT, nft)}
|
2022-01-10 17:23:53 +01:00
|
|
|
>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-icon">
|
|
|
|
<Identicon address={address} diameter={36} image={image} />
|
|
|
|
</div>
|
|
|
|
<div className="send-v2__asset-dropdown__asset-data">
|
2023-01-19 18:30:35 +01:00
|
|
|
<div className="send-v2__asset-dropdown__symbol">
|
2023-02-16 20:23:29 +01:00
|
|
|
{nftCollection.name || name}
|
2023-01-19 18:30:35 +01:00
|
|
|
</div>
|
2022-01-10 17:23:53 +01:00
|
|
|
<div className="send-v2__asset-dropdown__name">
|
|
|
|
<span className="send-v2__asset-dropdown__name__label">
|
|
|
|
{`${t('tokenId')}:`}
|
|
|
|
</span>
|
|
|
|
{tokenId}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{!insideDropdown && (
|
|
|
|
<i className="fa fa-caret-down fa-lg send-v2__asset-dropdown__caret" />
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-04-17 21:15:13 +02:00
|
|
|
}
|