import React, { Component } from 'react'; import PropTypes from 'prop-types'; import Fuse from 'fuse.js'; import InputAdornment from '@material-ui/core/InputAdornment'; import classnames from 'classnames'; import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app'; import { getEnvironmentType } from '../../../../app/scripts/lib/util'; import Identicon from '../../ui/identicon'; import SiteIcon from '../../ui/site-icon'; import UserPreferencedCurrencyDisplay from '../user-preferenced-currency-display'; import { PRIMARY } from '../../../helpers/constants/common'; import { SETTINGS_ROUTE, NEW_ACCOUNT_ROUTE, IMPORT_ACCOUNT_ROUTE, CONNECT_HARDWARE_ROUTE, ///: BEGIN:ONLY_INCLUDE_IN(snaps) NOTIFICATIONS_ROUTE, ///: END:ONLY_INCLUDE_IN ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) CUSTODY_ACCOUNT_ROUTE, COMPLIANCE_FEATURE_ROUTE, ///: END:ONLY_INCLUDE_IN } from '../../../helpers/constants/routes'; import TextField from '../../ui/text-field'; import SearchIcon from '../../ui/icon/search-icon'; import { IconColor } from '../../../helpers/constants/design-system'; import { Icon, IconName, IconSize } from '../../component-library'; ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) import { shortenAddress } from '../../../helpers/utils/util'; import CustodyLabels from '../../institutional/custody-labels'; import { toChecksumHexAddress } from '../../../../shared/modules/hexstring-utils'; ///: END:ONLY_INCLUDE_IN import KeyRingLabel from './keyring-label'; export function AccountMenuItem(props) { const { icon, children, text, subText, className, onClick } = props; const itemClassName = classnames('account-menu__item', className, { 'account-menu__item--clickable': Boolean(onClick), }); return children ? (
{children}
) : ( ); } AccountMenuItem.propTypes = { icon: PropTypes.node, children: PropTypes.node, text: PropTypes.node, subText: PropTypes.node, onClick: PropTypes.func, className: PropTypes.string, }; export default class AccountMenu extends Component { static contextTypes = { t: PropTypes.func, trackEvent: PropTypes.func, }; static propTypes = { shouldShowAccountsSearch: PropTypes.bool, accounts: PropTypes.array, history: PropTypes.object, isAccountMenuOpen: PropTypes.bool, keyrings: PropTypes.array, selectedAddress: PropTypes.string, setSelectedAccount: PropTypes.func, toggleAccountMenu: PropTypes.func, addressConnectedSubjectMap: PropTypes.object, originOfCurrentTab: PropTypes.string, ///: BEGIN:ONLY_INCLUDE_IN(snaps) unreadNotificationsCount: PropTypes.number, ///: END:ONLY_INCLUDE_IN ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) custodyAccountDetails: PropTypes.object, mmiPortfolioEnabled: PropTypes.bool, mmiPortfolioUrl: PropTypes.string, ///: END:ONLY_INCLUDE_IN }; accountsRef; state = { searchQuery: '', }; addressFuse = new Fuse([], { threshold: 0.55, location: 0, distance: 100, maxPatternLength: 32, minMatchCharLength: 1, ignoreFieldNorm: true, keys: [ { name: 'name', weight: 0.5 }, { name: 'address', weight: 0.5 }, ], }); componentDidUpdate(prevProps) { const { isAccountMenuOpen: prevIsAccountMenuOpen } = prevProps; const { isAccountMenuOpen } = this.props; if (!prevIsAccountMenuOpen && isAccountMenuOpen) { this.resetSearchQuery(); } } renderAccountsSearch() { const handleChange = (e) => { const val = e.target.value.length > 1 ? e.target.value : ''; this.setSearchQuery(val); }; const inputAdornment = ( ); return [ ,
, ]; } renderAccounts() { const { accounts, selectedAddress, keyrings, setSelectedAccount, addressConnectedSubjectMap, originOfCurrentTab, ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) custodyAccountDetails, ///: END:ONLY_INCLUDE_IN } = this.props; const { searchQuery } = this.state; let filteredIdentities = accounts; if (searchQuery) { this.addressFuse.setCollection(accounts); filteredIdentities = this.addressFuse.search(searchQuery); } if (filteredIdentities.length === 0) { return (

{this.context.t('noAccountsFound')}

); } return filteredIdentities.map((identity) => { const isSelected = identity.address === selectedAddress; const simpleAddress = identity.address.substring(2).toLowerCase(); const keyring = keyrings.find((kr) => { return ( kr.accounts.includes(simpleAddress) || kr.accounts.includes(identity.address) ); }); const addressSubjects = addressConnectedSubjectMap[identity.address] || {}; const iconAndNameForOpenSubject = addressSubjects[originOfCurrentTab]; ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) const labels = custodyAccountDetails ? custodyAccountDetails[toChecksumHexAddress(identity.address)] ?.labels || [] : []; ///: END:ONLY_INCLUDE_IN return ( ); }); } resetSearchQuery() { this.setSearchQuery(''); } setSearchQuery(searchQuery) { this.setState({ searchQuery }); } render() { const { t } = this.context; const { shouldShowAccountsSearch, isAccountMenuOpen, toggleAccountMenu, history, ///: BEGIN:ONLY_INCLUDE_IN(snaps) unreadNotificationsCount, ///: END:ONLY_INCLUDE_IN ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) mmiPortfolioEnabled, mmiPortfolioUrl, ///: END:ONLY_INCLUDE_IN } = this.props; if (!isAccountMenuOpen) { return null; } return (
{shouldShowAccountsSearch ? this.renderAccountsSearch() : null}
{ this.accountsRef = ref; }} > {this.renderAccounts()}
{ toggleAccountMenu(); history.push(NEW_ACCOUNT_ROUTE); }} icon={} text={t('createAccount')} /> { toggleAccountMenu(); history.push(IMPORT_ACCOUNT_ROUTE); }} icon={ } text={t('importAccount')} /> { toggleAccountMenu(); if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) { global.platform.openExtensionInBrowser(CONNECT_HARDWARE_ROUTE); } else { history.push(CONNECT_HARDWARE_ROUTE); } }} icon={ } text={t('connectHardwareWallet')} /> { ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) <> { toggleAccountMenu(); if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) { global.platform.openExtensionInBrowser(CUSTODY_ACCOUNT_ROUTE); } else { history.push(CUSTODY_ACCOUNT_ROUTE); } }} Icon={ } text={t('connectCustodialAccountMenu')} /> {mmiPortfolioEnabled && ( { toggleAccountMenu(); window.open(mmiPortfolioUrl, '_blank'); }} Icon={ } text={t('portfolioDashboard')} /> )} { toggleAccountMenu(); if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) { global.platform.openExtensionInBrowser( COMPLIANCE_FEATURE_ROUTE, ); } else { history.push(COMPLIANCE_FEATURE_ROUTE); } }} Icon={ } text={t('compliance')} /> ///: END:ONLY_INCLUDE_IN }
{ ///: BEGIN:ONLY_INCLUDE_IN(snaps) { toggleAccountMenu(); history.push(NOTIFICATIONS_ROUTE); }} icon={
{unreadNotificationsCount > 0 && (
{unreadNotificationsCount}
)}
} text={t('notifications')} /> ///: END:ONLY_INCLUDE_IN } { toggleAccountMenu(); history.push(SETTINGS_ROUTE); }} icon={ } text={t('settings')} />
); } }