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

chore: selectors confirmations dir #10397

Merged
merged 11 commits into from
Jul 31, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import Text, {
} from '../../../../component-library/components/Texts/Text';
import { useTheme } from '../../../../util/theme';
import { strings } from '../../../../../locales/i18n';
import { selectPermissionControllerState } from '../../../../selectors/snaps/permissionController';

const createStyles = (
_colors: ThemeColors,
Expand Down Expand Up @@ -91,11 +92,7 @@ const SDKSessionModal = ({ route }: SDKSEssionMoodalProps) => {
const { colors, typography } = useTheme();
const styles = createStyles(colors, typography, safeAreaInsets);
const { navigate } = useNavigation();
const permittedAccountsList = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.engine.backgroundState.PermissionController,
);
const permittedAccountsList = useSelector(selectPermissionControllerState);

const [permittedAccountsAddresses, setPermittedAccountsAddresses] = useState<
string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { selectChainId } from '../../../../../selectors/networkController';
import { AddContactViewSelectorsIDs } from '../../../../../../e2e/selectors/Settings/Contacts/AddContactView.selectors';
import { selectInternalAccounts } from '../../../../../selectors/accountsController';
import { toLowerCaseEquals } from '../../../../../util/general';
import { selectAddressBook } from '../../../../../selectors/addressBookController';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -504,7 +505,7 @@ class ContactForm extends PureComponent {
ContactForm.contextType = ThemeContext;

const mapStateToProps = (state) => ({
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
addressBook: selectAddressBook(state),
internalAccounts: selectInternalAccounts(state),
chainId: selectChainId(state),
});
Expand Down
3 changes: 2 additions & 1 deletion app/components/Views/Settings/Contacts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { selectChainId } from '../../../../selectors/networkController';
import Routes from '../../../../../app/constants/navigation/Routes';

import { ContactsViewSelectorIDs } from '../../../../../e2e/selectors/Settings/Contacts/ContacsView.selectors';
import { selectAddressBook } from '../../../../selectors/addressBookController';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -175,7 +176,7 @@ class Contacts extends PureComponent {
Contacts.contextType = ThemeContext;

const mapStateToProps = (state) => ({
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
addressBook: selectAddressBook(state),
chainId: selectChainId(state),
});

Expand Down
7 changes: 2 additions & 5 deletions app/components/Views/Snaps/SnapSettings/SnapSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { strings } from '../../../../../locales/i18n';
import { useStyles } from '../../../hooks/useStyles';
import { useSelector } from 'react-redux';
import SNAP_SETTINGS_REMOVE_BUTTON from './SnapSettings.constants';
import { selectPermissionControllerState } from '../../../../selectors/snaps/permissionController';

interface SnapSettingsProps {
snap: Snap;
Expand All @@ -42,11 +43,7 @@ const SnapSettings = () => {

const { snap } = useParams<SnapSettingsProps>();

const permissionsState = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.engine.backgroundState.PermissionController,
);
const permissionsState = useSelector(selectPermissionControllerState);

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { strings } from '../../../../../locales/i18n';
import { useStyles } from '../../../../component-library/hooks';
import stylesheet from './SnapsSettingsList.styles';
import { Snap } from '@metamask/snaps-utils';
import { selectSnaps } from '../../../../selectors/snaps/snapController';

export const createSnapsSettingsListNavDetails = createNavigationDetails(
Routes.SNAPS.SNAPS_SETTINGS_LIST,
Expand All @@ -21,11 +22,7 @@ const SnapsSettingsList = () => {
const navigation = useNavigation();
const { styles, theme } = useStyles(stylesheet, {});
const { colors } = theme;
const snaps = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.engine.backgroundState.SnapController.snaps,
);
const snaps = useSelector(selectSnaps);

useEffect(() => {
navigation.setOptions(
Expand Down
6 changes: 4 additions & 2 deletions app/components/Views/confirmations/Approval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import { withMetricsAwareness } from '../../../../components/hooks/useMetrics';
import { STX_NO_HASH_ERROR } from '../../../../util/smart-transactions/smart-publish-hook';
import { getSmartTransactionMetricsProperties } from '../../../../util/smart-transactions';
import { selectTransactionMetrics } from '../../../../core/redux/slices/transactionMetrics';
import { selectTransactions } from '../../../../selectors/transactionController';
import { selectShowCustomNonce } from '../../../../selectors/settings';
import { buildTransactionParams } from '../../../../util/confirmation/transactions';

const REVIEW = 'review';
Expand Down Expand Up @@ -653,10 +655,10 @@ class Approval extends PureComponent {

const mapStateToProps = (state) => ({
transaction: getNormalizedTxState(state),
transactions: state.engine.backgroundState.TransactionController.transactions,
transactions: selectTransactions(state),
selectedAddress: selectSelectedInternalAccountChecksummedAddress(state),
networkType: selectProviderType(state),
showCustomNonce: state.settings.showCustomNonce,
showCustomNonce: selectShowCustomNonce(state),
chainId: selectChainId(state),
activeTabUrl: getActiveTabUrl(state),
shouldUseSmartTransaction: selectShouldUseSmartTransaction(state),
Expand Down
14 changes: 10 additions & 4 deletions app/components/Views/confirmations/ApproveView/Approve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ import { selectGasFeeEstimates } from '../../../../../selectors/confirmTransacti
import { selectGasFeeControllerEstimateType } from '../../../../../selectors/gasFeeController';
import { selectShouldUseSmartTransaction } from '../../../../../selectors/smartTransactionsController';
import { STX_NO_HASH_ERROR } from '../../../../../util/smart-transactions/smart-publish-hook';
import { selectTransactions } from '../../../../../selectors/transactionController';
import {
selectPrimaryCurrency,
selectShowCustomNonce,
} from '../../../../../selectors/settings';
import { selectAddressBook } from '../../../../../selectors/addressBookController';
import { buildTransactionParams } from '../../../../../util/confirmation/transactions';

const EDIT = 'edit';
Expand Down Expand Up @@ -902,17 +908,17 @@ const mapStateToProps = (state) => ({
accounts: selectAccounts(state),
ticker: selectTicker(state),
transaction: getNormalizedTxState(state),
transactions: state.engine.backgroundState.TransactionController.transactions,
transactions: selectTransactions(state),
tokensLength: selectTokensLength(state),
accountsLength: selectAccountsLength(state),
primaryCurrency: state.settings.primaryCurrency,
primaryCurrency: selectPrimaryCurrency(state),
chainId: selectChainId(state),
gasFeeEstimates: selectGasFeeEstimates(state),
gasEstimateType: selectGasFeeControllerEstimateType(state),
conversionRate: selectConversionRate(state),
currentCurrency: selectCurrentCurrency(state),
showCustomNonce: state.settings.showCustomNonce,
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
showCustomNonce: selectShowCustomNonce(state),
addressBook: selectAddressBook(state),
providerType: selectProviderType(state),
providerRpcTarget: selectRpcUrl(state),
networkConfigurations: selectNetworkConfigurations(state),
Expand Down
3 changes: 2 additions & 1 deletion app/components/Views/confirmations/Send/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { withMetricsAwareness } from '../../../../components/hooks/useMetrics';
import { selectShouldUseSmartTransaction } from '../../../../selectors/smartTransactionsController';
import { STX_NO_HASH_ERROR } from '../../../../util/smart-transactions/smart-publish-hook';
import { toLowerCaseEquals } from '../../../../util/general';
import { selectAddressBook } from '../../../../selectors/addressBookController';
import TransactionTypes from '../../../../core/TransactionTypes';

const REVIEW = 'review';
Expand Down Expand Up @@ -780,7 +781,7 @@ class Send extends PureComponent {
}

const mapStateToProps = (state) => ({
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
addressBook: selectAddressBook(state),
accounts: selectAccounts(state),
contractBalances: selectContractBalances(state),
transaction: state.transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { selectInternalAccounts } from '../../../../../selectors/accountsControl
import { AddressListProps, Contact } from './AddressList.types';
import styleSheet from './AddressList.styles';
import { toChecksumHexAddress } from '@metamask/controller-utils';
import { selectAddressBook } from '../../../../../selectors/addressBookController';
import { RootState } from '../../../../../reducers';

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -51,16 +53,9 @@ const AddressList: React.FC<AddressListProps> = ({
const [fuse, setFuse] = useState<any>(undefined);
const chainId = useSelector(selectChainId);
const internalAccounts = useSelector(selectInternalAccounts);
const addressBook = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) =>
state.engine.backgroundState.AddressBookController.addressBook,
);
const addressBook = useSelector(selectAddressBook);
const ambiguousAddressEntries = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.user.ambiguousAddressEntries,
(state: RootState) => state.user.ambiguousAddressEntries,
);

const networkAddressBook: { [address: string]: AddressBookEntry } = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import EditGasFeeLegacy from '../../../../components/EditGasFeeLegacyUpdate';
import createStyles from './CustomGasModal.styles';
import { CustomGasModalProps } from './CustomGasModal.types';
import { selectGasFeeEstimates } from '../../../../../../../selectors/confirmTransaction';
import { selectGasFeeControllerEstimateType } from '../../../../../../..//selectors/gasFeeController';
import { RootState } from '../../../../../../../reducers';
import { selectPrimaryCurrency } from '../../../../../../..//selectors/settings';

const CustomGasModal = ({
gasSelected,
Expand All @@ -27,33 +30,15 @@ const CustomGasModal = ({
}: CustomGasModalProps) => {
const { colors } = useAppThemeFromContext();
const styles = createStyles();
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const transaction = useSelector((state: any) => state.transaction);
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const gasFeeEstimate = useSelector((state: any) =>
selectGasFeeEstimates(state),
);
const primaryCurrency = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.settings.primaryCurrency,
);
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chainId = useSelector((state: any) => selectChainId(state));

const transaction = useSelector((state: RootState) => state.transaction);
const gasFeeEstimate = useSelector(selectGasFeeEstimates);
const primaryCurrency = useSelector(selectPrimaryCurrency);
const chainId = useSelector(selectChainId);
const selectedAsset = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.transaction.selectedAsset,
);
const gasEstimateType = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) =>
state.engine.backgroundState.GasFeeController.gasEstimateType,
(state: RootState) => state.transaction.selectedAsset,
);
const gasEstimateType = useSelector(selectGasFeeControllerEstimateType);

const [selectedGas, setSelectedGas] = useState(gasSelected);
const [eip1559Txn, setEIP1559Txn] = useState(EIP1559GasTxn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { includes } from 'lodash';
import { SendViewSelectorsIDs } from '../../../../../../e2e/selectors/SendView.selectors';
import { withMetricsAwareness } from '../../../../../components/hooks/useMetrics';
import { toLowerCaseEquals } from '../../../../../util/general';
import { selectAddressBook } from '../../../../../selectors/addressBookController';

const dummy = () => true;

Expand Down Expand Up @@ -668,7 +669,7 @@ class SendFlow extends PureComponent {
SendFlow.contextType = ThemeContext;

const mapStateToProps = (state) => ({
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
addressBook: selectAddressBook(state),
chainId: selectChainId(state),
selectedAddress: selectSelectedInternalAccountChecksummedAddress(state),
selectedAsset: state.transaction.selectedAsset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { ContractNickNameViewSelectorsIDs } from '../../../../../../../e2e/selec
import { useMetrics } from '../../../../../../components/hooks/useMetrics';
import { selectInternalAccounts } from '../../../../../../selectors/accountsController';
import { RootState } from '../../../../../../reducers';
import { selectAddressBook } from '../../../../../../selectors/addressBookController';

const getAnalyticsParams = () => ({});

Expand Down Expand Up @@ -267,7 +268,7 @@ const mapStateToProps = (state: RootState) => ({
providerType: selectProviderType(state),
providerRpcTarget: selectRpcUrl(state),
providerChainId: selectChainId(state),
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
addressBook: selectAddressBook(state),
internalAccounts: selectInternalAccounts(state),
networkConfigurations: selectNetworkConfigurations(state),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
} from '../../../../../util/gasUtils';
import { useMetrics } from '../../../../../components/hooks/useMetrics';
import { selectGasFeeEstimates } from '../../../../../selectors/confirmTransaction';
import { selectPrimaryCurrency } from '../../../../../selectors/settings';
import { selectGasFeeControllerEstimateType } from '../../../../../selectors/gasFeeController';

const EditGasFeeLegacy = ({
onCancel,
Expand Down Expand Up @@ -77,22 +79,11 @@ const EditGasFeeLegacy = ({
const styles = createStyles(colors);
const gasFeeEstimate = useSelector(selectGasFeeEstimates);

const primaryCurrency = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) => state.settings.primaryCurrency,
);
const primaryCurrency = useSelector(selectPrimaryCurrency);

const gasEstimateType = useSelector(
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(state: any) =>
state.engine.backgroundState.GasFeeController.gasEstimateType,
);
const gasEstimateType = useSelector(selectGasFeeControllerEstimateType);

// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const chainId = useSelector((state: any) => selectChainId(state));
const chainId = useSelector(selectChainId);

const gasTransaction = useGasTransaction({
onlyGas,
Expand Down
5 changes: 2 additions & 3 deletions app/components/hooks/useHideFiatForTestnet/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { renderHook } from '@testing-library/react-hooks';
import { selectChainId } from '../../../selectors/networkController';
import { TEST_NETWORK_IDS } from '../../../constants/network';
import selectShowFiatInTestnets from '../../../selectors/settings';
import { selectShowFiatInTestnets } from '../../../selectors/settings';
import useHideFiatForTestnet from './index';

jest.mock('react-redux', () => ({
Expand All @@ -13,8 +13,7 @@ jest.mock('../../../selectors/networkController', () => ({
}));

jest.mock('../../../selectors/settings', () => ({
__esModule: true,
default: jest.fn(),
selectShowFiatInTestnets: jest.fn(),
}));

describe('useHideFiatForTestnet', () => {
Expand Down
2 changes: 1 addition & 1 deletion app/components/hooks/useHideFiatForTestnet/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useSelector } from 'react-redux';
import type { Hex } from '@metamask/utils';
import { selectChainId } from '../../../selectors/networkController';
import selectShowFiatInTestnets from '../../../selectors/settings';
import { selectShowFiatInTestnets } from '../../../selectors/settings';
import { TEST_NETWORK_IDS } from '../../../constants/network';

/**
Expand Down
33 changes: 33 additions & 0 deletions app/selectors/addressBookController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RootState } from '../reducers';
import { selectAddressBook } from './addressBookController';
import { AddressBookState } from '@metamask/address-book-controller';

describe('selectAddressBook', () => {
it('returns addressBook from state', () => {
const mockAddressBookState: AddressBookState = {
addressBook: {
'0x1': {
'0x123': {
address: '0x123',
name: 'Alice',
chainId: '0x1',
memo: 'Friend',
isEns: false,
},
},
},
};

const mockState = {
engine: {
backgroundState: {
AddressBookController: mockAddressBookState,
},
},
};

expect(selectAddressBook(mockState as RootState)).toEqual(
mockAddressBookState.addressBook,
);
});
});
12 changes: 12 additions & 0 deletions app/selectors/addressBookController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RootState } from '../reducers';
import { createSelector } from 'reselect';
import { AddressBookState } from '@metamask/address-book-controller';

export const selectAddressBookControllerState = (state: RootState) =>
state.engine.backgroundState.AddressBookController;

export const selectAddressBook = createSelector(
selectAddressBookControllerState,
(addressBookControllerState: AddressBookState) =>
addressBookControllerState.addressBook,
);
Loading
Loading