2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import copyToClipboard from 'copy-to-clipboard';
|
2021-05-17 23:19:39 +02:00
|
|
|
import { shortenAddress } from '../../../helpers/utils/util';
|
2018-06-04 18:33:25 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import Tooltip from '../../ui/tooltip';
|
2021-05-17 23:19:39 +02:00
|
|
|
import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../../../shared/constants/time';
|
2023-04-04 18:48:04 +02:00
|
|
|
import {
|
|
|
|
Icon,
|
|
|
|
ICON_NAMES,
|
|
|
|
ICON_SIZES,
|
|
|
|
} from '../../component-library/icon/deprecated';
|
2023-02-02 21:15:26 +01:00
|
|
|
import { IconColor } from '../../../helpers/constants/design-system';
|
2018-06-04 18:33:25 +02:00
|
|
|
|
|
|
|
class SelectedAccount extends Component {
|
|
|
|
state = {
|
|
|
|
copied: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-04 18:33:25 +02:00
|
|
|
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-04 18:33:25 +02:00
|
|
|
|
|
|
|
static propTypes = {
|
2020-04-02 16:19:19 +02:00
|
|
|
selectedIdentity: PropTypes.object.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-06-04 18:33:25 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.copyTimeout = null;
|
2020-10-23 22:39:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentWillUnmount() {
|
2020-10-23 22:39:26 +02:00
|
|
|
if (this.copyTimeout) {
|
2021-02-04 19:15:23 +01:00
|
|
|
clearTimeout(this.copyTimeout);
|
|
|
|
this.copyTimeout = null;
|
2020-10-23 22:39:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { t } = this.context;
|
|
|
|
const { selectedIdentity } = this.props;
|
2021-05-17 23:19:39 +02:00
|
|
|
const checksummedAddress = toChecksumHexAddress(selectedIdentity.address);
|
2018-06-04 18:33:25 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="selected-account">
|
|
|
|
<Tooltip
|
2020-04-02 04:38:45 +02:00
|
|
|
wrapperClassName="selected-account__tooltip-wrapper"
|
2018-06-04 18:33:25 +02:00
|
|
|
position="bottom"
|
2020-11-03 00:41:28 +01:00
|
|
|
title={
|
|
|
|
this.state.copied ? t('copiedExclamation') : t('copyToClipboard')
|
|
|
|
}
|
2018-06-04 18:33:25 +02:00
|
|
|
>
|
2020-11-05 19:05:41 +01:00
|
|
|
<button
|
2018-06-04 18:33:25 +02:00
|
|
|
className="selected-account__clickable"
|
2022-10-11 19:54:50 +02:00
|
|
|
data-testid="selected-account-click"
|
2018-06-04 18:33:25 +02:00
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ copied: true });
|
2020-11-03 00:41:28 +01:00
|
|
|
this.copyTimeout = setTimeout(
|
|
|
|
() => this.setState({ copied: false }),
|
2021-06-10 21:27:03 +02:00
|
|
|
SECOND * 3,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
copyToClipboard(checksummedAddress);
|
2018-06-04 18:33:25 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div className="selected-account__name">
|
2020-11-03 00:41:28 +01:00
|
|
|
{selectedIdentity.name}
|
2018-06-04 18:33:25 +02:00
|
|
|
</div>
|
|
|
|
<div className="selected-account__address">
|
2020-11-03 00:41:28 +01:00
|
|
|
{shortenAddress(checksummedAddress)}
|
2021-07-06 20:17:13 +02:00
|
|
|
<div className="selected-account__copy">
|
2023-01-31 18:00:26 +01:00
|
|
|
<Icon
|
|
|
|
name={
|
|
|
|
this.state.copied
|
|
|
|
? ICON_NAMES.COPY_SUCCESS
|
|
|
|
: ICON_NAMES.COPY
|
|
|
|
}
|
|
|
|
size={ICON_SIZES.SM}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={IconColor.iconAlternative}
|
2023-01-31 18:00:26 +01:00
|
|
|
/>
|
2021-07-06 20:17:13 +02:00
|
|
|
</div>
|
2018-06-04 18:33:25 +02:00
|
|
|
</div>
|
2020-11-05 19:05:41 +01:00
|
|
|
</button>
|
2018-06-04 18:33:25 +02:00
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-06-04 18:33:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
export default SelectedAccount;
|