2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { checkExistingAddresses } from '../../../helpers/utils/util';
|
|
|
|
import TokenListPlaceholder from './token-list-placeholder';
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-12-09 21:32:38 +01:00
|
|
|
export default class TokenList extends Component {
|
2018-05-20 08:04:19 +02:00
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
tokens: PropTypes.array,
|
|
|
|
results: PropTypes.array,
|
|
|
|
selectedTokens: PropTypes.object,
|
|
|
|
onToggleToken: PropTypes.func,
|
2021-09-09 22:56:27 +02:00
|
|
|
useTokenDetection: PropTypes.bool,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
results = [],
|
|
|
|
selectedTokens = {},
|
|
|
|
onToggleToken,
|
|
|
|
tokens = [],
|
2021-09-09 22:56:27 +02:00
|
|
|
useTokenDetection,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return results.length === 0 ? (
|
|
|
|
<TokenListPlaceholder />
|
|
|
|
) : (
|
|
|
|
<div className="token-list">
|
|
|
|
<div className="token-list__title">
|
|
|
|
{this.context.t('searchResults')}
|
|
|
|
</div>
|
|
|
|
<div className="token-list__tokens-container">
|
|
|
|
{Array(6)
|
|
|
|
.fill(undefined)
|
|
|
|
.map((_, i) => {
|
2021-09-09 22:56:27 +02:00
|
|
|
const { iconUrl, symbol, name, address } = results[i] || {};
|
|
|
|
// token from dynamic api list is fetched when useTokenDetection is true
|
|
|
|
const iconPath = useTokenDetection
|
|
|
|
? iconUrl
|
|
|
|
: `images/contract/${iconUrl}`;
|
2021-02-04 19:15:23 +01:00
|
|
|
const tokenAlreadyAdded = checkExistingAddresses(address, tokens);
|
2020-11-05 19:05:41 +01:00
|
|
|
const onClick = () =>
|
2021-02-04 19:15:23 +01:00
|
|
|
!tokenAlreadyAdded && onToggleToken(results[i]);
|
2018-05-20 08:04:19 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
2021-09-09 22:56:27 +02:00
|
|
|
Boolean(iconUrl || symbol || name) && (
|
2020-11-03 00:41:28 +01:00
|
|
|
<div
|
|
|
|
className={classnames('token-list__token', {
|
|
|
|
'token-list__token--selected': selectedTokens[address],
|
|
|
|
'token-list__token--disabled': tokenAlreadyAdded,
|
|
|
|
})}
|
2020-11-05 19:05:41 +01:00
|
|
|
onClick={onClick}
|
|
|
|
onKeyPress={(event) => event.key === 'Enter' && onClick()}
|
2020-11-03 00:41:28 +01:00
|
|
|
key={i}
|
2020-11-05 19:05:41 +01:00
|
|
|
tabIndex="0"
|
2020-11-03 00:41:28 +01:00
|
|
|
>
|
2018-05-20 08:04:19 +02:00
|
|
|
<div
|
2020-11-03 00:41:28 +01:00
|
|
|
className="token-list__token-icon"
|
|
|
|
style={{
|
2021-09-09 22:56:27 +02:00
|
|
|
backgroundImage: iconUrl && `url(${iconPath})`,
|
2020-11-03 00:41:28 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<div className="token-list__token-data">
|
|
|
|
<span className="token-list__token-name">{`${name} (${symbol})`}</span>
|
2018-05-20 08:04:19 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
|
|
|
)
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
})}
|
2018-05-20 08:04:19 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-20 08:04:19 +02:00
|
|
|
}
|
|
|
|
}
|