"use client"; import { Pencil, X } from "lucide-react"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { Dropdown } from "@/components/Dropdown"; import { Modal } from "@/components/Modal"; import { toast } from "@/components/Toast"; import { createClient } from "@/utils/supabase/client"; const KINDS = [ { value: "depository", label: "Bank account (checking or savings)" }, { value: "credit", label: "Credit card" }, { value: "loan", label: "Loan" }, { value: "investment", label: "Investment" }, { value: "other", label: "Something else (cash, a car, a home)" }, ]; // Rename an account, say what kind it is, or delete it. The kind decides where it counts: a credit card's balance is a // debt and its purchases are spending on the card. Linked accounts come back on the next sync, so only accounts you // added or imported can be deleted; a linked one is hidden instead, or its bank removed under Connections. export function AccountEdit({ id, name, bankName, type, manual }: { id: string; name: string; bankName: string; type: string; manual: boolean }) { const router = useRouter(); const [open, setOpen] = useState(false); const [label, setLabel] = useState(name); const [kind, setKind] = useState(type); const [busy, setBusy] = useState(false); const [confirm, setConfirm] = useState(false); const [err, setErr] = useState(""); const close = () => { setOpen(false); setConfirm(false); setErr(""); }; async function save(e: React.FormEvent) { e.preventDefault(); setBusy(true); const patch: Record = { custom_name: label.trim() && label.trim() !== bankName ? label.trim() : null }; if (kind !== type) patch.type_override = kind; // RLS blocks with 0 rows, not an error. const { data, error } = await createClient().from("accounts").update(patch).eq("id", id).select("id"); setBusy(false); if (error || !data?.length) return setErr(/type_override/.test(error?.message ?? "") ? "This needs a database update. Open Setup to run it." : "Couldn't save that. Try again."); close(); toast("Saved"); router.refresh(); } async function remove() { setBusy(true); const { data, error } = await createClient().from("accounts").delete().eq("id", id).select("id"); setBusy(false); if (error || !data?.length) return setErr("Couldn't delete it. Try again."); close(); toast(`Deleted ${name}`); router.refresh(); } return ( <> {open && (
Edit account
What kind of account is it?

A credit card's balance counts as money you owe. A bank account's counts as money you have.

{err &&

{err}

}
{!manual ? (

This account comes from your bank, so it comes back on the next sync. To stop counting it, use the switch next to it. To remove the whole bank, use Remove under Connections.

) : confirm ? (
Delete it and all its transactions? This can't be undone.
) : ( )}
)} ); }