Skip to content

Open source personal finance application

fix: wallet balance computed in native currency, show small amounts

Ilya Komichev9a63eb4ebd4f
diff --git a/app/components/amount/getTotal.test.ts b/app/components/amount/getTotal.test.ts
index d5ef20be..f84cb3d9 100644
--- a/app/components/amount/getTotal.test.ts
+++ b/app/components/amount/getTotal.test.ts
@@ -246,16 +246,4 @@ describe('getWalletsTotals', () => {
expect(totals.get('walletCreditUSD')).toBe(-40)
})
- it('converts currencies when rates provided', () => {
- const totals = getWalletsTotals({
- baseCurrencyCode: 'USD',
- rates,
- trnsItems: {
- t1: trnsItems.transactionIncomeWalletOneRUB700!,
- },
- walletsItems,
- })
-
- expect(totals.get('walletOneRUB')).toBeCloseTo(10.81, 1)
- })
})
diff --git a/app/components/amount/getTotal.ts b/app/components/amount/getTotal.ts
index ea79e1c9..4ed609c7 100644
--- a/app/components/amount/getTotal.ts
+++ b/app/components/amount/getTotal.ts
@@ -125,41 +125,31 @@ export function getTotal(props: TotalProps): TotalReturns {
* For each wallet, returns: income - expense + transfers + adjustments.
*/
export function getWalletsTotals(props: {
- baseCurrencyCode?: CurrencyCode
- rates?: Rates
trnsItems: Trns
walletsItems: Wallets
}): Map<WalletId, number> {
- const { baseCurrencyCode, rates, trnsItems, walletsItems } = props
+ const { trnsItems, walletsItems } = props
const totals = new Map<WalletId, number>()
function addToWallet(walletId: WalletId, amount: number) {
totals.set(walletId, (totals.get(walletId) ?? 0) + amount)
}
- function convert(amount: number, currencyCode: CurrencyCode): number {
- return getAmountInRate({ amount, baseCurrencyCode, currencyCode, rates })
- }
-
for (const trnId of Object.keys(trnsItems)) {
const trn = trnsItems[trnId]
if (!trn)
continue
if (trn.type === TrnType.Income || trn.type === TrnType.Expense) {
- const wallet = walletsItems[trn.walletId]
- if (!wallet)
+ if (!walletsItems[trn.walletId])
continue
- const amount = convert(trn.amount, wallet.currency)
- addToWallet(trn.walletId, trn.type === TrnType.Income ? amount : -amount)
+ addToWallet(trn.walletId, trn.type === TrnType.Income ? trn.amount : -trn.amount)
}
else if (trn.type === TrnType.Transfer && 'incomeWalletId' in trn) {
- const incomeWallet = walletsItems[trn.incomeWalletId]
- const expenseWallet = walletsItems[trn.expenseWalletId]
- if (!incomeWallet || !expenseWallet)
+ if (!walletsItems[trn.incomeWalletId] || !walletsItems[trn.expenseWalletId])
continue
- addToWallet(trn.incomeWalletId, convert(trn.incomeAmount, incomeWallet.currency))
- addToWallet(trn.expenseWalletId, -convert(trn.expenseAmount, expenseWallet.currency))
+ addToWallet(trn.incomeWalletId, trn.incomeAmount)
+ addToWallet(trn.expenseWalletId, -trn.expenseAmount)
}
}
diff --git a/app/components/amount/utils.ts b/app/components/amount/utils.ts
index 5f99b8ca..98216108 100644
--- a/app/components/amount/utils.ts
+++ b/app/components/amount/utils.ts
@@ -18,12 +18,20 @@ function getFormatter(precision: number) {
}
export function formatAmount(amount: number, currencyCode?: CurrencyCode, {
- precision = 0,
-} = {}) {
+ precision,
+}: { precision?: number } = {}) {
const currencySettings = currencyCode ? currencyMap.get(currencyCode) : undefined
const p = precision ?? currencySettings?.precision ?? 2
- return getFormatter(p).format(amount).replace(/,/g, ' ')
+ const formatted = getFormatter(p).format(amount)
+
+ // If rounding hides a non-zero value, show significant digits
+ if (amount !== 0 && Number(formatted.replace(/,/g, '')) === 0) {
+ const significant = getFormatter(Math.min(Math.max(-Math.floor(Math.log10(Math.abs(amount))) + 1, p), 20))
+ return significant.format(amount).replace(/,/g, ' ')
+ }
+
+ return formatted.replace(/,/g, ' ')
}
export function getCurrencySymbol(currencyCode?: CurrencyCode) {
diff --git a/app/components/wallets/useWalletsStore.ts b/app/components/wallets/useWalletsStore.ts
index e2c78b63..dcc02b75 100644
--- a/app/components/wallets/useWalletsStore.ts
+++ b/app/components/wallets/useWalletsStore.ts
@@ -193,8 +193,6 @@ export const useWalletsStore = defineStore('wallets', () => {
// O(N) single pass over all trns — replaces O(W×N) per-wallet getWalletTotal
const walletTotals = computed(() =>
getWalletsTotals({
- baseCurrencyCode: currenciesStore.base,
- rates: currenciesStore.rates,
trnsItems: trnsStore.items ?? {},
walletsItems: items.value ?? {},
}),
diff --git a/package.json b/package.json
index ec712fbd..3d31a63e 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
{
"name": "@ilkome/finapp",
"type": "module",
- "version": "7.0.0-beta.10",
+ "version": "7.0.0-beta.11",
"packageManager": "[email protected]",
"description": "Finapp helps you easily track and manage personal finances.",
"author": "Ilya Komichev @ilkome",