feat: initial finance SPA — phases 1 & 2

Next.js 16 personal finance dashboard connected to postgres-personal.

Phase 1 (Foundation):
- API routes: GET /api/transactions (paginated, filterable, sortable),
  GET /api/statements, GET /api/merchants
- Transactions data table with date/category/bank/search filters, pagination, sort
- Statements card grid with period, due date, amount, transaction count
- Sidebar layout with nav for all planned sections

Phase 2 (Normalisation):
- PATCH /api/transactions/[id] — upsert transaction_overrides
- POST /api/transactions/bulk — bulk categorize/normalize
- Inline click-to-edit category (22 options) and merchant name
- Blue dot override indicator, bulk action bar
- Effective values via COALESCE(override, llm_value) pattern

Stack: Next.js 16 (App Router, standalone), Prisma 7.x + @prisma/adapter-pg,
TanStack Query, Tailwind CSS. Auth via Traefik chain-oauth@file.
This commit is contained in:
2026-03-07 23:31:40 +11:00
parent 28207b42b5
commit 35a5be97b0
31 changed files with 2243 additions and 99 deletions
+358
View File
@@ -0,0 +1,358 @@
"use client";
import { useState, useCallback } from "react";
import { useTransactions, useBanks, useUpdateTransaction, useBulkAction } from "@/lib/hooks";
import { CATEGORIES, formatCategory } from "@/lib/categories";
function formatDate(d: string) {
return new Date(d).toLocaleDateString("en-AU", {
day: "2-digit",
month: "short",
year: "numeric",
});
}
function formatAmount(amount: number, type: string) {
const formatted = new Intl.NumberFormat("en-AU", {
style: "currency",
currency: "AUD",
}).format(amount);
return type === "debit" ? formatted : `+${formatted}`;
}
function TypeBadge({ type }: { type: string }) {
const colors: Record<string, string> = {
debit: "bg-red-900/30 text-red-400",
credit: "bg-green-900/30 text-green-400",
payment: "bg-blue-900/30 text-blue-400",
refund: "bg-emerald-900/30 text-emerald-400",
fee: "bg-yellow-900/30 text-yellow-400",
interest: "bg-orange-900/30 text-orange-400",
};
return (
<span className={`px-2 py-0.5 rounded text-xs font-medium ${colors[type] || "bg-zinc-800 text-zinc-400"}`}>
{type}
</span>
);
}
function InlineEdit({
value,
onSave,
type = "text",
options,
}: {
value: string;
onSave: (val: string) => void;
type?: "text" | "select";
options?: { value: string; label: string }[];
}) {
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState(value);
if (!editing) {
return (
<button
onClick={() => { setDraft(value); setEditing(true); }}
className="text-left hover:bg-zinc-800 px-1 -mx-1 rounded cursor-pointer transition-colors w-full truncate"
title="Click to edit"
>
{value || <span className="text-zinc-600 italic">unset</span>}
</button>
);
}
const commit = () => {
if (draft !== value) onSave(draft);
setEditing(false);
};
if (type === "select" && options) {
return (
<select
autoFocus
value={draft}
onChange={(e) => { setDraft(e.target.value); }}
onBlur={commit}
className="bg-zinc-800 border border-zinc-600 rounded px-1 py-0.5 text-sm w-full"
>
{options.map((o) => (
<option key={o.value} value={o.value}>{o.label}</option>
))}
</select>
);
}
return (
<input
autoFocus
value={draft}
onChange={(e) => setDraft(e.target.value)}
onBlur={commit}
onKeyDown={(e) => { if (e.key === "Enter") commit(); if (e.key === "Escape") setEditing(false); }}
className="bg-zinc-800 border border-zinc-600 rounded px-1 py-0.5 text-sm w-full"
/>
);
}
export default function TransactionsPage() {
const [filters, setFilters] = useState({
from: "",
to: "",
category: "",
bank_name: "",
search: "",
sort_by: "transaction_date",
sort_dir: "desc",
limit: 50,
offset: 0,
});
const [selected, setSelected] = useState<Set<number>>(new Set());
const [bulkCategory, setBulkCategory] = useState("");
const { data, isLoading } = useTransactions(filters);
const { data: banks } = useBanks();
const updateTxn = useUpdateTransaction();
const bulkAction = useBulkAction();
const toggleSelect = useCallback((id: number) => {
setSelected((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
}, []);
const toggleAll = useCallback(() => {
if (!data?.data) return;
setSelected((prev) => {
if (prev.size === data.data.length) return new Set();
return new Set(data.data.map((t) => t.id));
});
}, [data]);
const setPage = (newOffset: number) => {
setFilters((f) => ({ ...f, offset: newOffset }));
setSelected(new Set());
};
const toggleSort = (col: string) => {
setFilters((f) => ({
...f,
sort_by: col,
sort_dir: f.sort_by === col && f.sort_dir === "desc" ? "asc" : "desc",
offset: 0,
}));
};
const categoryOptions = CATEGORIES.map((c) => ({ value: c, label: formatCategory(c) }));
const totalPages = data ? Math.ceil(data.total / filters.limit) : 0;
const currentPage = Math.floor(filters.offset / filters.limit) + 1;
return (
<div>
<h2 className="text-xl font-semibold mb-4">Transactions</h2>
{/* Filter bar */}
<div className="flex flex-wrap gap-3 mb-4">
<input
type="text"
placeholder="Search..."
value={filters.search}
onChange={(e) => setFilters((f) => ({ ...f, search: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm w-48"
/>
<input
type="date"
value={filters.from}
onChange={(e) => setFilters((f) => ({ ...f, from: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
/>
<input
type="date"
value={filters.to}
onChange={(e) => setFilters((f) => ({ ...f, to: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
/>
<select
value={filters.category}
onChange={(e) => setFilters((f) => ({ ...f, category: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
>
<option value="">All Categories</option>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
<select
value={filters.bank_name}
onChange={(e) => setFilters((f) => ({ ...f, bank_name: e.target.value, offset: 0 }))}
className="bg-zinc-900 border border-zinc-700 rounded px-3 py-1.5 text-sm"
>
<option value="">All Banks</option>
{banks?.map((b) => (
<option key={b} value={b}>{b}</option>
))}
</select>
</div>
{/* Bulk action bar */}
{selected.size > 0 && (
<div className="flex items-center gap-3 mb-3 p-2 bg-zinc-900 border border-zinc-700 rounded">
<span className="text-sm text-zinc-400">{selected.size} selected</span>
<select
value={bulkCategory}
onChange={(e) => setBulkCategory(e.target.value)}
className="bg-zinc-800 border border-zinc-600 rounded px-2 py-1 text-sm"
>
<option value="">Set category...</option>
{CATEGORIES.map((c) => (
<option key={c} value={c}>{formatCategory(c)}</option>
))}
</select>
<button
disabled={!bulkCategory || bulkAction.isPending}
onClick={() => {
bulkAction.mutate(
{ action: "categorize", ids: Array.from(selected), category: bulkCategory },
{ onSuccess: () => { setSelected(new Set()); setBulkCategory(""); } }
);
}}
className="px-3 py-1 bg-blue-600 hover:bg-blue-700 disabled:opacity-50 rounded text-sm"
>
Apply
</button>
<button
onClick={() => setSelected(new Set())}
className="px-3 py-1 text-zinc-400 hover:text-white text-sm"
>
Clear
</button>
</div>
)}
{/* Table */}
<div className="overflow-x-auto border border-zinc-800 rounded-lg">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-zinc-800 bg-zinc-900/50">
<th className="p-2 w-8">
<input
type="checkbox"
checked={data?.data.length ? selected.size === data.data.length : false}
onChange={toggleAll}
className="accent-blue-600"
/>
</th>
<th
className="p-2 text-left cursor-pointer hover:text-white"
onClick={() => toggleSort("transaction_date")}
>
Date {filters.sort_by === "transaction_date" && (filters.sort_dir === "desc" ? "\u2193" : "\u2191")}
</th>
<th className="p-2 text-left">Description</th>
<th className="p-2 text-left">Merchant</th>
<th
className="p-2 text-right cursor-pointer hover:text-white"
onClick={() => toggleSort("amount")}
>
Amount {filters.sort_by === "amount" && (filters.sort_dir === "desc" ? "\u2193" : "\u2191")}
</th>
<th className="p-2 text-left">Type</th>
<th className="p-2 text-left">Category</th>
<th className="p-2 text-left">Bank</th>
</tr>
</thead>
<tbody>
{isLoading ? (
<tr><td colSpan={8} className="p-8 text-center text-zinc-500">Loading...</td></tr>
) : !data?.data.length ? (
<tr><td colSpan={8} className="p-8 text-center text-zinc-500">No transactions found</td></tr>
) : (
data.data.map((t) => (
<tr
key={t.id}
className={`border-b border-zinc-800/50 hover:bg-zinc-900/30 ${
selected.has(t.id) ? "bg-zinc-800/40" : ""
}`}
>
<td className="p-2">
<input
type="checkbox"
checked={selected.has(t.id)}
onChange={() => toggleSelect(t.id)}
className="accent-blue-600"
/>
</td>
<td className="p-2 whitespace-nowrap">{formatDate(t.transaction_date)}</td>
<td className="p-2 max-w-xs truncate" title={t.description}>{t.description}</td>
<td className="p-2 max-w-[150px]">
<div className="relative">
<InlineEdit
value={t.effective_merchant || ""}
onSave={(val) => updateTxn.mutate({ id: t.id, merchant_normalized: val })}
/>
{t.merchant_override && (
<span className="absolute -left-2 top-1/2 -translate-y-1/2 w-1.5 h-1.5 bg-blue-500 rounded-full" title="Manually overridden" />
)}
</div>
</td>
<td className={`p-2 text-right whitespace-nowrap font-mono ${
t.transaction_type === "debit" ? "text-red-400" : "text-green-400"
}`}>
{formatAmount(t.amount, t.transaction_type)}
</td>
<td className="p-2"><TypeBadge type={t.transaction_type} /></td>
<td className="p-2 max-w-[140px]">
<div className="relative">
<InlineEdit
value={t.effective_category}
onSave={(val) => updateTxn.mutate({ id: t.id, category: val })}
type="select"
options={categoryOptions}
/>
{t.category_override && (
<span className="absolute -left-2 top-1/2 -translate-y-1/2 w-1.5 h-1.5 bg-blue-500 rounded-full" title="Manually overridden" />
)}
</div>
</td>
<td className="p-2 text-zinc-400 whitespace-nowrap">{t.bank_name}</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
{data && data.total > filters.limit && (
<div className="flex items-center justify-between mt-4">
<span className="text-sm text-zinc-500">
Showing {filters.offset + 1}-{Math.min(filters.offset + filters.limit, data.total)} of {data.total}
</span>
<div className="flex gap-2">
<button
disabled={currentPage <= 1}
onClick={() => setPage(filters.offset - filters.limit)}
className="px-3 py-1 bg-zinc-800 hover:bg-zinc-700 disabled:opacity-50 rounded text-sm"
>
Previous
</button>
<span className="px-3 py-1 text-sm text-zinc-400">
Page {currentPage} of {totalPages}
</span>
<button
disabled={currentPage >= totalPages}
onClick={() => setPage(filters.offset + filters.limit)}
className="px-3 py-1 bg-zinc-800 hover:bg-zinc-700 disabled:opacity-50 rounded text-sm"
>
Next
</button>
</div>
</div>
)}
</div>
);
}