2023-03-23 11:08:33 +01:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2023-04-27 16:28:08 +02:00
|
|
|
import {
|
|
|
|
getCurrentChainId,
|
|
|
|
getDetectedTokensInCurrentNetwork,
|
|
|
|
} from '../../../selectors';
|
2023-03-23 11:08:33 +01:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
MetaMetricsTokenEventSource,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2023-03-23 11:08:33 +01:00
|
|
|
import { BannerAlert } from '../../component-library';
|
|
|
|
|
|
|
|
export const DetectedTokensBanner = ({
|
|
|
|
className,
|
|
|
|
actionButtonOnClick,
|
|
|
|
...props
|
|
|
|
}) => {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
|
|
|
|
|
|
|
const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork);
|
|
|
|
const detectedTokensDetails = detectedTokens.map(
|
|
|
|
({ address, symbol }) => `${symbol} - ${address}`,
|
|
|
|
);
|
|
|
|
|
2023-04-27 16:28:08 +02:00
|
|
|
const chainId = useSelector(getCurrentChainId);
|
|
|
|
|
2023-03-23 11:08:33 +01:00
|
|
|
const handleOnClick = () => {
|
|
|
|
actionButtonOnClick();
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
event: MetaMetricsEventName.TokenImportClicked,
|
|
|
|
category: MetaMetricsEventCategory.Wallet,
|
2023-03-23 11:08:33 +01:00
|
|
|
properties: {
|
2023-04-27 16:28:08 +02:00
|
|
|
source_connection_method: MetaMetricsTokenEventSource.Detected,
|
2023-03-23 11:08:33 +01:00
|
|
|
tokens: detectedTokensDetails,
|
2023-04-27 16:28:08 +02:00
|
|
|
chain_id: chainId,
|
2023-03-23 11:08:33 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<BannerAlert
|
|
|
|
className={classNames('multichain-detected-token-banner', className)}
|
|
|
|
actionButtonLabel={t('importTokensCamelCase')}
|
|
|
|
actionButtonOnClick={handleOnClick}
|
|
|
|
data-testid="detected-token-banner"
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{detectedTokens.length === 1
|
|
|
|
? t('numberOfNewTokensDetectedSingular')
|
|
|
|
: t('numberOfNewTokensDetectedPlural', [detectedTokens.length])}
|
|
|
|
</BannerAlert>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
DetectedTokensBanner.propTypes = {
|
|
|
|
/**
|
|
|
|
* Handler to be passed to the DetectedTokenBanner component
|
|
|
|
*/
|
|
|
|
actionButtonOnClick: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* An additional className to the DetectedTokenBanner component
|
|
|
|
*/
|
|
|
|
className: PropTypes.string,
|
|
|
|
};
|