@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
38 lines • 1.9 kB
JavaScript
import { useLeftAccountsModule } from "../hooks/useLeftAccounts";
import { useLeftAccountsApyModule } from "../hooks/useLeftAccountsApy";
import { useRightBalanceNetwork } from "../hooks/useRightBalanceNetwork";
import { compareByBalanceThenFiat } from "../utils/sortByBalance";
export const sortNetworks = (result, leftElement, rightElement) => {
let sorted = result;
if (leftElement === "numberOfAccounts" ||
leftElement === "numberOfAccountsAndApy" ||
leftElement === undefined) {
sorted = [...sorted].sort((a, b) => (b?.count || 0) - (a?.count || 0));
}
if (rightElement === "balance" || rightElement === undefined) {
sorted = [...sorted].sort((a, b) => compareByBalanceThenFiat(a?.balanceData, b?.balanceData));
}
return sorted;
};
export function useNetworkConfiguration(networks, options) {
const { leftElement = "numberOfAccounts", rightElement = "balance" } = options;
const params = { networks };
const emptyParams = { networks: [] };
const rightResults = {
balance: useRightBalanceNetwork(rightElement === "balance" || rightElement === undefined ? params : emptyParams, options),
};
const leftResults = {
numberOfAccounts: useLeftAccountsModule(leftElement === "numberOfAccounts" || leftElement === undefined ? params : emptyParams, options),
numberOfAccountsAndApy: useLeftAccountsApyModule(leftElement === "numberOfAccountsAndApy" ? params : emptyParams, options),
};
const merged = networks.map((network, i) => {
const asset = network.type === "TokenCurrency" ? network.parentCurrency : network;
return {
...asset,
...rightResults[rightElement]?.[i],
...leftResults[leftElement]?.[i],
};
});
return sortNetworks(merged, leftElement, rightElement);
}
//# sourceMappingURL=createNetworkConfiguration.js.map