Skip to content

The open alternative to Salesforce, designed for AI.

Match currency values to Figma (#28676)

Raphaël Bosib82dedc0c64d
diff --git a/packages/twenty-front/src/modules/localization/utils/getCurrencyPrefix.ts b/packages/twenty-front/src/modules/localization/utils/getCurrencyPrefix.ts
new file mode 100644
index 00000000000..7e140701266
--- /dev/null
+++ b/packages/twenty-front/src/modules/localization/utils/getCurrencyPrefix.ts
@@ -0,0 +1,22 @@
+import { isNonEmptyString } from '@sniptt/guards';
+
+export const getCurrencyPrefix = (currency: string): string => {
+ const normalizedCurrency = currency.toUpperCase();
+
+ try {
+ const currencyPrefix = new Intl.NumberFormat('en-US', {
+ style: 'currency',
+ currency: normalizedCurrency,
+ })
+ .formatToParts(0)
+ .filter((part) => part.type === 'currency' || part.type === 'literal')
+ .map((part) => part.value)
+ .join('');
+
+ return isNonEmptyString(currencyPrefix)
+ ? currencyPrefix
+ : normalizedCurrency;
+ } catch {
+ return normalizedCurrency;
+ }
+};
diff --git a/packages/twenty-front/src/modules/object-record/record-card/components/RecordCardBodyContainer.tsx b/packages/twenty-front/src/modules/object-record/record-card/components/RecordCardBodyContainer.tsx
index fd16e95cb28..4cc3d67ca09 100644
--- a/packages/twenty-front/src/modules/object-record/record-card/components/RecordCardBodyContainer.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-card/components/RecordCardBodyContainer.tsx
@@ -1,8 +1,6 @@
import { styled } from '@linaria/react';
import { themeCssVariables } from 'twenty-ui/theme';
-import { FIELD_DISPLAY_LEADING_ICON_CLASS_NAME } from '@/ui/field/display/constants/FieldDisplayLeadingIconClassName';
-
const StyledCardBodyContainer = styled.div<{ padding?: string }>`
display: flex;
flex-direction: column;
@@ -10,10 +8,6 @@ const StyledCardBodyContainer = styled.div<{ padding?: string }>`
padding: ${({ padding }) =>
padding ??
`0 ${themeCssVariables.spacing[2]} ${themeCssVariables.spacing[2]} 10px`};
-
- .${FIELD_DISPLAY_LEADING_ICON_CLASS_NAME} {
- margin-right: ${themeCssVariables.spacing[2]};
- }
`;
export { StyledCardBodyContainer as RecordCardBodyContainer };
diff --git a/packages/twenty-front/src/modules/object-record/record-inline-cell/components/__stories__/RecordInlineCellDisplayMode.stories.tsx b/packages/twenty-front/src/modules/object-record/record-inline-cell/components/__stories__/RecordInlineCellDisplayMode.stories.tsx
index c408a28d938..e5e04708974 100644
--- a/packages/twenty-front/src/modules/object-record/record-inline-cell/components/__stories__/RecordInlineCellDisplayMode.stories.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-inline-cell/components/__stories__/RecordInlineCellDisplayMode.stories.tsx
@@ -183,16 +183,19 @@ export const CurrencyStaysOnOneLine: Story = {
expect(amountBounds.top).toBeGreaterThanOrEqual(displayBounds.top);
expect(amountBounds.bottom).toBeLessThanOrEqual(displayBounds.bottom);
- const [fieldIcon, currencyIcon] = canvasElement.querySelectorAll('svg');
+ const currencySymbol = canvas.getByText('$', { exact: true });
+ const [fieldIcon] = canvasElement.querySelectorAll('svg');
const fieldIconBounds = fieldIcon.getBoundingClientRect();
- const currencyIconBounds = currencyIcon.getBoundingClientRect();
+ const currencySymbolBounds = currencySymbol.getBoundingClientRect();
const amountCenter = amountBounds.top + amountBounds.height / 2;
const fieldIconCenter = fieldIconBounds.top + fieldIconBounds.height / 2;
- const currencyIconCenter =
- currencyIconBounds.top + currencyIconBounds.height / 2;
+ const currencySymbolCenter =
+ currencySymbolBounds.top + currencySymbolBounds.height / 2;
expect(Math.abs(amountCenter - fieldIconCenter)).toBeLessThanOrEqual(1);
- expect(Math.abs(amountCenter - currencyIconCenter)).toBeLessThanOrEqual(1);
+ expect(Math.abs(amountCenter - currencySymbolCenter)).toBeLessThanOrEqual(
+ 1,
+ );
},
};
diff --git a/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts b/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts
index 58db8f7efb3..188e53c1b78 100644
--- a/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts
+++ b/packages/twenty-front/src/modules/settings/admin-panel/utils/__tests__/formatSubscriptionItemValue.test.ts
@@ -83,4 +83,30 @@ describe('formatSubscriptionItemValue', () => {
}),
).toBe('US15.00');
});
+
+ it.each(['CHF', 'SEK', 'PLN'])(
+ 'keeps the nonbreaking separator after %s',
+ (currency) => {
+ expect(
+ formatSubscriptionItemValue({
+ item: { unitAmount: 1500 },
+ currency,
+ formatNumber,
+ }),
+ ).toBe(`${currency}\u00a015.00`);
+ },
+ );
+
+ it.each([
+ { currency: 'CAD', prefix: 'CA$' },
+ { currency: 'AUD', prefix: 'A$' },
+ ])('keeps $currency adjacent to the amount', ({ currency, prefix }) => {
+ expect(
+ formatSubscriptionItemValue({
+ item: { unitAmount: 1500 },
+ currency,
+ formatNumber,
+ }),
+ ).toBe(`${prefix}15.00`);
+ });
});
diff --git a/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts b/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts
index 75c0f86ba18..31c063ad3b6 100644
--- a/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts
+++ b/packages/twenty-front/src/modules/settings/admin-panel/utils/formatSubscriptionItemValue.ts
@@ -2,6 +2,7 @@ import { plural } from '@lingui/core/macro';
import { isDefined } from 'twenty-shared/utils';
import { type FormatNumberOptions } from '@/localization/utils/formatNumber';
+import { getCurrencyPrefix } from '@/localization/utils/getCurrencyPrefix';
import { BillingProductKey } from '~/generated-metadata/graphql';
const EM_DASH = '—';
@@ -22,24 +23,6 @@ type FormatSubscriptionItemValueArgs = {
) => string;
};
-// Only the symbol comes from Intl; digits use formatNumber to honour the member's number format.
-const getCurrencySymbol = (currency: string): string => {
- const normalizedCurrency = currency.toUpperCase();
-
- try {
- return (
- new Intl.NumberFormat('en-US', {
- style: 'currency',
- currency: normalizedCurrency,
- })
- .formatToParts(0)
- .find((part) => part.type === 'currency')?.value ?? normalizedCurrency
- );
- } catch {
- return normalizedCurrency;
- }
-};
-
export const formatSubscriptionItemValue = ({
item,
currency,
@@ -73,7 +56,7 @@ export const formatSubscriptionItemValue = ({
if (isDefined(item.unitAmount)) {
parts.push(
- `${getCurrencySymbol(currency)}${formatNumber(item.unitAmount / 100, {
+ `${getCurrencyPrefix(currency)}${formatNumber(item.unitAmount / 100, {
decimals: 2,
})}`,
);
diff --git a/packages/twenty-front/src/modules/ui/field/display/components/CurrencyDisplay.tsx b/packages/twenty-front/src/modules/ui/field/display/components/CurrencyDisplay.tsx
index a1eba9562e2..61055d09e2e 100644
--- a/packages/twenty-front/src/modules/ui/field/display/components/CurrencyDisplay.tsx
+++ b/packages/twenty-front/src/modules/ui/field/display/components/CurrencyDisplay.tsx
@@ -1,28 +1,20 @@
-import { styled } from '@linaria/react';
import { useLingui } from '@lingui/react/macro';
+import { isNonEmptyString } from '@sniptt/guards';
import { formatToShortNumber, isDefined } from 'twenty-shared/utils';
import { Tooltip } from 'twenty-ui/primitives/surfaces';
-import { useTheme } from 'twenty-ui/theme';
import { useNumberFormat } from '@/localization/hooks/useNumberFormat';
import { getCurrencyLabel } from '@/localization/utils/getCurrencyLabel';
+import { getCurrencyPrefix } from '@/localization/utils/getCurrencyPrefix';
import { type FieldDefinition } from '@/object-record/record-field/ui/types/FieldDefinition';
import {
type FieldCurrencyMetadata,
type FieldCurrencyValue,
} from '@/object-record/record-field/ui/types/FieldMetadata';
-import { CURRENCY_CODE_ICONS } from '@/ui/input/components/internal/currency/constants/CurrencyCodeIcons';
-import { FIELD_DISPLAY_LEADING_ICON_CLASS_NAME } from '@/ui/field/display/constants/FieldDisplayLeadingIconClassName';
import { EllipsisDisplay } from '@/ui/field/display/components/internal/EllipsisDisplay/EllipsisDisplay';
import { TooltipDelay } from '@/ui/layout/tooltip/constants/TooltipDelay';
import { DEFAULT_DECIMAL_VALUE } from '@/localization/utils/formatNumber';
-const StyledCurrencyIconContainer = styled.span`
- align-items: center;
- display: inline-flex;
- vertical-align: text-bottom;
-`;
-
type CurrencyDisplayProps = {
currencyValue: FieldCurrencyValue | null | undefined;
fieldDefinition: FieldDefinition<FieldCurrencyMetadata>;
@@ -32,13 +24,12 @@ export const CurrencyDisplay = ({
currencyValue,
fieldDefinition,
}: CurrencyDisplayProps) => {
- const theme = useTheme();
const { i18n } = useLingui();
const currencyCode = currencyValue?.currencyCode;
- const CurrencyIcon = isDefined(currencyCode)
- ? CURRENCY_CODE_ICONS[currencyCode]
- : null;
+ const currencyPrefix = isDefined(currencyCode)
+ ? getCurrencyPrefix(currencyCode)
+ : undefined;
const amountToDisplay = !isDefined(currencyValue?.amountMicros)
? null
: currencyValue?.amountMicros / 1000000;
@@ -55,31 +46,21 @@ export const CurrencyDisplay = ({
? `${currencyCode}${isDefined(currencyLabel) ? ` - ${currencyLabel}` : ''}`
: undefined;
const shouldShowCurrencyTooltip =
- isDefined(CurrencyIcon) &&
+ isNonEmptyString(currencyPrefix) &&
amountToDisplay !== null &&
isDefined(currencyTooltipContent);
return (
<EllipsisDisplay>
{shouldShowCurrencyTooltip && (
- <>
- <Tooltip
- content={currencyTooltipContent}
- delay={TooltipDelay.shortDelay}
- side="top"
- positionMethod="fixed"
- >
- <StyledCurrencyIconContainer
- className={FIELD_DISPLAY_LEADING_ICON_CLASS_NAME}
- >
- <CurrencyIcon
- color={theme.font.color.primary}
- size={theme.icon.size.md}
- stroke={theme.icon.stroke.sm}
- />
- </StyledCurrencyIconContainer>
- </Tooltip>{' '}
- </>
+ <Tooltip
+ content={currencyTooltipContent}
+ delay={TooltipDelay.shortDelay}
+ side="top"
+ positionMethod="fixed"
+ >
+ <span>{currencyPrefix}</span>
+ </Tooltip>
)}
{amountToDisplay !== null
? !isDefined(format) || format === 'short'
diff --git a/packages/twenty-front/src/modules/ui/field/display/constants/FieldDisplayLeadingIconClassName.ts b/packages/twenty-front/src/modules/ui/field/display/constants/FieldDisplayLeadingIconClassName.ts
deleted file mode 100644
index 315925f242e..00000000000
--- a/packages/twenty-front/src/modules/ui/field/display/constants/FieldDisplayLeadingIconClassName.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export const FIELD_DISPLAY_LEADING_ICON_CLASS_NAME =
- 'field-display-leading-icon';