"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, CountryPick, KeyForm, ConnectSupabaseButton, MigrateButton } 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 next = required.find((s) => s.status !== "done");
const canSkipBank = next?.country !== undefined; // 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 ${required.indexOf(next) + 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.
}
{s.action === "connect" && (s.steps || s.fields) ? (
// Step 2: paste the connection string and password (the reliable way), or sign in to Supabase instead.
<>
{form}
setFallback(e.currentTarget.open)}>
Or sign in to Supabase instead of pasting
>
) : 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