UNPKG

@ledgerhq/live-common

Version:
43 lines 2.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useToState = void 0; const react_1 = require("react"); const _1 = require("."); const utils_1 = require("../utils"); const useToState = ({ accounts, fromCurrencyAccount, }) => { const { data: toCurrencies } = (0, _1.useFetchCurrencyTo)({ fromCurrencyAccount }); const [toState, setToState] = (0, react_1.useState)(_1.selectorStateDefaultValues); /* UPDATE to accounts */ const setToAccount = (0, react_1.useCallback)((currency, account, parentAccount) => setToState({ ..._1.selectorStateDefaultValues, currency, account, parentAccount, }), []); /* Get the list of possible target accounts given the target currency. */ const getTargetAccountsPairs = (0, react_1.useCallback)(currency => currency && accounts && (0, utils_1.getAccountTuplesForCurrency)(currency, accounts, false), [accounts]); const targetAccounts = (0, react_1.useMemo)(() => getTargetAccountsPairs(toState.currency)?.map(({ account, subAccount }) => subAccount || account), [toState.currency, getTargetAccountsPairs]); const setToCurrency = (0, react_1.useCallback)(currency => { const targetAccountsPairs = getTargetAccountsPairs(currency); const accountPair = targetAccountsPairs && targetAccountsPairs[0]; const account = accountPair && (accountPair.subAccount || accountPair.account); const parentAccount = accountPair && accountPair.subAccount && accountPair.account; setToState({ ..._1.selectorStateDefaultValues, currency, account, parentAccount, }); }, [getTargetAccountsPairs]); const setToAmount = (0, react_1.useCallback)(amount => setToState(previousState => ({ ...previousState, amount: amount })), []); return { toCurrencies: toCurrencies ?? [], toState, setToAccount, setToAmount, setToCurrency, targetAccounts, }; }; exports.useToState = useToState; //# sourceMappingURL=useToState.js.map