2023-04-26 15:18:48 +02:00
|
|
|
import React, { useState, useContext, useRef, useEffect } from 'react';
|
2023-03-22 11:00:08 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import Fuse from 'fuse.js';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
2023-05-17 16:58:00 +02:00
|
|
|
import {
|
|
|
|
IconName,
|
|
|
|
ButtonLink,
|
|
|
|
TextFieldSearch,
|
|
|
|
Text,
|
2023-06-23 11:55:31 +02:00
|
|
|
Box,
|
2023-05-17 16:58:00 +02:00
|
|
|
} from '../../component-library';
|
2023-06-13 17:07:01 +02:00
|
|
|
import { AccountListItem, CreateAccount, ImportAccount } from '..';
|
2023-03-22 11:00:08 +01:00
|
|
|
import {
|
|
|
|
BLOCK_SIZES,
|
|
|
|
Size,
|
|
|
|
TextColor,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
|
|
|
import Popover from '../../ui/popover';
|
|
|
|
import {
|
|
|
|
getSelectedAccount,
|
|
|
|
getMetaMaskAccountsOrdered,
|
|
|
|
getConnectedSubjectsForAllAddresses,
|
|
|
|
getOriginOfCurrentTab,
|
|
|
|
} from '../../../selectors';
|
|
|
|
import { toggleAccountMenu, setSelectedAccount } from '../../../store/actions';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventAccountType,
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2023-03-22 11:00:08 +01:00
|
|
|
import {
|
|
|
|
CONNECT_HARDWARE_ROUTE,
|
2023-05-17 16:58:00 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
|
|
|
CUSTODY_ACCOUNT_ROUTE,
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
2023-03-22 11:00:08 +01:00
|
|
|
} from '../../../helpers/constants/routes';
|
|
|
|
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
|
|
|
|
import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app';
|
|
|
|
|
|
|
|
export const AccountListMenu = ({ onClose }) => {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
|
|
|
const accounts = useSelector(getMetaMaskAccountsOrdered);
|
|
|
|
const selectedAccount = useSelector(getSelectedAccount);
|
|
|
|
const connectedSites = useSelector(getConnectedSubjectsForAllAddresses);
|
|
|
|
const currentTabOrigin = useSelector(getOriginOfCurrentTab);
|
|
|
|
const history = useHistory();
|
|
|
|
const dispatch = useDispatch();
|
2023-04-26 15:18:48 +02:00
|
|
|
const inputRef = useRef();
|
2023-03-22 11:00:08 +01:00
|
|
|
|
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
2023-06-13 17:07:01 +02:00
|
|
|
const [actionMode, setActionMode] = useState('');
|
2023-03-22 11:00:08 +01:00
|
|
|
|
|
|
|
let searchResults = accounts;
|
|
|
|
if (searchQuery) {
|
|
|
|
const fuse = new Fuse(accounts, {
|
|
|
|
threshold: 0.2,
|
|
|
|
location: 0,
|
|
|
|
distance: 100,
|
|
|
|
maxPatternLength: 32,
|
|
|
|
minMatchCharLength: 1,
|
|
|
|
keys: ['name', 'address'],
|
|
|
|
});
|
|
|
|
fuse.setCollection(accounts);
|
|
|
|
searchResults = fuse.search(searchQuery);
|
|
|
|
}
|
|
|
|
|
2023-04-26 15:18:48 +02:00
|
|
|
// Focus on the search box when the popover is opened
|
|
|
|
useEffect(() => {
|
|
|
|
if (inputRef.current) {
|
2023-05-09 14:38:36 +02:00
|
|
|
inputRef.current.rootNode.querySelector('input[type=search]')?.focus();
|
2023-04-26 15:18:48 +02:00
|
|
|
}
|
|
|
|
}, [inputRef]);
|
|
|
|
|
2023-06-13 17:07:01 +02:00
|
|
|
let title = t('selectAnAccount');
|
|
|
|
if (actionMode === 'add') {
|
|
|
|
title = t('addAccount');
|
|
|
|
} else if (actionMode === 'import') {
|
|
|
|
title = t('importAccount');
|
|
|
|
}
|
|
|
|
|
2023-03-22 11:00:08 +01:00
|
|
|
return (
|
2023-04-26 15:18:48 +02:00
|
|
|
<Popover
|
2023-06-13 17:07:01 +02:00
|
|
|
title={title}
|
2023-04-26 15:18:48 +02:00
|
|
|
ref={inputRef}
|
|
|
|
centerTitle
|
|
|
|
onClose={onClose}
|
2023-06-13 17:07:01 +02:00
|
|
|
onBack={actionMode === '' ? null : () => setActionMode('')}
|
2023-06-20 15:38:24 +02:00
|
|
|
className="multichain-account-menu-popover"
|
2023-04-26 15:18:48 +02:00
|
|
|
>
|
2023-06-13 17:07:01 +02:00
|
|
|
{actionMode === 'add' ? (
|
|
|
|
<Box paddingLeft={4} paddingRight={4} paddingBottom={4} paddingTop={0}>
|
|
|
|
<CreateAccount
|
|
|
|
onActionComplete={(confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
dispatch(toggleAccountMenu());
|
|
|
|
} else {
|
|
|
|
setActionMode('');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
) : null}
|
|
|
|
{actionMode === 'import' ? (
|
|
|
|
<Box paddingLeft={4} paddingRight={4} paddingBottom={4} paddingTop={0}>
|
|
|
|
<ImportAccount
|
|
|
|
onActionComplete={(confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
dispatch(toggleAccountMenu());
|
|
|
|
} else {
|
|
|
|
setActionMode('');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
) : null}
|
|
|
|
{actionMode === '' ? (
|
2023-06-20 15:38:24 +02:00
|
|
|
<Box>
|
2023-06-13 17:07:01 +02:00
|
|
|
{/* Search box */}
|
|
|
|
{accounts.length > 1 ? (
|
|
|
|
<Box
|
2023-03-22 11:00:08 +01:00
|
|
|
paddingLeft={4}
|
|
|
|
paddingRight={4}
|
2023-06-13 17:07:01 +02:00
|
|
|
paddingBottom={4}
|
|
|
|
paddingTop={0}
|
2023-03-22 11:00:08 +01:00
|
|
|
>
|
2023-06-13 17:07:01 +02:00
|
|
|
<TextFieldSearch
|
|
|
|
size={Size.SM}
|
|
|
|
width={BLOCK_SIZES.FULL}
|
|
|
|
placeholder={t('searchAccounts')}
|
|
|
|
value={searchQuery}
|
|
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
|
|
clearButtonOnClick={() => setSearchQuery('')}
|
|
|
|
clearButtonProps={{
|
|
|
|
size: Size.SM,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Box>
|
2023-03-22 11:00:08 +01:00
|
|
|
) : null}
|
2023-06-13 17:07:01 +02:00
|
|
|
{/* Account list block */}
|
2023-06-20 15:38:24 +02:00
|
|
|
<Box className="multichain-account-menu-popover__list">
|
2023-06-13 17:07:01 +02:00
|
|
|
{searchResults.length === 0 && searchQuery !== '' ? (
|
|
|
|
<Text
|
|
|
|
paddingLeft={4}
|
|
|
|
paddingRight={4}
|
|
|
|
color={TextColor.textMuted}
|
2023-06-20 15:38:24 +02:00
|
|
|
data-testid="multichain-account-menu-popover-no-results"
|
2023-06-13 17:07:01 +02:00
|
|
|
>
|
|
|
|
{t('noAccountsFound')}
|
|
|
|
</Text>
|
|
|
|
) : null}
|
|
|
|
{searchResults.map((account) => {
|
|
|
|
const connectedSite = connectedSites[account.address]?.find(
|
|
|
|
({ origin }) => origin === currentTabOrigin,
|
|
|
|
);
|
2023-03-22 11:00:08 +01:00
|
|
|
|
2023-06-13 17:07:01 +02:00
|
|
|
return (
|
|
|
|
<AccountListItem
|
|
|
|
onClick={() => {
|
|
|
|
dispatch(toggleAccountMenu());
|
|
|
|
trackEvent({
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.NavAccountSwitched,
|
|
|
|
properties: {
|
|
|
|
location: 'Main Menu',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dispatch(setSelectedAccount(account.address));
|
|
|
|
}}
|
|
|
|
identity={account}
|
|
|
|
key={account.address}
|
|
|
|
selected={selectedAccount.address === account.address}
|
|
|
|
closeMenu={onClose}
|
|
|
|
connectedAvatar={connectedSite?.iconUrl}
|
|
|
|
connectedAvatarName={connectedSite?.name}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Box>
|
|
|
|
{/* Add / Import / Hardware */}
|
|
|
|
<Box padding={4}>
|
|
|
|
<Box marginBottom={4}>
|
|
|
|
<ButtonLink
|
|
|
|
size={Size.SM}
|
|
|
|
startIconName={IconName.Add}
|
|
|
|
onClick={() => {
|
|
|
|
trackEvent({
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.AccountAddSelected,
|
|
|
|
properties: {
|
|
|
|
account_type: MetaMetricsEventAccountType.Default,
|
|
|
|
location: 'Main Menu',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setActionMode('add');
|
|
|
|
}}
|
2023-06-20 15:38:24 +02:00
|
|
|
data-testid="multichain-account-menu-popover-add-account"
|
2023-06-13 17:07:01 +02:00
|
|
|
>
|
|
|
|
{t('addAccount')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
|
|
|
<Box marginBottom={4}>
|
|
|
|
<ButtonLink
|
|
|
|
size={Size.SM}
|
|
|
|
startIconName={IconName.Import}
|
|
|
|
onClick={() => {
|
|
|
|
trackEvent({
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event: MetaMetricsEventName.AccountAddSelected,
|
|
|
|
properties: {
|
|
|
|
account_type: MetaMetricsEventAccountType.Imported,
|
|
|
|
location: 'Main Menu',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
setActionMode('import');
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('importAccount')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
2023-06-23 11:55:31 +02:00
|
|
|
<Box marginBottom={4}>
|
2023-06-13 17:07:01 +02:00
|
|
|
<ButtonLink
|
|
|
|
size={Size.SM}
|
|
|
|
startIconName={IconName.Hardware}
|
2023-03-22 11:00:08 +01:00
|
|
|
onClick={() => {
|
|
|
|
dispatch(toggleAccountMenu());
|
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
2023-06-13 17:07:01 +02:00
|
|
|
event: MetaMetricsEventName.AccountAddSelected,
|
2023-03-22 11:00:08 +01:00
|
|
|
properties: {
|
2023-06-13 17:07:01 +02:00
|
|
|
account_type: MetaMetricsEventAccountType.Hardware,
|
2023-03-22 11:00:08 +01:00
|
|
|
location: 'Main Menu',
|
|
|
|
},
|
|
|
|
});
|
2023-06-13 17:07:01 +02:00
|
|
|
if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) {
|
|
|
|
global.platform.openExtensionInBrowser(
|
|
|
|
CONNECT_HARDWARE_ROUTE,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
history.push(CONNECT_HARDWARE_ROUTE);
|
|
|
|
}
|
2023-03-22 11:00:08 +01:00
|
|
|
}}
|
2023-06-13 17:07:01 +02:00
|
|
|
>
|
|
|
|
{t('hardwareWallet')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
2023-06-23 11:55:31 +02:00
|
|
|
{
|
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
|
|
|
<Box>
|
|
|
|
<ButtonLink
|
|
|
|
size={Size.SM}
|
|
|
|
startIconName={IconName.Custody}
|
|
|
|
onClick={() => {
|
|
|
|
dispatch(toggleAccountMenu());
|
|
|
|
trackEvent({
|
|
|
|
category: MetaMetricsEventCategory.Navigation,
|
|
|
|
event:
|
|
|
|
MetaMetricsEventName.UserClickedConnectCustodialAccount,
|
|
|
|
});
|
|
|
|
if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) {
|
|
|
|
global.platform.openExtensionInBrowser(
|
|
|
|
CUSTODY_ACCOUNT_ROUTE,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
history.push(CUSTODY_ACCOUNT_ROUTE);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t('connectCustodialAccountMenu')}
|
|
|
|
</ButtonLink>
|
|
|
|
</Box>
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
}
|
2023-03-22 11:00:08 +01:00
|
|
|
</Box>
|
|
|
|
</Box>
|
2023-06-13 17:07:01 +02:00
|
|
|
) : null}
|
2023-03-22 11:00:08 +01:00
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
AccountListMenu.propTypes = {
|
|
|
|
/**
|
|
|
|
* Function that executes when the menu closes
|
|
|
|
*/
|
|
|
|
onClose: PropTypes.func.isRequired,
|
|
|
|
};
|