mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
Improve SignatureRequest
performance (#17052)
* Improve SignatureRequestData performance * Memoize more selectors
This commit is contained in:
parent
b9d9112b97
commit
05d50eee73
@ -1,7 +1,11 @@
|
|||||||
import React from 'react';
|
import React, { memo } from 'react';
|
||||||
import { useSelector } from 'react-redux';
|
import { useSelector } from 'react-redux';
|
||||||
|
import { isEqual } from 'lodash';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { getMetaMaskIdentities, getAccountName } from '../../../../selectors';
|
import {
|
||||||
|
getMemoizedMetaMaskIdentities,
|
||||||
|
getAccountName,
|
||||||
|
} from '../../../../selectors';
|
||||||
import Address from '../../transaction-decoding/components/decoding/address';
|
import Address from '../../transaction-decoding/components/decoding/address';
|
||||||
import {
|
import {
|
||||||
isValidHexAddress,
|
isValidHexAddress,
|
||||||
@ -16,15 +20,15 @@ import {
|
|||||||
TYPOGRAPHY,
|
TYPOGRAPHY,
|
||||||
} from '../../../../helpers/constants/design-system';
|
} from '../../../../helpers/constants/design-system';
|
||||||
|
|
||||||
export default function SignatureRequestData({ data }) {
|
function SignatureRequestData({ data }) {
|
||||||
const identities = useSelector(getMetaMaskIdentities);
|
const identities = useSelector(getMemoizedMetaMaskIdentities);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box className="signature-request-data__node">
|
<Box className="signature-request-data__node">
|
||||||
{Object.entries(data).map(([label, value], i) => (
|
{Object.entries(data).map(([label, value], i) => (
|
||||||
<Box
|
<Box
|
||||||
className="signature-request-data__node"
|
className="signature-request-data__node"
|
||||||
key={i}
|
key={`${label}-${i}`}
|
||||||
paddingLeft={2}
|
paddingLeft={2}
|
||||||
display={
|
display={
|
||||||
typeof value !== 'object' || value === null ? DISPLAY.FLEX : null
|
typeof value !== 'object' || value === null ? DISPLAY.FLEX : null
|
||||||
@ -77,3 +81,7 @@ export default function SignatureRequestData({ data }) {
|
|||||||
SignatureRequestData.propTypes = {
|
SignatureRequestData.propTypes = {
|
||||||
data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
|
data: PropTypes.oneOfType([PropTypes.object, PropTypes.array]).isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export default memo(SignatureRequestData, (prevProps, nextProps) => {
|
||||||
|
return isEqual(prevProps.data, nextProps.data);
|
||||||
|
});
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import React, { PureComponent } from 'react';
|
import React, { PureComponent } from 'react';
|
||||||
|
import { memoize } from 'lodash';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import LedgerInstructionField from '../ledger-instruction-field';
|
import LedgerInstructionField from '../ledger-instruction-field';
|
||||||
import { sanitizeMessage, getURLHostName } from '../../../helpers/utils/util';
|
import { sanitizeMessage, getURLHostName } from '../../../helpers/utils/util';
|
||||||
@ -107,6 +108,12 @@ export default class SignatureRequest extends PureComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
memoizedParseMessage = memoize((data) => {
|
||||||
|
const { message, domain = {}, primaryType, types } = JSON.parse(data);
|
||||||
|
const sanitizedMessage = sanitizeMessage(message, primaryType, types);
|
||||||
|
return { sanitizedMessage, domain };
|
||||||
|
});
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
txData: {
|
txData: {
|
||||||
@ -126,8 +133,8 @@ export default class SignatureRequest extends PureComponent {
|
|||||||
conversionRate,
|
conversionRate,
|
||||||
nativeCurrency,
|
nativeCurrency,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
const { message, domain = {}, primaryType, types } = JSON.parse(data);
|
|
||||||
const { trackEvent } = this.context;
|
const { trackEvent } = this.context;
|
||||||
|
const { sanitizedMessage, domain } = this.memoizedParseMessage(data);
|
||||||
|
|
||||||
const currentNetwork = this.getNetworkName();
|
const currentNetwork = this.getNetworkName();
|
||||||
|
|
||||||
@ -236,7 +243,7 @@ export default class SignatureRequest extends PureComponent {
|
|||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
<Message
|
<Message
|
||||||
data={sanitizeMessage(message, primaryType, types)}
|
data={sanitizedMessage}
|
||||||
onMessageScrolled={() => this.setState({ hasScrolledMessage: true })}
|
onMessageScrolled={() => this.setState({ hasScrolledMessage: true })}
|
||||||
setMessageRootRef={this.setMessageRootRef.bind(this)}
|
setMessageRootRef={this.setMessageRootRef.bind(this)}
|
||||||
messageRootRef={this.messageRootRef}
|
messageRootRef={this.messageRootRef}
|
||||||
|
@ -6,8 +6,8 @@ import { shortenAddress } from '../../../../../../helpers/utils/util';
|
|||||||
import Identicon from '../../../../../ui/identicon';
|
import Identicon from '../../../../../ui/identicon';
|
||||||
import { useI18nContext } from '../../../../../../hooks/useI18nContext';
|
import { useI18nContext } from '../../../../../../hooks/useI18nContext';
|
||||||
import {
|
import {
|
||||||
getMetadataContractName,
|
getMemoizedMetadataContractName,
|
||||||
getAddressBook,
|
getMemoizedAddressBook,
|
||||||
} from '../../../../../../selectors';
|
} from '../../../../../../selectors';
|
||||||
import NicknamePopovers from '../../../../modals/nickname-popovers';
|
import NicknamePopovers from '../../../../modals/nickname-popovers';
|
||||||
|
|
||||||
@ -21,14 +21,14 @@ const Address = ({
|
|||||||
const t = useI18nContext();
|
const t = useI18nContext();
|
||||||
const [showNicknamePopovers, setShowNicknamePopovers] = useState(false);
|
const [showNicknamePopovers, setShowNicknamePopovers] = useState(false);
|
||||||
|
|
||||||
const addressBook = useSelector(getAddressBook);
|
const addressBook = useSelector(getMemoizedAddressBook);
|
||||||
const addressBookEntryObject = addressBook.find(
|
const addressBookEntryObject = addressBook.find(
|
||||||
(entry) =>
|
(entry) =>
|
||||||
entry.address.toLowerCase() === checksummedRecipientAddress.toLowerCase(),
|
entry.address.toLowerCase() === checksummedRecipientAddress.toLowerCase(),
|
||||||
);
|
);
|
||||||
const recipientNickname = addressBookEntryObject?.name;
|
const recipientNickname = addressBookEntryObject?.name;
|
||||||
const recipientMetadataName = useSelector((state) =>
|
const recipientMetadataName = useSelector((state) =>
|
||||||
getMetadataContractName(state, checksummedRecipientAddress),
|
getMemoizedMetadataContractName(state, checksummedRecipientAddress),
|
||||||
);
|
);
|
||||||
|
|
||||||
const recipientToRender = addressOnly
|
const recipientToRender = addressOnly
|
||||||
|
@ -814,6 +814,27 @@ export function getShowWhatsNewPopup(state) {
|
|||||||
|
|
||||||
const createDeepEqualSelector = createSelectorCreator(defaultMemoize, isEqual);
|
const createDeepEqualSelector = createSelectorCreator(defaultMemoize, isEqual);
|
||||||
|
|
||||||
|
export const getMemoizedMetaMaskIdentities = createDeepEqualSelector(
|
||||||
|
getMetaMaskIdentities,
|
||||||
|
(identities) => identities,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const getMemoizedAddressBook = createDeepEqualSelector(
|
||||||
|
getAddressBook,
|
||||||
|
(addressBook) => addressBook,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const getMemoizedMetadataContractName = createDeepEqualSelector(
|
||||||
|
getTokenList,
|
||||||
|
(_tokenList, address) => address,
|
||||||
|
(tokenList, address) => {
|
||||||
|
const entry = Object.values(tokenList).find((identity) =>
|
||||||
|
isEqualCaseInsensitive(identity.address, toChecksumHexAddress(address)),
|
||||||
|
);
|
||||||
|
return entry && entry.name !== '' ? entry.name : '';
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
export const getUnapprovedTransactions = (state) =>
|
export const getUnapprovedTransactions = (state) =>
|
||||||
state.metamask.unapprovedTxs;
|
state.metamask.unapprovedTxs;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user