MCPcopy
hub / github.com/Uniswap/interface / useFormatter

Function useFormatter

src/utils/formatNumbers.ts:710–851  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

708
709// Constructs an object that injects the correct locale and local currency into each of the above formatter functions.
710export 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(

Callers 15

ChartDeltaFunction · 0.90
ChartHeaderFunction · 0.90
SwapModalHeaderAmountFunction · 0.90
AdvancedSwapDetailsFunction · 0.90
MaxSlippageTooltipFunction · 0.90
PriceImpactModalFunction · 0.90
TradePriceFunction · 0.90
useGasPriceFunction · 0.90
PriceImpactRowFunction · 0.90
GasCostItemFunction · 0.90
GaslessSwapLabelFunction · 0.90
GasEstimateTooltipFunction · 0.90

Calls 14

useFormatterLocalesFunction · 0.85
usePreviousFunction · 0.85
handleFallbackCurrencyFunction · 0.85
formatNumberFunction · 0.85
formatPriceFunction · 0.85
formatTickPriceFunction · 0.85
formatNumberOrStringFunction · 0.85
formatFiatPriceFunction · 0.85
formatDeltaFunction · 0.85
formatPercentFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…