UNPKG

@ledgerhq/live-common

Version:
43 lines 1.64 kB
import { useCallback, useMemo, useReducer } from "react"; import { getAccountCurrency } from "../../../account/helpers"; function accountReducer(state, action) { switch (action.type) { case "SET_ACCOUNT": return { account: action.account, parentAccount: action.parentAccount, currency: getAccountCurrency(action.account), }; case "CLEAR": return { account: null, parentAccount: null, currency: null }; default: return state; } } function createInitialState(initialAccount, initialParentAccount) { if (!initialAccount) { return { account: null, parentAccount: null, currency: null }; } return { account: initialAccount, parentAccount: initialParentAccount ?? null, currency: getAccountCurrency(initialAccount), }; } export function useSendFlowAccount({ initialAccount, initialParentAccount, } = {}) { const [state, dispatch] = useReducer(accountReducer, undefined, () => createInitialState(initialAccount, initialParentAccount)); const setAccount = useCallback((account, parentAccount) => { dispatch({ type: "SET_ACCOUNT", account, parentAccount: parentAccount ?? null }); }, []); const clearAccount = useCallback(() => { dispatch({ type: "CLEAR" }); }, []); return useMemo(() => ({ state, setAccount, clearAccount, hasAccount: state.account !== null, currencyId: state.currency?.id ?? null, }), [state, setAccount, clearAccount]); } //# sourceMappingURL=useSendFlowAccount.js.map