import { ChevronRight } from "lucide-react"; import Link from "next/link"; import { Suspense } from "react"; import { CategoryCheck, type Checks } from "@/components/CategoryCheck"; import { CountryBanner } from "@/components/CountryPicker"; import { EmptyBank } from "@/components/EmptyBank"; import { GoLiveCard } from "@/components/GoLiveCard"; import { Merchant } from "@/components/Merchant"; import { PayFound } from "@/components/PayFound"; import { PayQuestion } from "@/components/PayQuestion"; import { SyncButton } from "@/components/PlaidButtons"; import { TxnList } from "@/components/TxnList"; import { WeekCard } from "@/components/WeekCard"; import { categories, getUser, moneyByMonth, months, today, TXN_SELECT, withAccountNames, type Txn } from "@/lib/data"; import { dateLabel, money0, ymd } from "@/lib/format"; import { deterministicMoves, ladder } from "@/lib/plan"; import { myPlanFacts } from "@/lib/plan-facts"; import { payGuess } from "@/lib/pay"; import { moneyPicture } from "@/lib/steps"; import { fetchChecks } from "@/lib/unsure"; // "Is this right?", collapsed to one line, only when a category waits to be checked. It streams, so it never holds the page. async function HomeCheck({ checks }: { checks: Promise }) { const c = await checks; return c.total > 0 ? : null; } // Pay the bank filed as something else (lib/pay.ts). Streams in beside the rest. async function PayCheck({ uid, today, notPay }: { uid: string; today: string; notPay: string[] }) { const { supabase } = await getUser(); const since = ymd(new Date(Date.parse(today) - 90 * 864e5)); const { data } = await supabase.from("transactions").select("date, amount, name, merchant_name, custom_name, category:categories(id, key, label, kind)") .eq("user_id", uid).eq("ignored", false).lt("amount", -200).gte("date", since).limit(500); type R = { date: string; amount: number; name: string; merchant_name: string | null; custom_name: string | null; category: { key: string; label: string; kind: string } | null }; const rows = (data ?? []) as unknown as R[]; const name = (r: R) => r.merchant_name ?? r.name; const pay = payGuess(rows.map((r) => ({ name: name(r), date: r.date, amount: Number(r.amount), kind: r.category?.kind ?? null })), notPay); if (!pay) return null; const { data: income } = await supabase.from("categories").select("id").eq("key", "INCOME").is("user_id", null).maybeSingle(); if (!income) return null; const filedAs = rows.find((r) => name(r) === pay.name)?.category?.label ?? "something else"; return ; } // One line pointing at the plan, or the one question broke needs answered. Streams in: the plan facts are the slow part. async function NextStep({ profile }: { profile: Record }) { const facts = await myPlanFacts(); if (ladder(facts).spare == null) return

What do you get paid?

; const next = deterministicMoves(facts, money0)[0]; if (!next) return null; return ( Next {next.title} ); } // Home is for a glance: what you have, what went out this month, what's coming up, what you spent last. // The working pages (Spending, Budgets, Money plan) are one click away. export default async function Dashboard() { const { supabase, user } = await getUser(); const uid = user!.id; const profile = user?.user_metadata?.profile; const now = await today(); // the user's date at UTC midnight: read it with getUTC* and ymd const day = ymd(now), soon = ymd(new Date(now.getTime() + 14 * 864e5)); const checks = (fetchChecks(supabase, uid, day) as Promise).catch(() => ({ rows: [], total: 0 })); // starts now, beside the queries below // Pay months on: "this month" is the pay month today is in, named after the calendar month it mostly covers. const thisMonth = await (await months()).current(); // Own rows only: an account a partner shares is on the Accounts page, not in your totals. const [{ data: recent }, { data: accounts }, { data: noRate }, { data: bills }, rows, cats] = await Promise.all([ supabase.from("transactions").select(TXN_SELECT).eq("user_id", uid).order("date", { ascending: false }).order("id").limit(5), supabase.from("accounts").select("type, subtype, current_balance:home_balance, included").eq("user_id", uid), supabase.rpc("fx_missing"), supabase.from("recurring").select("id, merchant_name, logo_url, average_amount:home_average, next_date").eq("user_id", uid).eq("active", true).eq("is_income", false).neq("status", "TOMBSTONED").gte("next_date", day).lte("next_date", soon).order("next_date").limit(4), moneyByMonth(supabase, `${thisMonth}-01`), categories(), ]); if (!accounts?.length) return ; const txns = withAccountNames((recent ?? []) as unknown as Txn[]); type Acct = { type: string; subtype: string | null }; const bal = (pred: (a: Acct) => boolean) => { const list = accounts.filter((a) => a.included && pred(a)); return list.length ? list.reduce((s, a) => s + Number(a.current_balance ?? 0), 0) : null; }; const owed = bal((a) => a.type === "credit" || a.type === "loan"); const tiles = [ { label: "In your bank", value: bal((a) => a.type === "depository" && a.subtype !== "savings"), href: "/accounts" }, { label: "Savings", value: bal((a) => a.type === "depository" && a.subtype === "savings"), href: "/accounts" }, { label: "Investments", value: bal((a) => a.type === "investment"), href: "/accounts" }, { label: "You owe", value: owed == null ? null : Math.abs(owed), href: "/debt" }, ].filter((t) => t.value != null); const pic = moneyPicture(rows, cats, thisMonth, null); const monthName = dateLabel(`${thisMonth}-01`, { month: "long" }); return (
{!profile?.country && } {!!noRate?.length &&

Totals leave out money in {(noRate as string[]).join(", ")}. No exchange rate is available for it.

}
= 4 ? "@3xl:grid-cols-4" : tiles.length === 3 ? "@3xl:grid-cols-3" : ""}`}> {tiles.map((t) => ( {t.label} {money0(t.value!)} ))}
Spent in {monthName} See all

{money0(pic.spent)}

{pic.spent > 0 ? ( <>
{pic.slices.map((s) =>
)}
    {pic.slices.slice(0, 3).map((s) => (
  • {s.label} {money0(s.amount)}
  • ))}
) :

Nothing yet this month.

}
Coming up See all
{bills?.length ? (
    {bills.map((b) => (
  • {(b.merchant_name as string).replace(/\s*\(\s*\)\s*$/, "")} {dateLabel(b.next_date as string, { month: "short", day: "numeric" })} {money0(Number(b.average_amount))}
  • ))}
) :

Nothing due in the next 2 weeks.

}
Latest spending See all
{txns.length ? :

No transactions yet. Press sync in a minute.

}
); }