-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add ellipsis for permit fiat values (#26001)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> This PR goal is to add ellipsis to the fiat value if it's more than 15 character in permit simulations. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26001?quickstart=1) ## **Related issues** Fixes: MetaMask/MetaMask-planning#2842 ## **Manual testing steps** 1. Go to cowswap 2. Swap a token with gas-free approve for another token 3. Notice the permit signature screen ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <img width="472" alt="Screenshot 2024-07-22 at 14 40 57" src="https://github.com/user-attachments/assets/c5879989-7719-4e4b-902f-1f57144d8889"> ## **Pre-merge author checklist** - [X] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [X] I've completed the PR template to the best of my ability - [X] I’ve included tests if applicable - [X] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [X] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
- Loading branch information
Showing
7 changed files
with
143 additions
and
18 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
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
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,35 +1,98 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { getIntlLocale } from '../ducks/locale/locale'; | ||
import { getCurrentCurrency } from '../selectors'; | ||
import { shortenString } from '../helpers/utils/util'; | ||
|
||
/** | ||
* Returns a function that formats a fiat amount as a localized string. | ||
* The hook takes an optional options object to configure the formatting. | ||
* | ||
* Example usage: | ||
* | ||
* ``` | ||
* const formatFiat = useFiatFormatter(); | ||
* const formattedAmount = formatFiat(1000); | ||
* | ||
* const shorteningFiatFormatter = useFiatFormatter(); | ||
* const shortenedAmount = shorteningFiatFormatter(100000000000000000, { shorten: true }); | ||
* ``` | ||
* | ||
* @returns A function that takes a fiat amount as a number and returns a formatted string. | ||
*/ | ||
|
||
type FiatFormatter = (fiatAmount: number) => string; | ||
const TRUNCATED_CHAR_LIMIT_FOR_SHORTENED_FIAT = 15; | ||
const TRUNCATED_START_CHARS_FOR_SHORTENED_FIAT = 12; | ||
|
||
type FiatFormatterOptions = { | ||
shorten?: boolean; | ||
}; | ||
|
||
type FiatFormatter = ( | ||
fiatAmount: number, | ||
options?: FiatFormatterOptions, | ||
) => string; | ||
|
||
export const useFiatFormatter = (): FiatFormatter => { | ||
const locale = useSelector(getIntlLocale); | ||
const fiatCurrency = useSelector(getCurrentCurrency); | ||
|
||
return (fiatAmount: number) => { | ||
return (fiatAmount: number, options: FiatFormatterOptions = {}) => { | ||
const { shorten } = options; | ||
|
||
try { | ||
return new Intl.NumberFormat(locale, { | ||
const formatter = new Intl.NumberFormat(locale, { | ||
style: 'currency', | ||
currency: fiatCurrency, | ||
}).format(fiatAmount); | ||
}); | ||
|
||
if (!shorten) { | ||
return formatter.format(fiatAmount); | ||
} | ||
|
||
const parts = formatter.formatToParts(fiatAmount); | ||
|
||
let currencySymbol = ''; | ||
let numberString = ''; | ||
|
||
parts.forEach((part) => { | ||
if (part.type === 'currency') { | ||
currencySymbol += part.value; | ||
} else { | ||
numberString += part.value; | ||
} | ||
}); | ||
|
||
// Shorten the number part while preserving commas | ||
const shortenedNumberString = shortenString(numberString, { | ||
truncatedCharLimit: TRUNCATED_CHAR_LIMIT_FOR_SHORTENED_FIAT, | ||
truncatedStartChars: TRUNCATED_START_CHARS_FOR_SHORTENED_FIAT, | ||
truncatedEndChars: 0, | ||
skipCharacterInEnd: true, | ||
}); | ||
|
||
// Determine the position of the currency symbol | ||
const currencyBeforeNumber = | ||
parts.findIndex((part) => part.type === 'currency') < | ||
parts.findIndex((part) => part.type === 'integer'); | ||
|
||
// Reassemble the formatted string | ||
return currencyBeforeNumber | ||
? `${currencySymbol}${shortenedNumberString}` | ||
: `${shortenedNumberString}${currencySymbol}`; | ||
} catch (error) { | ||
// Fallback for unknown or unsupported currencies | ||
return `${fiatAmount} ${fiatCurrency}`; | ||
const formattedNumber = new Intl.NumberFormat(locale).format(fiatAmount); | ||
const shortenedNumberString = shortenString(formattedNumber, { | ||
truncatedCharLimit: TRUNCATED_CHAR_LIMIT_FOR_SHORTENED_FIAT, | ||
truncatedStartChars: TRUNCATED_START_CHARS_FOR_SHORTENED_FIAT, | ||
truncatedEndChars: 0, | ||
skipCharacterInEnd: true, | ||
}); | ||
|
||
if (shorten) { | ||
return `${shortenedNumberString} ${fiatCurrency}`; | ||
} | ||
return `${formattedNumber} ${fiatCurrency}`; | ||
} | ||
}; | ||
}; |
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
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