"use client"; import type React from "react"; import { useEffect, useRef } from "react"; import TagInput from "@/components/tag-input"; import ToggleButtonGroup from "@/components/toggle-button-group"; import DatePicker from "@/components/date-picker"; type NewEntryForm = { amountDollars: string; occurredAt: string; necessity: string; notes: string; tags: string[]; entryType: "SPENDING" | "INCOME"; }; type NewEntryModalProps = { isOpen: boolean; form: NewEntryForm; error: string; onClose: () => void; onSubmit: (event: React.FormEvent) => void; onChange: (next: Partial) => void; tagSuggestions: string[]; emptyTagActionLabel?: string; emptyTagActionDisabled?: boolean; onEmptyTagAction?: () => void; amountInputRef?: React.Ref; tagsInputRef?: React.Ref; }; export default function NewEntryModal({ isOpen, form, error, onClose, onSubmit, onChange, tagSuggestions, emptyTagActionLabel, emptyTagActionDisabled = false, onEmptyTagAction, amountInputRef, tagsInputRef }: NewEntryModalProps) { const typeLabel = form.entryType === "INCOME" ? "Income" : "Expense"; const formRef = useRef(null); useEffect(() => { if (!isOpen) return; function handleKeyDown(event: KeyboardEvent) { if (event.key === "Escape") onClose(); } if (!form.occurredAt) { const today = new Date().toISOString().slice(0, 10); onChange({ occurredAt: today }); } window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [form.occurredAt, isOpen, onChange, onClose]); if (!isOpen) return null; return (
event.stopPropagation()} >

New {typeLabel} Entry

onChange({ entryType })} ariaLabel="Entry type" className="flex items-center gap-[-50px] rounded-full border border-accent-weak bg-panel" sizeClassName="px-4 py-2.5 text-xs font-semibold" options={[ { value: "SPENDING", label: "Spending", className: "mr-[-10px]" }, { value: "INCOME", label: "Income" } ]} />
{ if (event.key !== "Enter" || event.defaultPrevented || event.shiftKey) return; const target = event.target as HTMLElement; if (target?.tagName === "TEXTAREA") return; event.preventDefault(); formRef.current?.requestSubmit(); }} className="mt-3 grid gap-3 md:grid-cols-2" >
onChange({ occurredAt })} required className="mt-1" />
onChange({ necessity })} ariaLabel="Necessity" className="mt-1 flex items-center gap-2 rounded-full border border-accent-weak bg-panel" sizeClassName="px-3 py-2.5 text-xs font-semibold" options={[ { value: "NECESSARY", label: "Necessary", className: "flex-1" }, { value: "BOTH", label: "Both", className: "flex-1" }, { value: "UNNECESSARY", label: "Unnecessary", className: "flex-1" } ]} />
onChange({ tags: form.tags.filter(item => item !== tag) })} onAddTag={tag => onChange({ tags: [...form.tags, tag] })} emptySuggestionLabel={emptyTagActionLabel} emptySuggestionDisabled={emptyTagActionDisabled} onEmptySuggestionClick={onEmptyTagAction} invalid={!form.tags.length} inputRef={tagsInputRef} />