1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
metamask-extension/ui/components/ui/sender-to-recipient/sender-to-recipient.component.js
George Marshall 8ef28702f2
Dark Mode: more icon updates (#14064)
* Updating settings icons to use fontawesome and removing unused svgs

* Updating more icons to use font-awesome icons and removing unused svgs
2022-03-21 08:29:57 -07:00

237 lines
6.8 KiB
JavaScript

import React, { useState } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import copyToClipboard from 'copy-to-clipboard';
import Tooltip from '../tooltip';
import IconCaretRight from '../icon/icon-caret-right';
import Identicon from '../identicon';
import { shortenAddress } from '../../../helpers/utils/util';
import AccountMismatchWarning from '../account-mismatch-warning/account-mismatch-warning.component';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
import NicknamePopovers from '../../app/modals/nickname-popovers';
import {
DEFAULT_VARIANT,
CARDS_VARIANT,
FLAT_VARIANT,
} from './sender-to-recipient.constants';
const variantHash = {
[DEFAULT_VARIANT]: 'sender-to-recipient--default',
[CARDS_VARIANT]: 'sender-to-recipient--cards',
[FLAT_VARIANT]: 'sender-to-recipient--flat',
};
function SenderAddress({
addressOnly,
checksummedSenderAddress,
senderName,
onSenderClick,
senderAddress,
warnUserOnAccountMismatch,
}) {
const t = useI18nContext();
const [addressCopied, setAddressCopied] = useState(false);
let tooltipHtml = <p>{t('copiedExclamation')}</p>;
if (!addressCopied) {
tooltipHtml = addressOnly ? (
<p>{t('copyAddress')}</p>
) : (
<p>
{shortenAddress(checksummedSenderAddress)}
<br />
{t('copyAddress')}
</p>
);
}
return (
<div
className={classnames(
'sender-to-recipient__party sender-to-recipient__party--sender',
)}
onClick={() => {
setAddressCopied(true);
copyToClipboard(checksummedSenderAddress);
if (onSenderClick) {
onSenderClick();
}
}}
>
<div className="sender-to-recipient__sender-icon">
<Identicon
address={toChecksumHexAddress(senderAddress)}
diameter={24}
/>
</div>
<Tooltip
position="bottom"
html={tooltipHtml}
wrapperClassName="sender-to-recipient__tooltip-wrapper"
containerClassName="sender-to-recipient__tooltip-container"
onHidden={() => setAddressCopied(false)}
>
<div className="sender-to-recipient__name">
{addressOnly ? (
<span>
{`${senderName || shortenAddress(checksummedSenderAddress)}`}
</span>
) : (
senderName
)}
</div>
</Tooltip>
{warnUserOnAccountMismatch && (
<AccountMismatchWarning address={senderAddress} />
)}
</div>
);
}
SenderAddress.propTypes = {
senderName: PropTypes.string,
checksummedSenderAddress: PropTypes.string,
addressOnly: PropTypes.bool,
senderAddress: PropTypes.string,
onSenderClick: PropTypes.func,
warnUserOnAccountMismatch: PropTypes.bool,
};
export function RecipientWithAddress({
checksummedRecipientAddress,
onRecipientClick,
addressOnly,
recipientNickname,
recipientEns,
recipientName,
}) {
const t = useI18nContext();
const [showNicknamePopovers, setShowNicknamePopovers] = useState(false);
return (
<>
<div
className="sender-to-recipient__party sender-to-recipient__party--recipient sender-to-recipient__party--recipient-with-address"
onClick={() => {
setShowNicknamePopovers(true);
if (onRecipientClick) {
onRecipientClick();
}
}}
>
<div className="sender-to-recipient__sender-icon">
<Identicon address={checksummedRecipientAddress} diameter={24} />
</div>
<div className="sender-to-recipient__name">
{addressOnly
? recipientNickname ||
recipientEns ||
shortenAddress(checksummedRecipientAddress)
: recipientNickname ||
recipientEns ||
recipientName ||
t('newContract')}
</div>
</div>
{showNicknamePopovers ? (
<NicknamePopovers
onClose={() => setShowNicknamePopovers(false)}
address={checksummedRecipientAddress}
/>
) : null}
</>
);
}
RecipientWithAddress.propTypes = {
checksummedRecipientAddress: PropTypes.string,
recipientName: PropTypes.string,
recipientEns: PropTypes.string,
recipientNickname: PropTypes.string,
addressOnly: PropTypes.bool,
onRecipientClick: PropTypes.func,
};
function Arrow({ variant }) {
return variant === DEFAULT_VARIANT ? (
<div className="sender-to-recipient__arrow-container">
<div className="sender-to-recipient__arrow-circle">
<i className="fa fa-arrow-right sender-to-recipient__arrow-circle__icon" />
</div>
</div>
) : (
<div className="sender-to-recipient__arrow-container">
<IconCaretRight />
</div>
);
}
Arrow.propTypes = {
variant: PropTypes.oneOf([DEFAULT_VARIANT, CARDS_VARIANT, FLAT_VARIANT]),
};
export default function SenderToRecipient({
senderAddress,
addressOnly,
senderName,
recipientNickname,
recipientName,
recipientEns,
onRecipientClick,
onSenderClick,
recipientAddress,
variant,
warnUserOnAccountMismatch,
}) {
const t = useI18nContext();
const checksummedSenderAddress = toChecksumHexAddress(senderAddress);
const checksummedRecipientAddress = toChecksumHexAddress(recipientAddress);
return (
<div className={classnames('sender-to-recipient', variantHash[variant])}>
<SenderAddress
checksummedSenderAddress={checksummedSenderAddress}
addressOnly={addressOnly}
senderName={senderName}
onSenderClick={onSenderClick}
senderAddress={senderAddress}
warnUserOnAccountMismatch={warnUserOnAccountMismatch}
/>
<Arrow variant={variant} />
{recipientAddress ? (
<RecipientWithAddress
checksummedRecipientAddress={checksummedRecipientAddress}
onRecipientClick={onRecipientClick}
addressOnly={addressOnly}
recipientNickname={recipientNickname}
recipientEns={recipientEns}
recipientName={recipientName}
/>
) : (
<div className="sender-to-recipient__party sender-to-recipient__party--recipient">
<i className="fa fa-file-text-o" />
<div className="sender-to-recipient__name">{t('newContract')}</div>
</div>
)}
</div>
);
}
SenderToRecipient.defaultProps = {
variant: DEFAULT_VARIANT,
warnUserOnAccountMismatch: true,
};
SenderToRecipient.propTypes = {
senderName: PropTypes.string,
senderAddress: PropTypes.string,
recipientName: PropTypes.string,
recipientEns: PropTypes.string,
recipientAddress: PropTypes.string,
recipientNickname: PropTypes.string,
variant: PropTypes.oneOf([DEFAULT_VARIANT, CARDS_VARIANT, FLAT_VARIANT]),
addressOnly: PropTypes.bool,
onRecipientClick: PropTypes.func,
onSenderClick: PropTypes.func,
warnUserOnAccountMismatch: PropTypes.bool,
};