1- import React , { useEffect , useCallback , useContext } from 'react' ;
1+ import React , { useEffect , useCallback , useContext , useRef } from 'react' ;
22import { useDispatch , useSelector } from 'react-redux' ;
33import { useHistory , useLocation } from 'react-router-dom' ;
44import {
55 addHistoryEntry ,
6+ getDraftTransactionExists ,
67 getIsUsingMyAccountForRecipientSearch ,
78 getRecipient ,
89 getRecipientUserInput ,
910 getSendStage ,
1011 resetRecipientInput ,
1112 resetSendState ,
1213 SEND_STAGES ,
14+ startNewDraftTransaction ,
1315 updateRecipient ,
1416 updateRecipientUserInput ,
1517} from '../../ducks/send' ;
@@ -18,6 +20,7 @@ import { getSendHexDataFeatureFlagState } from '../../ducks/metamask/metamask';
1820import { showQrScanner } from '../../store/actions' ;
1921import { MetaMetricsContext } from '../../contexts/metametrics' ;
2022import { EVENT } from '../../../shared/constants/metametrics' ;
23+ import { ASSET_TYPES } from '../../../shared/constants/transaction' ;
2124import SendHeader from './send-header' ;
2225import AddRecipient from './send-content/add-recipient' ;
2326import SendContent from './send-content' ;
@@ -29,6 +32,7 @@ const sendSliceIsCustomPriceExcessive = (state) =>
2932
3033export default function SendTransactionScreen ( ) {
3134 const history = useHistory ( ) ;
35+ const startedNewDraftTransaction = useRef ( false ) ;
3236 const stage = useSelector ( getSendStage ) ;
3337 const gasIsExcessive = useSelector ( sendSliceIsCustomPriceExcessive ) ;
3438 const isUsingMyAccountsForRecipientSearch = useSelector (
@@ -37,6 +41,7 @@ export default function SendTransactionScreen() {
3741 const recipient = useSelector ( getRecipient ) ;
3842 const showHexData = useSelector ( getSendHexDataFeatureFlagState ) ;
3943 const userInput = useSelector ( getRecipientUserInput ) ;
44+ const draftTransactionExists = useSelector ( getDraftTransactionExists ) ;
4045 const location = useLocation ( ) ;
4146 const trackEvent = useContext ( MetaMetricsContext ) ;
4247
@@ -46,6 +51,23 @@ export default function SendTransactionScreen() {
4651 dispatch ( resetSendState ( ) ) ;
4752 } , [ dispatch ] ) ;
4853
54+ /**
55+ * It is possible to route to this page directly, either by typing in the url
56+ * or by clicking the browser back button after progressing to the confirm
57+ * screen. In the case where a draft transaction does not yet exist, this
58+ * hook is responsible for creating it. We will assume that this is a native
59+ * asset send.
60+ */
61+ useEffect ( ( ) => {
62+ if (
63+ draftTransactionExists === false &&
64+ startedNewDraftTransaction . current === false
65+ ) {
66+ startedNewDraftTransaction . current = true ;
67+ dispatch ( startNewDraftTransaction ( { type : ASSET_TYPES . NATIVE } ) ) ;
68+ }
69+ } , [ draftTransactionExists , dispatch ] ) ;
70+
4971 useEffect ( ( ) => {
5072 window . addEventListener ( 'beforeunload' , cleanup ) ;
5173 } , [ cleanup ] ) ;
@@ -70,7 +92,10 @@ export default function SendTransactionScreen() {
7092
7193 let content ;
7294
73- if ( [ SEND_STAGES . EDIT , SEND_STAGES . DRAFT ] . includes ( stage ) ) {
95+ if (
96+ draftTransactionExists &&
97+ [ SEND_STAGES . EDIT , SEND_STAGES . DRAFT ] . includes ( stage )
98+ ) {
7499 content = (
75100 < >
76101 < SendContent
0 commit comments