import React, { useContext, useState } from 'react'; import { useDispatch } from 'react-redux'; import { MetaMetricsEventAccountImportType, MetaMetricsEventAccountType, MetaMetricsEventCategory, MetaMetricsEventName, } from '../../../../shared/constants/metametrics'; import { ButtonLink, Label, Text } from '../../../components/component-library'; import Box from '../../../components/ui/box'; import Dropdown from '../../../components/ui/dropdown'; import { MetaMetricsContext } from '../../../contexts/metametrics'; import { BLOCK_SIZES, BorderColor, FONT_WEIGHT, JustifyContent, Size, TextVariant, } from '../../../helpers/constants/design-system'; import ZENDESK_URLS from '../../../helpers/constants/zendesk-url'; import { useI18nContext } from '../../../hooks/useI18nContext'; import { useRouting } from '../../../hooks/useRouting'; import * as actions from '../../../store/actions'; // Subviews import JsonImportView from './json'; import PrivateKeyImportView from './private-key'; export default function NewAccountImportForm() { const t = useI18nContext(); const dispatch = useDispatch(); const trackEvent = useContext(MetaMetricsContext); const { navigateToMostRecentOverviewPage } = useRouting(); const menuItems = [t('privateKey'), t('jsonFile')]; const [type, setType] = useState(menuItems[0]); function importAccount(strategy, importArgs) { const loadingMessage = getLoadingMessage(strategy); dispatch(actions.importNewAccount(strategy, importArgs, loadingMessage)) .then(({ selectedAddress }) => { if (selectedAddress) { trackImportEvent(strategy, true); dispatch(actions.hideWarning()); navigateToMostRecentOverviewPage(); } else { dispatch(actions.displayWarning(t('importAccountError'))); } }) .catch((error) => { trackImportEvent(strategy, error.message); translateWarning(error.message); }); } function trackImportEvent(strategy, wasSuccessful) { const accountImportType = strategy === 'Private Key' ? MetaMetricsEventAccountImportType.PrivateKey : MetaMetricsEventAccountImportType.Json; const event = wasSuccessful ? MetaMetricsEventName.AccountAdded : MetaMetricsEventName.AccountAddFailed; trackEvent({ category: MetaMetricsEventCategory.Accounts, event, properties: { account_type: MetaMetricsEventAccountType.Imported, account_import_type: accountImportType, }, }); } function getLoadingMessage(strategy) { if (strategy === 'JSON File') { return (
{t('importAccountJsonLoading1')}

{t('importAccountJsonLoading2')}
); } return ''; } /** * @param {string} message - an Error/Warning message caught in importAccount() * This function receives a message that is a string like: * `t('importAccountErrorNotHexadecimal')` * `t('importAccountErrorIsSRP')` * `t('importAccountErrorNotAValidPrivateKey')` * and feeds it through useI18nContext */ function translateWarning(message) { if (message && !message.startsWith('t(')) { // This is just a normal error message dispatch(actions.displayWarning(message)); } else { // This is an error message in a form like // `t('importAccountErrorNotHexadecimal')` // so slice off the first 3 chars and last 2 chars, and feed to i18n dispatch(actions.displayWarning(t(message.slice(3, -2)))); } } function PrivateKeyOrJson() { switch (type) { case menuItems[0]: return ; case menuItems[1]: default: return ; } } return ( <> {t('importAccount')} {t('importAccountMsg')}{' '} {t('here')} ); }