import React, { useContext, useEffect, useState } from 'react'; import { useSelector, useDispatch } from 'react-redux'; import { useHistory } from 'react-router-dom'; import qrCode from 'qrcode-generator'; import { requestRevealSeedWords, showModal } from '../../store/actions'; import ExportTextContainer from '../../components/ui/export-text-container'; import { getMostRecentOverviewPage } from '../../ducks/history/history'; import { MetaMetricsEventCategory, MetaMetricsEventKeyType, MetaMetricsEventName, } from '../../../shared/constants/metametrics'; import { TextVariant, SEVERITIES, Size, BLOCK_SIZES, JustifyContent, AlignItems, DISPLAY, } from '../../helpers/constants/design-system'; import Box from '../../components/ui/box'; import { Text, Label, BannerAlert, Button, TextField, HelpText, BUTTON_VARIANT, TEXT_FIELD_SIZES, TEXT_FIELD_TYPES, BUTTON_SIZES, } from '../../components/component-library'; import { useI18nContext } from '../../hooks/useI18nContext'; import { MetaMetricsContext } from '../../contexts/metametrics'; import ZENDESK_URLS from '../../helpers/constants/zendesk-url'; import { Tabs, Tab } from '../../components/ui/tabs'; const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN'; const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN'; const RevealSeedPage = () => { const history = useHistory(); const dispatch = useDispatch(); const t = useI18nContext(); const trackEvent = useContext(MetaMetricsContext); const [screen, setScreen] = useState(PASSWORD_PROMPT_SCREEN); const [password, setPassword] = useState(''); const [seedWords, setSeedWords] = useState(null); const [completedLongPress, setCompletedLongPress] = useState(false); const [error, setError] = useState(null); const mostRecentOverviewPage = useSelector(getMostRecentOverviewPage); useEffect(() => { const passwordBox = document.getElementById('password-box'); if (passwordBox) { passwordBox.focus(); } }, []); const renderQR = () => { const qrImage = qrCode(0, 'L'); qrImage.addData(seedWords); qrImage.make(); return qrImage; }; const handleSubmit = (event) => { event.preventDefault(); setSeedWords(null); setCompletedLongPress(false); setError(null); dispatch(requestRevealSeedWords(password)) .then((revealedSeedWords) => { trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.KeyExportRevealed, properties: { key_type: MetaMetricsEventKeyType.Srp, }, }); setSeedWords(revealedSeedWords); dispatch( showModal({ name: 'HOLD_TO_REVEAL_SRP', onLongPressed: () => { setCompletedLongPress(true); setScreen(REVEAL_SEED_SCREEN); }, holdToRevealType: 'SRP', }), ); }) .catch((e) => { trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.KeyExportFailed, properties: { key_type: MetaMetricsEventKeyType.Srp, reason: e.message, // 'incorrect_password', }, }); setError(e.message); }); }; const renderWarning = () => { return ( {t('revealSeedWordsWarning', [ {t('revealSeedWordsWarning2')} , ])} ); }; const renderPasswordPromptContent = () => { return (
handleSubmit(event)}> setPassword(event.target.value)} error={error} width={BLOCK_SIZES.FULL} /> {error && {error}} ); }; const renderRevealSeedContent = () => { // default for SRP_VIEW_SRP_TEXT event because this is the first thing shown after rendering trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.SrpViewSrpText, properties: { key_type: MetaMetricsEventKeyType.Srp, }, }); return (
{ if (tabName === 'text-seed') { trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.SrpViewSrpText, properties: { key_type: MetaMetricsEventKeyType.Srp, }, }); } else if (tabName === 'qr-srp') { trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.SrpViewsSrpQR, properties: { key_type: MetaMetricsEventKeyType.Srp, }, }); } }} > { trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.KeyExportCopied, properties: { key_type: MetaMetricsEventKeyType.Srp, copy_method: 'clipboard', }, }); trackEvent({ category: MetaMetricsEventCategory.Keys, event: MetaMetricsEventName.SrpCopiedToClipboard, properties: { key_type: MetaMetricsEventKeyType.Srp, copy_method: 'clipboard', }, }); }} />
); }; const renderPasswordPromptFooter = () => { return ( ); }; const renderRevealSeedFooter = () => { return ( ); }; const renderContent = () => { return screen === PASSWORD_PROMPT_SCREEN || !completedLongPress ? renderPasswordPromptContent() : renderRevealSeedContent(); }; const renderFooter = () => { return screen === PASSWORD_PROMPT_SCREEN || !completedLongPress ? renderPasswordPromptFooter() : renderRevealSeedFooter(); }; return ( {t('secretRecoveryPhrase')} {t('revealSeedWordsDescription1', [ , {t('revealSeedWordsDescription3')} , ])} {t('revealSeedWordsDescription2', [ , ])} {renderWarning()} {renderContent()} {renderFooter()} ); }; export default RevealSeedPage;