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(suite-native): unmemoized selectors #15562

Merged
merged 1 commit into from
Nov 27, 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
76 changes: 41 additions & 35 deletions suite-native/assets/src/assetsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,22 @@ export const selectVisibleDeviceAccountsKeysByNetworkSymbol = (
return accounts.map(account => account.key);
};

export const selectDeviceNetworksWithAssets = (state: AssetsRootState) => {
const accounts = selectVisibleDeviceAccounts(state);

return pipe(
accounts,
A.map(account => account.symbol),
A.uniq,
A.sort((a, b) => {
const aOrder = orderedNetworkSymbols.indexOf(a) ?? Number.MAX_SAFE_INTEGER;
const bOrder = orderedNetworkSymbols.indexOf(b) ?? Number.MAX_SAFE_INTEGER;

return aOrder - bOrder;
}),
);
};
export const selectDeviceNetworksWithAssets = createMemoizedSelector(
[selectVisibleDeviceAccounts],
accounts => {
return pipe(
accounts,
A.map(account => account.symbol),
A.uniq,
A.sort((a, b) => {
const aOrder = orderedNetworkSymbols.indexOf(a) ?? Number.MAX_SAFE_INTEGER;
const bOrder = orderedNetworkSymbols.indexOf(b) ?? Number.MAX_SAFE_INTEGER;

return aOrder - bOrder;
}),
);
},
);

export const selectBottomSheetDeviceNetworkItems = createMemoizedSelector(
[
Expand Down Expand Up @@ -154,29 +155,34 @@ export const selectAssetCryptoValue = (state: AssetsRootState, symbol: NetworkSy
return asset?.assetBalance ?? '0';
};

export const selectAssetFiatValue = (state: AssetsRootState, symbol: NetworkSymbol) => {
const assets = selectDeviceAssetsWithBalances(state);
const asset = assets.assets.find(a => a.symbol === symbol);

return asset?.fiatBalance ?? null;
};
export const selectAssetFiatValue = createMemoizedSelector(
[selectDeviceAssetsWithBalances, (_state, networkSymbol: NetworkSymbol) => networkSymbol],
(assets, networkSymbol) => {
const asset = assets.assets.find(a => a.symbol === networkSymbol);

const selectAssetsFiatValuePercentage = (state: AssetsRootState) => {
const assets = selectDeviceAssetsWithBalances(state);
const percentages = calculateAssetsPercentage(assets.assets);
return asset?.fiatBalance ?? null;
},
);

return percentages;
};
const selectAssetsFiatValuePercentage = createMemoizedSelector(
[selectDeviceAssetsWithBalances],
assets => {
const percentages = calculateAssetsPercentage(assets.assets);

export const selectAssetFiatValuePercentage = (state: AssetsRootState, symbol: NetworkSymbol) => {
const assetsPercentages = selectAssetsFiatValuePercentage(state);
return percentages;
},
);

const asset = assetsPercentages.find(a => a.symbol === symbol);
export const selectAssetFiatValuePercentage = createMemoizedSelector(
[selectAssetsFiatValuePercentage, (_state, networkSymbol: NetworkSymbol) => networkSymbol],
(assetsPercentages, networkSymbol) => {
const asset = assetsPercentages.find(a => a.symbol === networkSymbol);

const assetPercentage = {
fiatPercentage: Math.ceil(asset?.fiatPercentage ?? 0),
fiatPercentageOffset: Math.floor(asset?.fiatPercentageOffset ?? 0),
};
const assetPercentage = {
fiatPercentage: Math.ceil(asset?.fiatPercentage ?? 0),
fiatPercentageOffset: Math.floor(asset?.fiatPercentageOffset ?? 0),
};

return assetPercentage;
};
return assetPercentage;
},
);
23 changes: 13 additions & 10 deletions suite-native/tokens/src/tokensSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,16 @@ export const selectAccountHasAnyKnownToken = (state: TokensRootState, accountKey
return anyOfTokensIsKnown;
};

export const selectNetworkSymbolsOfAccountsWithTokensAllowed = (state: TokensRootState) =>
selectAccounts(state)
.filter(a => isCoinWithTokens(a.symbol))
.reduce((acc, account) => {
if (!acc.includes(account.symbol)) {
acc.push(account.symbol);
}

return acc;
}, new Array<NetworkSymbol>());
export const selectNetworkSymbolsOfAccountsWithTokensAllowed = createMemoizedSelector(
[selectAccounts],
accounts =>
accounts
.filter(a => isCoinWithTokens(a.symbol))
.reduce((acc, account) => {
if (!acc.includes(account.symbol)) {
acc.push(account.symbol);
}

return acc;
}, new Array<NetworkSymbol>()),
);
Loading