Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

refactor(frontend): share currency formatter (#570)

Leonardo Zanivan96c77ae02ac1
diff --git a/frontend/src/components/app-layout.tsx b/frontend/src/components/app-layout.tsx
index 03284f73..8bf3b25f 100644
--- a/frontend/src/components/app-layout.tsx
+++ b/frontend/src/components/app-layout.tsx
@@ -60,6 +60,7 @@ import { GlobalChatPanel } from '@/components/global-chat-panel'
import { useFeatureFlags } from '@/hooks/use-feature-flags'
import { Bot, Search, Sparkles } from 'lucide-react'
import { setThemeBasedOnSystem } from '@/lib/theme-utils'
+import { formatCurrency } from '@/lib/format'
/** Placeholder rows shown while the workspace's module list is in flight. */
function NavSkeleton() {
@@ -82,12 +83,6 @@ function NavSkeleton() {
)
}
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(
- value,
- )
-}
-
export function AppLayout() {
const { t } = useTranslation()
const { user, logout, updateUser } = useAuth()
diff --git a/frontend/src/components/link-transfer-dialog.tsx b/frontend/src/components/link-transfer-dialog.tsx
index 66f9ffef..d7a8f465 100644
--- a/frontend/src/components/link-transfer-dialog.tsx
+++ b/frontend/src/components/link-transfer-dialog.tsx
@@ -16,10 +16,7 @@ import { Skeleton } from '@/components/ui/skeleton'
import { ArrowRight, AlertTriangle, Info, ArrowLeft, Search, Sparkles } from 'lucide-react'
import { transactions as transactionsApi } from '@/lib/api'
import type { Account, Transaction } from '@/types'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
type CounterpartCardProps = {
label: string
diff --git a/frontend/src/components/mobile-transaction-row.tsx b/frontend/src/components/mobile-transaction-row.tsx
index 04a62aad..e7ca65e3 100644
--- a/frontend/src/components/mobile-transaction-row.tsx
+++ b/frontend/src/components/mobile-transaction-row.tsx
@@ -5,10 +5,7 @@ import type { Transaction, Account } from '@/types'
import { AlertTriangle, ArrowLeftRight, Clock, EyeClosed, Paperclip } from 'lucide-react'
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useTranslation } from 'react-i18next'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
interface MobileTransactionRowProps {
tx: Transaction
diff --git a/frontend/src/components/transaction-calendar-view.tsx b/frontend/src/components/transaction-calendar-view.tsx
index 2a6a9459..1626b57d 100644
--- a/frontend/src/components/transaction-calendar-view.tsx
+++ b/frontend/src/components/transaction-calendar-view.tsx
@@ -10,15 +10,12 @@ import { getAccountName } from '@/lib/account-utils'
import { activityChartData, dayActivity } from '@/lib/calendar-activity'
import { weekdayShortLabels } from '@/lib/date-utils'
import { cn } from '@/lib/utils'
+import { formatCurrency } from '@/lib/format'
function parseLocalDate(value: string) {
return new Date(`${value}T00:00:00`)
}
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
function compactCurrency(value: number, currency = 'USD', locale = 'en-US') {
return new Intl.NumberFormat(locale, {
style: 'currency',
diff --git a/frontend/src/components/transaction-drill-down.tsx b/frontend/src/components/transaction-drill-down.tsx
index 26f76039..b5ea03a6 100644
--- a/frontend/src/components/transaction-drill-down.tsx
+++ b/frontend/src/components/transaction-drill-down.tsx
@@ -8,6 +8,7 @@ import { CategoryIcon } from '@/components/category-icon'
import { useAuth } from '@/contexts/auth-context'
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import type { Transaction } from '@/types'
+import { formatCurrency } from '@/lib/format'
export type DrillDownFilter = {
title: string
@@ -37,10 +38,6 @@ type DisplayItem = {
transaction: Transaction | null
}
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
export function TransactionDrillDown({
filter,
onClose,
diff --git a/frontend/src/lib/format.ts b/frontend/src/lib/format.ts
index df9428eb..ee11db6a 100644
--- a/frontend/src/lib/format.ts
+++ b/frontend/src/lib/format.ts
@@ -138,8 +138,28 @@ export function resolveDateLocale(
return ORDER_EN_LOCALE[order]
}
+/**
+ * Build a currency formatter, falling back to a known-good locale *and*
+ * currency. A malformed locale throws just like a malformed currency code
+ * does, and `useDisplayLocale` can surface one (i18next reads `?lng=` from
+ * the querystring), so the fallback has to replace both.
+ */
+function currencyFormatter(currency: string, locale: string): Intl.NumberFormat {
+ try {
+ return new Intl.NumberFormat(locale, {
+ style: 'currency',
+ currency: currency || 'USD',
+ })
+ } catch {
+ return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
+ }
+}
+
/**
* Format a numeric value as currency using Intl.NumberFormat.
+ *
+ * Fraction digits are left to Intl so each currency keeps its own precision:
+ * pinning two would render JPY as "¥1,000.00" and CLP as "$1.000,00".
*/
export function formatCurrency(
value: number | null | undefined,
@@ -147,10 +167,10 @@ export function formatCurrency(
locale = 'en-US',
): string {
if (value == null) return '—'
- return new Intl.NumberFormat(locale, {
- style: 'currency',
- currency,
- minimumFractionDigits: 2,
- maximumFractionDigits: 2,
- }).format(value)
+ const formatter = currencyFormatter(currency, locale)
+ // Anything that rounds to zero at the currency's own precision would render
+ // with a stray minus ("-R$ 0,00"), which covers -0 as well as the sub-cent
+ // residue that summed balances and FX conversions leave behind.
+ const factor = 10 ** (formatter.resolvedOptions().maximumFractionDigits ?? 2)
+ return formatter.format(Math.round(value * factor) === 0 ? 0 : value)
}
diff --git a/frontend/src/pages/account-detail.tsx b/frontend/src/pages/account-detail.tsx
index 1f2a3e6e..89a5db13 100644
--- a/frontend/src/pages/account-detail.tsx
+++ b/frontend/src/pages/account-detail.tsx
@@ -25,6 +25,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
+import { formatCurrency } from '@/lib/format'
import {
AreaChart,
Area,
@@ -230,10 +231,6 @@ function creditCardCycleBoundaries(
}
}
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
function formatDateStr(dateStr: string, locale = 'pt-BR') {
return new Date(dateStr + 'T00:00:00').toLocaleDateString(locale)
}
diff --git a/frontend/src/pages/accounts.tsx b/frontend/src/pages/accounts.tsx
index af1fb165..e0276a70 100644
--- a/frontend/src/pages/accounts.tsx
+++ b/frontend/src/pages/accounts.tsx
@@ -37,6 +37,7 @@ import { ConnectionSettingsDialog } from '@/components/connection-settings-dialo
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
+import { formatCurrency } from '@/lib/format'
// Account types offered in the create/edit dialog. Shared between the manual
// type selector and the connected-account override selector so the list stays
@@ -49,10 +50,6 @@ const ACCOUNT_TYPE_OPTIONS = [
{ value: 'wallet', labelKey: 'accounts.typeWallet' },
] as const
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
function daysUntil(dateStr: string | null): number | null {
if (!dateStr) return null
const due = new Date(dateStr + 'T00:00:00')
diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsx
index 5d3a8556..669c6f2f 100644
--- a/frontend/src/pages/assets.tsx
+++ b/frontend/src/pages/assets.tsx
@@ -55,14 +55,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- try {
- return new Intl.NumberFormat(locale, { style: 'currency', currency: currency || 'USD' }).format(value)
- } catch {
- return new Intl.NumberFormat(locale, { style: 'currency', currency: 'USD' }).format(value)
- }
-}
+import { formatCurrency } from '@/lib/format'
// Renders a logo image when one is available, falling back to the asset's
// type-based Lucide icon on missing URL or broken image. Uses the type's
diff --git a/frontend/src/pages/budgets.tsx b/frontend/src/pages/budgets.tsx
index ea5bff65..e415473c 100644
--- a/frontend/src/pages/budgets.tsx
+++ b/frontend/src/pages/budgets.tsx
@@ -26,10 +26,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
function currentMonth() {
const now = new Date()
diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx
index e856ab88..e02e4cd3 100644
--- a/frontend/src/pages/dashboard.tsx
+++ b/frontend/src/pages/dashboard.tsx
@@ -51,11 +51,7 @@ import { useAuth } from '@/contexts/auth-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
import { resolveDateFnsLocale } from '@/lib/date-fns-locale'
import type { Rule, Transaction } from '@/types'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
+import { formatCurrency } from '@/lib/format'
function formatDate(dateStr: string, locale = 'pt-BR') {
return new Date(dateStr + 'T00:00:00').toLocaleDateString(locale)
diff --git a/frontend/src/pages/goals.tsx b/frontend/src/pages/goals.tsx
index da14dd2f..3b19f8c5 100644
--- a/frontend/src/pages/goals.tsx
+++ b/frontend/src/pages/goals.tsx
@@ -32,10 +32,7 @@ import { PageHeader } from '@/components/page-header'
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
function getGoalIcon(iconKey: string | null) {
return (iconKey && ICON_MAP[iconKey]) || Target
diff --git a/frontend/src/pages/group-detail.tsx b/frontend/src/pages/group-detail.tsx
index e7e8f774..ca838246 100644
--- a/frontend/src/pages/group-detail.tsx
+++ b/frontend/src/pages/group-detail.tsx
@@ -50,10 +50,7 @@ import { CategoryIcon } from '@/components/category-icon'
import { DatePickerInput } from '@/components/ui/date-picker-input'
import { PageHeader } from '@/components/page-header'
import type { GroupMember, GroupSettlement, Transaction } from '@/types'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
function SectionCard({ children }: { children: React.ReactNode }) {
return (
diff --git a/frontend/src/pages/payees.tsx b/frontend/src/pages/payees.tsx
index 2436232a..12dbf299 100644
--- a/frontend/src/pages/payees.tsx
+++ b/frontend/src/pages/payees.tsx
@@ -46,10 +46,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import type { Payee } from '@/types'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
export default function PayeesPage() {
const { t } = useTranslation()
diff --git a/frontend/src/pages/recurring.tsx b/frontend/src/pages/recurring.tsx
index 1ddb286f..0b2d95a6 100644
--- a/frontend/src/pages/recurring.tsx
+++ b/frontend/src/pages/recurring.tsx
@@ -26,10 +26,7 @@ import { DatePickerInput } from '@/components/ui/date-picker-input'
import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
-
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
+import { formatCurrency } from '@/lib/format'
const TH = 'text-xs font-medium text-muted-foreground py-3'
diff --git a/frontend/src/pages/reports.tsx b/frontend/src/pages/reports.tsx
index 50e46598..4909cd96 100644
--- a/frontend/src/pages/reports.tsx
+++ b/frontend/src/pages/reports.tsx
@@ -28,6 +28,7 @@ import { usePrivacyMode } from '@/hooks/use-privacy-mode'
import { useAuth } from '@/contexts/auth-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
import type { ReportResponse, CategoryTrendItem } from '@/types'
+import { formatCurrency } from '@/lib/format'
// A small qualitative palette of well-separated hues for the composition
// detail ring. Capped to a handful of slices, distinct colours make each
@@ -49,10 +50,6 @@ const SLICE_COLORS = [
]
const OTHER_SLICE_COLOR = '#9CA3AF'
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
function formatCompact(value: number, currency = 'USD', locale = 'en-US') {
return new Intl.NumberFormat(locale, {
style: 'currency',
diff --git a/frontend/src/pages/transactions.tsx b/frontend/src/pages/transactions.tsx
index 22b9f4ff..9c5647dc 100644
--- a/frontend/src/pages/transactions.tsx
+++ b/frontend/src/pages/transactions.tsx
@@ -58,6 +58,7 @@ import { MobileTransactionRow } from '@/components/mobile-transaction-row'
import { useAuth } from '@/contexts/auth-context'
import { useWorkspace } from '@/contexts/workspace-context'
import { useCollectionFilter } from '@/contexts/collection-filter-context'
+import { formatCurrency } from '@/lib/format'
type TransactionUpdatePayload = Partial<Transaction> & {
apply_to_transfer_pair?: boolean
@@ -68,10 +69,6 @@ type PendingTransferCategoryUpdate = {
data: TransactionUpdatePayload
}
-function formatCurrency(value: number, currency = 'USD', locale = 'en-US') {
- return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(value)
-}
-
function parseHashtags(notes: string | null): string[] {
if (!notes) return []
const matches = notes.match(/#[\w\u00C0-\u017E-]+/g)