import React, { useContext, useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { ApprovalType } from '@metamask/controller-utils';
import { I18nContext } from '../../../contexts/i18n';
import Box from '../../ui/box';
import {
AlignItems,
Display,
FlexDirection,
TextVariant,
JustifyContent,
BorderRadius,
BackgroundColor,
TextColor,
IconColor,
Size,
} from '../../../helpers/constants/design-system';
import Button from '../../ui/button';
import Tooltip from '../../ui/tooltip';
import {
getNetworkConfigurations,
getUnapprovedConfirmations,
} from '../../../selectors';
import {
ENVIRONMENT_TYPE_FULLSCREEN,
ENVIRONMENT_TYPE_POPUP,
ORIGIN_METAMASK,
} from '../../../../shared/constants/app';
import { requestUserApproval } from '../../../store/actions';
import Popover from '../../ui/popover';
import ConfirmationPage from '../../../pages/confirmation/confirmation';
import { FEATURED_RPCS } from '../../../../shared/constants/network';
import { ADD_NETWORK_ROUTE } from '../../../helpers/constants/routes';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
import {
Icon,
IconName,
IconSize,
AvatarNetwork,
Text,
} from '../../component-library';
import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics';
const AddNetwork = () => {
const t = useContext(I18nContext);
const dispatch = useDispatch();
const history = useHistory();
const networkConfigurations = useSelector(getNetworkConfigurations);
const networkConfigurationChainIds = Object.values(networkConfigurations).map(
(net) => net.chainId,
);
const infuraRegex = /infura.io/u;
const nets = FEATURED_RPCS.sort((a, b) =>
a.nickname > b.nickname ? 1 : -1,
).slice(0, FEATURED_RPCS.length);
const notExistingNetworkConfigurations = nets.filter(
(net) => networkConfigurationChainIds.indexOf(net.chainId) === -1,
);
const unapprovedConfirmations = useSelector(getUnapprovedConfirmations);
const [showPopover, setShowPopover] = useState(false);
useEffect(() => {
const anAddNetworkConfirmationFromMetaMaskExists =
unapprovedConfirmations?.find((confirmation) => {
return (
confirmation.origin === 'metamask' &&
confirmation.type === ApprovalType.AddEthereumChain
);
});
if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) {
setShowPopover(true);
}
if (showPopover && !anAddNetworkConfirmationFromMetaMaskExists) {
setShowPopover(false);
}
}, [unapprovedConfirmations, showPopover]);
return (
<>
{Object.keys(notExistingNetworkConfigurations).length === 0 ? (
{t('youHaveAddedAll', [
{t('here')}.
,
,
])}
) : (
{getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN && (
{t('networks')}
{' > '}
{t('addANetwork')}
)}
{t('addFromAListOfPopularNetworks')}
{t('popularCustomNetworks')}
{notExistingNetworkConfigurations.map((item, index) => (
{item.nickname}
{
// Warning for the networks that doesn't use infura.io as the RPC
!infuraRegex.test(item.rpcUrl) && (
{t('addNetworkTooltipWarning', [
{t('learnMoreUpperCase')}
,
])}
}
trigger="mouseenter"
>
)
}
))}
)}
{showPopover && (
)}
>
);
};
export default AddNetwork;