Skip to content

Commit

Permalink
fix: show balance in unit based on settings on Send screen (#276)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel <[email protected]>
  • Loading branch information
secondl1ght and Daniel authored May 25, 2022
1 parent 28574dd commit b0c8c4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
32 changes: 1 addition & 31 deletions src/components/Balance.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react'
import { BTC, SATS, btcToSats, satsToBtc } from '../utils'
import { BTC, SATS, btcToSats, satsToBtc, formatBtc, formatSats } from '../utils'
import Sprite from './Sprite'
import * as rb from 'react-bootstrap'
import styles from './Balance.module.css'
Expand All @@ -8,43 +8,13 @@ const DISPLAY_MODE_BTC = 0
const DISPLAY_MODE_SATS = 1
const DISPLAY_MODE_HIDDEN = 2

const decimalPoint = '\u002E'
const nbHalfSpace = '\u202F'

const getDisplayMode = (unit, showBalance) => {
if (showBalance && unit === SATS) return DISPLAY_MODE_SATS
if (showBalance && unit === BTC) return DISPLAY_MODE_BTC

return DISPLAY_MODE_HIDDEN
}

const formatBtc = (value) => {
const formatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 1,
minimumFractionDigits: 8,
})

const numberString = formatter.format(value)

const [integerPart, fractionalPart] = numberString.split(decimalPoint)

const formattedFractionalPart = fractionalPart
.split('')
.map((char, idx) => (idx === 2 || idx === 5 ? `${nbHalfSpace}${char}` : char))
.join('')

return integerPart + decimalPoint + formattedFractionalPart
}

const formatSats = (value) => {
const formatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 1,
minimumFractionDigits: 0,
})

return formatter.format(value)
}

const BalanceComponent = ({ symbol, value, symbolIsPrefix }) => {
return (
<span className="d-inline-flex align-items-center">
Expand Down
7 changes: 5 additions & 2 deletions src/components/Send.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { useServiceInfo, useReloadServiceInfo } from '../context/ServiceInfoCont
import { useSettings } from '../context/SettingsContext'
import { useBalanceSummary } from '../hooks/BalanceSummary'
import * as Api from '../libs/JmWalletApi'
import { btcToSats, SATS, formatBtc, formatSats } from '../utils'
import { routes } from '../constants/routes'
import styles from './Send.module.css'
import { SATS } from '../utils'

const IS_COINJOIN_DEFAULT_VAL = true
// initial value for `minimum_makers` from the default joinmarket.cfg (last check on 2022-02-20 of v0.9.5)
Expand Down Expand Up @@ -617,7 +617,10 @@ export default function Send() {
{settings.useAdvancedWalletMode
? t('send.account_selector_option_dev_mode', { number: accountIndex })
: t('send.account_selector_option', { number: accountIndex })}{' '}
{settings.showBalance && `(\u20BF${totalBalance})`}
{settings.showBalance &&
(settings.unit === 'sats'
? `(${formatSats(btcToSats(totalBalance))} sats)`
: `(\u20BF${formatBtc(totalBalance)})`)}
</option>
))}
</rb.Form.Select>
Expand Down
30 changes: 30 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,33 @@ export const copyToClipboard = (
}
})
}

export const formatBtc = (value: number) => {
const decimalPoint = '\u002E'
const nbHalfSpace = '\u202F'

const formatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 1,
minimumFractionDigits: 8,
})

const numberString = formatter.format(value)

const [integerPart, fractionalPart] = numberString.split(decimalPoint)

const formattedFractionalPart = fractionalPart
.split('')
.map((char, idx) => (idx === 2 || idx === 5 ? `${nbHalfSpace}${char}` : char))
.join('')

return integerPart + decimalPoint + formattedFractionalPart
}

export const formatSats = (value: number) => {
const formatter = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 1,
minimumFractionDigits: 0,
})

return formatter.format(value)
}

0 comments on commit b0c8c4f

Please sign in to comment.