Skip to content

chore: Improve account list performance #14910

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
83 changes: 47 additions & 36 deletions app/components/UI/AccountSelectorList/AccountSelectorList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
import React, { useCallback, useRef } from 'react';
import { Alert, ListRenderItem, View, ViewStyle } from 'react-native';
import { FlatList } from 'react-native-gesture-handler';
import { useSelector } from 'react-redux';
import { shallowEqual, useSelector } from 'react-redux';
import { useNavigation } from '@react-navigation/native';
import { KeyringTypes } from '@metamask/keyring-controller';

// External dependencies.
import { selectInternalAccounts } from '../../../selectors/accountsController';
import Cell, {
CellVariant,
} from '../../../component-library/components/Cells/Cell';
import { InternalAccount } from '@metamask/keyring-internal-api';
import { useStyles } from '../../../component-library/hooks';
import { TextColor } from '../../../component-library/components/Texts/Text';
import SensitiveText, {
Expand All @@ -37,6 +35,7 @@ import { AccountSelectorListProps } from './AccountSelectorList.types';
import styleSheet from './AccountSelectorList.styles';
import { AccountListBottomSheetSelectorsIDs } from '../../../../e2e/selectors/wallet/AccountListBottomSheet.selectors';
import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors';
import { RootState } from '../../../reducers';

const AccountSelectorList = ({
onSelectAccount,
Expand All @@ -60,15 +59,14 @@ const AccountSelectorList = ({
const accountListRef = useRef<any>(null);
const accountsLengthRef = useRef<number>(0);
const { styles } = useStyles(styleSheet, {});
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const accountAvatarType = useSelector((state: any) =>
state.settings.useBlockieIcon
? AvatarAccountType.Blockies
: AvatarAccountType.JazzIcon,
);

const internalAccounts = useSelector(selectInternalAccounts);
const accountAvatarType = useSelector(
(state: RootState) =>
state.settings.useBlockieIcon
? AvatarAccountType.Blockies
: AvatarAccountType.JazzIcon,
shallowEqual,
);
const getKeyExtractor = ({ address }: Account) => address;

const renderAccountBalances = useCallback(
Expand Down Expand Up @@ -175,10 +173,9 @@ const AccountSelectorList = ({
);

const onNavigateToAccountActions = useCallback(
(selectedAccount: string) => {
const account = internalAccounts.find(
(accountData: InternalAccount) =>
accountData.address.toLowerCase() === selectedAccount.toLowerCase(),
(selectedAccountAddress: string) => {
const account = Engine.context.AccountsController.getAccountByAddress(
selectedAccountAddress,
);

if (!account) return;
Expand All @@ -188,7 +185,7 @@ const AccountSelectorList = ({
params: { selectedAccount: account },
});
},
[navigate, internalAccounts],
[navigate],
);

const renderAccountItem: ListRenderItem<Account> = useCallback(
Expand Down Expand Up @@ -226,37 +223,51 @@ const AccountSelectorList = ({
cellStyle.alignItems = 'center';
}

const handleLongPress = () => {
onLongPress({
address,
isAccountRemoveable:
type === KeyringTypes.simple || type === KeyringTypes.snap,
isSelected: isSelectedAccount,
index,
});
};

const handlePress = () => {
onSelectAccount?.(address, isSelectedAccount);
};

const handleButtonClick = () => {
onNavigateToAccountActions(address);
};

const buttonProps = {
onButtonClick: handleButtonClick,
buttonTestId: `${WalletViewSelectorsIDs.ACCOUNT_ACTIONS}-${index}`,
};

const avatarProps = {
variant: AvatarVariant.Account as const,
type: accountAvatarType,
accountAddress: address,
};

return (
<Cell
key={address}
onLongPress={() => {
onLongPress({
address,
isAccountRemoveable:
type === KeyringTypes.simple || type === KeyringTypes.snap,
isSelected: isSelectedAccount,
index,
});
}}
onLongPress={handleLongPress}
variant={cellVariant}
isSelected={isSelectedAccount}
title={accountName}
secondaryText={shortAddress}
showSecondaryTextIcon={false}
tertiaryText={balanceError}
onPress={() => onSelectAccount?.(address, isSelectedAccount)}
avatarProps={{
variant: AvatarVariant.Account,
type: accountAvatarType,
accountAddress: address,
}}
onPress={handlePress}
avatarProps={avatarProps}
tagLabel={tagLabel}
disabled={isDisabled}
style={cellStyle}
buttonProps={{
onButtonClick: () => onNavigateToAccountActions(address),
buttonTestId: `${WalletViewSelectorsIDs.ACCOUNT_ACTIONS}-${index}`,
}}
buttonProps={buttonProps}
>
{renderRightAccessory?.(address, accountName) ||
(assets && renderAccountBalances(assets, address))}
Expand Down Expand Up @@ -312,4 +323,4 @@ const AccountSelectorList = ({
);
};

export default AccountSelectorList;
export default React.memo(AccountSelectorList);
Loading