"use client"; import { Check, ChevronDown, CircleAlert, ExternalLink, RefreshCw } from "lucide-react"; import Link from "next/link"; import { useCallback, useEffect, useState } from "react"; import { Logo } from "@/components/Logo"; import type { AiRow, Part, Section, SetupStatus } from "@/lib/setup"; import { CopyCommand, KeyForm, ConnectSupabaseButton, MigrateButton, ProjectPicker } from "./SetupActions"; export function ExtLink({ href, children }: { href: string; children: React.ReactNode }) { return ( {children} ); } // `code` inside plain text renders as an inline code chip. const ticks = (t: string) => t.split(/(`[^`]+`)/).map((p, i) => (p.startsWith("`") ? {p.slice(1, -1)} : p)); const line = (parts: Part[]) => parts.map((p, i) => (typeof p === "string" ? p : {p.label})); // Height animates via grid rows (0fr -> 1fr); globals.css turns transitions off for reduced motion. export function Collapse({ open, children }: { open: boolean; children: React.ReactNode }) { return (
{children}
); } function Badge({ s, n }: { s: Section["status"]; n: number }) { if (s === "done") return ; if (s === "error") return ; return {n}; } export function SetupChecklist({ signedIn }: { signedIn: boolean }) { const [data, setData] = useState(null); const [checking, setChecking] = useState(true); const [failed, setFailed] = useState(false); const [open, setOpen] = useState(null); const [moreOpen, setMoreOpen] = useState(false); const load = useCallback(async (keepOpen?: string) => { setChecking(true); try { const res = await fetch("/api/setup/status", { cache: "no-store" }); if (!res.ok) throw new Error(String(res.status)); const next = (await res.json()) as SetupStatus; setData(next); setFailed(false); // Open the next thing to do; keep a section open while the user is part-way through it. const first = next.sections.find((s) => s.kind !== "more" && s.status !== "done"); setOpen((cur) => { const still = (id?: string | null) => id && next.sections.find((s) => s.id === id)?.status !== "done"; return still(keepOpen) ? keepOpen! : still(cur) ? cur : first?.id ?? null; }); } catch { setFailed(true); } setChecking(false); }, []); // Connect Supabase opens Supabase in its own tab. Back from Supabase, that tab passes the result here and closes itself. useEffect(() => { const db = new URLSearchParams(window.location.search).get("db"); if (db && window.opener) { try { localStorage.setItem("broke.supabase", `${db}|${Date.now()}`); } catch { /* private window: this tab just stays open */ } window.close(); return; } const onStorage = (e: StorageEvent) => { if (e.key === "broke.supabase" && e.newValue) window.location.href = `/setup?db=${encodeURIComponent(e.newValue.split("|")[0])}`; }; addEventListener("storage", onStorage); return () => removeEventListener("storage", onStorage); }, []); useEffect(() => { load(); }, [load]); const sections = data?.sections ?? []; const main = sections.filter((s) => s.kind !== "more"); const more = sections.filter((s) => s.kind === "more"); const required = main.filter((s) => s.kind === "required"); const done = required.filter((s) => s.status === "done").length; const next = required.find((s) => s.status !== "done"); const canSkipBank = next?.id === "plaid"; // the database works, so broke can open: statements can be imported without a bank feed return (
{signedIn && Back to broke}

{!data ? "Set up broke" : !next ? "You're all set" : `Step ${done + 1} of ${required.length}: ${next.title}`}

{!data ? "Checking what's connected… " : !next ? "Open broke and sign in with your email. " : "It's free. Follow the steps below and paste each key in. "} Your keys stay on this computer.

{(data ? required.map((s) => s.status === "done") : [false, false, false]).map((d, i) => ( ))}
{next ? ( ) : ( data && Open broke )} {canSkipBank && Skip: use made-up data} {failed && Couldn't check. Is broke still running?}
{!data && !failed &&
{[0, 1, 2, 3].map((i) =>
)}
} {data && main.map((s, i) => ( setOpen(open === s.id ? null : s.id)} data={data} signedIn={signedIn} reload={() => load(s.id)} /> ))} {data && !!more.length && (
{more.map((s) => (
{s.title}

{s.what}

{s.status === "done" && On}
load()} />
))}
)}

Keys are saved to .env.local next to Start broke.command. This page only ever shows whether each one is saved.

); } function SectionCard({ s, n, open, onToggle, data, signedIn, reload }: { s: Section; n: number; open: boolean; onToggle: () => void; data: SetupStatus; signedIn: boolean; reload: () => void }) { const summary = s.status === "done" || s.status === "error" ? s.note ?? s.what : s.what; return (
); } function Steps({ steps }: { steps?: Section["steps"] }) { if (!steps) return null; return (
    {steps.map((p, i) => (
  1. {i + 1}. {line(p)}
  2. ))}
); } export function Body({ s, data, signedIn, reload }: { s: Section; data: SetupStatus; signedIn: boolean; reload: () => void }) { const [fallback, setFallback] = useState(false); const form = s.fields && ; return ( <> {s.note && s.status !== "done" && s.status !== "error" &&

{s.note}

} {s.note && s.status === "done" && s.kind === "more" &&

{s.note}

} {s.action === "connect" && setFallback(true)} />} {s.action === "pick" && window.location.reload()} />} {(s.action === "connect" || s.action === "pick") && (s.steps || s.fields) ? (
setFallback(e.currentTarget.open)}> {s.fallback ?? "Button didn't work? Paste a connection string instead"}
{form}
) : s.status === "done" && s.kind !== "more" && s.fields ? ( // Connected: the steps and empty boxes are noise now. They stay one click away for changing a key.
Change these keys
{form}
) : ( <> {s.command && } {form} )} {s.action === "migrate" && } {s.providers && } ); } function Providers({ rows }: { rows: AiRow[] }) { const [open, setOpen] = useState(null); return (
    {rows.map((p) => { const can = !p.ready && !!p.howTo?.length; return (
  • {p.label} {p.ready ? "Ready" : ticks(p.reason ?? "Not set up")} {can && ( )}
    {can && (
      {p.howTo!.map((h, i) => (
    1. {ticks(h.text)}{h.href && <> Open} {h.command && }
    2. ))}
    )}
  • ); })}
); }