Skip to content

Commit

Permalink
chore(wallet-mobile): updated PairedBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Apr 26, 2024
1 parent 417869f commit 7309d88
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {storiesOf} from '@storybook/react-native'
import {tokenBalanceMocks} from '@yoroi/portfolio'
import React from 'react'
import {View} from 'react-native'

Expand All @@ -19,7 +20,7 @@ storiesOf('PairedBalance', module)
>
<View style={{flex: 1, justifyContent: 'center'}}>
<View style={{borderWidth: 1}}>
<PairedBalance amount={{quantity: '2', tokenId: mocks.wallet.primaryTokenInfo.id}} />
<PairedBalance amount={tokenBalanceMocks.primaryETH} privacyPlaceholder="*.****" />
</View>
</View>
</SelectedWalletProvider>
Expand All @@ -37,7 +38,7 @@ storiesOf('PairedBalance', module)
>
<View style={{flex: 1, justifyContent: 'center'}}>
<View style={{borderWidth: 1}}>
<PairedBalance amount={{quantity: '2', tokenId: mocks.wallet.primaryTokenInfo.id}} />
<PairedBalance amount={tokenBalanceMocks.primaryETH} privacyPlaceholder="-.--" />
</View>
</View>
</SelectedWalletProvider>
Expand All @@ -55,7 +56,11 @@ storiesOf('PairedBalance', module)
>
<View style={{flex: 1, justifyContent: 'center'}}>
<View style={{borderWidth: 1}}>
<PairedBalance amount={{quantity: '2', tokenId: mocks.wallet.primaryTokenInfo.id}} isPrivacyOff />
<PairedBalance
amount={tokenBalanceMocks.primaryETH}
privacyPlaceholder="you should see this"
isPrivacyOff
/>
</View>
</View>
</SelectedWalletProvider>
Expand All @@ -73,7 +78,7 @@ storiesOf('PairedBalance', module)
>
<View style={{flex: 1, justifyContent: 'center'}}>
<View style={{borderWidth: 1}}>
<PairedBalance amount={{quantity: '2', tokenId: mocks.wallet.primaryTokenInfo.id}} />
<PairedBalance amount={tokenBalanceMocks.primaryETH} privacyPlaceholder="hide it" />
</View>
</View>
</SelectedWalletProvider>
Expand Down
78 changes: 37 additions & 41 deletions apps/wallet-mobile/src/components/PairedBalance/PairedBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {amountBreakdown} from '@yoroi/portfolio'
import {useTheme} from '@yoroi/theme'
import {Portfolio} from '@yoroi/types'
import * as React from 'react'
Expand All @@ -8,62 +9,61 @@ import {useCurrencyContext} from '../../features/Settings/Currency'
import {useSelectedWallet} from '../../features/WalletManager/Context'
import {useExchangeRate} from '../../yoroi-wallets/hooks'
import {CurrencySymbol} from '../../yoroi-wallets/types'
import {Quantities} from '../../yoroi-wallets/utils'
import {Boundary, ResetError, ResetErrorRef} from '..'
import { splitBigInt } from '@yoroi/common'

type Props = {
amount: Portfolio.Token.Amount
privacyPlaceholder: string
isPrivacyOff?: boolean
textStyle?: TextStyle
}
export const PairedBalance = React.forwardRef<ResetErrorRef, Props>(({isPrivacyOff, amount, textStyle, privacyPlaceholder}, ref) => {
const {currency} = useCurrencyContext()
export const PairedBalance = React.forwardRef<ResetErrorRef, Props>(
({isPrivacyOff, amount, textStyle, privacyPlaceholder}, ref) => {
const {currency} = useCurrencyContext()

return (
<Boundary
key={currency}
loading={{size: 'small'}}
error={{
fallback: ({resetErrorBoundary}) => (
<ResetError resetErrorBoundary={resetErrorBoundary} ref={ref}>
<BalanceError textStyle={textStyle} />
</ResetError>
),
}}
>
<Price isPrivacyOff={isPrivacyOff} amount={amount} privacyPlaceholder={privacyPlaceholder} textStyle={textStyle} />
</Boundary>
)
})
// hide pairing when set to the primary token
if (currency === 'ADA') return null

return (
<Boundary
key={currency}
loading={{size: 'small'}}
error={{
fallback: ({resetErrorBoundary}) => (
<ResetError resetErrorBoundary={resetErrorBoundary} ref={ref}>
<BalanceError textStyle={textStyle} />
</ResetError>
),
}}
>
<Price
isPrivacyOff={isPrivacyOff}
amount={amount}
privacyPlaceholder={privacyPlaceholder}
textStyle={textStyle}
/>
</Boundary>
)
},
)

const Price = ({isPrivacyOff, amount, textStyle, privacyPlaceholder}: Props) => {
const wallet = useSelectedWallet()
const styles = useStyles()
const {currency, config} = useCurrencyContext()
const rate = useExchangeRate({wallet, to: currency})

// hide pairing when set to the primary token
if (currency === 'ADA') return null
const price = React.useMemo(() => {
if (rate == null) return `... ${currency}`

if (rate == null)
return (
<Text style={[styles.pairedBalanceText, textStyle]}>
... {currency}
</Text>
)
return !isPrivacyOff
? `${amountBreakdown(amount).bn.times(rate).toFormat(config.decimals)} ${currency}`
: `${privacyPlaceholder} ${currency}`
}, [amount, config.decimals, currency, isPrivacyOff, privacyPlaceholder, rate])

const price = splitBigInt()

const secondaryExchangeQuantity = Quantities.decimalPlaces(
Quantities.product([primaryExchangeQuantity, `${rate}`]),
config.decimals,
)
const pairedTotal = isPrivacyOff ? hiddenPairedTotal : secondaryExchangeQuantity
return (
<Text style={[styles.pairedBalanceText, textStyle]} testID="pairedTotalText">
{`${pairedTotal} ${currency}`}
{price}
</Text>
)
}
Expand All @@ -73,11 +73,7 @@ const BalanceError = ({textStyle}: {textStyle?: TextStyle}) => {
const styles = useStyles()
const {currency} = useCurrencyContext()

return (
<Text style={[styles.pairedBalanceText, textStyle]}>
{strings.pairedBalanceError(currency)}
</Text>
)
return <Text style={[styles.pairedBalanceText, textStyle]}>{strings.pairedBalanceError(currency)}</Text>
}

const messages = defineMessages({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"defaultMessage": "!!!Error obtaining {currency} pairing",
"file": "src/components/PairedBalance/PairedBalance.tsx",
"start": {
"line": 84,
"line": 80,
"column": 22,
"index": 2633
"index": 2560
},
"end": {
"line": 87,
"line": 83,
"column": 3,
"index": 2765
"index": 2692
}
}
]
2 changes: 2 additions & 0 deletions packages/portfolio/src/balance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ export const portfolioBalanceManagerMaker = (
balances: [...secondaries.values(), newPrimaryBalance],
})
sortedBalances = splitByType(sorted)
primaryBreakdown = newPrimaryBreakdown
primaryBalance = newPrimaryBalance

observer.notify({
on: Portfolio.Event.ManagerOn.Sync,
Expand Down

0 comments on commit 7309d88

Please sign in to comment.