"use client"; import { useState } from "react"; import { Trash2, Sparkles } from "lucide-react"; import { toast } from "sonner"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Skeleton } from "@/components/ui/skeleton"; import { useCorpus, useDeleteCorpusEntry, type CorpusDecision } from "@/lib/api/training"; import { CorpusDetailDrawer } from "./corpus-detail-drawer"; /* * Corpus tab: table of all decisions currently in the style corpus. * * Click any row → opens CorpusDetailDrawer with the enriched metadata * + edit UI. The trash button is now in its own narrow column and uses * stopPropagation so deleting a row doesn't also open the drawer. * * We use browser confirm() for the destructive action rather than a * full shadcn AlertDialog because this is a single admin operation * gated by an API-level safety net (FK cascade is best-effort but * style_corpus DELETE returns 404 on missing rows, so the worst case * is a no-op). */ function formatChars(n: number) { return `${(n / 1000).toFixed(1)}K`; } function formatDate(iso: string) { if (!iso) return "—"; try { return new Date(iso).toLocaleDateString("he-IL"); } catch { return iso; } } function Row({ item, onOpen, }: { item: CorpusDecision; onOpen: () => void }) { const del = useDeleteCorpusEntry(); const onDelete = async (e: React.MouseEvent) => { e.stopPropagation(); if (!window.confirm(`למחוק את החלטה ${item.decision_number} מהקורפוס?`)) return; try { await del.mutateAsync(item.id); toast.success("נמחק מהקורפוס"); } catch (e) { toast.error(e instanceof Error ? e.message : "שגיאה במחיקה"); } }; return ( {item.decision_number || "—"} {formatDate(item.decision_date)} {item.subject_categories.length === 0 ? ( ) : (
{item.subject_categories.slice(0, 3).map((s) => ( {s} ))} {item.subject_categories.length > 3 && ( +{item.subject_categories.length - 3} )}
)}
{item.legal_citation || "—"} {item.lessons_count > 0 && ( {item.lessons_count} )}
{formatChars(item.chars)} {item.page_count > 0 && ( · {item.page_count} ע׳ )} {formatDate(item.created_at)}
); } export function CorpusPanel() { const { data, isPending, error } = useCorpus(); const [selected, setSelected] = useState(null); if (error) { return (
{error.message}
); } return ( <>
מס׳ החלטה תאריך נושאים מראה מקום תווים / עמודים נוסף בתאריך {isPending ? ( [...Array(4)].map((_, i) => ( {[...Array(7)].map((_, j) => ( ))} )) ) : data?.length === 0 ? ( הקורפוס ריק ) : ( data?.map((item) => ( setSelected(item)} /> )) )}
{ if (!open) setSelected(null); }} /> ); }