2021-06-03 18:08:37 +02:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Identicon from '../../../../components/ui/identicon';
|
|
|
|
import UrlIcon from '../../../../components/ui/url-icon';
|
2021-06-03 18:08:37 +02:00
|
|
|
import Button from '../../../../components/ui/button';
|
2021-07-02 21:07:56 +02:00
|
|
|
import ActionableMessage from '../../../../components/ui/actionable-message/actionable-message';
|
2021-06-03 18:08:37 +02:00
|
|
|
import { I18nContext } from '../../../../contexts/i18n';
|
|
|
|
import {
|
|
|
|
getCurrentChainId,
|
|
|
|
getRpcPrefsForCurrentProvider,
|
2023-01-17 16:23:04 +01:00
|
|
|
getUseCurrencyRateCheck,
|
2021-06-03 18:08:37 +02:00
|
|
|
} from '../../../../selectors';
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsEventCategory } from '../../../../../shared/constants/metametrics';
|
2021-06-03 18:08:37 +02:00
|
|
|
import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../../shared/constants/swaps';
|
2021-08-25 00:28:16 +02:00
|
|
|
import { getURLHostName } from '../../../../helpers/utils/util';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../../../contexts/metametrics';
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function ItemList({
|
2020-10-06 20:28:38 +02:00
|
|
|
results = [],
|
|
|
|
onClickItem,
|
2021-06-03 18:08:37 +02:00
|
|
|
onOpenImportTokenModalClick,
|
2020-10-06 20:28:38 +02:00
|
|
|
Placeholder,
|
|
|
|
listTitle,
|
|
|
|
maxListItems = 6,
|
|
|
|
searchQuery = '',
|
|
|
|
containerRef,
|
|
|
|
hideRightLabels,
|
|
|
|
hideItemIf,
|
|
|
|
listContainerClassName,
|
|
|
|
}) {
|
2021-06-03 18:08:37 +02:00
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const chainId = useSelector(getCurrentChainId);
|
|
|
|
const rpcPrefs = useSelector(getRpcPrefsForCurrentProvider);
|
|
|
|
const blockExplorerLink =
|
|
|
|
rpcPrefs.blockExplorerUrl ??
|
|
|
|
SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP[chainId] ??
|
|
|
|
null;
|
2023-01-17 16:23:04 +01:00
|
|
|
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
|
2022-07-13 18:42:50 +02:00
|
|
|
const blockExplorerHostName = getURLHostName(blockExplorerLink);
|
2022-03-25 14:21:29 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-06-03 18:08:37 +02:00
|
|
|
|
|
|
|
// If there is a token for import based on a contract address, it's the only one in the list.
|
|
|
|
const hasTokenForImport = results.length === 1 && results[0].notImported;
|
2021-10-21 18:11:31 +02:00
|
|
|
const placeholder = Placeholder ? (
|
|
|
|
<Placeholder searchQuery={searchQuery} />
|
|
|
|
) : null;
|
2020-11-03 00:41:28 +01:00
|
|
|
return results.length === 0 ? (
|
2021-10-21 18:11:31 +02:00
|
|
|
placeholder
|
2020-11-03 00:41:28 +01:00
|
|
|
) : (
|
|
|
|
<div className="searchable-item-list">
|
2021-10-21 18:11:31 +02:00
|
|
|
{listTitle ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="searchable-item-list__title">{listTitle}</div>
|
2021-10-21 18:11:31 +02:00
|
|
|
) : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
<div
|
|
|
|
className={classnames(
|
|
|
|
'searchable-item-list__list-container',
|
|
|
|
listContainerClassName,
|
2020-10-06 20:28:38 +02:00
|
|
|
)}
|
2020-11-03 00:41:28 +01:00
|
|
|
ref={containerRef}
|
|
|
|
>
|
|
|
|
{results.slice(0, maxListItems).map((result, i) => {
|
2020-11-05 16:35:58 +01:00
|
|
|
if (hideItemIf?.(result)) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2020-10-06 20:28:38 +02:00
|
|
|
|
2021-06-03 18:08:37 +02:00
|
|
|
const onClick = () => {
|
|
|
|
if (result.notImported) {
|
|
|
|
onOpenImportTokenModalClick(result);
|
|
|
|
} else {
|
|
|
|
onClickItem?.(result);
|
|
|
|
}
|
|
|
|
};
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
iconUrl,
|
|
|
|
identiconAddress,
|
|
|
|
selected,
|
|
|
|
disabled,
|
|
|
|
primaryLabel,
|
|
|
|
secondaryLabel,
|
|
|
|
rightPrimaryLabel,
|
|
|
|
rightSecondaryLabel,
|
|
|
|
IconComponent,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = result;
|
2020-11-03 00:41:28 +01:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
tabIndex="0"
|
|
|
|
className={classnames('searchable-item-list__item', {
|
|
|
|
'searchable-item-list__item--selected': selected,
|
|
|
|
'searchable-item-list__item--disabled': disabled,
|
|
|
|
})}
|
2022-10-04 18:55:05 +02:00
|
|
|
data-testid="searchable-item-list__item"
|
2020-11-03 00:41:28 +01:00
|
|
|
onClick={onClick}
|
|
|
|
onKeyUp={(e) => e.key === 'Enter' && onClick()}
|
|
|
|
key={`searchable-item-list-item-${i}`}
|
|
|
|
>
|
2021-10-21 18:11:31 +02:00
|
|
|
{iconUrl || primaryLabel ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<UrlIcon url={iconUrl} name={primaryLabel} />
|
2021-10-21 18:11:31 +02:00
|
|
|
) : null}
|
|
|
|
{!(iconUrl || primaryLabel) && identiconAddress ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="searchable-item-list__identicon">
|
|
|
|
<Identicon address={identiconAddress} diameter={24} />
|
|
|
|
</div>
|
2021-10-21 18:11:31 +02:00
|
|
|
) : null}
|
|
|
|
{IconComponent ? <IconComponent /> : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
<div className="searchable-item-list__labels">
|
|
|
|
<div className="searchable-item-list__item-labels">
|
2021-10-21 18:11:31 +02:00
|
|
|
{primaryLabel ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="searchable-item-list__primary-label">
|
|
|
|
{primaryLabel}
|
|
|
|
</span>
|
2021-10-21 18:11:31 +02:00
|
|
|
) : null}
|
|
|
|
{secondaryLabel ? (
|
2020-11-03 00:41:28 +01:00
|
|
|
<span className="searchable-item-list__secondary-label">
|
|
|
|
{secondaryLabel}
|
|
|
|
</span>
|
2021-10-21 18:11:31 +02:00
|
|
|
) : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
|
|
|
{!hideRightLabels &&
|
2021-10-21 18:11:31 +02:00
|
|
|
(rightPrimaryLabel || rightSecondaryLabel) ? (
|
|
|
|
<div className="searchable-item-list__right-labels">
|
|
|
|
{rightPrimaryLabel ? (
|
|
|
|
<span className="searchable-item-list__right-primary-label">
|
|
|
|
{rightPrimaryLabel}
|
|
|
|
</span>
|
|
|
|
) : null}
|
2023-01-17 16:23:04 +01:00
|
|
|
{rightSecondaryLabel && useCurrencyRateCheck ? (
|
2021-10-21 18:11:31 +02:00
|
|
|
<span className="searchable-item-list__right-secondary-label">
|
|
|
|
{rightSecondaryLabel}
|
|
|
|
</span>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-06-03 18:08:37 +02:00
|
|
|
{result.notImported && (
|
2022-02-25 23:11:22 +01:00
|
|
|
<Button type="primary" onClick={onClick}>
|
2021-06-03 18:08:37 +02:00
|
|
|
{t('import')}
|
|
|
|
</Button>
|
|
|
|
)}
|
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
|
|
|
})}
|
2022-07-13 18:42:50 +02:00
|
|
|
{!hasTokenForImport && blockExplorerLink && (
|
2021-06-03 18:08:37 +02:00
|
|
|
<div
|
|
|
|
tabIndex="0"
|
|
|
|
className="searchable-item-list__item searchable-item-list__item--add-token"
|
|
|
|
key="searchable-item-list-item-last"
|
|
|
|
>
|
|
|
|
<ActionableMessage
|
2022-07-13 18:42:50 +02:00
|
|
|
message={t('addCustomTokenByContractAddress', [
|
|
|
|
<a
|
|
|
|
key="searchable-item-list__etherscan-link"
|
|
|
|
onClick={() => {
|
2022-10-04 18:55:05 +02:00
|
|
|
/* istanbul ignore next */
|
2022-07-13 18:42:50 +02:00
|
|
|
trackEvent({
|
|
|
|
event: 'Clicked Block Explorer Link',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Swaps,
|
2022-07-13 18:42:50 +02:00
|
|
|
properties: {
|
|
|
|
link_type: 'Token Tracker',
|
|
|
|
action: 'Verify Contract Address',
|
|
|
|
block_explorer_domain: blockExplorerHostName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
global.platform.openTab({
|
|
|
|
url: blockExplorerLink,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{blockExplorerHostName}
|
|
|
|
</a>,
|
|
|
|
])}
|
2021-06-03 18:08:37 +02:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-10-06 20:28:38 +02:00
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-10-06 20:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ItemList.propTypes = {
|
2020-11-03 00:41:28 +01:00
|
|
|
results: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
iconUrl: PropTypes.string,
|
|
|
|
selected: PropTypes.bool,
|
|
|
|
disabled: PropTypes.bool,
|
|
|
|
primaryLabel: PropTypes.string,
|
|
|
|
secondaryLabel: PropTypes.string,
|
|
|
|
rightPrimaryLabel: PropTypes.string,
|
|
|
|
rightSecondaryLabel: PropTypes.string,
|
|
|
|
}),
|
|
|
|
),
|
2020-10-06 20:28:38 +02:00
|
|
|
onClickItem: PropTypes.func,
|
2021-06-03 18:08:37 +02:00
|
|
|
onOpenImportTokenModalClick: PropTypes.func,
|
2020-10-06 20:28:38 +02:00
|
|
|
Placeholder: PropTypes.func,
|
|
|
|
listTitle: PropTypes.string,
|
|
|
|
maxListItems: PropTypes.number,
|
|
|
|
searchQuery: PropTypes.string,
|
2020-11-03 00:41:28 +01:00
|
|
|
containerRef: PropTypes.shape({
|
|
|
|
current: PropTypes.instanceOf(window.Element),
|
|
|
|
}),
|
2020-10-06 20:28:38 +02:00
|
|
|
hideRightLabels: PropTypes.bool,
|
|
|
|
hideItemIf: PropTypes.func,
|
|
|
|
listContainerClassName: PropTypes.string,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|