Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Pocketbook

Personal finance PWA: a React + Vite frontend backed by Supabase (Postgres + auth). A Google Apps Script reads Discovery Bank emails from Gmail and saves each transaction to Supabase. Deployed to GitHub Pages under /pocketbook/.

Running it

npm install
npm run dev        # http://localhost:5173
npm run build      # production build (PWA)
npm run preview    # serve the build locally
npm run deploy     # publish dist/ to gh-pages

.env must contain (Supabase → Project Settings → API):

VITE_SUPABASE_URL=https://<ref>.supabase.co
VITE_SUPABASE_ANON_KEY=<anon / publishable key>

The anon key is meant to be public; row level security keeps the data private. Without these the app runs on mock data (writes are disabled). Localhost uses the real database.

Architecture

  • Styling — no CSS framework; inline styles fed by theme tokens (src/theme/tokens.js). Light/dark themes served through src/theme/ThemeContext.jsx (useTheme() → { T, mode, preference, setPreference }); preference persists in localStorage pb:themePref, default follows the OS.
  • Layout — src/components/Layout.jsx: sidebar on desktop, bottom nav on mobile, breakpoint via useIsMobile() (src/hooks/useMediaQuery.js).
  • Data — src/hooks/useSheetData.js loads every table through src/api/sheet.js (Supabase client; it hands rows back in the old sheet column order so the parsers didn't change) and exposes optimistic write actions.
  • Tags — per-transaction tags live in localStorage behind a reactive store (src/hooks/useTags.js). Tagging a transaction One-off/Holiday keeps it in actuals but excludes it from spend projections (src/utils/projection.js).
  • Splits — shared purchases live in localStorage (pb:splits, src/utils/splits.js): an expense is linked to the repayment row that pays it back, and both sides net off.
  • Feature utilities — src/utils/insights.js (category 3-month average, merchant stats, top movers, compareMonths), src/utils/recurring.js (subscription detection + left-to-spend inputs), src/utils/fixed.js (recurring template → synthetic monthly transactions), src/utils/bankImport.js (bank CSV parse + match plan), src/utils/aiSummary.js (totals-only payload for the AI card).

Money model

Everything on the Overview follows one rule:

  • Income is recurring (salary etc.). Reimbursements are not income.
  • Spent = credit-card spend + fixed/recurring spend.
  • Left over = income − spent, and that is the savings rate.
  • Investments are an allocation of the left over, never spend.
  • Transfers and refunds aren't spend either; a refund lowers the month it lands in.

Categories Income, Transfer, Reimbursement and Investments are excluded from spend everywhere (NON_SPEND_CATEGORIES in src/utils/compute.js).

Tabs

Tab What it shows
Overview dashboard widgets, income / spent / left over, where the left over went
Spending credit-card only — date slicer, monthly chart, transaction list, bank CSV import, manual "Add expense"
Recurring the Fixed template: bills, recurring income and recurring investments, each with From/Until months
Analysis AI Insights card, Compare Months table (a column per month), movers
Planning / Investments / Settings goals + envelopes, holdings, config

Database (supabase/schema.sql)

Table Columns Written by
transactions date, vendor, amount, currency, category, subcategory, amount_zar, card Gmail ingest, manual add, bank CSV import
budgets category, monthly_limit Settings → doubles as envelope allocations
assets asset, value, type (asset/liability) manual (Supabase table editor)
settings key, value (envelopeMode, stackedChart, aiProvider, aiModel, …) Settings screen
goals name, target, saved, deadline, icon Settings screen
watchlists name, type (category/vendor), match, monthly_limit Settings screen
net_worth_history date, assets, liabilities, net snapshot_net_worth(), 1st of each month via pg_cron
mapping key, category, subcategory in-app teach loop
fixed name, amount, category, subcategory, day, active, start_month, end_month Recurring tab. Category Income = recurring income, Investments = recurring investment, anything else = a bill
investments ticker, name, units, price, value, notes manual. Prices don't update by themselves (no GOOGLEFINANCE any more)

Every table is locked by row level security to users listed in members. Whole-list saves (budgets, goals, watchlists, fixed) go through replace_rows(), which deletes and re-inserts in one transaction.

Auto-categorization

A database trigger fills a blank category on every new transaction from mapping: case-insensitive substring, longest key first — so woolworths catches every branch and a short key like pharm acts as a keyword rule. Refunds keep their Refund subcategory. Unmatched transactions show in the app's "uncategorised — review" banner (Spending); teaching a category there calls add_mapping(), which saves the rule and backfills all matching rows.

Setup

  1. Database: Supabase → SQL Editor → paste supabase/schema.sql → Run. Test it locally first with supabase/tests/run.sh (needs Docker).
  2. Your login: Authentication → Users → Add user. Then in the SQL Editor: insert into members (user_id) select id from auth.users where email = 'you@example.com'; Also turn off Authentication → Sign In / Providers → "Allow new users to sign up".
  3. AI card: npx supabase login, then npx supabase functions deploy ai-analyse --project-ref <ref> and npx supabase secrets set ANTHROPIC_API_KEY=... --project-ref <ref> (and/or OPENROUTER_API_KEY; optional AI_DAILY_LIMIT, default 50).
  4. Gmail ingest (below), then run migrateToSupabase() once to copy the Google Sheet over.

Gmail ingest (apps-script/Code.js, pushed with clasp)

One-time setup:

  1. Turn on the Apps Script API at https://script.google.com/home/usersettings
  2. npm run script:login (opens Google sign-in; credentials stay in your home folder).
  3. Copy .clasp.json.example to .clasp.json and paste the Script ID (Apps Script → Project Settings → IDs). .clasp.json is git-ignored.
  4. In Apps Script → Project Settings → Script properties, add SUPABASE_URL and SUPABASE_SERVICE_KEY (the service_role key — it skips row level security, so it lives only here).

After every script change: npm run script:push. The script is no longer a web app, so there's nothing to deploy. checkEmails runs on a 5-minute trigger.

Sign-in

Supabase email + password. Sessions refresh automatically and stay on the device. Settings → Account → "Sign out on all devices" ends every session. AI requests are capped per day by take_ai_quota().

Gotchas

  • Spend math excludes Income, Transfer, Reimbursement and Investments (money movement or allocation, not consumption) — see the money model above.
  • Bank reconciliation: Discovery's "Total spend" is all money out — card, fees, interest, EFT and FX transfers, prepaid electricity/airtime and pending — minus card refunds. The app counts EFT/FX as Transfer, so its spend number is deliberately lower than the bank's.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages