Open-source personal finance manager. Self-hosted, privacy-first.
fix: show the asset transaction total in the holding's currency (#1167)
Shayn Hornik1cfab1e36f6a
diff --git a/frontend/src/lib/asset-transaction-total.test.ts b/frontend/src/lib/asset-transaction-total.test.tsnew file mode 100644index 00000000..f0256a48--- /dev/null+++ b/frontend/src/lib/asset-transaction-total.test.ts@@ -0,0 +1,40 @@+import { describe, expect, it } from 'vitest'++import {+ formatTransactionTotal,+ getTransactionTotalCurrency,+} from '@/lib/asset-transaction-total'++describe('asset transaction total', () => {+ const holdings = [+ { id: 'petr4', currency: 'BRL' },+ { id: 'aapl', currency: 'USD' },+ ]++ it('uses the selected holding currency, not a fixed one', () => {+ expect(getTransactionTotalCurrency(holdings, 'petr4', null)).toBe('BRL')+ expect(getTransactionTotalCurrency(holdings, 'aapl', null)).toBe('USD')+ })++ it('falls back to the edited transaction currency when its holding is not listed', () => {+ expect(getTransactionTotalCurrency(holdings, 'sold', { currency: 'EUR' })).toBe('EUR')+ expect(getTransactionTotalCurrency(holdings, 'petr4', { currency: 'EUR' })).toBe('BRL')+ })++ it('has no currency for a new ticker until the server prices it', () => {+ expect(getTransactionTotalCurrency(holdings, '__new__', null)).toBeNull()+ expect(getTransactionTotalCurrency(holdings, 'sold', { currency: null })).toBeNull()+ })++ it('formats the total in the given currency', () => {+ expect(formatTransactionTotal(100, 'BRL', 'pt-BR')).toBe(+ new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(100),+ )+ expect(formatTransactionTotal(100, 'BRL', 'pt-BR')).not.toContain('US$')+ })++ it('formats a bare amount when the currency is unknown', () => {+ expect(formatTransactionTotal(1234.5, null, 'en-US')).toBe('1,234.50')+ expect(formatTransactionTotal(1234.5, null, 'pt-BR')).toBe('1.234,50')+ })+})diff --git a/frontend/src/lib/asset-transaction-total.ts b/frontend/src/lib/asset-transaction-total.tsnew file mode 100644index 00000000..6482f33d--- /dev/null+++ b/frontend/src/lib/asset-transaction-total.ts@@ -0,0 +1,32 @@+import { formatCurrency } from '@/lib/format'+import type { Asset, AssetTransaction } from '@/types'++type CurrencyHolding = Pick<Asset, 'id' | 'currency'>+type CurrencyTransaction = Pick<AssetTransaction, 'currency'>++// Currency the transaction form's total is in. A transaction is stored in its+// holding's currency, so that is the answer for an existing holding and for an+// edit. A new ticker has no currency until the server prices it on save, so+// this returns null rather than guessing.+export function getTransactionTotalCurrency(+ holdings: CurrencyHolding[],+ holdingId: string,+ editingTx: CurrencyTransaction | null,+): string | null {+ const holding = holdings.find((h) => h.id === holdingId)+ return holding?.currency ?? editingTx?.currency ?? null+}++// Renders the total in its currency, or as a bare amount while the currency+// is still unknown.+export function formatTransactionTotal(+ total: number,+ currency: string | null,+ locale: string,+): string {+ if (currency) return formatCurrency(total, currency, locale)+ return new Intl.NumberFormat(locale, {+ minimumFractionDigits: 2,+ maximumFractionDigits: 2,+ }).format(total)+}diff --git a/frontend/src/pages/assets.tsx b/frontend/src/pages/assets.tsxindex 32ccf721..db09df6b 100644--- a/frontend/src/pages/assets.tsx+++ b/frontend/src/pages/assets.tsx@@ -59,6 +59,7 @@ import { useWorkspace } from '@/contexts/workspace-context'import { useCollectionFilter } from '@/contexts/collection-filter-context'import { getAssetProfit } from '@/lib/asset-profit'import { getPortfolioShare, getPortfolioTotalPrimary } from '@/lib/asset-portfolio-share'+import { formatTransactionTotal, getTransactionTotalCurrency } from '@/lib/asset-transaction-total'import { formatCurrency } from '@/lib/format'// Renders a logo image when one is available, falling back to the asset's@@ -2640,9 +2641,9 @@ function AssetTransactionsTab({<div className="flex items-center justify-between p-3 rounded-lg border border-border bg-muted/30"><span className="text-xs font-medium text-muted-foreground">{t('assets.txTotal')}</span><span className="text-sm font-bold tabular-nums text-foreground">- {formatCurrency(+ {formatTransactionTotal(parseFloat(formQuantity) * parseFloat(formPrice) + (formFee ? parseFloat(formFee) : 0) * (formKind === 'buy' ? 1 : -1),- 'USD',+ getTransactionTotalCurrency(holdings, formHolding, editingTx),locale,)}</span>