Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve performance in large signature request confirmations #26209

Merged
merged 3 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions ui/components/app/confirm/info/row/address.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NameType } from '@metamask/name-controller';
import React, { useState } from 'react';
import React, { memo, useState } from 'react';
import { useSelector } from 'react-redux';
import {
AlignItems,
Expand All @@ -24,55 +24,57 @@ export type ConfirmInfoRowAddressProps = {
isSnapUsingThis?: boolean;
};

export const ConfirmInfoRowAddress = ({
address,
isSnapUsingThis,
}: ConfirmInfoRowAddressProps) => {
const isPetNamesEnabled = useSelector(getPetnamesEnabled);
const { displayName, hexAddress } = useFallbackDisplayName(address);
const [isNicknamePopoverShown, setIsNicknamePopoverShown] = useState(false);
const handleDisplayNameClick = () => setIsNicknamePopoverShown(true);
const onCloseHandler = () => setIsNicknamePopoverShown(false);
export const ConfirmInfoRowAddress = memo(
({ address, isSnapUsingThis }: ConfirmInfoRowAddressProps) => {
const isPetNamesEnabled = useSelector(getPetnamesEnabled);
const { displayName, hexAddress } = useFallbackDisplayName(address);
const [isNicknamePopoverShown, setIsNicknamePopoverShown] = useState(false);
const handleDisplayNameClick = () => setIsNicknamePopoverShown(true);
const onCloseHandler = () => setIsNicknamePopoverShown(false);

return (
<Box
display={Display.Flex}
flexDirection={FlexDirection.Row}
alignItems={AlignItems.center}
>
{
// PetNames on this component are disabled for snaps until the `<Name />`
// component can support variations. See this comment for context: //
// https://github.com/MetaMask/metamask-extension/pull/23487#discussion_r1525055546
isPetNamesEnabled && !isSnapUsingThis ? (
<Name value={hexAddress} type={NameType.ETHEREUM_ADDRESS} />
) : (
<>
<Box
display={Display.Flex}
flexDirection={FlexDirection.Row}
alignItems={AlignItems.center}
onClick={handleDisplayNameClick}
>
<AvatarAccount
address={address}
size={AvatarAccountSize.Xs}
borderColor={BorderColor.transparent}
/>
<Text
marginLeft={2}
color={TextColor.inherit}
data-testid="confirm-info-row-display-name"
return (
<Box
display={Display.Flex}
flexDirection={FlexDirection.Row}
alignItems={AlignItems.center}
>
{
// PetNames on this component are disabled for snaps until the `<Name />`
// component can support variations. See this comment for context: //
// https://github.com/MetaMask/metamask-extension/pull/23487#discussion_r1525055546
isPetNamesEnabled && !isSnapUsingThis ? (
<Name value={hexAddress} type={NameType.ETHEREUM_ADDRESS} />
) : (
<>
<Box
display={Display.Flex}
flexDirection={FlexDirection.Row}
alignItems={AlignItems.center}
onClick={handleDisplayNameClick}
>
{displayName}
</Text>
</Box>
{isNicknamePopoverShown ? (
<NicknamePopovers onClose={onCloseHandler} address={hexAddress} />
) : null}
</>
)
}
</Box>
);
};
<AvatarAccount
address={address}
size={AvatarAccountSize.Xs}
borderColor={BorderColor.transparent}
/>
<Text
marginLeft={2}
color={TextColor.inherit}
data-testid="confirm-info-row-display-name"
>
{displayName}
</Text>
</Box>
{isNicknamePopoverShown ? (
<NicknamePopovers
onClose={onCloseHandler}
address={hexAddress}
/>
) : null}
</>
)
}
</Box>
);
},
);
160 changes: 85 additions & 75 deletions ui/components/app/name/name.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useCallback, useContext, useEffect, useState } from 'react';
import React, {
memo,
useCallback,
useContext,
useEffect,
useState,
} from 'react';
import { NameType } from '@metamask/name-controller';
import classnames from 'classnames';
import { toChecksumAddress } from 'ethereumjs-util';
Expand Down Expand Up @@ -44,81 +50,85 @@ function formatValue(value: string, type: NameType): string {
}
}

export default function Name({
value,
type,
disableEdit,
internal,
preferContractSymbol = false,
}: NameProps) {
const [modalOpen, setModalOpen] = useState(false);
const trackEvent = useContext(MetaMetricsContext);

const { name, hasPetname } = useDisplayName(
const Name = memo(
({
value,
type,
preferContractSymbol,
);

useEffect(() => {
if (internal) {
return;
}

trackEvent({
event: MetaMetricsEventName.PetnameDisplayed,
category: MetaMetricsEventCategory.Petnames,
properties: {
petname_category: type,
has_petname: Boolean(name?.length),
},
});
}, []);

const handleClick = useCallback(() => {
setModalOpen(true);
}, [setModalOpen]);

const handleModalClose = useCallback(() => {
setModalOpen(false);
}, [setModalOpen]);

const formattedValue = formatValue(value, type);
const hasDisplayName = Boolean(name);

return (
<div>
{!disableEdit && modalOpen && (
<NameDetails value={value} type={type} onClose={handleModalClose} />
)}
<div
className={classnames({
name: true,
name__saved: hasPetname,
name__recognized_unsaved: !hasPetname && hasDisplayName,
name__missing: !hasDisplayName,
})}
onClick={handleClick}
>
{hasDisplayName ? (
<Identicon address={value} diameter={16} />
) : (
<Icon
name={IconName.Question}
className="name__icon"
size={IconSize.Md}
/>
)}
{hasDisplayName ? (
<Text className="name__name" variant={TextVariant.bodyMd}>
{name}
</Text>
) : (
<Text className="name__value" variant={TextVariant.bodyMd}>
{formattedValue}
</Text>
disableEdit,
internal,
preferContractSymbol = false,
}: NameProps) => {
const [modalOpen, setModalOpen] = useState(false);
const trackEvent = useContext(MetaMetricsContext);

const { name, hasPetname } = useDisplayName(
value,
type,
preferContractSymbol,
);

useEffect(() => {
if (internal) {
return;
}

trackEvent({
event: MetaMetricsEventName.PetnameDisplayed,
category: MetaMetricsEventCategory.Petnames,
properties: {
petname_category: type,
has_petname: Boolean(name?.length),
},
});
}, []);

const handleClick = useCallback(() => {
setModalOpen(true);
}, [setModalOpen]);

const handleModalClose = useCallback(() => {
setModalOpen(false);
}, [setModalOpen]);

const formattedValue = formatValue(value, type);
const hasDisplayName = Boolean(name);

return (
<div>
{!disableEdit && modalOpen && (
<NameDetails value={value} type={type} onClose={handleModalClose} />
)}
<div
className={classnames({
name: true,
name__saved: hasPetname,
name__recognized_unsaved: !hasPetname && hasDisplayName,
name__missing: !hasDisplayName,
})}
onClick={handleClick}
>
{hasDisplayName ? (
<Identicon address={value} diameter={16} />
) : (
<Icon
name={IconName.Question}
className="name__icon"
size={IconSize.Md}
/>
)}
{hasDisplayName ? (
<Text className="name__name" variant={TextVariant.bodyMd}>
{name}
</Text>
) : (
<Text className="name__value" variant={TextVariant.bodyMd}>
{formattedValue}
</Text>
)}
</div>
</div>
</div>
);
}
);
},
);

export default Name;
16 changes: 7 additions & 9 deletions ui/hooks/useDisplayName.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NameEntry, NameType } from '@metamask/name-controller';
import { NftContract } from '@metamask/assets-controllers';
import { getMemoizedMetadataContracts } from '../selectors';
import { getRemoteTokens } from '../selectors';
import { getNftContractsByAddressOnCurrentChain } from '../selectors/nft';
import { useDisplayName } from './useDisplayName';
import { useNames } from './useName';
Expand All @@ -21,7 +21,7 @@ jest.mock('./useFirstPartyContractName', () => ({
}));

jest.mock('../selectors', () => ({
getMemoizedMetadataContracts: jest.fn(),
getRemoteTokens: jest.fn(),
getCurrentChainId: jest.fn(),
}));

Expand Down Expand Up @@ -55,9 +55,7 @@ const WATCHED_NFT_FOUND_RETURN_VALUE = {

describe('useDisplayName', () => {
const useNamesMock = jest.mocked(useNames);
const getMemoizedMetadataContractsMock = jest.mocked(
getMemoizedMetadataContracts,
);
const getRemoteTokensMock = jest.mocked(getRemoteTokens);
const useFirstPartyContractNamesMock = jest.mocked(
useFirstPartyContractNames,
);
Expand All @@ -72,7 +70,7 @@ describe('useDisplayName', () => {
useFirstPartyContractNamesMock.mockReturnValue([
NO_FIRST_PARTY_CONTRACT_NAME_FOUND_RETURN_VALUE,
]);
getMemoizedMetadataContractsMock.mockReturnValue([
getRemoteTokensMock.mockReturnValue([
{
name: NO_CONTRACT_NAME_FOUND_RETURN_VALUE,
},
Expand All @@ -94,7 +92,7 @@ describe('useDisplayName', () => {
useFirstPartyContractNamesMock.mockReturnValue([
FIRST_PARTY_CONTRACT_NAME_MOCK,
]);
getMemoizedMetadataContractsMock.mockReturnValue([
getRemoteTokensMock.mockReturnValue([
{
name: CONTRACT_NAME_MOCK,
},
Expand All @@ -114,7 +112,7 @@ describe('useDisplayName', () => {
useFirstPartyContractNamesMock.mockReturnValue([
FIRST_PARTY_CONTRACT_NAME_MOCK,
]);
getMemoizedMetadataContractsMock.mockReturnValue({
getRemoteTokensMock.mockReturnValue({
name: CONTRACT_NAME_MOCK,
});
getNftContractsByAddressOnCurrentChainMock.mockReturnValue(
Expand All @@ -128,7 +126,7 @@ describe('useDisplayName', () => {
});

it('prioritizes a contract name over a watched NFT name', () => {
getMemoizedMetadataContractsMock.mockReturnValue([
getRemoteTokensMock.mockReturnValue([
{
name: CONTRACT_NAME_MOCK,
},
Expand Down
4 changes: 2 additions & 2 deletions ui/hooks/useDisplayName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NameType } from '@metamask/name-controller';
import { useSelector } from 'react-redux';
import { getMemoizedMetadataContracts } from '../selectors';
import { getRemoteTokens } from '../selectors';
import { getNftContractsByAddressOnCurrentChain } from '../selectors/nft';
import { useNames } from './useName';
import { useFirstPartyContractNames } from './useFirstPartyContractName';
Expand Down Expand Up @@ -32,7 +32,7 @@ export function useDisplayNames(
const contractInfo = useSelector((state) =>
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(getMemoizedMetadataContracts as any)(state, values, true),
(getRemoteTokens as any)(state, values),
);

const watchedNftNames = useSelector(getNftContractsByAddressOnCurrentChain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { shortenAddress } from '../../../../../../../helpers/utils/util';
import Identicon from '../../../../../../../components/ui/identicon';
import { useI18nContext } from '../../../../../../../hooks/useI18nContext';
import {
getMemoizedMetadataContractName,
getMemoizedAddressBook,
getMetadataContractName,
} from '../../../../../../../selectors';
import NicknamePopovers from '../../../../../../../components/app/modals/nickname-popovers';
import { COPY_OPTIONS } from '../../../../../../../../shared/constants/copy';
Expand All @@ -29,7 +29,7 @@ const Address = ({
);
const recipientNickname = addressBookEntryObject?.name;
const recipientMetadataName = useSelector((state) =>
getMemoizedMetadataContractName(state, checksummedRecipientAddress),
getMetadataContractName(state, checksummedRecipientAddress),
);

const recipientToRender = addressOnly
Expand Down
Loading