'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Trash2, Plus } from 'lucide-react' interface Rule { id: string pattern: string } interface Props { open: boolean onOpenChange: (open: boolean) => void budgetId: string budgetName: string rules: Rule[] } export function BudgetRulesDialog({ open, onOpenChange, budgetId, budgetName, rules }: Props) { const router = useRouter() const [pattern, setPattern] = useState('') const [adding, setAdding] = useState(false) async function handleAdd(e: React.FormEvent) { e.preventDefault() if (!pattern.trim()) return setAdding(true) await fetch('/api/budget-rules', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ budgetId, pattern: pattern.trim() }), }) setPattern('') setAdding(false) router.refresh() } async function handleDelete(id: string) { await fetch(`/api/budget-rules/${id}`, { method: 'DELETE' }) router.refresh() } return ( Auto-assign rules — {budgetName}

Transactions whose description contains a pattern (case-insensitive) are automatically assigned to this budget on upload. First matching rule wins.

{rules.length === 0 ? (

No rules yet.

) : ( )}
setPattern(e.target.value)} className="flex-1" />
) }