Skip to content

Commit

Permalink
open channel: use AmountInput to display fundMax
Browse files Browse the repository at this point in the history
  • Loading branch information
myxmaster committed Jan 23, 2025
1 parent 0abecd8 commit da42480
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 95 deletions.
53 changes: 35 additions & 18 deletions components/AmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface AmountInputProps {
title?: string;
hideConversion?: boolean;
hideUnitChangeButton?: boolean;
forceUnit?: 'sats' | 'BTC' | 'fiat';
FiatStore?: FiatStore;
SettingsStore?: SettingsStore;
UnitsStore?: UnitsStore;
Expand All @@ -33,12 +34,13 @@ interface AmountInputState {
satAmount: string | number;
}

const getSatAmount = (amount: string | number) => {
const getSatAmount = (amount: string | number, forceUnit?: string) => {
const { fiatStore, settingsStore, unitsStore } = Stores;
const { fiatRates } = fiatStore;
const { settings } = settingsStore;
const { fiat } = settings;
const { units } = unitsStore;
const effectiveUnits = forceUnit || units;

// replace , with . for unit separator
const value = amount ? amount.toString().replace(/,/g, ',') : '';
Expand All @@ -51,7 +53,7 @@ const getSatAmount = (amount: string | number) => {
const rate = fiat && fiatRates && fiatEntry ? fiatEntry.rate : 0;

let satAmount: string | number = 0;
switch (units) {
switch (effectiveUnits) {
case 'sats':
satAmount = value;
break;
Expand Down Expand Up @@ -89,7 +91,8 @@ export default class AmountInput extends React.Component<

const { amount, onAmountChange } = props;
let satAmount = '0';
if (amount) satAmount = getSatAmount(amount).toString();
if (amount)
satAmount = getSatAmount(amount, props.forceUnit).toString();

onAmountChange(amount, satAmount);
this.state = {
Expand All @@ -99,25 +102,35 @@ export default class AmountInput extends React.Component<

componentDidMount() {
const { amount, onAmountChange }: any = this.props;
const satAmount = getSatAmount(amount);
const satAmount = getSatAmount(amount, this.props.forceUnit);
onAmountChange(amount, satAmount);
this.setState({ satAmount });
}

UNSAFE_componentWillReceiveProps(
nextProps: Readonly<AmountInputProps>
): void {
const { amount } = nextProps;
if (amount) {
const satAmount = getSatAmount(amount);
const { amount, forceUnit } = nextProps;
if (forceUnit === 'sats' && forceUnit !== this.props.forceUnit) {
const currentSatAmount = getSatAmount(
amount || '',
this.props.forceUnit
);
this.setState({ satAmount: currentSatAmount });
this.props.onAmountChange(
currentSatAmount.toString(),
currentSatAmount
);
} else {
const satAmount = getSatAmount(amount || '', forceUnit);
this.setState({ satAmount });
}
}

onChangeUnits = () => {
const { amount, onAmountChange, UnitsStore }: any = this.props;
UnitsStore.changeUnits();
const satAmount = getSatAmount(amount);
const satAmount = getSatAmount(amount, this.props.forceUnit);
onAmountChange(amount, satAmount);
this.setState({ satAmount });
};
Expand All @@ -136,6 +149,7 @@ export default class AmountInput extends React.Component<
SettingsStore
} = this.props;
const { units }: any = UnitsStore;
const effectiveUnits = this.props.forceUnit || units;
const { getRate, getSymbol }: any = FiatStore;
const { settings }: any = SettingsStore;
const { fiatEnabled } = settings;
Expand All @@ -160,24 +174,27 @@ export default class AmountInput extends React.Component<
onChangeText={(text: string) => {
// remove spaces and non-numeric chars
const formatted = text.replace(/[^\d.,-]/g, '');
const satAmount = getSatAmount(formatted);
const satAmount = getSatAmount(
formatted,
this.props.forceUnit
);
onAmountChange(formatted, satAmount);
this.setState({ satAmount });
}}
locked={locked}
prefix={
units !== 'sats' &&
(units === 'BTC'
effectiveUnits !== 'sats' &&
(effectiveUnits === 'BTC'
? '₿'
: !getSymbol().rtl
? getSymbol().symbol
: null)
}
suffix={
units === 'sats'
? units
effectiveUnits === 'sats'
? effectiveUnits
: getSymbol().rtl &&
units === 'fiat' &&
effectiveUnits === 'fiat' &&
getSymbol().symbol
}
style={{
Expand Down Expand Up @@ -215,16 +232,16 @@ export default class AmountInput extends React.Component<
color: themeColor('text')
}}
>
{getRate(units === 'sats')}
{getRate(effectiveUnits === 'sats')}
</Text>
)}
{fiatEnabled && units !== 'fiat' && (
{fiatEnabled && effectiveUnits !== 'fiat' && (
<Amount sats={satAmount} fixedUnits="fiat" />
)}
{units !== 'BTC' && (
{effectiveUnits !== 'BTC' && (
<Amount sats={satAmount} fixedUnits="BTC" />
)}
{units !== 'sats' && (
{effectiveUnits !== 'sats' && (
<Amount sats={satAmount} fixedUnits="sats" />
)}
</View>
Expand Down
61 changes: 26 additions & 35 deletions views/OpenChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { Route } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import { Tab } from 'react-native-elements';

import Amount from '../components/Amount';
import AmountInput from '../components/AmountInput';
import Button from '../components/Button';
import DropdownSetting from '../components/DropdownSetting';
Expand Down Expand Up @@ -569,40 +568,32 @@ export default class OpenChannel extends React.Component<

{!connectPeerOnly && (
<>
{!fundMax && (
<AmountInput
amount={local_funding_amount}
title={localeString(
'views.OpenChannel.localAmt'
)}
onAmountChange={(
amount: string,
satAmount: string | number
) => {
this.setState({
local_funding_amount: amount,
satAmount
});
}}
hideConversion={
local_funding_amount === 'all'
}
/>
)}

{(local_funding_amount === 'all' ||
fundMax) && (
<View style={{ marginBottom: 20 }}>
<Amount
sats={
utxoBalance > 0
? utxoBalance
: confirmedBlockchainBalance
}
toggleable
/>
</View>
)}
<AmountInput
amount={
fundMax
? utxoBalance > 0
? utxoBalance.toString()
: confirmedBlockchainBalance.toString()
: local_funding_amount
}
title={localeString(
'views.OpenChannel.localAmt'
)}
onAmountChange={(
amount: string,
satAmount: string | number
) => {
this.setState({
local_funding_amount: amount,
satAmount
});
}}
hideConversion={
local_funding_amount === 'all'
}
locked={fundMax}
forceUnit={fundMax ? 'sats' : undefined}
/>

{BackendUtils.supportsChannelFundMax() &&
additionalChannels.length === 0 && (
Expand Down
64 changes: 22 additions & 42 deletions views/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import TransactionsStore from '../stores/TransactionsStore';
import UTXOsStore from '../stores/UTXOsStore';
import ContactStore from '../stores/ContactStore';

import Amount from '../components/Amount';
import AmountInput from '../components/AmountInput';
import Button from '../components/Button';
import FeeLimit from '../components/FeeLimit';
Expand Down Expand Up @@ -987,47 +986,28 @@ export default class Send extends React.Component<SendProps, SendState> {
{transactionType === 'On-chain' &&
BackendUtils.supportsOnchainSends() && (
<React.Fragment>
{!fundMax && (
<AmountInput
amount={amount}
title={localeString(
'views.Send.amount'
)}
onAmountChange={(
amount: string,
satAmount: string | number
) => {
this.setState({
amount,
satAmount
});
}}
hideConversion={amount === 'all'}
/>
)}

<View style={{ paddingBottom: 15 }}>
{fundMax && (
<>
<Amount
sats={
utxoBalance > 0
? utxoBalance
: confirmedBlockchainBalance
}
fixedUnits="BTC"
/>
<Amount
sats={
utxoBalance > 0
? utxoBalance
: confirmedBlockchainBalance
}
fixedUnits="sats"
/>
</>
)}
</View>
<AmountInput
amount={
fundMax
? utxoBalance > 0
? utxoBalance.toString()
: confirmedBlockchainBalance.toString()
: amount
}
title={localeString('views.Send.amount')}
onAmountChange={(
amount: string,
satAmount: string | number
) => {
this.setState({
amount,
satAmount
});
}}
hideConversion={amount === 'all'}
locked={fundMax}
forceUnit={fundMax ? 'sats' : undefined}
/>

{BackendUtils.supportsOnchainSendMax() &&
additionalOutputs.length === 0 &&
Expand Down

0 comments on commit da42480

Please sign in to comment.