"use client"; import { ChevronRight, EyeOff, Plus } from "lucide-react"; import { useRouter } from "next/navigation"; import { Fragment, useState } from "react"; import { CategoryIcon } from "@/components/Icons"; import { useMoney } from "@/components/Money"; import { emptyRule, RuleDialog, type Draft } from "@/components/RuleForm"; import { toast } from "@/components/Toast"; import type { Category } from "@/lib/data"; import { plural } from "@/lib/format"; import { conditions, describeMatch, type Rule } from "@/lib/rules"; import { createClient } from "@/utils/supabase/client"; const pill = "inline-flex items-center gap-1.5 rounded-full bg-canvas border border-line px-3 py-1 text-xs text-muted"; export function RulesManager({ rules, counts, cats, allTags, accounts, missing }: { rules: Rule[]; counts: Record; cats: Category[]; allTags: string[]; accounts: { id: string; label: string }[]; missing: boolean; }) { const { money, money0 } = useMoney(); const router = useRouter(); const [editing, setEditing] = useState(null); const [resetting, setResetting] = useState(false); const done = (msg: string) => { setEditing(null); toast(msg); router.refresh(); }; async function resetAll() { const db = createClient(); const uid = (await db.auth.getSession()).data.session?.user.id; const { error } = uid ? await db.from("rules").delete().eq("user_id", uid) : { error: true }; setResetting(false); if (error) return toast("Couldn't reset your rules."); toast("All rules deleted. Past transactions keep their changes."); router.refresh(); } return ( <>

Transaction rules

Change categories, names and more on matching transactions automatically. New ones from your bank follow these as they arrive.

{!missing && }
{missing &&

Rules need a database update. Open Setup to run it.

} {!!rules.length && (
{rules.map((r) => { const cat = cats.find((c) => c.id === r.category_id); const fmt = (n: number) => (Number.isInteger(n) ? money0(n) : money(n)); const acct = accounts.find((a) => a.id === r.account_id)?.label; const conds = conditions(r, fmt, acct); return ( ); })}
)} {!rules.length && !missing && (
No rules yet

Change a transaction's category and press Always, or add one here.

)} {!!rules.length && (
{resetting ? ( Delete all {plural(rules.length, "rule")}? Past transactions keep their changes. ) : ( <>Looking to start over? )}
)} {editing && setEditing(null)} onDone={done} />} ); }