Skip to content

Commit

Permalink
refactor: Use selectors for preferences controller state access
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptodev-2s authored and Salah-eddineS committed Jul 10, 2023
1 parent 05c02a6 commit e2ff8ac
Show file tree
Hide file tree
Showing 80 changed files with 378 additions and 310 deletions.
14 changes: 7 additions & 7 deletions app/components/Nav/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ import WalletActions from '../../Views/WalletActions';
import NetworkSelector from '../../../components/Views/NetworkSelector';
import EditAccountName from '../../Views/EditAccountName/EditAccountName';
import WC2Manager from '../../../../app/core/WalletConnect/WalletConnectV2';
import {
selectFrequentRpcList,
selectSelectedAddress,
} from '../../../selectors/preferencesController';

const clearStackNavigatorOptions = {
headerShown: false,
Expand Down Expand Up @@ -229,16 +233,12 @@ const App = ({ userLoggedIn }) => {
dispatch(setCurrentBottomNavRoute(route));
}
};
const frequentRpcList = useSelector(
(state) =>
state?.engine?.backgroundState?.PreferencesController?.frequentRpcList,
);
const frequentRpcList = useSelector(selectFrequentRpcList);
const selectedAddress = useSelector(selectSelectedAddress);

useEffect(() => {
if (prevNavigator.current || !navigator) return;
const appTriggeredAuth = async () => {
const { PreferencesController } = Engine.context;
const selectedAddress = PreferencesController.state.selectedAddress;
const existingUser = await AsyncStorage.getItem(EXISTING_USER);
try {
if (existingUser && selectedAddress) {
Expand Down Expand Up @@ -269,7 +269,7 @@ const App = ({ userLoggedIn }) => {
}
};
appTriggeredAuth();
}, [navigator]);
}, [navigator, selectedAddress]);

const handleDeeplink = useCallback(({ error, params, uri }) => {
if (error) {
Expand Down
4 changes: 2 additions & 2 deletions app/components/Nav/Main/RootRPCMethodsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
selectChainId,
selectProviderType,
} from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';
import { createAccountConnectNavDetails } from '../../Views/AccountConnect';

const APPROVAL_TYPES_WITH_DISABLED_CLOSE_ON_APPROVE = [
Expand Down Expand Up @@ -892,8 +893,7 @@ RootRPCMethodsUI.propTypes = {
};

const mapStateToProps = (state) => ({
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
selectedAddress: selectSelectedAddress(state),
chainId: selectChainId(state),
tokens: state.engine.backgroundState.TokensController.tokens,
swapsTransactions:
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/AccountApproval/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
selectChainId,
selectProviderType,
} from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';
import AppConstants from '../../../../app/core/AppConstants';
import { shuffle } from 'lodash';
import SDKConnect from '../../../core/SDKConnect/SDKConnect';
Expand Down Expand Up @@ -461,8 +462,7 @@ const mapStateToProps = (state) => ({
accountsLength: Object.keys(
state.engine.backgroundState.AccountTrackerController.accounts || {},
).length,
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
selectedAddress: selectSelectedAddress(state),
tokensLength: state.engine.backgroundState.TokensController.tokens.length,
networkType: selectProviderType(state),
chainId: selectChainId(state),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
selectNetwork,
selectTicker,
} from '../../../selectors/networkController';
import { selectIdentities } from '../../../selectors/preferencesController';
import { collectConfusables } from '../../../util/confusables';
import { decodeTransferData } from '../../../util/transactions';
import { doENSReverseLookup } from '../../../util/ENSUtils';
Expand Down Expand Up @@ -185,7 +186,7 @@ const mapStateToProps = (state: any) => ({
accounts: state.engine.backgroundState.AccountTrackerController.accounts,
contractBalances:
state.engine.backgroundState.TokenBalancesController.contractBalances,
identities: state.engine.backgroundState.PreferencesController.identities,
identities: selectIdentities(state),
network: selectNetwork(state),
ticker: selectTicker(state),
});
Expand Down
3 changes: 2 additions & 1 deletion app/components/UI/AccountInfoCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
selectConversionRate,
selectCurrentCurrency,
} from '../../../selectors/currencyRateController';
import { selectIdentities } from '../../../selectors/preferencesController';
import ApproveTransactionHeader from '../ApproveTransactionHeader';

const createStyles = (colors) =>
Expand Down Expand Up @@ -239,7 +240,7 @@ class AccountInfoCard extends PureComponent {

const mapStateToProps = (state) => ({
accounts: state.engine.backgroundState.AccountTrackerController.accounts,
identities: state.engine.backgroundState.PreferencesController.identities,
identities: selectIdentities(state),
conversionRate: selectConversionRate(state),
currentCurrency: selectCurrentCurrency(state),
ticker: selectTicker(state),
Expand Down
14 changes: 8 additions & 6 deletions app/components/UI/AccountOverview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ import AppConstants from '../../../core/AppConstants';
import Engine from '../../../core/Engine';
import { selectNetwork } from '../../../selectors/networkController';
import { selectCurrentCurrency } from '../../../selectors/currencyRateController';
import {
selectIdentities,
selectSelectedAddress,
} from '../../../selectors/preferencesController';
import { createAccountSelectorNavDetails } from '../../Views/AccountSelector';

const createStyles = (colors) =>
Expand Down Expand Up @@ -251,11 +255,10 @@ class AccountOverview extends PureComponent {

setAccountLabel = () => {
const { PreferencesController } = Engine.context;
const { selectedAddress } = this.props;
const { selectedAddress, identities } = this.props;
const { accountLabel } = this.state;

const lastAccountLabel =
PreferencesController.state.identities[selectedAddress].name;
const lastAccountLabel = identities[selectedAddress].name;

PreferencesController.setAccountLabel(
selectedAddress,
Expand Down Expand Up @@ -452,9 +455,8 @@ class AccountOverview extends PureComponent {
}

const mapStateToProps = (state) => ({
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
identities: state.engine.backgroundState.PreferencesController.identities,
selectedAddress: selectSelectedAddress(state),
identities: selectIdentities(state),
currentCurrency: selectCurrentCurrency(state),
network: String(selectNetwork(state)),
browserTabs: state.browser.tabs,
Expand Down
6 changes: 2 additions & 4 deletions app/components/UI/AddCustomCollectible/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
NFT_IDENTIFIER_INPUT_BOX_ID,
} from '../../../../wdio/screen-objects/testIDs/Screens/NFTImportScreen.testIds';
import { selectChainId } from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';

const createStyles = (colors: any) =>
StyleSheet.create({
Expand Down Expand Up @@ -81,10 +82,7 @@ const AddCustomCollectible = ({
const { colors, themeAppearance } = useTheme();
const styles = createStyles(colors);

const selectedAddress = useSelector(
(state: any) =>
state.engine.backgroundState.PreferencesController.selectedAddress,
);
const selectedAddress = useSelector(selectSelectedAddress);
const chainId = useSelector(selectChainId);

useEffect(() => {
Expand Down
19 changes: 7 additions & 12 deletions app/components/UI/AddressCopy/AddressCopy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import generateTestId from '../../../../wdio/utils/generateTestId';
// Internal dependencies
import styleSheet from './AddressCopy.styles';
import { AddressCopyProps } from './AddressCopy.types';
import {
selectIdentities,
selectSelectedAddress,
} from '../../../selectors/preferencesController';

const AddressCopy = ({ formatAddressType = 'full' }: AddressCopyProps) => {
const { styles } = useStyles(styleSheet, {});
Expand All @@ -45,23 +49,14 @@ const AddressCopy = ({ formatAddressType = 'full' }: AddressCopyProps) => {
/**
* A string that represents the selected address
*/
const selectedAddress = useSelector(
(state: any) =>
state.engine.backgroundState.PreferencesController.selectedAddress,
);
const selectedAddress = useSelector(selectSelectedAddress);

/**
* An object containing each identity in the format address => account
*/
const identities = useSelector(
(state: any) =>
state.engine.backgroundState.PreferencesController.identities,
);
const identities = useSelector(selectIdentities);

const account = {
address: selectedAddress,
...identities[selectedAddress],
};
const account = identities[selectedAddress];
const copyAccountToClipboard = async () => {
await ClipboardManager.setString(selectedAddress);
handleShowAlert({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { BadgeVariant } from '../../../component-library/components/Badges/Badge
import TagUrl from '../../../component-library/components/Tags/TagUrl';
import { useStyles } from '../../../component-library/hooks';
import { selectProviderConfig } from '../../../selectors/networkController';
import { selectIdentities } from '../../../selectors/preferencesController';
import { renderAccountName, renderShortAddress } from '../../../util/address';
import {
getHost,
Expand Down Expand Up @@ -51,10 +52,7 @@ const ApproveTransactionHeader = ({
state.engine.backgroundState.AccountTrackerController.accounts,
);

const identities = useSelector(
(state: any) =>
state.engine.backgroundState.PreferencesController.identities,
);
const identities = useSelector(selectIdentities);
const activeAddress = toChecksumAddress(from);

const networkProvider = useSelector(selectProviderConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import {
selectProviderType,
selectRpcTarget,
} from '../../../../selectors/networkController';
import {
selectFrequentRpcList,
selectIdentities,
} from '../../../../selectors/preferencesController';

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

Expand Down Expand Up @@ -263,9 +267,8 @@ const mapStateToProps = (state: any) => ({
providerChainId: selectChainId(state),
providerNetwork: selectNetwork(state),
addressBook: state.engine.backgroundState.AddressBookController.addressBook,
identities: state.engine.backgroundState.PreferencesController.identities,
frequentRpcList:
state.engine.backgroundState.PreferencesController.frequentRpcList,
identities: selectIdentities(state),
frequentRpcList: selectFrequentRpcList(state),
});

const mapDispatchToProps = (dispatch: any) => ({
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/ApproveTransactionReview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import {
selectTicker,
selectRpcTarget,
} from '../../../selectors/networkController';
import { selectFrequentRpcList } from '../../../selectors/preferencesController';
import Text, {
TextVariant,
} from '../../../component-library/components/Texts/Text';
Expand Down Expand Up @@ -1175,8 +1176,7 @@ class ApproveTransactionReview extends PureComponent {

const mapStateToProps = (state) => ({
ticker: selectTicker(state),
frequentRpcList:
state.engine.backgroundState.PreferencesController.frequentRpcList,
frequentRpcList: selectFrequentRpcList(state),
transaction: getNormalizedTxState(state),
accountsLength: Object.keys(
state.engine.backgroundState.AccountTrackerController.accounts || {},
Expand Down
6 changes: 2 additions & 4 deletions app/components/UI/AssetOverview/AssetOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
selectChainId,
selectTicker,
} from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';
import Logger from '../../../util/Logger';
import { safeToChecksumAddress } from '../../../util/address';
import {
Expand Down Expand Up @@ -66,10 +67,7 @@ const AssetOverview: React.FC<AssetOverviewProps> = ({
const primaryCurrency = useSelector(
(state: RootStateOrAny) => state.settings.primaryCurrency,
);
const selectedAddress = useSelector(
(state: RootStateOrAny) =>
state.engine.backgroundState.PreferencesController.selectedAddress,
);
const selectedAddress = useSelector(selectSelectedAddress);
const tokenBalances = useSelector(
(state: RootStateOrAny) =>
state.engine.backgroundState.TokenBalancesController.contractBalances,
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/CollectibleContractElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { removeFavoriteCollectible } from '../../../actions/collectibles';
import { collectibleContractsSelector } from '../../../reducers/collectibles';
import { useTheme } from '../../../util/theme';
import { selectChainId } from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';

const DEVICE_WIDTH = Device.getDeviceWidth();
const COLLECTIBLE_WIDTH = (DEVICE_WIDTH - 30 - 16) / 3;
Expand Down Expand Up @@ -296,8 +297,7 @@ CollectibleContractElement.propTypes = {
const mapStateToProps = (state) => ({
collectibleContracts: collectibleContractsSelector(state),
chainId: selectChainId(state),
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
selectedAddress: selectSelectedAddress(state),
});

const mapDispatchToProps = (dispatch) => ({
Expand Down
11 changes: 7 additions & 4 deletions app/components/UI/CollectibleContracts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ import {
selectChainId,
selectProviderType,
} from '../../../selectors/networkController';
import {
selectSelectedAddress,
selectUseNftDetection,
} from '../../../selectors/preferencesController';
import {
IMPORT_NFT_BUTTON_ID,
NFT_TAB_CONTAINER_ID,
} from '../../../../wdio/screen-objects/testIDs/Screens/WalletView.testIds';

const createStyles = (colors) =>
StyleSheet.create({
wrapper: {
Expand Down Expand Up @@ -361,10 +366,8 @@ CollectibleContracts.propTypes = {
const mapStateToProps = (state) => ({
networkType: selectProviderType(state),
chainId: selectChainId(state),
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
useNftDetection:
state.engine.backgroundState.PreferencesController.useNftDetection,
selectedAddress: selectSelectedAddress(state),
useNftDetection: selectUseNftDetection(state),
nftDetectionDismissed: state.user.nftDetectionDismissed,
collectibleContracts: collectibleContractsSelector(state),
collectibles: collectiblesSelector(state),
Expand Down
4 changes: 2 additions & 2 deletions app/components/UI/CollectibleOverview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import AppConstants from '../../../core/AppConstants';
import { useTheme } from '../../../util/theme';
import { selectChainId } from '../../../selectors/networkController';
import { selectSelectedAddress } from '../../../selectors/preferencesController';

const ANIMATION_VELOCITY = 250;
const HAS_NOTCH = Device.hasNotch();
Expand Down Expand Up @@ -521,8 +522,7 @@ CollectibleOverview.propTypes = {

const mapStateToProps = (state, props) => ({
chainId: selectChainId(state),
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
selectedAddress: selectSelectedAddress(state),
isInFavorites: isCollectibleInFavoritesSelector(state, props.collectible),
});

Expand Down
13 changes: 8 additions & 5 deletions app/components/UI/DrawerView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ import {
selectTicker,
} from '../../../selectors/networkController';
import { selectCurrentCurrency } from '../../../selectors/currencyRateController';
import {
selectFrequentRpcList,
selectIdentities,
selectSelectedAddress,
} from '../../../selectors/preferencesController';

import { createAccountSelectorNavDetails } from '../../Views/AccountSelector';
import NetworkInfo from '../NetworkInfo';
Expand Down Expand Up @@ -1239,12 +1244,10 @@ class DrawerView extends PureComponent {

const mapStateToProps = (state) => ({
providerConfig: selectProviderConfig(state),
selectedAddress:
state.engine.backgroundState.PreferencesController.selectedAddress,
selectedAddress: selectSelectedAddress(state),
accounts: state.engine.backgroundState.AccountTrackerController.accounts,
identities: state.engine.backgroundState.PreferencesController.identities,
frequentRpcList:
state.engine.backgroundState.PreferencesController.frequentRpcList,
identities: selectIdentities(state),
frequentRpcList: selectFrequentRpcList(state),
currentCurrency: selectCurrentCurrency(state),
keyrings: state.engine.backgroundState.KeyringController.keyrings,
networkModalVisible: state.modals.networkModalVisible,
Expand Down
6 changes: 2 additions & 4 deletions app/components/UI/FiatOnRampAggregator/Views/OrderDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { useTheme } from '../../../../util/theme';
import Logger from '../../../../util/Logger';
import { selectProviderConfig } from '../../../../selectors/networkController';
import { selectFrequentRpcList } from '../../../../selectors/preferencesController';

interface OrderDetailsParams {
orderId?: string;
Expand All @@ -34,10 +35,7 @@ export const createOrderDetailsNavDetails =
const OrderDetails = () => {
const trackEvent = useAnalytics();
const providerConfig = useSelector(selectProviderConfig);
const frequentRpcList = useSelector(
(state: any) =>
state.engine.backgroundState.PreferencesController.frequentRpcList,
);
const frequentRpcList = useSelector(selectFrequentRpcList);
const params = useParams<OrderDetailsParams>();
const order = useSelector((state) => getOrderById(state, params.orderId));
const { colors } = useTheme();
Expand Down
Loading

0 comments on commit e2ff8ac

Please sign in to comment.