"use client"; import { useState } from "react"; import { Link2, Loader2, X } from "lucide-react"; import { toast } from "sonner"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { usePrecedents, useLinkRelatedCase, useUnlinkRelatedCase, RelatedCase, } from "@/lib/api/precedent-library"; const LEVEL_LABELS: Record = { "עליון": "עליון", "מנהלי": "מנהלי", "ועדת_ערר_ארצית": "ארצי", "ועדת_ערר_מחוזית": "מחוזי", }; const LEVEL_COLORS: Record = { "עליון": "bg-red-50 text-red-700 border-red-200", "מנהלי": "bg-orange-50 text-orange-700 border-orange-200", "ועדת_ערר_ארצית": "bg-blue-50 text-blue-700 border-blue-200", "ועדת_ערר_מחוזית": "bg-green-50 text-green-700 border-green-200", }; // ── Search Dialog ──────────────────────────────────────────────────── type DialogProps = { caseId: string; currentRelated: RelatedCase[]; open: boolean; onOpenChange: (open: boolean) => void; }; function LinkDialog({ caseId, currentRelated, open, onOpenChange }: DialogProps) { const [query, setQuery] = useState(""); const { mutateAsync: linkCase, isPending } = useLinkRelatedCase(caseId); const alreadyLinkedIds = new Set([...currentRelated.map((r) => r.id), caseId]); const { data, isPending: searching } = usePrecedents( query.length >= 2 ? { search: query, limit: 10 } : {}, ); const candidates = (data?.items ?? []).filter((p) => !alreadyLinkedIds.has(p.id)); async function handleLink(relatedId: string) { try { await linkCase({ relatedId }); toast.success("הפסיקות קושרו"); setQuery(""); } catch { toast.error("שגיאה בקישור"); } } return ( קשר החלטה קשורה
setQuery(e.target.value)} autoFocus dir="rtl" /> {query.length >= 2 && (
{searching ? (
מחפש...
) : candidates.length === 0 ? (

לא נמצאו תוצאות

) : ( candidates.map((p) => ( )) )}
)} {query.length > 0 && query.length < 2 && (

הקלד לפחות 2 תווים

)}
); } // ── Related Case Card ──────────────────────────────────────────────── function RelatedCaseCard({ caseId, related }: { caseId: string; related: RelatedCase }) { const { mutateAsync: unlinkCase, isPending } = useUnlinkRelatedCase(caseId); async function handleUnlink() { try { await unlinkCase(related.id); toast.success("הקישור הוסר"); } catch { toast.error("שגיאה בהסרת הקישור"); } } return (
{related.case_name || related.case_number}
{related.precedent_level && ( {LEVEL_LABELS[related.precedent_level] ?? related.precedent_level} )} {related.court && ( {related.court} )} {related.date && ( {related.date.slice(0, 10)} )}
); } // ── Public section component ───────────────────────────────────────── type SectionProps = { caseId: string; related: RelatedCase[]; }; export function RelatedCasesSection({ caseId, related }: SectionProps) { const [dialogOpen, setDialogOpen] = useState(false); return (

החלטות קשורות{related.length > 0 ? ` (${related.length})` : ""}

{related.length === 0 ? (

אין החלטות קשורות עדיין

) : (
{related.map((r) => ( ))}
)}
); }