1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 11:28:51 +01:00
metamask-extension/ui/components/multichain/network-list-item/network-list-item.js
Danica Shen 00d155ce2f
feat(878): implement network txn toggle and new style (#20363)
* feat(878): implement new incoming transaction toggle networks for setting and onboarding

* Update state snapshots

* feat(878): change gaps, migration types based on comment

---------

Co-authored-by: Mark Stacey <markjstacey@gmail.com>
2023-08-24 11:27:42 +01:00

140 lines
3.3 KiB
JavaScript

import React, { useEffect, useRef } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import {
AlignItems,
IconColor,
BorderRadius,
Color,
Size,
JustifyContent,
TextColor,
BackgroundColor,
BlockSize,
Display,
} from '../../../helpers/constants/design-system';
import {
AvatarNetwork,
ButtonIcon,
Text,
IconName,
Box,
} from '../../component-library';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { getAvatarNetworkColor } from '../../../helpers/utils/accounts';
import Tooltip from '../../ui/tooltip/tooltip';
const MAXIMUM_CHARACTERS_WITHOUT_TOOLTIP = 20;
export const NetworkListItem = ({
name,
iconSrc,
selected = false,
focus = true,
onClick,
onDeleteClick,
}) => {
const t = useI18nContext();
const networkRef = useRef();
useEffect(() => {
if (networkRef.current && focus) {
networkRef.current.focus();
}
}, [networkRef, focus]);
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,
})}
display={Display.Flex}
alignItems={AlignItems.center}
justifyContent={JustifyContent.spaceBetween}
width={BlockSize.Full}
>
{selected && (
<Box
className="multichain-network-list-item__selected-indicator"
borderRadius={BorderRadius.pill}
backgroundColor={Color.primaryDefault}
/>
)}
<AvatarNetwork
backgroundColor={getAvatarNetworkColor(name)}
name={name}
src={iconSrc}
/>
<Box className="multichain-network-list-item__network-name">
<Text
ref={networkRef}
as="button"
onClick={(e) => {
e.stopPropagation();
onClick();
}}
color={TextColor.textDefault}
backgroundColor={BackgroundColor.transparent}
ellipsis
>
{name.length > MAXIMUM_CHARACTERS_WITHOUT_TOOLTIP ? (
<Tooltip
title={name}
position="bottom"
wrapperClassName="multichain-network-list-item__tooltip"
>
{name}
</Tooltip>
) : (
name
)}
</Text>
</Box>
{onDeleteClick ? (
<ButtonIcon
className="multichain-network-list-item__delete"
color={IconColor.errorDefault}
iconName={IconName.Trash}
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,
/**
* Represents if the network item should be keyboard selected
*/
focus: PropTypes.bool,
};