"use client";
import { Check, Copy, Eye, EyeOff } from "lucide-react";
import { useEffect, useState } from "react";
import { Dropdown } from "@/components/Dropdown";
import { COUNTRIES, countryOf, guessCountry, OTHER } from "@/lib/countries";
import type { Field } from "@/lib/setup";
export function CopyCommand({ command }: { command: string }) {
const [copied, setCopied] = useState(false);
return (
{command}
);
}
const MODE_LABELS: Record = { sandbox: "Test banks", production: "Real banks" };
// Two or three choices side by side. No choice picked = keep what's saved.
export function Segmented({ label, value, options, onChange }: { label: string; value: string; options: { value: string; label: string }[]; onChange: (v: string) => void }) {
return (
{options.map((o) => (
))}
);
}
type Result = { ok: boolean; message: string } | null;
// Keys typed here go to POST /api/setup/env (localhost only). An empty input keeps the saved value.
export function KeyForm({ fields, canWrite, writeNote, onSaved }: { fields: Field[]; canWrite: boolean; writeNote?: string; onSaved: () => void }) {
const [values, setValues] = useState>({});
const [show, setShow] = useState>({});
const [errors, setErrors] = useState>({});
const [busy, setBusy] = useState(false);
const [result, setResult] = useState<{ ok: boolean; text: string } | null>(null);
const dirty = Object.values(values).some((v) => v.trim());
async function save(e: React.FormEvent) {
e.preventDefault();
const send = Object.fromEntries(Object.entries(values).filter(([, v]) => v.trim()));
if (!Object.keys(send).length) return;
setBusy(true);
setResult(null);
setErrors({});
try {
const res = await fetch("/api/setup/env", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ values: send }) });
const body = (await res.json().catch(() => ({}))) as { errors?: Record; error?: string; checks?: Record; restart?: boolean };
if (body.errors) setErrors(body.errors);
else if (!res.ok) setResult({ ok: false, text: body.error ?? "Couldn't save." });
else {
const checks = Object.values(body.checks ?? {}).filter((c): c is { ok: boolean; message: string } => !!c);
const bad = checks.find((c) => !c.ok);
const text = bad ? `Saved, but: ${bad.message}` : checks.map((c) => c.message).join(" ") || "Saved.";
setResult({ ok: !bad, text: body.restart ? `${text} Restart broke to use the new keys.` : text });
setValues({});
onSaved();
}
} catch {
setResult({ ok: false, text: "Couldn't reach broke. Is it still running?" });
}
setBusy(false);
}
if (!canWrite) return {writeNote}
;
return (
);
}
export function MigrateButton({ onDone }: { onDone: () => void }) {
const [busy, setBusy] = useState(false);
const [msg, setMsg] = useState("");
return (
{msg && {msg}}
);
}
// One click: Supabase's own sign-in page, then back to /setup with the tables created.
export function ConnectSupabaseButton({ onDone, onFail }: { onDone: () => void; onFail?: () => void }) {
const [busy, setBusy] = useState(false);
const [msg, setMsg] = useState("");
return (
{msg && {msg}}
);
}
const COUNTRY_OPTIONS = [...COUNTRIES, OTHER].map((c) => ({ value: c.code, label: c.name }));
// Step 3 asks where they live first, so it can show that country's bank feed. Saved as BROKE_COUNTRY; onboarding uses it
// instead of asking again. Nothing saved yet: the guess from this computer, with a Continue button.
export function CountryPick({ saved, canWrite, writeNote, onSaved }: { saved: string; canWrite: boolean; writeNote?: string; onSaved: () => void }) {
const [guess, setGuess] = useState("");
const [value, setValue] = useState(saved);
const [busy, setBusy] = useState(false);
const [err, setErr] = useState("");
useEffect(() => {
if (saved) return; // the checklist remounts this (key) when the saved country changes
const g = guessCountry(navigator.languages ?? [navigator.language], Intl.DateTimeFormat().resolvedOptions().timeZone);
setGuess(g);
setValue((v) => v || g);
}, [saved]);
async function save(code: string) {
setBusy(true);
setErr("");
try {
const res = await fetch("/api/setup/env", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ values: { BROKE_COUNTRY: code } }) });
const body = (await res.json().catch(() => ({}))) as { error?: string; errors?: Record };
if (res.ok) onSaved();
else setErr(body.errors?.BROKE_COUNTRY ?? body.error ?? "Couldn't save.");
} catch {
setErr("Couldn't reach broke. Is it still running?");
}
setBusy(false);
}
if (!canWrite) return Where you live: {saved ? countryOf(saved).name : "not set"}. {writeNote} Set BROKE_COUNTRY there.
;
return (
Where do you live?
{ setValue(c); if (saved) save(c); }} placeholder="Choose a country" searchable />
{!saved &&
}
{saved && busy &&
Saving...}
{!saved && value && value === guess &&
Guessed from this computer's time zone and language. Change it if that's wrong.
}
{err &&
{err}
}
);
}