Skip to content

Commit

Permalink
Merge branch 'develop' into jl/fix-grant-permitted-chain-on-switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi committed Nov 26, 2024
2 parents 5690670 + c272b25 commit 6adc13c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
getCurrentChainId,
getCurrentNetwork,
getPreferences,
getChainIdsToPoll,
getShouldHideZeroBalanceTokens,
getSelectedAccount,
getAllChainsToPoll,
} from '../../../../../selectors';
import { getNetworkConfigurationsByChainId } from '../../../../../../shared/modules/selectors/networks';
import { useI18nContext } from '../../../../../hooks/useI18nContext';
Expand Down Expand Up @@ -50,7 +50,7 @@ const NetworkFilter = ({ handleClose }: SortControlProps) => {
const shouldHideZeroBalanceTokens = useSelector(
getShouldHideZeroBalanceTokens,
);
const allChainIDs = useSelector(getChainIdsToPoll);
const allChainIDs = useSelector(getAllChainsToPoll);
const { formattedTokensWithBalancesPerChain } = useGetFormattedTokensPerChain(
selectedAccount,
shouldHideZeroBalanceTokens,
Expand Down
36 changes: 36 additions & 0 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,42 @@ export const getAllEnabledNetworks = createDeepEqualSelector(
),
);

/*
* USE THIS WITH CAUTION
*
* Only use this selector if you are absolutely sure that your UI component needs
* data from _all chains_ to compute a value. Else, use `getChainIdsToPoll`.
*
* Examples:
* - Components that should NOT use this selector:
* - Token list: This only needs to poll for chains based on the network filter
* (potentially only one chain). In this case, use `getChainIdsToPoll`.
* - Components that SHOULD use this selector:
* - Aggregated balance: This needs to display data regardless of network filter
* selection (always showing aggregated balances across all chains).
*
* Key Considerations:
* - This selector can cause expensive computations. It should only be used when
* necessary, and where possible, optimized to use `getChainIdsToPoll` instead.
* - Logic Overview:
* - If `PORTFOLIO_VIEW` is not enabled, the selector returns only the `currentChainId`.
* - Otherwise, it includes all chains from `networkConfigurations`, excluding
* `TEST_CHAINS`, while ensuring the `currentChainId` is included.
*/
export const getAllChainsToPoll = createDeepEqualSelector(
getNetworkConfigurationsByChainId,
getCurrentChainId,
(networkConfigurations, currentChainId) => {
if (!process.env.PORTFOLIO_VIEW) {
return [currentChainId];
}

return Object.keys(networkConfigurations).filter(
(chainId) => chainId === currentChainId || !TEST_CHAINS.includes(chainId),
);
},
);

export const getChainIdsToPoll = createDeepEqualSelector(
getNetworkConfigurationsByChainId,
getCurrentChainId,
Expand Down

0 comments on commit 6adc13c

Please sign in to comment.