UNPKG

@ledgerhq/live-common

Version:
85 lines 3.62 kB
import { formatCurrencyUnit } from "@ledgerhq/coin-framework/currencies/formatCurrencyUnit"; import { useCallback, useMemo } from "react"; import { getChangeToReturn } from "../utils/changeToReturn"; import { useBitcoinUtxoDisplayData, } from "../../../../families/bitcoin/react"; import { useSendFlowAmountReviewCore } from "../../hooks/useSendFlowAmountReviewCore"; import { useCoinControlAmountInput } from "./useCoinControlAmountInput"; export function useCoinControlScreenViewModelCore({ account, parentAccount, transaction, status, bridgePending, uiConfig: _uiConfig, transactionActions, locale, accountUnit, amountError, networkFees, labels, onLearnMoreClick, }) { const amountReviewCore = useSendFlowAmountReviewCore({ account, parentAccount, transaction, status, bridgePending, transactionActions, labels: { getCtaLabel: labels.getCtaLabel, reviewCta: labels.reviewCta, }, }); const { updateTransactionWithPatch, amountComputationPending } = amountReviewCore; const amountInput = useCoinControlAmountInput({ transaction, status, onUpdateTransaction: updateTransactionWithPatch, locale, accountUnit, }); const utxoDisplayData = useBitcoinUtxoDisplayData({ account, transaction, status, locale, }); const changeToReturnFormatted = useMemo(() => { const hasAmountForChange = transaction.useAllAmount || transaction.amount?.gt(0); if (!hasAmountForChange) return ""; const changeAmount = getChangeToReturn(status); return formatCurrencyUnit(accountUnit, changeAmount, { showCode: true, disableRounding: true, locale, }); }, [accountUnit, locale, status, transaction.amount, transaction.useAllAmount]); const onSelectStrategy = useCallback((value) => { const strategy = Number.parseInt(value, 10); if (Number.isNaN(strategy)) return; if (!("utxoStrategy" in transaction) || transaction.utxoStrategy == null) return; updateTransactionWithPatch({ utxoStrategy: { ...transaction.utxoStrategy, strategy, excludeUTXOs: [] }, }); }, [transaction, updateTransactionWithPatch]); const strategyOptionsWithLabels = useMemo(() => { const options = utxoDisplayData?.pickingStrategyOptions ?? []; return options.map(opt => ({ value: opt.value, label: labels.getStrategyOptionLabel(opt.labelKey), })); }, [utxoDisplayData?.pickingStrategyOptions, labels]); return { amountValue: amountInput.amountValue, onAmountChange: amountInput.onAmountChange, amountError, utxoDisplayData, strategyOptionsWithLabels, changeToReturnFormatted, onSelectStrategy, reviewLabel: amountReviewCore.reviewLabel, reviewShowIcon: amountReviewCore.reviewShowIcon, reviewDisabled: amountReviewCore.reviewDisabled, reviewLoading: amountComputationPending, strategyLabel: labels.strategyLabel, onLearnMoreClick, learnMoreLabel: labels.learnMoreLabel, coinToSendLabel: labels.coinToSendLabel, changeToReturnLabel: labels.changeToReturnLabel, enterAmountPlaceholder: labels.enterAmountPlaceholder, amountToSendLabel: labels.amountToSendLabel, amountInputLabel: labels.amountInputLabel, networkFees, }; } //# sourceMappingURL=useCoinControlScreenViewModelCore.js.map