The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix currency display crash on a code missing from the currency labels (#28370)
Charles Bochetdacb586d050e
diff --git a/packages/twenty-front/src/modules/localization/utils/__tests__/getCurrencyLabel.test.ts b/packages/twenty-front/src/modules/localization/utils/__tests__/getCurrencyLabel.test.tsindex 179638e0559..4078fd44c53 100644--- a/packages/twenty-front/src/modules/localization/utils/__tests__/getCurrencyLabel.test.ts+++ b/packages/twenty-front/src/modules/localization/utils/__tests__/getCurrencyLabel.test.ts@@ -20,4 +20,14 @@ describe('getCurrencyLabel', () => {getCurrencyLabel({ currencyCode: CurrencyCode.USD, locale: '%%' }),).toBe('United States dollar');});++ it('should name a valid currency missing from the source labels', () => {+ expect(getCurrencyLabel({ currencyCode: 'XCG', locale: 'en' })).toBe(+ 'Caribbean guilder',+ );+ });++ it('should fall back to the code for an unknown currency', () => {+ expect(getCurrencyLabel({ currencyCode: 'XYZ', locale: 'en' })).toBe('XYZ');+ });});diff --git a/packages/twenty-front/src/modules/localization/utils/getCurrencyLabel.ts b/packages/twenty-front/src/modules/localization/utils/getCurrencyLabel.tsindex 5402c32eed4..48d340f8a30 100644--- a/packages/twenty-front/src/modules/localization/utils/getCurrencyLabel.ts+++ b/packages/twenty-front/src/modules/localization/utils/getCurrencyLabel.ts@@ -1,19 +1,20 @@-import {- CURRENCY_CODE_LABELS,- type CurrencyCode,-} from 'twenty-shared/constants';+import { CURRENCY_CODE_LABELS } from 'twenty-shared/constants';import { SOURCE_LOCALE } from 'twenty-shared/translations';+import { isDefined } from 'twenty-shared/utils';++const SOURCE_LOCALE_LABELS: Partial<Record<string, { label: string }>> =+ CURRENCY_CODE_LABELS;export const getCurrencyLabel = ({currencyCode,locale,}: {- currencyCode: CurrencyCode;+ currencyCode: string;locale: string;}): string => {- const sourceLocaleLabel = CURRENCY_CODE_LABELS[currencyCode].label;+ const sourceLocaleLabel = SOURCE_LOCALE_LABELS[currencyCode]?.label;- if (locale === SOURCE_LOCALE) {+ if (locale === SOURCE_LOCALE && isDefined(sourceLocaleLabel)) {return sourceLocaleLabel;}@@ -22,9 +23,11 @@ export const getCurrencyLabel = ({new Intl.DisplayNames([locale], {type: 'currency',fallback: 'none',- }).of(currencyCode) ?? sourceLocaleLabel+ }).of(currencyCode) ??+ sourceLocaleLabel ??+ currencyCode);} catch {- return sourceLocaleLabel;+ return sourceLocaleLabel ?? currencyCode;}};diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/currency/hooks/useCurrencyPickerCurrencies.ts b/packages/twenty-front/src/modules/ui/input/components/internal/currency/hooks/useCurrencyPickerCurrencies.tsindex 8c7eae6dcc3..d348abe94bc 100644--- a/packages/twenty-front/src/modules/ui/input/components/internal/currency/hooks/useCurrencyPickerCurrencies.ts+++ b/packages/twenty-front/src/modules/ui/input/components/internal/currency/hooks/useCurrencyPickerCurrencies.ts@@ -1,6 +1,5 @@import { useLingui } from '@lingui/react/macro';import { useMemo } from 'react';-import { type CurrencyCode } from 'twenty-shared/constants';import { type CurrencyPickerOption } from 'twenty-ui/components';import { getCurrencyLabel } from '@/localization/utils/getCurrencyLabel';@@ -14,7 +13,7 @@ export const useCurrencyPickerCurrencies = (): CurrencyPickerOption[] => {CURRENCY_PICKER_CURRENCIES.map((currency) => ({...currency,name: getCurrencyLabel({- currencyCode: currency.code as CurrencyCode,+ currencyCode: currency.code,locale: i18n.locale,}),})),diff --git a/packages/twenty-front/src/modules/ui/input/components/internal/hooks/useCurrencies.ts b/packages/twenty-front/src/modules/ui/input/components/internal/hooks/useCurrencies.tsindex 4ac1300a9e8..7e090877d71 100644--- a/packages/twenty-front/src/modules/ui/input/components/internal/hooks/useCurrencies.ts+++ b/packages/twenty-front/src/modules/ui/input/components/internal/hooks/useCurrencies.ts@@ -1,6 +1,5 @@import { useLingui } from '@lingui/react/macro';import { useMemo } from 'react';-import { type CurrencyCode } from 'twenty-shared/constants';import { getCurrencyLabel } from '@/localization/utils/getCurrencyLabel';import { CURRENCIES } from '@/ui/input/components/internal/currency/constants/Currencies';@@ -13,7 +12,7 @@ export const useCurrencies = (): Currency[] => {() =>CURRENCIES.map((currency) => {const currencyLabel = getCurrencyLabel({- currencyCode: currency.value as CurrencyCode,+ currencyCode: currency.value,locale: i18n.locale,});