diff --git a/src/components/common/balances/Balance.tsx b/src/components/common/balances/Balance.tsx index b9870e0e0..80f6dbc30 100644 --- a/src/components/common/balances/Balance.tsx +++ b/src/components/common/balances/Balance.tsx @@ -19,52 +19,69 @@ const log = newLogger('useCreateBallance') const M_LENGTH = 6 + 1 const K_LENGTH = 3 + 1 -function format( - value: Compact | BN | string, - currency: string, - decimals: number, - withSi?: boolean, - _isShort?: boolean, - precision?: number, +function format({ + value, + currency, + decimals, + withSi, + isShort: _isShort, + fixedDecimalsLength, + precision, withMutedDecimals = true, alwaysShowDecimals = false, -): React.ReactNode { +}: { + value: Compact | BN | string + currency: string + fixedDecimalsLength?: number + decimals: number + withSi?: boolean + isShort?: boolean + precision?: number + withMutedDecimals?: boolean + alwaysShowDecimals?: boolean +}): React.ReactNode { // Remove any excess decimals, because this expects big integers const balanceValue = value.toString().split('.')[0] - const [prefix, postfix] = formatBalance(balanceValue, { - forceUnit: '-', - decimals, - withSi: false, - withZero: true, - }).split('.') - const isShort = _isShort || (withSi && prefix.length >= K_LENGTH && !alwaysShowDecimals) + try { + const [prefix, postfix] = formatBalance(balanceValue, { + forceUnit: '-', + decimals, + withSi: false, + withZero: true, + }).split('.') + const isShort = _isShort || (withSi && prefix.length >= K_LENGTH && !alwaysShowDecimals) + + if (prefix.length > M_LENGTH && !alwaysShowDecimals) { + const balance = formatBalance(balanceValue, { decimals, withUnit: false }) + return ( + <> + {balance} {currency} + + ) + } - if (prefix.length > M_LENGTH && !alwaysShowDecimals) { - const balance = formatBalance(balanceValue, { decimals, withUnit: false }) return ( <> - {balance} {currency} + {prefix} + {!isShort && ( + <> + . + + {fixedDecimalsLength + ? postfix.substring(0, fixedDecimalsLength).padEnd(fixedDecimalsLength, '0') + : precision + ? parseFloat(`0.${postfix}`).toPrecision(precision).substring(2) + : postfix || '0000'} + + + )} +  {currency} ) + } catch { + return null } - - return ( - <> - {prefix} - {!isShort && ( - <> - . - - {precision - ? parseFloat(`0.${postfix}`).toPrecision(precision).substring(2) - : postfix || '0000'} - - - )} -  {currency} - - ) } type FormatBalanceProps = BareProps & { @@ -75,6 +92,7 @@ type FormatBalanceProps = BareProps & { precision?: number withMutedDecimals?: boolean alwaysShowDecimals?: boolean + fixedDecimalsLength?: number } export const FormatBalance = ({ @@ -84,6 +102,7 @@ export const FormatBalance = ({ isShort, className, precision, + fixedDecimalsLength, withMutedDecimals = true, alwaysShowDecimals, ...bareProps @@ -92,16 +111,17 @@ export const FormatBalance = ({ const { unit: defaultCurrency, decimals: defaultDecimal } = formatBalance.getDefaults() - const balance = format( + const balance = format({ value, - currency || defaultCurrency, - decimals || defaultDecimal, - true, + currency: currency || defaultCurrency, + decimals: decimals || defaultDecimal, + withSi: true, isShort, precision, + fixedDecimalsLength, withMutedDecimals, alwaysShowDecimals, - ) + }) return ( diff --git a/src/components/leaderboard/UserLeaderboardPage.tsx b/src/components/leaderboard/UserLeaderboardPage.tsx index 89af27cbc..5e0569f38 100644 --- a/src/components/leaderboard/UserLeaderboardPage.tsx +++ b/src/components/leaderboard/UserLeaderboardPage.tsx @@ -156,6 +156,7 @@ export default function UserLeaderboardPage({ address }: UserLeaderboardPageProp {data && (
- + diff --git a/src/components/leaderboard/common/LeaderboardTable.tsx b/src/components/leaderboard/common/LeaderboardTable.tsx index d110f8281..fdaa29802 100644 --- a/src/components/leaderboard/common/LeaderboardTable.tsx +++ b/src/components/leaderboard/common/LeaderboardTable.tsx @@ -29,10 +29,19 @@ import styles from './LeaderboardTable.module.sass' export type LeaderboardTableProps = ComponentProps<'div'> & { role: LeaderboardRole + currentUserRank?: { + rank: number + reward: string + address: string + } } const TABLE_LIMIT = 10 -export default function LeaderboardTable({ role, ...props }: LeaderboardTableProps) { +export default function LeaderboardTable({ + role, + currentUserRank, + ...props +}: LeaderboardTableProps) { const [isOpenModal, setIsOpenModal] = useState(false) const { page, data } = useGetLeaderboardData(role) const dispatch = useAppDispatch() @@ -42,13 +51,25 @@ export default function LeaderboardTable({ role, ...props }: LeaderboardTablePro } }, [dispatch, role]) - const { addresses, slicedData } = useMemo( - () => ({ - addresses: data.slice(0, TABLE_LIMIT).map(row => row.address), - slicedData: data.slice(0, TABLE_LIMIT), - }), - [data], - ) + const { addresses, slicedData } = useMemo<{ + addresses: string[] + slicedData: typeof data + }>(() => { + if (!currentUserRank || currentUserRank.rank < TABLE_LIMIT) { + return { + addresses: data.slice(0, TABLE_LIMIT).map(row => row.address), + slicedData: data.slice(0, TABLE_LIMIT), + } + } + return { + addresses: [ + ...data.slice(0, TABLE_LIMIT - 1).map(row => row.address), + currentUserRank.address, + ], + slicedData: [...data.slice(0, TABLE_LIMIT - 1), currentUserRank], + } + }, [data, currentUserRank]) + const { loading } = useFetchProfileSpaces({ ids: addresses }) return ( @@ -65,7 +86,13 @@ export default function LeaderboardTable({ role, ...props }: LeaderboardTablePro {slicedData.length === 0 && Array.from({ length: 3 }).map((_, idx) => )} {slicedData.map(row => ( - + ))}