From b7dc3a6f57cf0204e226235cd1323128cc7b6a20 Mon Sep 17 00:00:00 2001 From: minimus7 Date: Tue, 10 Nov 2026 21:39:15 -0600 Subject: [PATCH] Budgets: Suggest offers changes to budgets that are well off from real spending (#3) The Suggest sheet said "Nothing to suggest right now." once every main category had a budget. It now lists budgets $25 and a fifth or more away from the suggested amount as current -> suggested, then main categories with no budget. Apply all saves them, each row can be edited or removed first, and the sheet carries the page's Ask AI to tune my budget button. Co-authored-by: Claude Opus 5.5 --- src/components/BudgetEditor.tsx | 26 +++++++++++++++++--------- src/lib/budget.test.ts | 15 ++++++++++++++- src/lib/budget.ts | 10 ++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/components/BudgetEditor.tsx b/src/components/BudgetEditor.tsx index f0c445e..1d9117f 100644 --- a/src/components/BudgetEditor.tsx +++ b/src/components/BudgetEditor.tsx @@ -13,7 +13,7 @@ import { PageDates } from "@/components/PageDates"; import { CATEGORY_ICON_KEYS, CategoryIcon } from "@/components/Icons"; import { CountUp, EASE, firstVisit, reduced } from "@/components/motion"; import { toast } from "@/components/Toast"; -import { budgetContext, daysBetween, merge, month, perDay, scaleBudget, startPeriod, step, tone, yearView, type MonthSpend } from "@/lib/budget"; +import { budgetContext, daysBetween, merge, month, perDay, retune, scaleBudget, startPeriod, step, tone, yearView, type MonthSpend } from "@/lib/budget"; import { useMoney } from "@/components/Money"; export type BudgetRange = { from: string; to: string; start: string; end: string; spent: Record }; @@ -93,10 +93,13 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in // Fills main categories that have no budget yet; amounts you set stay. const suggestion = cats.filter((c) => c.main && !(c.id in budgets)).sort((a, b) => b.suggested - a.suggested); + // The Suggest sheet: budgets well off from real spending, plus main categories with none yet. + const retuned = retune(cats, budgets); // Saves exactly what the suggestion list shows after any edits. - const useSuggested = (picked: Record) => { + const saveSuggested = (picked: Record, verb = "Added") => { setSheet(null); - apply(picked, `Added ${Object.keys(picked).length} budgets`); + const n = Object.keys(picked).length; + apply(picked, `${verb} ${n} ${n === 1 ? "budget" : "budgets"}`); }; const sheets = ( @@ -123,7 +126,10 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in c.label)} onDone={(id, label, amount) => { setSheet(null); setBudgets((x) => ({ ...x, [id]: amount })); setFresh(id); toast(`Added ${label}`); }} /> setSheet(null)} title="Suggested budgets"> - c.id).join()} cats={suggestion} open={open} income={0} basis={basis} onUse={useSuggested} disabled={error === SETUP} sticky /> + {/* Each open starts from the latest numbers. The AI button closes the sheet so the assistant isn't behind it. */} + c.id).join()}`} cats={retuned} open={open} current={budgets} income={0} basis={basis} onUse={(p) => saveSuggested(p, "Updated")} disabled={error === SETUP} sticky + empty="Your budgets are close to what you really spend." + alt={aside &&
setSheet(null)}>{aside}
} />
); @@ -136,7 +142,7 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in

Set a budget from how you really spend

{basis} Press an amount to change it. Remove any you don't want, or add another.

- setSheet("add")}>Add one at a time} /> + setSheet("add")}>Add one at a time} /> {error &&
} {sheets} @@ -561,13 +567,14 @@ function Editor({ label, value, onSave, onCancel, onDelete }: { label: string; v // The suggested budgets, the usual month per category, as a draft: change an amount, remove a row or add a category // before saving. Nothing saves until Use these, which saves exactly the rows shown. -function Suggest({ cats, open, income, basis, onUse, disabled, alt, sticky }: { cats: BudgetCat[]; open: BudgetCat[]; income: number; basis: string; onUse: (picked: Record) => void; disabled: boolean; alt?: React.ReactNode; sticky?: boolean }) { +// With `current` (budgets you already have), a row with a budget shows the change: $650 → $350. +function Suggest({ cats, open, current, income, basis, onUse, disabled, alt, sticky, empty = "Nothing to suggest right now." }: { cats: BudgetCat[]; open: BudgetCat[]; current?: Record; income: number; basis: string; onUse: (picked: Record) => void; disabled: boolean; alt?: React.ReactNode; sticky?: boolean; empty?: string }) { const { money0, symbol } = useMoney(); const [draft, setDraft] = useState>(() => Object.fromEntries(cats.map((c) => [c.id, c.suggested]))); const set = (id: string, n: number | null) => setDraft((d) => merge(d, { [id]: n })); - const rows = Object.keys(draft).map((id) => open.find((c) => c.id === id)).filter((c) => !!c); + const rows = Object.keys(draft).map((id) => cats.find((c) => c.id === id) ?? open.find((c) => c.id === id)).filter((c) => !!c); const total = Object.values(draft).reduce((s, n) => s + n, 0); - if (!cats.length) return

Nothing to suggest right now.

{alt}
; + if (!cats.length) return

{empty}

{alt}
; return ( <>
    @@ -575,6 +582,7 @@ function Suggest({ cats, open, income, basis, onUse, disabled, alt, sticky }: {
  • {c.label} + {current && c.id in current && {money0(current[c.id])} →} set(c.id, n)} onType={(n) => set(c.id, n)} />
  • @@ -585,7 +593,7 @@ function Suggest({ cats, open, income, basis, onUse, disabled, alt, sticky }: {

    {basis} Never less than you've already spent this month.

    {income > 0 &&

    You bring in about {money0(income)} a month. {income >= total ? `This leaves ${money0(income - total)} to save.` : `You're spending ${money0(total - income)} more than you earn.`}

    }
    - + {alt}
    diff --git a/src/lib/budget.test.ts b/src/lib/budget.test.ts index 1215bc7..81bd847 100644 --- a/src/lib/budget.test.ts +++ b/src/lib/budget.test.ts @@ -1,6 +1,6 @@ // Run: node src/lib/budget.test.ts import assert from "node:assert/strict"; -import { budgetChanges, daysBetween, scaleBudget, startPeriod, yearView, merge, month, perDay, roundUp, step, suggest, tone } from "./budget.ts"; +import { budgetChanges, retune, wellOff, daysBetween, scaleBudget, startPeriod, yearView, merge, month, perDay, roundUp, step, suggest, tone } from "./budget.ts"; assert.equal(roundUp(0), 0); assert.equal(roundUp(42.1), 50); @@ -68,3 +68,16 @@ assert.equal(med([100, 200]), 150); assert.equal(med([]), 0); assert.equal(ml(["2026-10", "2026-08", "2026-09"]), "August, September and October"); assert.equal(ml(["2026-10"]), "October"); + +assert.equal(wellOff(650, 350), true); +assert.equal(wellOff(100, 110), false); // $10 gap +assert.equal(wellOff(1600, 1650), false); // $50 is under a fifth +assert.equal(wellOff(100, 0), false); // nothing spent: leave it +{ + const cats = [ + { id: "a", suggested: 350, main: true }, { id: "b", suggested: 300, main: true }, { id: "c", suggested: 100, main: true }, + { id: "d", suggested: 80, main: true }, { id: "e", suggested: 200, main: true }, { id: "f", suggested: 5, main: false }, + ]; + assert.deepEqual(retune(cats, { a: 650, b: 100, c: 105 }).map((c) => c.id), ["a", "b", "e", "d"]); + assert.deepEqual(retune(cats, { a: 350, b: 300, c: 100, d: 80, e: 200 }), []); +} diff --git a/src/lib/budget.ts b/src/lib/budget.ts index 66a1a18..b339280 100644 --- a/src/lib/budget.ts +++ b/src/lib/budget.ts @@ -23,6 +23,16 @@ export function monthList(months: string[]) { // "Use these" never puts the user over budget on day one. export const suggest = (avg: number, spentThisMonth: number) => roundUp(Math.max(avg, spentThisMonth)); +// A budget is "well off" when it is $25 and a fifth or more away from the suggested amount. Small gaps are noise. +export const wellOff = (budget: number, suggested: number) => suggested > 0 && Math.abs(budget - suggested) >= Math.max(25, budget * 0.2); + +// What the Suggest sheet offers: budgets well off from real spending (biggest gap first), then main categories with no budget. +export function retune(cats: C[], budgets: Record): C[] { + const off = cats.filter((c) => c.id in budgets && wellOff(budgets[c.id], c.suggested)).sort((a, b) => Math.abs(b.suggested - budgets[b.id]) - Math.abs(a.suggested - budgets[a.id])); + const fresh = cats.filter((c) => c.main && !(c.id in budgets)).sort((a, b) => b.suggested - a.suggested); + return [...off, ...fresh]; +} + // Where the month is: today counts as a day left. export function month(now: Date) { const day = now.getUTCDate();