diff --git a/src/components/cases/collections-tab.tsx b/src/components/cases/collections-tab.tsx index 23d48cd..65dceac 100644 --- a/src/components/cases/collections-tab.tsx +++ b/src/components/cases/collections-tab.tsx @@ -847,12 +847,35 @@ function BucketCell({ value, danger, positive }: { value: number; danger?: boole } +// Extract defendant surname from a case title like +// "Corner Lake Estates HOA v Benitez [2074 Corner Glen Drive]" +function extractDefendantSurname(title: string): string | null { + if (!title) return null; + // Match " v " or " v. " or " vs " (case-insensitive) + const m = title.match(/\sv\.?s?\.?\s+([A-Za-z'’\-]+)/i); + if (!m) return null; + return m[1].trim().toLowerCase(); +} + +function findUniqueHomeownerByTitle( + title: string, + homeowners: any[], +): any | null { + const surname = extractDefendantSurname(title); + if (!surname) return null; + const matches = homeowners.filter( + (h) => (h.last_name || "").toLowerCase() === surname, + ); + return matches.length === 1 ? matches[0] : null; +} + // ─── Add collection dialog ────────────────────────────────────────── function AddCollectionDialog({ open, onOpenChange, caseId, clientId, + caseTitle, homeowners, existingHomeownerIds, onCreated, @@ -861,6 +884,7 @@ function AddCollectionDialog({ onOpenChange: (v: boolean) => void; caseId: string; clientId: string; + caseTitle: string; homeowners: any[]; existingHomeownerIds: string[]; onCreated: () => void; @@ -871,18 +895,21 @@ function AddCollectionDialog({ const [notes, setNotes] = useState(""); const [submitting, setSubmitting] = useState(false); - useEffect(() => { - if (open) { - setHomeownerId(""); - setStatus("none"); - setNotes(""); - } - }, [open]); - const available = homeowners.filter( (h) => !existingHomeownerIds.includes(h.id), ); + useEffect(() => { + if (open) { + // Try to pre-select a homeowner whose surname matches the case title + const match = findUniqueHomeownerByTitle(caseTitle, available); + setHomeownerId(match ? match.id : ""); + setStatus("none"); + setNotes(""); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [open, caseTitle]); + const submit = async () => { if (!homeownerId) { toast.error("Pick a homeowner");