Skip to content

Commit

Permalink
Refactor tooltip balance value
Browse files Browse the repository at this point in the history
  • Loading branch information
kpyszkowski committed Nov 6, 2024
1 parent 0fe9988 commit 9d13616
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
20 changes: 14 additions & 6 deletions dapp/src/components/shared/CurrencyBalance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Tooltip,
} from "@chakra-ui/react"
import {
fixedPointNumberToString,
formatTokenAmount,
getCurrencyByType,
numberToLocaleString,
Expand Down Expand Up @@ -60,16 +61,24 @@ export function CurrencyBalance({
decimals,
desiredDecimals: currencyDesiredDecimals,
} = getCurrencyByType(currency)
const desiredDecimals = customDesiredDecimals ?? currencyDesiredDecimals

const getBalance = useCallback(
(desiredDecimals: number) => {
const value = amount ?? 0
(options: { withFullPrecision?: boolean } = {}) => {
const { withFullPrecision } = options

const value = amount ?? 0n

if (withFullPrecision) {
return fixedPointNumberToString(BigInt(value), decimals, withRoundUp)
}

if (shouldBeFormatted || typeof value === "bigint")
return formatTokenAmount(value, decimals, desiredDecimals, withRoundUp)

return numberToLocaleString(value, desiredDecimals)
},
[amount, decimals, shouldBeFormatted, withRoundUp],
[amount, decimals, desiredDecimals, shouldBeFormatted, withRoundUp],
)

const content = (
Expand All @@ -80,7 +89,7 @@ export function CurrencyBalance({
{...textProps}
{...balanceTextProps}
>
{getBalance(customDesiredDecimals ?? currencyDesiredDecimals)}
{getBalance()}
{withDots && "..."}
</Box>
<Box as="span" __css={styles.symbol} {...textProps} {...symbolTextProps}>
Expand All @@ -90,8 +99,7 @@ export function CurrencyBalance({
)

if (withTooltip) {
const fullPrecisionDecimals = (amount ?? 0).toString().length
const tooltipLabel = `${getBalance(fullPrecisionDecimals)} ${symbol}`
const tooltipLabel = `${getBalance({ withFullPrecision: true })} ${symbol}`

return (
<Tooltip label={tooltipLabel} shouldWrapChildren>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import { IconArrowUpRight } from "@tabler/icons-react"
import { useActivities } from "#/hooks"

Check failure on line 16 in dapp/src/pages/DashboardPage/TransactionHistory/TransactionTable.tsx

View workflow job for this annotation

GitHub Actions / dapp-format

'useActivities' is defined but never used. Allowed unused vars must match /^_+$/u
import { semanticTokens } from "#/theme/utils"
import EstimatedDuration from "./EstimatedDuration"
import { createActivity } from "#/tests/factories"

Check failure on line 19 in dapp/src/pages/DashboardPage/TransactionHistory/TransactionTable.tsx

View workflow job for this annotation

GitHub Actions / dapp-format

`#/tests/factories` import should occur before import of `./EstimatedDuration`

const BLOCK_EXPLORER_CELL_MIN_WIDTH = 16

export default function TransactionTable() {
const activities = useActivities()
// const activities = useActivities()
const activities = [
createActivity({
amount: 122200000n,
}),
]

return (
<Pagination data={activities} pageSize={10} spacing={6}>
Expand Down

0 comments on commit 9d13616

Please sign in to comment.