import React, { useContext, useState } from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import {
MetaMetricsEventAccountImportType,
MetaMetricsEventAccountType,
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import { ButtonLink, Label, Text } from '../../component-library';
import Box from '../../ui/box';
import Dropdown from '../../ui/dropdown';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
BlockSize,
FontWeight,
JustifyContent,
Size,
TextVariant,
} from '../../../helpers/constants/design-system';
import ZENDESK_URLS from '../../../helpers/constants/zendesk-url';
import { useI18nContext } from '../../../hooks/useI18nContext';
import * as actions from '../../../store/actions';
// Subviews
import JsonImportView from './json';
import PrivateKeyImportView from './private-key';
export const ImportAccount = ({ onActionComplete }) => {
const t = useI18nContext();
const dispatch = useDispatch();
const trackEvent = useContext(MetaMetricsContext);
const menuItems = [t('privateKey'), t('jsonFile')];
const [type, setType] = useState(menuItems[0]);
async function importAccount(strategy, importArgs) {
const loadingMessage = getLoadingMessage(strategy);
try {
const { selectedAddress } = await dispatch(
actions.importNewAccount(strategy, importArgs, loadingMessage),
);
if (selectedAddress) {
trackImportEvent(strategy, true);
dispatch(actions.hideWarning());
onActionComplete(true);
} else {
dispatch(actions.displayWarning(t('importAccountError')));
return false;
}
} catch (error) {
trackImportEvent(strategy, error.message);
translateWarning(error.message);
return false;
}
return true;
}
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))));
}
}
return (
<>
{t('importAccountMsg')}{' '}
{t('here')}
{type === menuItems[0] ? (
) : (
)}
>
);
};
ImportAccount.propTypes = {
onActionComplete: PropTypes.func.isRequired,
};