()
| 708 | |
| 709 | // Constructs an object that injects the correct locale and local currency into each of the above formatter functions. |
| 710 | export function useFormatter() { |
| 711 | const { formatterLocale, formatterLocalCurrency } = useFormatterLocales() |
| 712 | |
| 713 | const formatterLocalCurrencyIsUSD = formatterLocalCurrency === GqlCurrency.Usd |
| 714 | const { data: localCurrencyConversionRate, isLoading: localCurrencyConversionRateIsLoading } = |
| 715 | useLocalCurrencyConversionRate(formatterLocalCurrency, formatterLocalCurrencyIsUSD) |
| 716 | |
| 717 | const previousSelectedCurrency = usePrevious(formatterLocalCurrency) |
| 718 | const previousConversionRate = usePrevious(localCurrencyConversionRate) |
| 719 | |
| 720 | const shouldFallbackToPrevious = !localCurrencyConversionRate && localCurrencyConversionRateIsLoading |
| 721 | const shouldFallbackToUSD = !localCurrencyConversionRate && !localCurrencyConversionRateIsLoading |
| 722 | const currencyToFormatWith = handleFallbackCurrency( |
| 723 | formatterLocalCurrency, |
| 724 | previousSelectedCurrency, |
| 725 | previousConversionRate, |
| 726 | shouldFallbackToUSD, |
| 727 | shouldFallbackToPrevious |
| 728 | ) |
| 729 | const localCurrencyConversionRateToFormatWith = shouldFallbackToPrevious |
| 730 | ? previousConversionRate |
| 731 | : localCurrencyConversionRate |
| 732 | |
| 733 | type LocalesType = 'locale' | 'localCurrency' | 'conversionRate' |
| 734 | const formatNumberWithLocales = useCallback( |
| 735 | (options: Omit<FormatNumberOptions, LocalesType>) => |
| 736 | formatNumber({ |
| 737 | ...options, |
| 738 | locale: formatterLocale, |
| 739 | localCurrency: currencyToFormatWith, |
| 740 | conversionRate: localCurrencyConversionRateToFormatWith, |
| 741 | }), |
| 742 | [currencyToFormatWith, formatterLocale, localCurrencyConversionRateToFormatWith] |
| 743 | ) |
| 744 | |
| 745 | const formatCurrencyAmountWithLocales = useCallback( |
| 746 | (options: Omit<FormatCurrencyAmountOptions, LocalesType>) => |
| 747 | formatCurrencyAmount({ |
| 748 | ...options, |
| 749 | locale: formatterLocale, |
| 750 | localCurrency: currencyToFormatWith, |
| 751 | conversionRate: localCurrencyConversionRateToFormatWith, |
| 752 | }), |
| 753 | [currencyToFormatWith, formatterLocale, localCurrencyConversionRateToFormatWith] |
| 754 | ) |
| 755 | |
| 756 | const formatPriceWithLocales = useCallback( |
| 757 | (options: Omit<FormatPriceOptions, LocalesType>) => |
| 758 | formatPrice({ |
| 759 | ...options, |
| 760 | locale: formatterLocale, |
| 761 | localCurrency: currencyToFormatWith, |
| 762 | conversionRate: localCurrencyConversionRateToFormatWith, |
| 763 | }), |
| 764 | [currencyToFormatWith, formatterLocale, localCurrencyConversionRateToFormatWith] |
| 765 | ) |
| 766 | |
| 767 | const formatReviewSwapCurrencyAmountWithLocales = useCallback( |
no test coverage detected
searching dependent graphs…