"use client"; import { useState } from "react"; import { Plus, Paperclip } from "lucide-react"; import { toast } from "sonner"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; import { useCreatePrecedent, usePrecedentLibrarySearch, uploadPrecedentPdf, } from "@/lib/api/precedents"; import type { PracticeArea } from "@/lib/practice-area"; /* * Inline form for adding a new precedent. Opens in a Popover adjacent * to the trigger button so the user can see the surrounding context * (the threshold_claim body, the chair editor) while they fill it in. * * The citation field has cross-case typeahead: once the user types * 2+ characters, we hit /api/precedents/search and show distinct * matches. Picking one prefills quote + chair_note but keeps them * editable — the new row is a copy, so a customized quote for this * case doesn't affect the library. */ export function PrecedentAttacher({ caseNumber, sectionId, practiceArea, }: { caseNumber: string; sectionId: string | null; practiceArea: PracticeArea | null | undefined; }) { const [open, setOpen] = useState(false); const [citation, setCitation] = useState(""); const [quote, setQuote] = useState(""); const [chairNote, setChairNote] = useState(""); const [pdfFile, setPdfFile] = useState(null); const [submitting, setSubmitting] = useState(false); const [picked, setPicked] = useState(false); const create = useCreatePrecedent(caseNumber); const library = usePrecedentLibrarySearch( citation, practiceArea, /* pause typeahead once the user has picked one and we're just editing */ !picked, ); const reset = () => { setCitation(""); setQuote(""); setChairNote(""); setPdfFile(null); setPicked(false); }; const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!quote.trim() || !citation.trim()) { toast.error("ציטוט ומראה-מקום חובה"); return; } setSubmitting(true); try { let pdfDocumentId: string | undefined; if (pdfFile) { const res = await uploadPrecedentPdf(caseNumber, pdfFile); pdfDocumentId = res.document_id; } await create.mutateAsync({ quote: quote.trim(), citation: citation.trim(), chair_note: chairNote.trim(), section_id: sectionId ?? undefined, pdf_document_id: pdfDocumentId, }); toast.success("נוספה פסיקה"); reset(); setOpen(false); } catch (err) { toast.error(err instanceof Error ? err.message : "שגיאה בשמירה"); } finally { setSubmitting(false); } }; return ( { setOpen(v); if (!v) reset(); }}>
{ setCitation(e.target.value); setPicked(false); }} placeholder="ערר (ירושלים) 1126-08-25 ... נ' ... (נבו 9.3.2026)" autoComplete="off" className="mt-1" /> {!picked && library.data && library.data.length > 0 && citation.length >= 2 && (
    {library.data.map((m) => (
  • ))}
)}