Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format amount review modal #1265

Merged
merged 7 commits into from
Dec 8, 2022
Merged

Conversation

DiogoSoaress
Copy link
Member

@DiogoSoaress DiogoSoaress commented Nov 28, 2022

What it solves

Resolves #705

How this PR fixes it

Implements a new formatter formatAmountPrecise to format a number with a passed precision without adhering to our style guide notation.
Uses the app formatAmountPrecise formatter for review modal header's amounts.

How to test it

  1. Click to create a transaction with thousands and decimals
  2. Observe the review modal displays the amount formatted as per locale

Analytics changes

N/A

Screenshots

Screenshot 2022-12-07 at 15 49 12

@cloudflare-workers-and-pages
Copy link

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 81b6f02
Status: ✅  Deploy successful!
Preview URL: https://3c72d04e.web-core.pages.dev
Branch Preview URL: https://fix-format-amount-review-mod.web-core.pages.dev

View logs

@github-actions
Copy link

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@github-actions
Copy link

@@ -268,4 +268,24 @@ describe('formatNumber', () => {
expect(formatCurrency(amount4, 'BHD')).toBe('< -0.001 BHD')
})
})

describe('localeNumberFormatter', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add international tests.


export const localeNumberFormatter = (amount: number | string): string => {
// get locale decimal separator
const number = 123456.789
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the arbitrary value confused me at first. I would suggest using Intl.NumberFormat.prototype.formatToParts instead as you can directly get the separators from amount directly.

// trailing zeros
const decimalFinalWithoutTrailingZeros = decimalString ? decimalFinal.replace(/0+$/, '') : ''

const integerFinal = integerString.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are thousand separators universal/set every three digits in all number formats?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely not: https://learn.microsoft.com/en-us/globalization/locale/number-formatting
Hindi – two digits, Japanese four.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point here, the regex was very opinionated. I'm using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString now to format amount's integer part.

@DiogoSoaress DiogoSoaress force-pushed the fix-format-amount-review-modal branch from 81b6f02 to 3cfcd5b Compare December 7, 2022 09:52
@github-actions
Copy link

github-actions bot commented Dec 7, 2022

Branch preview

✅ Deploy successful!

https://fix-format-amount-review-modal--webcore.review-web-core.5afe.dev

@github-actions
Copy link

github-actions bot commented Dec 7, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@katspaugh
Copy link
Member

Looks good!
Screenshot 2022-12-07 at 16 02 33

Noticed a different bug when testing this. The same amount in the select and input below is formatted differently:

Screenshot 2022-12-07 at 16 02 15

@DiogoSoaress
Copy link
Member Author

Using our utils formatter does not give a very precise view to the amount of tokens to be transferred so I've created a new ticket for this enhancement #1321

@DiogoSoaress
Copy link
Member Author

DiogoSoaress commented Dec 7, 2022

Noticed a different bug when testing this. The same amount in the select and input below is formatted differently.

I'm going to create a new ticket to address that bug

Edit:
Created a bug -> #1325

@@ -26,7 +27,7 @@ export const TokenTransferReview = ({

<Box mt={1} fontSize={20}>
{children}
{amount} {tokenInfo.symbol}
{formatAmount(amount)} {tokenInfo.symbol}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use the current formatter for the reasons you stated here. This is arguably less clear than non-formatted numbers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't merge this PR until we discuss it tomorrow

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree it might be worse than no formatting in some (rather extreme?) cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, this is how we format numbers in other places in the app, like it or not. I don’t see why this modal has to be different than tx list, balances etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the one place that I would argue we always display the exact precision though as assets have aribtrary values. If I were sending 100,400, I would much sooner "review" that than 100K. The same with decimal truncation.

Copy link
Member

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@DiogoSoaress DiogoSoaress merged commit f51a756 into dev Dec 8, 2022
@DiogoSoaress DiogoSoaress deleted the fix-format-amount-review-modal branch December 8, 2022 12:16
@gitpoap-bot
Copy link

gitpoap-bot bot commented Dec 8, 2022

Congrats, your important contribution to this open-source project has earned you a GitPOAP!

GitPOAP: 2022 Safe Web Core Contributor:

GitPOAP: 2022 Safe Web Core Contributor GitPOAP Badge

Head to gitpoap.io & connect your GitHub account to mint!

Learn more about GitPOAPs here.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add thousand separators to review amount
3 participants