@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
20 lines • 1.04 kB
JavaScript
import { useEffect } from "react";
import { useCurrenciesByMarketcap } from "../../../currencies/hooks";
import { listCryptoCurrencies, listTokens } from "../../../currencies";
import { getAvailableAccountsById } from "../utils";
// Pick a default source account if none are selected.
export const usePickDefaultAccount = (accounts, fromAccount, setFromAccount) => {
const list = [...listCryptoCurrencies(), ...listTokens()];
const allCurrencies = useCurrenciesByMarketcap(list);
useEffect(() => {
if (!fromAccount && allCurrencies.length > 0) {
const defaultAccount = allCurrencies
.map(({ id }) => getAvailableAccountsById(id, accounts).filter(account => account.balance.gt(0)))
.flat(1)
.find(Boolean);
if (defaultAccount && defaultAccount?.id !== fromAccount?.["id"])
setFromAccount(defaultAccount);
}
}, [accounts, allCurrencies, fromAccount, setFromAccount]);
};
//# sourceMappingURL=usePickDefaultAccount.js.map