2022-07-18 19:01:10 +02:00
|
|
|
import React, { useEffect, useCallback, useContext, useRef } from 'react';
|
2021-06-23 23:35:25 +02:00
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { useHistory, useLocation } from 'react-router-dom';
|
|
|
|
import {
|
2022-05-04 18:54:46 +02:00
|
|
|
addHistoryEntry,
|
2022-07-18 19:01:10 +02:00
|
|
|
getDraftTransactionExists,
|
2021-06-23 23:35:25 +02:00
|
|
|
getIsUsingMyAccountForRecipientSearch,
|
|
|
|
getRecipient,
|
|
|
|
getRecipientUserInput,
|
|
|
|
getSendStage,
|
|
|
|
resetRecipientInput,
|
|
|
|
resetSendState,
|
|
|
|
SEND_STAGES,
|
2022-07-18 19:01:10 +02:00
|
|
|
startNewDraftTransaction,
|
2021-06-23 23:35:25 +02:00
|
|
|
updateRecipient,
|
|
|
|
updateRecipientUserInput,
|
|
|
|
} from '../../ducks/send';
|
2022-07-01 15:58:35 +02:00
|
|
|
import { isCustomPriceExcessive } from '../../selectors';
|
2021-06-23 23:35:25 +02:00
|
|
|
import { getSendHexDataFeatureFlagState } from '../../ducks/metamask/metamask';
|
|
|
|
import { showQrScanner } from '../../store/actions';
|
2022-04-01 21:11:12 +02:00
|
|
|
import { MetaMetricsContext } from '../../contexts/metametrics';
|
2023-04-03 17:31:04 +02:00
|
|
|
import { MetaMetricsEventCategory } from '../../../shared/constants/metametrics';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { AssetType } from '../../../shared/constants/transaction';
|
2021-06-23 23:35:25 +02:00
|
|
|
import SendHeader from './send-header';
|
|
|
|
import AddRecipient from './send-content/add-recipient';
|
|
|
|
import SendContent from './send-content';
|
|
|
|
import SendFooter from './send-footer';
|
2022-11-21 16:19:11 +01:00
|
|
|
import DomainInput from './send-content/add-recipient/domain-input';
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
const sendSliceIsCustomPriceExcessive = (state) =>
|
|
|
|
isCustomPriceExcessive(state, true);
|
|
|
|
|
|
|
|
export default function SendTransactionScreen() {
|
|
|
|
const history = useHistory();
|
2022-07-18 19:01:10 +02:00
|
|
|
const startedNewDraftTransaction = useRef(false);
|
2021-06-23 23:35:25 +02:00
|
|
|
const stage = useSelector(getSendStage);
|
|
|
|
const gasIsExcessive = useSelector(sendSliceIsCustomPriceExcessive);
|
|
|
|
const isUsingMyAccountsForRecipientSearch = useSelector(
|
|
|
|
getIsUsingMyAccountForRecipientSearch,
|
|
|
|
);
|
|
|
|
const recipient = useSelector(getRecipient);
|
|
|
|
const showHexData = useSelector(getSendHexDataFeatureFlagState);
|
|
|
|
const userInput = useSelector(getRecipientUserInput);
|
2022-07-18 19:01:10 +02:00
|
|
|
const draftTransactionExists = useSelector(getDraftTransactionExists);
|
2021-06-23 23:35:25 +02:00
|
|
|
const location = useLocation();
|
2022-03-14 19:12:38 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
const dispatch = useDispatch();
|
2021-08-04 23:53:13 +02:00
|
|
|
|
|
|
|
const cleanup = useCallback(() => {
|
|
|
|
dispatch(resetSendState());
|
|
|
|
}, [dispatch]);
|
|
|
|
|
2022-07-18 19:01:10 +02:00
|
|
|
/**
|
|
|
|
* It is possible to route to this page directly, either by typing in the url
|
|
|
|
* or by clicking the browser back button after progressing to the confirm
|
|
|
|
* screen. In the case where a draft transaction does not yet exist, this
|
|
|
|
* hook is responsible for creating it. We will assume that this is a native
|
|
|
|
* asset send.
|
|
|
|
*/
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
draftTransactionExists === false &&
|
|
|
|
startedNewDraftTransaction.current === false
|
|
|
|
) {
|
|
|
|
startedNewDraftTransaction.current = true;
|
2023-01-18 15:47:29 +01:00
|
|
|
dispatch(startNewDraftTransaction({ type: AssetType.native }));
|
2022-07-18 19:01:10 +02:00
|
|
|
}
|
|
|
|
}, [draftTransactionExists, dispatch]);
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
useEffect(() => {
|
2022-07-01 15:58:35 +02:00
|
|
|
window.addEventListener('beforeunload', cleanup);
|
|
|
|
}, [cleanup]);
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (location.search === '?scan=true') {
|
|
|
|
dispatch(showQrScanner());
|
|
|
|
|
|
|
|
// Clear the queryString param after showing the modal
|
|
|
|
const cleanUrl = window.location.href.split('?')[0];
|
|
|
|
window.history.pushState({}, null, `${cleanUrl}`);
|
|
|
|
window.location.hash = '#send';
|
|
|
|
}
|
|
|
|
}, [location, dispatch]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
dispatch(resetSendState());
|
2021-08-04 23:53:13 +02:00
|
|
|
window.removeEventListener('beforeunload', cleanup);
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
2021-08-04 23:53:13 +02:00
|
|
|
}, [dispatch, cleanup]);
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
let content;
|
|
|
|
|
2022-07-18 19:01:10 +02:00
|
|
|
if (
|
|
|
|
draftTransactionExists &&
|
|
|
|
[SEND_STAGES.EDIT, SEND_STAGES.DRAFT].includes(stage)
|
|
|
|
) {
|
2021-06-23 23:35:25 +02:00
|
|
|
content = (
|
|
|
|
<>
|
|
|
|
<SendContent
|
|
|
|
showHexData={showHexData}
|
|
|
|
gasIsExcessive={gasIsExcessive}
|
|
|
|
/>
|
|
|
|
<SendFooter key="send-footer" history={history} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
content = <AddRecipient />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="page-container">
|
|
|
|
<SendHeader history={history} />
|
2022-11-21 16:19:11 +01:00
|
|
|
<DomainInput
|
2021-06-23 23:35:25 +02:00
|
|
|
userInput={userInput}
|
|
|
|
className="send__to-row"
|
|
|
|
onChange={(address) => dispatch(updateRecipientUserInput(address))}
|
2022-07-14 00:15:38 +02:00
|
|
|
onValidAddressTyped={async (address) => {
|
2022-05-04 18:54:46 +02:00
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(`sendFlow - Valid address typed ${address}`),
|
|
|
|
);
|
2022-07-14 00:15:38 +02:00
|
|
|
await dispatch(updateRecipientUserInput(address));
|
2022-05-04 18:54:46 +02:00
|
|
|
dispatch(updateRecipient({ address, nickname: '' }));
|
|
|
|
}}
|
2021-06-23 23:35:25 +02:00
|
|
|
internalSearch={isUsingMyAccountsForRecipientSearch}
|
|
|
|
selectedAddress={recipient.address}
|
|
|
|
selectedName={recipient.nickname}
|
2022-05-04 18:54:46 +02:00
|
|
|
onPaste={(text) => {
|
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - User pasted ${text} into address field`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}}
|
2021-06-23 23:35:25 +02:00
|
|
|
onReset={() => dispatch(resetRecipientInput())}
|
|
|
|
scanQrCode={() => {
|
2022-03-14 19:12:38 +01:00
|
|
|
trackEvent({
|
|
|
|
event: 'Used QR scanner',
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Transactions,
|
2022-03-14 19:12:38 +01:00
|
|
|
properties: {
|
|
|
|
action: 'Edit Screen',
|
|
|
|
legacy_event: true,
|
|
|
|
},
|
|
|
|
});
|
2021-06-23 23:35:25 +02:00
|
|
|
dispatch(showQrScanner());
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{content}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|