@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
46 lines • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSendFlowAccount = useSendFlowAccount;
const react_1 = require("react");
const helpers_1 = require("../../../account/helpers");
function accountReducer(state, action) {
switch (action.type) {
case "SET_ACCOUNT":
return {
account: action.account,
parentAccount: action.parentAccount,
currency: (0, helpers_1.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: (0, helpers_1.getAccountCurrency)(initialAccount),
};
}
function useSendFlowAccount({ initialAccount, initialParentAccount, } = {}) {
const [state, dispatch] = (0, react_1.useReducer)(accountReducer, undefined, () => createInitialState(initialAccount, initialParentAccount));
const setAccount = (0, react_1.useCallback)((account, parentAccount) => {
dispatch({ type: "SET_ACCOUNT", account, parentAccount: parentAccount ?? null });
}, []);
const clearAccount = (0, react_1.useCallback)(() => {
dispatch({ type: "CLEAR" });
}, []);
return (0, react_1.useMemo)(() => ({
state,
setAccount,
clearAccount,
hasAccount: state.account !== null,
currencyId: state.currency?.id ?? null,
}), [state, setAccount, clearAccount]);
}
//# sourceMappingURL=useSendFlowAccount.js.map