import { Banknote, BarChart3, Bitcoin, Car, CreditCard, Gem, HandCoins, House, Landmark, type LucideIcon } from "lucide-react"; import Link from "next/link"; import { AccountToggle } from "@/components/AccountToggle"; import { HoldingFee } from "@/components/HoldingFee"; import { CardArt } from "@/components/CardArt"; import { Merchant } from "@/components/Merchant"; import { ownedCards } from "@/lib/cards"; import { ConnectionActions, SyncButton } from "@/components/PlaidButtons"; import { getUser } from "@/lib/data"; import { money, money0, plural } from "@/lib/format"; type Holding = { id: string; name: string | null; ticker: string | null; type: string | null; quantity: number | null; value: number | null; cost_basis: number | null }; type Account = { id: string; name: string; official_name?: string | null; custom_name: string | null; mask: string | null; type: string; subtype: string | null; current_balance: number | null; currency: string | null; home: number | null; included: boolean; holdings: Holding[] }; type Item = { id: string; institution_name: string | null; institution_logo: string | null; institution_color: string | null; last_synced_at: string | null; status?: string; error?: string | null; provider?: string; accounts: Account[]; }; const GROUPS: { type: string; label: string; icon: LucideIcon; debt: boolean }[] = [ { type: "depository", label: "Cash", icon: Landmark, debt: false }, { type: "investment", label: "Investments", icon: BarChart3, debt: false }, { type: "other", label: "Property and other", icon: Gem, debt: false }, // manual cars, homes, valuables (Add accounts) { type: "credit", label: "Credit cards", icon: CreditCard, debt: true }, { type: "loan", label: "Loans", icon: HandCoins, debt: true }, ]; // Plaid subtypes are lowercase codes ("hsa", "401k", "roth"); everything else just gets capitalised. const SUBTYPES: Record = { hsa: "HSA", "401k": "401(k)", "403b": "403(b)", "457b": "457(b)", ira: "IRA", roth: "Roth IRA", "roth 401k": "Roth 401(k)", "529": "529", sep: "SEP IRA", "simple ira": "SIMPLE IRA", brokerage: "Brokerage", cd: "CD" }; // Manual accounts keep the kind picked on Add accounts as their subtype. const MANUAL_ICON: Record = { vehicle: Car, property: House, crypto: Bitcoin, cash: Banknote, other: Gem }; // Plaid's cash position inside a brokerage ("CUR:USD U S Dollar"). const isCash = (h: Holding) => h.type === "cash" || /^(CASH|CUR:)/.test(h.ticker ?? ""); // What a connection holds, so two from the same bank read apart: "Cash, credit cards and loans". const holds = (accounts: Account[]) => { const ls = GROUPS.filter((g) => accounts.some((a) => a.type === g.type)).map((g, i) => (i ? g.label.toLowerCase() : g.label)); return ls.length > 1 ? `${ls.slice(0, -1).join(", ")} and ${ls.at(-1)}` : ls[0] ?? ""; }; const subtypeLabel = (s: string) => SUBTYPES[s] ?? s.charAt(0).toUpperCase() + s.slice(1); const ago = (iso: string) => { const m = Math.round((Date.now() - new Date(iso).getTime()) / 60000); return m < 1 ? "just now" : m < 60 ? `${m} min ago` : m < 1440 ? `${Math.round(m / 60)} h ago` : `${Math.round(m / 1440)} d ago`; }; export default async function Accounts() { const { supabase, user } = await getUser(); // Manual accounts (from "Add transaction") have no Plaid item; balance (migration 0014) is the starting balance less what went out. const [{ data }, { data: manual }] = await Promise.all([ supabase .from("plaid_items") .select("id, institution_name, institution_logo, institution_color, last_synced_at, status, error, provider, accounts(id, name, official_name, custom_name, mask, type, subtype, current_balance, currency, home:home_balance, included, holdings(id, name, ticker, type, quantity, value, cost_basis))") .order("created_at"), supabase.from("accounts").select("id, name, custom_name, mask, type, subtype, current_balance:balance, currency, home:home_balance, included").eq("manual", true).eq("user_id", user!.id), ]); const items = (data ?? []) as unknown as Item[]; const manualItem = { id: "manual", institution_name: "Added by you", institution_logo: null, institution_color: null, last_synced_at: null, accounts: [] }; const all = [ ...items.flatMap((i) => i.accounts.map((a) => ({ ...a, item: i }))), ...((manual ?? []) as unknown as Account[]).map((a) => ({ ...a, holdings: [], item: manualItem })), ]; const sum = (debt: boolean) => all.filter((a) => a.included && GROUPS.find((g) => g.type === a.type)?.debt === debt).reduce((s, a) => s + Number(a.home ?? 0), 0); const assets = sum(false); const debts = sum(true); return (
Add account {!!items.length && }
{!!all.length && (
{[ ["Assets", assets, ""], ["Debts", debts, "text-bad"], ["Net worth", assets - debts, ""], ].map(([label, v, cls]) => (
{label}
{money0(v as number)}
))}
)} {GROUPS.map(({ type, label, icon: Icon, debt }) => { const rows = all.filter((a) => a.type === type); if (!rows.length) return null; const total = rows.filter((a) => a.included).reduce((s, a) => s + Number(a.home ?? 0), 0); return (
{label} {money(total)}
{rows.map((a) => (
{(() => { const card = a.type === "credit" ? ownedCards([a])[0] : undefined; if (card) return ; const RowIcon = (a.item.id === "manual" && a.subtype && MANUAL_ICON[a.subtype]) || Icon; return ; })()}
{a.custom_name || a.name} {a.mask ? `··${a.mask}` : ""}
{a.item.institution_name}{a.subtype ? ` · ${subtypeLabel(a.subtype)}` : ""}
{money(a.current_balance, a.currency)}
{!!a.holdings?.length && (
    {[...a.holdings].sort((x, y) => Number(y.value ?? 0) - Number(x.value ?? 0)).map((h) => { const cash = isCash(h); const gain = !cash && h.cost_basis != null && h.value != null ? Number(h.value) - Number(h.cost_basis) : null; return (
  • {cash ? "" : h.ticker ?? ""} {cash ? "Cash" : h.name} {money(h.value, a.currency)}
    {!cash && (
    {gain != null && {gain < 0 ? "down " : "up "}{money0(Math.abs(gain), a.currency)}}
    )}
  • ); })}
)}
))}
); })} {!!items.length && (
Connections
{items.map((item) => (
{item.institution_name ?? "Bank"} {item.status && item.status !== "ok" && Needs attention}
{item.status && item.status !== "ok" ? item.status === "ITEM_LOGIN_REQUIRED" ? "Sign in to your bank again to keep syncing." : (item.error ?? "This connection stopped syncing.") : `${holds(item.accounts) ? `${holds(item.accounts)} · ` : ""}${plural(item.accounts.length, "account")} · synced ${item.last_synced_at ? ago(item.last_synced_at) : "never"}`}
))}
)} {!all.length && (
No accounts yet

Link a bank to see balances, transactions and recurring charges, and a brokerage, 401(k) or HSA to see what you hold.

)}
); }