UNPKG

@ledgerhq/live-common

Version:
30 lines 1.1 kB
import { useCallback, useMemo } from "react"; import { flattenAccounts } from "../../../account"; export const useReverseAccounts = ({ accounts, toAccount, fromAccount, fromParentAccount, fromCurrency, setFromAccount, setToAccount, }) => { const isSwapReversable = useMemo(() => { if (!toAccount || !fromCurrency) return false; const allAccounstWithSub = accounts ? flattenAccounts(accounts) : []; const isToSwappable = !!allAccounstWithSub.find(account => account.id === toAccount?.id); return isToSwappable; }, [toAccount, fromCurrency, accounts]); const reverseSwap = useCallback(() => { if (isSwapReversable === false) return; setFromAccount(toAccount); setToAccount(fromCurrency, fromAccount, fromParentAccount); }, [ toAccount, fromCurrency, fromAccount, fromParentAccount, setFromAccount, setToAccount, isSwapReversable, ]); return { isSwapReversable, reverseSwap, }; }; //# sourceMappingURL=useReverseAccounts.js.map