1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/ui/pages/send/send.js
Dan J Miller c63714c4f2 Show users a warning when they are sending directly to a token contract (#13588)
* Fix warning dialog when sending tokens to a known token contract address

Fixing after rebase

Covering missed cases

Rebased and ran yarn setup

Rebased

Fix checkContractAddress condition

Lint fix

Applied requested changes

Fix unit tests

Applying requested changes

Applied requested changes

Refactor and update

Lint fix

Use V2 of ActionableMessage component

Adding Learn More Link

Updating warning copy

Addressing review feedback

Fix up copy changes

Simplify validation of pasted addresses

Improve detection of whether this is a token contract

Refactor to leave updateRecipient unchanged, and to prevent the double calling of update recipient

Update tests

fix

* Fix unit tests

* Fix e2e tests

* Ensure next button is disabled while recipient type is loading

* Add optional chaining and a fallback to getRecipientWarningAcknowledgement

* Fix lint

* Don't reset recipient warning on asset change, because we should show recipient warnings regardless of asset

* Update unit tests

* Update unit tests

Co-authored-by: Filip Sekulic <filip.sekulic@consensys.net>
2022-07-13 19:46:30 -02:30

128 lines
3.9 KiB
JavaScript

import React, { useEffect, useCallback, useContext } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useLocation } from 'react-router-dom';
import {
addHistoryEntry,
getIsUsingMyAccountForRecipientSearch,
getRecipient,
getRecipientUserInput,
getSendStage,
resetRecipientInput,
resetSendState,
SEND_STAGES,
updateRecipient,
updateRecipientUserInput,
} from '../../ducks/send';
import { isCustomPriceExcessive } from '../../selectors';
import { getSendHexDataFeatureFlagState } from '../../ducks/metamask/metamask';
import { showQrScanner } from '../../store/actions';
import { MetaMetricsContext } from '../../contexts/metametrics';
import { EVENT } from '../../../shared/constants/metametrics';
import SendHeader from './send-header';
import AddRecipient from './send-content/add-recipient';
import SendContent from './send-content';
import SendFooter from './send-footer';
import EnsInput from './send-content/add-recipient/ens-input';
const sendSliceIsCustomPriceExcessive = (state) =>
isCustomPriceExcessive(state, true);
export default function SendTransactionScreen() {
const history = useHistory();
const stage = useSelector(getSendStage);
const gasIsExcessive = useSelector(sendSliceIsCustomPriceExcessive);
const isUsingMyAccountsForRecipientSearch = useSelector(
getIsUsingMyAccountForRecipientSearch,
);
const recipient = useSelector(getRecipient);
const showHexData = useSelector(getSendHexDataFeatureFlagState);
const userInput = useSelector(getRecipientUserInput);
const location = useLocation();
const trackEvent = useContext(MetaMetricsContext);
const dispatch = useDispatch();
const cleanup = useCallback(() => {
dispatch(resetSendState());
}, [dispatch]);
useEffect(() => {
window.addEventListener('beforeunload', cleanup);
}, [cleanup]);
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());
window.removeEventListener('beforeunload', cleanup);
};
}, [dispatch, cleanup]);
let content;
if ([SEND_STAGES.EDIT, SEND_STAGES.DRAFT].includes(stage)) {
content = (
<>
<SendContent
showHexData={showHexData}
gasIsExcessive={gasIsExcessive}
/>
<SendFooter key="send-footer" history={history} />
</>
);
} else {
content = <AddRecipient />;
}
return (
<div className="page-container">
<SendHeader history={history} />
<EnsInput
userInput={userInput}
className="send__to-row"
onChange={(address) => dispatch(updateRecipientUserInput(address))}
onValidAddressTyped={async (address) => {
dispatch(
addHistoryEntry(`sendFlow - Valid address typed ${address}`),
);
await dispatch(updateRecipientUserInput(address));
dispatch(updateRecipient({ address, nickname: '' }));
}}
internalSearch={isUsingMyAccountsForRecipientSearch}
selectedAddress={recipient.address}
selectedName={recipient.nickname}
onPaste={(text) => {
dispatch(
addHistoryEntry(
`sendFlow - User pasted ${text} into address field`,
),
);
}}
onReset={() => dispatch(resetRecipientInput())}
scanQrCode={() => {
trackEvent({
event: 'Used QR scanner',
category: EVENT.CATEGORIES.TRANSACTIONS,
properties: {
action: 'Edit Screen',
legacy_event: true,
},
});
dispatch(showQrScanner());
}}
/>
{content}
</div>
);
}