2022-06-30 18:19:07 +02:00
|
|
|
import React, { useContext, useEffect, useState } from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
2023-05-10 07:36:01 +02:00
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2022-02-25 17:50:16 +01:00
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
|
|
|
import Box from '../../ui/box';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
AlignItems,
|
2023-06-06 10:50:45 +02:00
|
|
|
Display,
|
|
|
|
FlexDirection,
|
2023-03-29 00:04:22 +02:00
|
|
|
TextVariant,
|
2023-02-02 21:15:26 +01:00
|
|
|
JustifyContent,
|
|
|
|
BorderRadius,
|
|
|
|
BackgroundColor,
|
|
|
|
TextColor,
|
2023-02-23 22:20:07 +01:00
|
|
|
IconColor,
|
2023-06-08 19:26:20 +02:00
|
|
|
Size,
|
2022-02-25 17:50:16 +01:00
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import Button from '../../ui/button';
|
2022-04-25 20:30:46 +02:00
|
|
|
import Tooltip from '../../ui/tooltip';
|
2022-06-30 18:19:07 +02:00
|
|
|
import {
|
2023-03-09 22:00:28 +01:00
|
|
|
getNetworkConfigurations,
|
2022-06-30 18:19:07 +02:00
|
|
|
getUnapprovedConfirmations,
|
|
|
|
} from '../../../selectors';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
|
|
|
ENVIRONMENT_TYPE_POPUP,
|
2023-03-09 22:00:28 +01:00
|
|
|
ORIGIN_METAMASK,
|
2022-06-30 18:19:07 +02:00
|
|
|
} from '../../../../shared/constants/app';
|
2023-03-09 22:00:28 +01:00
|
|
|
import { requestUserApproval } from '../../../store/actions';
|
2022-06-30 18:19:07 +02:00
|
|
|
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';
|
2022-08-23 16:19:31 +02:00
|
|
|
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
|
2023-06-08 19:26:20 +02:00
|
|
|
import {
|
|
|
|
Icon,
|
|
|
|
IconName,
|
|
|
|
IconSize,
|
|
|
|
AvatarNetwork,
|
2023-07-20 00:32:35 +02:00
|
|
|
Text,
|
2023-06-08 19:26:20 +02:00
|
|
|
} from '../../component-library';
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsNetworkEventSource } from '../../../../shared/constants/metametrics';
|
2022-02-25 17:50:16 +01:00
|
|
|
|
2022-06-30 18:19:07 +02:00
|
|
|
const AddNetwork = () => {
|
2022-02-25 17:50:16 +01:00
|
|
|
const t = useContext(I18nContext);
|
2022-06-30 18:19:07 +02:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
const history = useHistory();
|
2023-03-09 22:00:28 +01:00
|
|
|
const networkConfigurations = useSelector(getNetworkConfigurations);
|
2022-06-30 18:19:07 +02:00
|
|
|
|
2023-03-09 22:00:28 +01:00
|
|
|
const networkConfigurationChainIds = Object.values(networkConfigurations).map(
|
2022-06-30 18:19:07 +02:00
|
|
|
(net) => net.chainId,
|
|
|
|
);
|
2022-04-25 20:30:46 +02:00
|
|
|
|
|
|
|
const infuraRegex = /infura.io/u;
|
2022-02-25 17:50:16 +01:00
|
|
|
|
2022-06-30 18:19:07 +02:00
|
|
|
const nets = FEATURED_RPCS.sort((a, b) =>
|
2022-10-14 16:17:14 +02:00
|
|
|
a.nickname > b.nickname ? 1 : -1,
|
2022-06-30 18:19:07 +02:00
|
|
|
).slice(0, FEATURED_RPCS.length);
|
|
|
|
|
2023-03-09 22:00:28 +01:00
|
|
|
const notExistingNetworkConfigurations = nets.filter(
|
|
|
|
(net) => networkConfigurationChainIds.indexOf(net.chainId) === -1,
|
2022-06-30 18:19:07 +02:00
|
|
|
);
|
|
|
|
const unapprovedConfirmations = useSelector(getUnapprovedConfirmations);
|
|
|
|
const [showPopover, setShowPopover] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-31 20:26:40 +02:00
|
|
|
const anAddNetworkConfirmationFromMetaMaskExists =
|
|
|
|
unapprovedConfirmations?.find((confirmation) => {
|
2022-06-30 18:19:07 +02:00
|
|
|
return (
|
|
|
|
confirmation.origin === 'metamask' &&
|
2023-05-10 07:36:01 +02:00
|
|
|
confirmation.type === ApprovalType.AddEthereumChain
|
2022-06-30 18:19:07 +02:00
|
|
|
);
|
2022-07-31 20:26:40 +02:00
|
|
|
});
|
2022-06-30 18:19:07 +02:00
|
|
|
if (!showPopover && anAddNetworkConfirmationFromMetaMaskExists) {
|
|
|
|
setShowPopover(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showPopover && !anAddNetworkConfirmationFromMetaMaskExists) {
|
|
|
|
setShowPopover(false);
|
|
|
|
}
|
|
|
|
}, [unapprovedConfirmations, showPopover]);
|
2022-02-25 17:50:16 +01:00
|
|
|
|
|
|
|
return (
|
2022-06-30 18:19:07 +02:00
|
|
|
<>
|
2023-03-09 22:00:28 +01:00
|
|
|
{Object.keys(notExistingNetworkConfigurations).length === 0 ? (
|
2022-06-30 18:19:07 +02:00
|
|
|
<Box
|
|
|
|
className="add-network__edge-case-box"
|
2023-02-02 21:15:26 +01:00
|
|
|
borderRadius={BorderRadius.MD}
|
2022-06-30 18:19:07 +02:00
|
|
|
padding={4}
|
2022-07-20 22:47:51 +02:00
|
|
|
marginTop={4}
|
|
|
|
marginRight={6}
|
|
|
|
marginLeft={6}
|
2023-06-06 10:50:45 +02:00
|
|
|
display={Display.Flex}
|
|
|
|
flexDirection={FlexDirection.Row}
|
2023-02-02 21:15:26 +01:00
|
|
|
backgroundColor={BackgroundColor.backgroundAlternative}
|
2022-02-25 17:50:16 +01:00
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
<Box marginRight={4}>
|
|
|
|
<img src="images/info-fox.svg" />
|
|
|
|
</Box>
|
|
|
|
<Box>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text variant={TextVariant.bodySm} as="h6">
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('youHaveAddedAll', [
|
|
|
|
<a
|
|
|
|
key="link"
|
|
|
|
className="add-network__edge-case-box__link"
|
|
|
|
href="https://chainlist.wtf/"
|
|
|
|
target="_blank"
|
|
|
|
rel="noreferrer"
|
|
|
|
>
|
|
|
|
{t('here')}.
|
|
|
|
</a>,
|
|
|
|
<Button
|
|
|
|
key="button"
|
|
|
|
type="inline"
|
|
|
|
onClick={(event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
|
|
|
|
? global.platform.openExtensionInBrowser(
|
|
|
|
ADD_NETWORK_ROUTE,
|
|
|
|
)
|
|
|
|
: history.push(ADD_NETWORK_ROUTE);
|
|
|
|
}}
|
|
|
|
>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.infoDefault}
|
2022-06-30 18:19:07 +02:00
|
|
|
>
|
|
|
|
{t('addMoreNetworks')}.
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Button>,
|
|
|
|
])}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
) : (
|
|
|
|
<Box className="add-network__networks-container">
|
|
|
|
{getEnvironmentType() === ENVIRONMENT_TYPE_FULLSCREEN && (
|
|
|
|
<Box
|
2023-06-06 10:50:45 +02:00
|
|
|
display={Display.Flex}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2023-06-06 10:50:45 +02:00
|
|
|
flexDirection={FlexDirection.Row}
|
2022-06-30 18:19:07 +02:00
|
|
|
marginTop={7}
|
|
|
|
marginBottom={4}
|
|
|
|
paddingBottom={2}
|
|
|
|
className="add-network__header"
|
|
|
|
>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.headingSm}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textMuted}
|
2023-03-29 00:04:22 +02:00
|
|
|
as="h4"
|
2023-02-02 21:15:26 +01:00
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('networks')}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
<span className="add-network__header__subtitle">{' > '}</span>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.headingSm}
|
|
|
|
as="h4"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('addANetwork')}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Box>
|
|
|
|
)}
|
2022-02-25 17:50:16 +01:00
|
|
|
<Box
|
2022-07-20 22:47:51 +02:00
|
|
|
marginTop={getEnvironmentType() === ENVIRONMENT_TYPE_POPUP ? 0 : 4}
|
|
|
|
marginBottom={1}
|
2022-06-30 18:19:07 +02:00
|
|
|
className="add-network__main-container"
|
2022-02-25 17:50:16 +01:00
|
|
|
>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textAlternative}
|
2022-07-20 22:47:51 +02:00
|
|
|
margin={0}
|
|
|
|
marginTop={4}
|
2022-06-30 18:19:07 +02:00
|
|
|
>
|
|
|
|
{t('addFromAListOfPopularNetworks')}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textMuted}
|
2022-07-20 22:47:51 +02:00
|
|
|
marginTop={4}
|
|
|
|
marginBottom={3}
|
2022-06-30 18:19:07 +02:00
|
|
|
>
|
|
|
|
{t('popularCustomNetworks')}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2023-03-09 22:00:28 +01:00
|
|
|
{notExistingNetworkConfigurations.map((item, index) => (
|
2022-06-30 18:19:07 +02:00
|
|
|
<Box
|
|
|
|
key={index}
|
2023-06-06 10:50:45 +02:00
|
|
|
display={Display.Flex}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
2022-06-30 18:19:07 +02:00
|
|
|
marginBottom={6}
|
|
|
|
className="add-network__list-of-networks"
|
2022-04-25 20:30:46 +02:00
|
|
|
>
|
2023-06-06 10:50:45 +02:00
|
|
|
<Box display={Display.Flex} alignItems={AlignItems.center}>
|
2023-06-08 19:26:20 +02:00
|
|
|
<AvatarNetwork
|
|
|
|
size={Size.SM}
|
|
|
|
src={item.rpcPrefs?.imageUrl}
|
|
|
|
name={item.nickname}
|
|
|
|
/>
|
2022-06-30 18:19:07 +02:00
|
|
|
<Box marginLeft={2}>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodySmBold}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.textDefault}
|
2022-06-30 18:19:07 +02:00
|
|
|
>
|
|
|
|
{item.nickname}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
<Box
|
2023-06-06 10:50:45 +02:00
|
|
|
display={Display.Flex}
|
2023-02-02 21:15:26 +01:00
|
|
|
alignItems={AlignItems.center}
|
2022-06-30 18:19:07 +02:00
|
|
|
marginLeft={1}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
// Warning for the networks that doesn't use infura.io as the RPC
|
|
|
|
!infuraRegex.test(item.rpcUrl) && (
|
|
|
|
<Tooltip
|
|
|
|
position="top"
|
|
|
|
interactive
|
|
|
|
html={
|
|
|
|
<Box
|
|
|
|
margin={3}
|
|
|
|
className="add-network__warning-tooltip"
|
2022-04-25 20:30:46 +02:00
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('addNetworkTooltipWarning', [
|
|
|
|
<a
|
|
|
|
key="zendesk_page_link"
|
2022-08-23 16:19:31 +02:00
|
|
|
href={ZENDESK_URLS.UNKNOWN_NETWORK}
|
2022-06-30 18:19:07 +02:00
|
|
|
rel="noreferrer"
|
|
|
|
target="_blank"
|
|
|
|
>
|
|
|
|
{t('learnMoreUpperCase')}
|
|
|
|
</a>,
|
|
|
|
])}
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
trigger="mouseenter"
|
|
|
|
>
|
2023-02-23 22:20:07 +01:00
|
|
|
<Icon
|
|
|
|
className="add-network__warning-icon"
|
2023-04-19 23:16:49 +02:00
|
|
|
name={IconName.Danger}
|
2023-02-23 22:20:07 +01:00
|
|
|
color={IconColor.iconMuted}
|
2023-04-19 23:16:49 +02:00
|
|
|
size={IconSize.Sm}
|
2022-06-30 18:19:07 +02:00
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
<Button
|
|
|
|
type="inline"
|
|
|
|
className="add-network__add-button"
|
|
|
|
onClick={async () => {
|
2023-03-09 22:00:28 +01:00
|
|
|
await dispatch(
|
|
|
|
requestUserApproval({
|
|
|
|
origin: ORIGIN_METAMASK,
|
2023-05-10 07:36:01 +02:00
|
|
|
type: ApprovalType.AddEthereumChain,
|
2023-03-09 22:00:28 +01:00
|
|
|
requestData: {
|
|
|
|
chainId: item.chainId,
|
|
|
|
rpcUrl: item.rpcUrl,
|
|
|
|
ticker: item.ticker,
|
|
|
|
rpcPrefs: item.rpcPrefs,
|
|
|
|
imageUrl: item.rpcPrefs?.imageUrl,
|
|
|
|
chainName: item.nickname,
|
|
|
|
referrer: ORIGIN_METAMASK,
|
2023-04-03 17:31:04 +02:00
|
|
|
source:
|
|
|
|
MetaMetricsNetworkEventSource.PopularNetworkList,
|
2023-03-09 22:00:28 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
2022-06-30 18:19:07 +02:00
|
|
|
}}
|
2022-04-25 20:30:46 +02:00
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('add')}
|
|
|
|
</Button>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
))}
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
padding={
|
|
|
|
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
|
|
|
|
? [2, 0, 2, 6]
|
|
|
|
: [2, 0, 2, 0]
|
|
|
|
}
|
|
|
|
className="add-network__footer"
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
type="link"
|
2022-10-27 12:25:30 +02:00
|
|
|
data-testid="add-network-manually"
|
2022-06-30 18:19:07 +02:00
|
|
|
onClick={(event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
|
|
|
|
? global.platform.openExtensionInBrowser(ADD_NETWORK_ROUTE)
|
|
|
|
: history.push(ADD_NETWORK_ROUTE);
|
|
|
|
}}
|
|
|
|
>
|
2023-03-29 00:04:22 +02:00
|
|
|
<Text
|
|
|
|
variant={TextVariant.bodySm}
|
|
|
|
as="h6"
|
2023-02-02 21:15:26 +01:00
|
|
|
color={TextColor.primaryDefault}
|
2022-04-25 20:30:46 +02:00
|
|
|
>
|
2022-06-30 18:19:07 +02:00
|
|
|
{t('addANetworkManually')}
|
2023-03-29 00:04:22 +02:00
|
|
|
</Text>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Button>
|
2022-02-25 17:50:16 +01:00
|
|
|
</Box>
|
2022-06-30 18:19:07 +02:00
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{showPopover && (
|
|
|
|
<Popover>
|
2022-07-27 22:19:05 +02:00
|
|
|
<ConfirmationPage redirectToHomeOnZeroConfirmations={false} />
|
2022-06-30 18:19:07 +02:00
|
|
|
</Popover>
|
|
|
|
)}
|
|
|
|
</>
|
2022-02-25 17:50:16 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AddNetwork;
|