1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/ui/components/multichain/address-copy-button/address-copy-button.js
David Walsh c1a7f46e8f
UX: Show Checksum Addresses in Account Menu (#20135)
* UX: Show Checksum Addresses in Account Menu

* Checksum the account details -> export private key address copy button

* Update tests
2023-07-26 14:37:33 -05:00

78 lines
2.2 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { toChecksumHexAddress } from '@metamask/controller-utils';
import { ButtonBase, IconName } from '../../component-library';
import {
BackgroundColor,
TextVariant,
TextColor,
Size,
BorderRadius,
AlignItems,
} from '../../../helpers/constants/design-system';
import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard';
import { shortenAddress } from '../../../helpers/utils/util';
import Tooltip from '../../ui/tooltip/tooltip';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { MINUTE } from '../../../../shared/constants/time';
export const AddressCopyButton = ({
address,
shorten = false,
wrap = false,
onClick,
}) => {
const checksummedAddress = toChecksumHexAddress(address);
const displayAddress = shorten
? shortenAddress(checksummedAddress)
: checksummedAddress;
const [copied, handleCopy] = useCopyToClipboard(MINUTE);
const t = useI18nContext();
return (
<Tooltip position="bottom" title={copied ? t('copiedExclamation') : null}>
<ButtonBase
backgroundColor={BackgroundColor.primaryMuted}
onClick={() => {
handleCopy(checksummedAddress);
onClick?.();
}}
paddingRight={4}
paddingLeft={4}
size={Size.SM}
variant={TextVariant.bodySm}
color={TextColor.primaryDefault}
endIconName={copied ? IconName.CopySuccess : IconName.Copy}
className={classnames('multichain-address-copy-button', {
'multichain-address-copy-button__address--wrap': wrap,
})}
borderRadius={BorderRadius.pill}
alignItems={AlignItems.center}
data-testid="address-copy-button-text"
>
{displayAddress}
</ButtonBase>
</Tooltip>
);
};
AddressCopyButton.propTypes = {
/**
* Address to be copied
*/
address: PropTypes.string.isRequired,
/**
* Represents if the address should be shortened
*/
shorten: PropTypes.bool,
/**
* Represents if the element should wrap to multiple lines
*/
wrap: PropTypes.bool,
/**
* Fires when the button is clicked
*/
onClick: PropTypes.func,
};