2023-04-21 15:37:51 +02:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2023-03-31 19:58:25 +02:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Box from '../../ui/box/box';
|
|
|
|
import {
|
|
|
|
AlignItems,
|
|
|
|
IconColor,
|
|
|
|
BorderRadius,
|
|
|
|
Color,
|
|
|
|
Size,
|
|
|
|
JustifyContent,
|
|
|
|
TextColor,
|
|
|
|
BLOCK_SIZES,
|
2023-05-08 16:50:04 +02:00
|
|
|
BackgroundColor,
|
2023-03-31 19:58:25 +02:00
|
|
|
} from '../../../helpers/constants/design-system';
|
2023-04-14 00:24:47 +02:00
|
|
|
import {
|
|
|
|
AvatarNetwork,
|
|
|
|
ButtonIcon,
|
|
|
|
ButtonLink,
|
|
|
|
IconName,
|
|
|
|
} from '../../component-library';
|
2023-03-31 19:58:25 +02:00
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import Tooltip from '../../ui/tooltip/tooltip';
|
2023-05-08 16:50:04 +02:00
|
|
|
import {
|
|
|
|
GOERLI_DISPLAY_NAME,
|
|
|
|
LINEA_TESTNET_DISPLAY_NAME,
|
|
|
|
SEPOLIA_DISPLAY_NAME,
|
|
|
|
} from '../../../../shared/constants/network';
|
2023-03-31 19:58:25 +02:00
|
|
|
|
|
|
|
const MAXIMUM_CHARACTERS_WITHOUT_TOOLTIP = 17;
|
|
|
|
|
2023-05-08 16:50:04 +02:00
|
|
|
function getAvatarNetworkColor(name) {
|
|
|
|
switch (name) {
|
|
|
|
case GOERLI_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.goerli;
|
|
|
|
case LINEA_TESTNET_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.lineaTestnet;
|
|
|
|
case SEPOLIA_DISPLAY_NAME:
|
|
|
|
return BackgroundColor.sepolia;
|
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
export const NetworkListItem = ({
|
|
|
|
name,
|
|
|
|
iconSrc,
|
|
|
|
selected = false,
|
|
|
|
onClick,
|
|
|
|
onDeleteClick,
|
|
|
|
}) => {
|
|
|
|
const t = useI18nContext();
|
2023-04-21 15:37:51 +02:00
|
|
|
const networkRef = useRef();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (networkRef.current && selected) {
|
|
|
|
networkRef.current.querySelector('.mm-button-link').focus();
|
|
|
|
}
|
|
|
|
}, [networkRef, selected]);
|
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
onClick={onClick}
|
|
|
|
padding={4}
|
|
|
|
gap={2}
|
|
|
|
backgroundColor={selected ? Color.primaryMuted : Color.transparent}
|
|
|
|
className={classnames('multichain-network-list-item', {
|
|
|
|
'multichain-network-list-item--selected': selected,
|
|
|
|
})}
|
|
|
|
alignItems={AlignItems.center}
|
|
|
|
justifyContent={JustifyContent.spaceBetween}
|
|
|
|
width={BLOCK_SIZES.FULL}
|
2023-04-21 15:37:51 +02:00
|
|
|
ref={networkRef}
|
2023-03-31 19:58:25 +02:00
|
|
|
>
|
|
|
|
{selected && (
|
|
|
|
<Box
|
|
|
|
className="multichain-network-list-item__selected-indicator"
|
|
|
|
borderRadius={BorderRadius.pill}
|
|
|
|
backgroundColor={Color.primaryDefault}
|
|
|
|
/>
|
|
|
|
)}
|
2023-05-08 16:50:04 +02:00
|
|
|
<AvatarNetwork
|
|
|
|
backgroundColor={getAvatarNetworkColor(name)}
|
|
|
|
name={name}
|
|
|
|
src={iconSrc}
|
|
|
|
/>
|
2023-03-31 19:58:25 +02:00
|
|
|
<Box className="multichain-network-list-item__network-name">
|
2023-04-21 15:37:51 +02:00
|
|
|
<ButtonLink
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
onClick();
|
|
|
|
}}
|
|
|
|
color={TextColor.textDefault}
|
|
|
|
ellipsis
|
|
|
|
>
|
2023-03-31 19:58:25 +02:00
|
|
|
{name.length > MAXIMUM_CHARACTERS_WITHOUT_TOOLTIP ? (
|
|
|
|
<Tooltip
|
|
|
|
title={name}
|
|
|
|
position="bottom"
|
|
|
|
wrapperClassName="multichain-network-list-item__tooltip"
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</Tooltip>
|
|
|
|
) : (
|
|
|
|
name
|
|
|
|
)}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
|
|
|
{onDeleteClick ? (
|
|
|
|
<ButtonIcon
|
|
|
|
className="multichain-network-list-item__delete"
|
|
|
|
color={IconColor.errorDefault}
|
2023-04-14 00:24:47 +02:00
|
|
|
iconName={IconName.Trash}
|
2023-03-31 19:58:25 +02:00
|
|
|
ariaLabel={t('deleteNetwork')}
|
|
|
|
size={Size.SM}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
onDeleteClick();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
NetworkListItem.propTypes = {
|
|
|
|
/**
|
|
|
|
* The name of the network
|
|
|
|
*/
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
/**
|
|
|
|
* Path to the Icon image
|
|
|
|
*/
|
|
|
|
iconSrc: PropTypes.string,
|
|
|
|
/**
|
|
|
|
* Represents if the network item is selected
|
|
|
|
*/
|
|
|
|
selected: PropTypes.bool,
|
|
|
|
/**
|
|
|
|
* Executes when the item is clicked
|
|
|
|
*/
|
|
|
|
onClick: PropTypes.func.isRequired,
|
|
|
|
/**
|
|
|
|
* Executes when the delete icon is clicked
|
|
|
|
*/
|
|
|
|
onDeleteClick: PropTypes.func,
|
|
|
|
};
|