From 57b972d2ae546fdbcd9304c2e4dda9e1024e9847 Mon Sep 17 00:00:00 2001 From: Corey Martin Date: Thu, 14 Nov 2024 18:41:54 -0800 Subject: [PATCH] [uma-bridge] Confirm quote (#13359) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Screenshot 2024-11-14 at 5 33 55 PM GitOrigin-RevId: b6ed44d00fb0a282409969ee72e43ebc5a6c997c --- packages/core/src/utils/currency.ts | 4 ++- packages/ui/src/components/CurrencyAmount.tsx | 31 ++++++++++++------- packages/ui/src/components/Flex.tsx | 22 ++++++++++--- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/packages/core/src/utils/currency.ts b/packages/core/src/utils/currency.ts index e171960c..f8751b2b 100644 --- a/packages/core/src/utils/currency.ts +++ b/packages/core/src/utils/currency.ts @@ -338,7 +338,7 @@ function asNumber(value: string | number | null | undefined) { return value || 0; } -function getCurrencyAmount(currencyAmountArg: CurrencyAmountArg) { +export function getCurrencyAmount(currencyAmountArg: CurrencyAmountArg) { let value = 0; let unit = undefined; @@ -536,6 +536,8 @@ export const abbrCurrencyUnit = (unit: CurrencyUnitType) => { return "MSAT"; case CurrencyUnit.USD: return "USD"; + case CurrencyUnit.MXN: + return "MXN"; } return "Unsupported CurrencyUnit"; }; diff --git a/packages/ui/src/components/CurrencyAmount.tsx b/packages/ui/src/components/CurrencyAmount.tsx index cf648ac9..ae6c10f0 100644 --- a/packages/ui/src/components/CurrencyAmount.tsx +++ b/packages/ui/src/components/CurrencyAmount.tsx @@ -8,6 +8,7 @@ import type { import { CurrencyUnit, formatCurrencyStr, + getCurrencyAmount, isCurrencyMap, mapCurrencyAmount, } from "@lightsparkdev/core"; @@ -31,7 +32,7 @@ type CurrencyAmountProps = { export function CurrencyAmount({ amount, - displayUnit = CurrencyUnit.SATOSHI, + displayUnit: displayUnitProp, shortNumber = false, showUnits = false, includeEstimatedIndicator = false, @@ -41,32 +42,38 @@ export function CurrencyAmount({ typography, unitsPerBtc, }: CurrencyAmountProps) { - const unit = displayUnit; - - const amountMap = isCurrencyMap(amount) - ? amount - : mapCurrencyAmount(amount, unitsPerBtc); + let displayUnit: CurrencyUnitType; + let amountMap: CurrencyMap; + if (isCurrencyMap(amount)) { + displayUnit = displayUnitProp || CurrencyUnit.SATOSHI; + amountMap = amount; + } else { + const resolvedCurrencyAmount = getCurrencyAmount(amount); + /* default to the currency amount unit if defined and displayUnit is not provided: */ + displayUnit = displayUnitProp || resolvedCurrencyAmount.unit; + amountMap = mapCurrencyAmount(amount, unitsPerBtc); + } - const value = amountMap[unit]; - const defaultFormattedNumber = amountMap.formatted[unit]; + const value = amountMap[displayUnit]; + const defaultFormattedNumber = amountMap.formatted[displayUnit]; /* There are just a few ways that CurrencyAmounts need to be formatted throughout the UI. In general the default should always be used: */ let formattedNumber = defaultFormattedNumber; if (shortNumber) { formattedNumber = formatCurrencyStr( - { value: Number(value), unit }, + { value: Number(value), unit: displayUnit }, { precision: 1, compact: true }, ); } else if (fullPrecision) { formattedNumber = formatCurrencyStr( - { value: Number(value), unit }, + { value: Number(value), unit: displayUnit }, { precision: "full" }, ); } if (showUnits) { - formattedNumber += ` ${shorttext(unit, value)}`; + formattedNumber += ` ${shorttext(displayUnit, value)}`; } let content: string | ReactNode = formattedNumber; @@ -81,7 +88,7 @@ export function CurrencyAmount({ return ( {includeEstimatedIndicator && "Est. "} - + {content} ); diff --git a/packages/ui/src/components/Flex.tsx b/packages/ui/src/components/Flex.tsx index 50111946..5a05a658 100644 --- a/packages/ui/src/components/Flex.tsx +++ b/packages/ui/src/components/Flex.tsx @@ -3,8 +3,20 @@ import { type ElementType, type ReactNode } from "react"; type FlexProps = { center?: boolean | undefined; - justify?: "center" | "flex-start" | "flex-end" | "space-between" | undefined; - align?: "center" | "flex-start" | "flex-end" | "space-between" | undefined; + justify?: + | "stretch" + | "center" + | "flex-start" + | "flex-end" + | "space-between" + | undefined; + align?: + | "stretch" + | "center" + | "flex-start" + | "flex-end" + | "space-between" + | undefined; children?: ReactNode; as?: ElementType | undefined; column?: boolean | undefined; @@ -38,8 +50,8 @@ export function Flex({ mr, ml, }: FlexProps) { - const justify = justifyProp ? justifyProp : center ? "center" : "flex-start"; - const align = alignProp ? alignProp : center ? "center" : "flex-start"; + const justify = justifyProp ? justifyProp : center ? "center" : "stretch"; + const align = alignProp ? alignProp : center ? "center" : "stretch"; return ( ` +export const StyledFlex = styled.div` display: flex; text-overflow: ellipsis;