feat(folio): add computeHomeTotalBalance helper

Pure helper that tracks Home multi-account total only and carries the last
Home sum across off-Home steps. Ledger's single-account balance is excluded
because mixing it would corrupt cross-screen scale comparisons.
This commit is contained in:
pj committed 2026-05-31 21:15:16 +05:30
1 parent 9647d5e042
commit 5a5b15e89e
1 file changed
+22
+22
View File
@@ -1,3 +1,25 @@
// Computes the Home-screen total balance from the visible AccountCard balances.
// When no Home cards are visible (Ledger, AddTransaction, etc.) the carrier
// value is returned so the property compares apples to apples across screen
// transitions. The Ledger LedgerBalance is intentionally ignored because it is
// a single-account number on a different scale than the Home multi-account sum.
export function computeHomeTotalBalance(args: {
cardBalanceTexts: (string | undefined)[];
previousCarrier: number;
}): number {
const { cardBalanceTexts, previousCarrier } = args;
if (cardBalanceTexts.length === 0) return previousCarrier;
return cardBalanceTexts.reduce((sum, text) => sum + parseDollarCents(text), 0);
}
// Parses formatCents output like "$5.00", "-$1,234.56", "+$0.50" back to integer cents.
function parseDollarCents(text: string | undefined): number {
if (!text) return 0;
const sign = text.startsWith("-") ? -1 : 1;
const digits = text.replace(/[^0-9]/g, "");
return digits ? sign * parseInt(digits, 10) : 0;
}
// Parses raw user input in the transaction amount field into integer cents.
// Mirrors the Folio app's parseCents: whole numbers like "50" become 5000
// cents, decimals like "5.50" become 550, more than 2 decimals or non-numeric