Skip to content

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

fix(dashboard): count a shared credit line once in the card total (#1025)

Albert Dolkunf5ef97fcbfc6
diff --git a/frontend/src/components/app-layout.tsx b/frontend/src/components/app-layout.tsx
index 8d023e11..46b5952e 100644
--- a/frontend/src/components/app-layout.tsx
+++ b/frontend/src/components/app-layout.tsx
@@ -1,5 +1,5 @@
import { useState, useCallback, useEffect, useMemo, lazy, Suspense } from 'react'
-import { getAccountName } from '@/lib/account-utils'
+import { getAccountName, sumAccountBalances } from '@/lib/account-utils'
import { Link, Outlet, useLocation, useNavigate } from 'react-router-dom'
import { useTranslation } from 'react-i18next'
import { useDisplayLocale } from '@/hooks/use-display-locale'
@@ -205,14 +205,7 @@ export function AppLayout() {
const visibleAccounts = activeAccountIds
? allAccounts.filter((a) => activeAccountIds.includes(a.id))
: allAccounts
- const sharedBalanceGroups = new Set<string>()
- const totalBalance = visibleAccounts.reduce((sum, a) => {
- if (a.shared_balance_group) {
- if (sharedBalanceGroups.has(a.shared_balance_group)) return sum
- sharedBalanceGroups.add(a.shared_balance_group)
- }
- return sum + Number(a.balance_primary ?? a.current_balance)
- }, 0)
+ const totalBalance = sumAccountBalances(visibleAccounts)
const versionA11yLabel = t('app.versionAriaLabel', { version: APP_VERSION })
return (
diff --git a/frontend/src/lib/account-utils.ts b/frontend/src/lib/account-utils.ts
index 1a16b12d..95da8f26 100644
--- a/frontend/src/lib/account-utils.ts
+++ b/frontend/src/lib/account-utils.ts
@@ -1,7 +1,27 @@
+import type { Account } from '@/types'
+
export function getAccountName(account: { name: string; display_name?: string | null }): string {
return account.display_name ?? account.name
}
+/**
+ * Total of the accounts' primary-currency balances. Cards on one shared credit
+ * line each report the whole line's balance, so each `shared_balance_group`
+ * counts once, the same way the backend totals it.
+ */
+export function sumAccountBalances(
+ accounts: readonly Pick<Account, 'balance_primary' | 'current_balance' | 'shared_balance_group'>[],
+): number {
+ const seenGroups = new Set<string>()
+ return accounts.reduce((sum, a) => {
+ if (a.shared_balance_group) {
+ if (seenGroups.has(a.shared_balance_group)) return sum
+ seenGroups.add(a.shared_balance_group)
+ }
+ return sum + Number(a.balance_primary ?? a.current_balance)
+ }, 0)
+}
+
/**
* Return a presentation-only copy ordered by the name users see.
* The input is never mutated, which keeps React Query's cached account list intact.
diff --git a/frontend/src/pages/dashboard.test.tsx b/frontend/src/pages/dashboard.test.tsx
new file mode 100644
index 00000000..44c4d367
--- /dev/null
+++ b/frontend/src/pages/dashboard.test.tsx
@@ -0,0 +1,138 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { screen, within } from '@testing-library/react'
+
+import DashboardPage from '@/pages/dashboard'
+import { TooltipProvider } from '@/components/ui/tooltip'
+import { formatCurrency } from '@/lib/format'
+import { renderWithProviders, t } from '@/test/utils'
+import type { Account, DashboardSummary } from '@/types'
+
+const api = vi.hoisted(() => ({
+ dashboard: {
+ summary: vi.fn(),
+ spendingByCategory: vi.fn(),
+ balanceHistory: vi.fn(),
+ projectedTransactions: vi.fn(),
+ },
+ transactions: { list: vi.fn(), calendar: vi.fn() },
+ budgets: { comparison: vi.fn() },
+ categories: { list: vi.fn() },
+ categoryGroups: { list: vi.fn() },
+ accounts: { list: vi.fn() },
+ goals: { summary: vi.fn() },
+ groups: { list: vi.fn() },
+ payees: { list: vi.fn() },
+ rules: { create: vi.fn() },
+}))
+
+vi.mock('@/lib/api', () => api)
+
+vi.mock('@/hooks/use-display-locale', () => ({
+ useDisplayLocale: () => 'en-US',
+ useDateLocale: () => 'en-US',
+}))
+
+vi.mock('@/hooks/use-privacy-mode', () => ({
+ usePrivacyMode: () => ({ mask: (value: string) => value, privacyMode: false, MASK: '••••' }),
+}))
+
+vi.mock('@/contexts/auth-context', () => ({
+ useAuth: () => ({ user: { preferences: { currency_display: 'USD' } } }),
+}))
+
+vi.mock('@/contexts/collection-filter-context', () => ({
+ useCollectionFilter: () => ({ activeAccountIds: null, activeWalletIds: null }),
+}))
+
+function account(overrides: Partial<Account>): Account {
+ return {
+ id: 'acc',
+ user_id: 'user-1',
+ connection_id: null,
+ external_id: null,
+ name: 'Account',
+ display_name: null,
+ masked_number: null,
+ institution_name: null,
+ institution_logo_url: null,
+ type: 'checking',
+ balance: 0,
+ current_balance: 0,
+ previous_balance: null,
+ balance_primary: null,
+ currency: 'USD',
+ credit_limit: null,
+ available_credit: null,
+ statement_close_day: null,
+ payment_due_day: null,
+ next_close_date: null,
+ next_due_date: null,
+ minimum_payment: null,
+ card_brand: null,
+ card_level: null,
+ shared_balance_group: null,
+ is_closed: false,
+ closed_at: null,
+ ...overrides,
+ }
+}
+
+// Two cards on one shared credit line both report the line's full -300.
+const accounts = [
+ account({ id: 'checking', name: 'Checking', current_balance: 1000 }),
+ account({ id: 'card-a', name: 'Card A', type: 'credit_card', current_balance: -300, shared_balance_group: 'line-1' }),
+ account({ id: 'card-b', name: 'Card B', type: 'credit_card', current_balance: -300, shared_balance_group: 'line-1' }),
+ account({ id: 'card-c', name: 'Card C', type: 'credit_card', current_balance: -200 }),
+]
+
+const summary: DashboardSummary = {
+ total_balance: { USD: 500 },
+ total_balance_primary: 500,
+ projected_balance: { USD: 500 },
+ projected_balance_primary: 500,
+ balance_date: '2026-09-12',
+ monthly_income: 0,
+ monthly_expenses: 0,
+ monthly_income_primary: 0,
+ monthly_expenses_primary: 0,
+ accounts_count: accounts.length,
+ pending_categorization: 0,
+ pending_categorization_amount: 0,
+ assets_value: {},
+ assets_value_primary: 0,
+ primary_currency: 'USD',
+ pending_shares_net: 0,
+}
+
+beforeEach(() => {
+ vi.clearAllMocks()
+ api.dashboard.summary.mockResolvedValue(summary)
+ api.dashboard.spendingByCategory.mockResolvedValue([])
+ api.dashboard.balanceHistory.mockResolvedValue({ current: [], previous: [] })
+ api.dashboard.projectedTransactions.mockResolvedValue([])
+ api.transactions.list.mockResolvedValue({ items: [], total: 0 })
+ api.budgets.comparison.mockResolvedValue([])
+ api.categories.list.mockResolvedValue([])
+ api.categoryGroups.list.mockResolvedValue([])
+ api.accounts.list.mockResolvedValue(accounts)
+ api.goals.summary.mockResolvedValue([])
+ api.groups.list.mockResolvedValue([])
+ api.payees.list.mockResolvedValue([])
+})
+
+describe('Dashboard net worth breakdown', () => {
+ it('counts a shared credit line once in the credit card total', async () => {
+ const { user } = renderWithProviders(
+ <TooltipProvider delayDuration={0}>
+ <DashboardPage />
+ </TooltipProvider>,
+ )
+
+ await screen.findByText(formatCurrency(500, 'USD', 'en-US'))
+ await user.hover(screen.getByRole('button', { name: 'i' }))
+
+ const tooltip = await screen.findByRole('tooltip')
+ const cardRow = within(tooltip).getByText(t('dashboard.creditCardBalance')).parentElement!
+ expect(cardRow).toHaveTextContent(formatCurrency(-500, 'USD', 'en-US'))
+ })
+})
diff --git a/frontend/src/pages/dashboard.tsx b/frontend/src/pages/dashboard.tsx
index 8d3af2fb..9733621a 100644
--- a/frontend/src/pages/dashboard.tsx
+++ b/frontend/src/pages/dashboard.tsx
@@ -1,5 +1,5 @@
import { useState, useEffect, useMemo, useRef } from 'react'
-import { getAccountLabel, getAccountName } from '@/lib/account-utils'
+import { getAccountLabel, getAccountName, sumAccountBalances } from '@/lib/account-utils'
import { currentMonth, shiftMonth, monthLastDay, monthLabel, monthRange } from '@/lib/month-utils'
import { useTranslation } from 'react-i18next'
import { useDisplayLocale, useDateLocale } from '@/hooks/use-display-locale'
@@ -408,9 +408,8 @@ export default function DashboardPage() {
// While accounts are loading or failed to load, treat their balance
// components as unavailable rather than silently rendering zero.
const accountsUnavailable = accountsLoading || accountsError
- const creditCardBalance = (accountsList ?? [])
- .filter((a) => (activeAccountIds ? activeAccountIds.includes(a.id) : true) && a.type === 'credit_card')
- .reduce((sum, a) => sum + Number(a.balance_primary ?? a.current_balance), 0)
+ const creditCardBalance = sumAccountBalances((accountsList ?? [])
+ .filter((a) => (activeAccountIds ? activeAccountIds.includes(a.id) : true) && a.type === 'credit_card'))
// Net worth's "Available balance" breakdown row: every non-card account
// (unlike the headline `availableBalance`, which is checking/savings only),
// so it reconciles with `totalBalance` — which sums all account types.