-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Ensure localised currency display after merge
After merging the main branch, updated the code to ensure localised currency is correctly displayed in both existing functionality and newly added features
- Loading branch information
Showing
4 changed files
with
113 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2025 @polkadot-cloud/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type BigNumber from 'bignumber.js' | ||
import { useTokenPrices } from 'contexts/TokenPrice' | ||
import { formatFiatCurrency } from 'locales/src/util' | ||
|
||
interface LocalizedFiatValueProps { | ||
value?: number | BigNumber | ||
tokenBalance?: number | BigNumber | ||
showPlus?: boolean | ||
wrapParentheses?: boolean | ||
withApprox?: boolean | ||
} | ||
|
||
/** | ||
* Component for displaying fiat values in the user's local currency. | ||
* Can accept either a direct value or a token balance to be converted. | ||
*/ | ||
export const LocalizedFiatValue = ({ | ||
value, | ||
tokenBalance, | ||
showPlus = false, | ||
wrapParentheses = false, | ||
withApprox = false, | ||
}: LocalizedFiatValueProps) => { | ||
const { price } = useTokenPrices() | ||
|
||
const calculateValue = (): number => { | ||
if (value !== undefined) { | ||
return typeof value === 'number' ? value : value.toNumber() | ||
} | ||
|
||
// Calculate from token balance | ||
if (tokenBalance !== undefined && price) { | ||
const balance = | ||
typeof tokenBalance === 'number' | ||
? tokenBalance | ||
: tokenBalance.toNumber() | ||
|
||
return balance * price | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
const finalValue = calculateValue() | ||
|
||
let formatted = formatFiatCurrency(finalValue) | ||
|
||
if (showPlus && finalValue > 0) { | ||
formatted = `+${formatted}` | ||
} | ||
|
||
if (withApprox) { | ||
formatted = `≈ ${formatted}` | ||
} | ||
|
||
if (wrapParentheses) { | ||
formatted = `(${formatted})` | ||
} | ||
|
||
return formatted | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
// Copyright 2025 @polkadot-cloud/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import BigNumber from 'bignumber.js' | ||
import { useNetwork } from 'contexts/Network' | ||
import type BigNumber from 'bignumber.js' | ||
import { useTokenPrices } from 'contexts/TokenPrice' | ||
import { formatFiatCurrency } from 'locales/src/util' | ||
|
||
export const FiatValue = ({ | ||
tokenBalance, | ||
currency, | ||
}: { | ||
tokenBalance: number | ||
currency: string | ||
}) => { | ||
const { network } = useNetwork() | ||
interface FiatValueProps { | ||
tokenBalance: number | BigNumber | ||
currency?: string | ||
} | ||
|
||
export const FiatValue = ({ tokenBalance }: FiatValueProps) => { | ||
const { price } = useTokenPrices() | ||
|
||
// Convert balance to fiat value | ||
const freeFiat = new BigNumber(tokenBalance * price).decimalPlaces(2) | ||
|
||
// Formatter for price feed | ||
const formatter = new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency, | ||
}) | ||
const balance = | ||
typeof tokenBalance === 'number' ? tokenBalance : tokenBalance.toNumber() | ||
|
||
if (network === 'westend') { | ||
return null | ||
} | ||
const fiatValue = balance * price | ||
|
||
return <>{formatter.format(freeFiat.toNumber())}</> | ||
// Format using the user's preferred currency | ||
return <>{formatFiatCurrency(fiatValue)}</> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters