Open-source personal finance manager. Self-hosted, privacy-first.
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)
}
/**
* The accounts the sidebar lists. Loans and financing are long-term debt, not
* money to move day to day: they live on the Accounts page and in net worth.
*/
export function sidebarAccounts<T extends Pick<Account, 'type'>>(accounts: readonly T[]): T[] {
return accounts.filter((account) => account.type !== 'loan')
}
/**
* The number a sidebar account row shows.
*
* Every card on one shared credit line repeats the contract-wide balance, so
* the row for such a card leads with its own open bill (the one due today or
* later) instead; the "shared" label next to the name still identifies the
* line. Without an open bill, and for every other account, the balance stands.
* The returned amount is signed like a balance: money owed is negative.
*/
export function sidebarAccountBalance(
account: Pick<
Account,
| 'type'
| 'shared_balance_group'
| 'balance_primary'
| 'current_balance'
| 'open_bill_total'
| 'open_bill_total_primary'
>,
): { amount: number; isPrimary: boolean } {
const sharedCard =
account.type === 'credit_card' && account.shared_balance_group != null
if (sharedCard && account.open_bill_total != null) {
const raw = account.open_bill_total_primary ?? account.open_bill_total
// A bill is an amount to pay, so the row shows it as debt.
return {
amount: -Number(raw) || 0,
isPrimary: account.open_bill_total_primary != null,
}
}
return {
amount: Number(account.balance_primary ?? account.current_balance) || 0,
isPrimary: account.balance_primary != null,
}
}
/**
* The accounts total the sidebar shows. Loans are long-term debt that the
* net worth already counts, so they stay out of this money-on-hand total.
*/
export function sumAccountsTotal(
accounts: readonly Pick<Account, 'type' | 'balance_primary' | 'current_balance' | 'shared_balance_group'>[],
): number {
return sumAccountBalances(accounts.filter((account) => account.type !== 'loan'))
}
/**
* 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.
*/
export function sortAccountsByDisplayName<
T extends { name: string; display_name?: string | null },
>(accounts: readonly T[]): T[] {
return [...accounts].sort((left, right) =>
getAccountName(left).localeCompare(getAccountName(right), undefined, {
numeric: true,
sensitivity: 'base',
}),
)
}
/**
* Return a presentation-only copy with the largest account balances first.
* Debt accounts participate by magnitude, matching the balance list in the
* sidebar, while invalid/missing balances sort as zero.
* Pass a selector to compare multi-currency accounts in a common currency.
*/
export function sortAccountsByAbsoluteBalance<
T extends { current_balance?: number | string | null },
>(
accounts: readonly T[],
getBalance: (account: T) => number | string | null | undefined = (account) => account.current_balance,
): T[] {
return [...accounts].sort((left, right) => {
const leftBalance = Number(getBalance(left)) || 0
const rightBalance = Number(getBalance(right)) || 0
return Math.abs(rightBalance) - Math.abs(leftBalance)
})
}
/**
* The bank's identifier for an account, masked to its last 4 chars, e.g. "•••• 1234".
*
* Banks commonly report every account under the same label (often the holder's
* name), so this is what tells two of them apart. Null when the provider gave us
* no identifier, so callers render nothing rather than an empty mask. Locale-neutral
* by construction: dots and the bank's own digits, nothing to translate.
*/
export function formatAccountMask(account: { masked_number?: string | null }): string | null {
return account.masked_number ? `•••• ${account.masked_number}` : null
}
/**
* Account name with its mask appended, e.g. "Checking •••• 1234", for compact
* single-line surfaces such as the account <select> options, where there is no
* room for a secondary line. When the account has an explicit display_name, the
* mask is omitted since the user-chosen label already distinguishes it.
*/
export function getAccountLabel(account: {
name: string
display_name?: string | null
masked_number?: string | null
}): string {
const name = getAccountName(account)
// When the account has an explicit display_name the user set, it already
// distinguishes this account from others so the mask suffix is redundant.
if (account.display_name) return name
const mask = formatAccountMask(account)
return mask ? `${name} ${mask}` : name
}