Phase 5: secondary screens (diagnostics, skills, training)

Port the remaining read views from the vanilla UI to Next.js:

- /diagnostics — system health snapshot (DB connected, table counts,
  active tasks, failed and stuck documents). Uses the existing
  /api/system/diagnostics payload with a 10s refetchInterval so the
  page self-updates while the user watches.
- /skills — Paperclip skill inventory with sync status (DB-only,
  disk-only, synced, not-synced) as a card grid driven by
  /api/admin/skills.
- /training — Dafna's style portrait as three tabs on one page:
  * Report: corpus KPIs + CSS conic-gradient subject donut
    (SubjectDonut ported from index.html renderHero) + horizontal
    anatomy bars + top-12 signature phrases.
  * Corpus: TanStack Table of style_corpus rows with an inline
    delete mutation (useDeleteCorpusEntry invalidates both the
    corpus list and the style report so KPIs update).
  * Compare: two-decision selector backed by /api/training/compare,
    side-by-side panels plus shared / only-A / only-B pattern
    lists.

New API modules: lib/api/system.ts, lib/api/skills.ts,
lib/api/training.ts. All three use TanStack Query with staleTime
profiles tuned per endpoint (10s for diagnostics, 30s for skills,
60s for training reports).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-11 17:33:33 +00:00
parent ac0a5ee30b
commit fb1f73fa25
11 changed files with 1228 additions and 6 deletions

View File

@@ -0,0 +1,189 @@
"use client";
import { useState } from "react";
import { Card, CardContent } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Skeleton } from "@/components/ui/skeleton";
import {
Select, SelectContent, SelectItem, SelectTrigger, SelectValue,
} from "@/components/ui/select";
import {
useCorpus, useCompare, type CompareSide, type PatternEntry,
} from "@/lib/api/training";
/*
* Compare two decisions from the style corpus side-by-side. Uses the
* training/compare endpoint which already does the heavy lifting (pattern
* extraction, section stats, shared/unique pattern sets). Our job is
* layout: two columns of metadata + section bars, plus a third "shared"
* section listing patterns that appear in both.
*/
function SideColumn({ side }: { side: CompareSide }) {
return (
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-5 py-4 space-y-3">
<header>
<h3 className="text-navy text-lg mb-0 tabular-nums">
{side.decision_number || "—"}
</h3>
<p className="text-[0.78rem] text-ink-muted tabular-nums">
{side.decision_date || "—"} · {(side.chars / 1000).toFixed(1)}K תווים
</p>
</header>
{side.subjects.length > 0 && (
<div className="flex flex-wrap gap-1">
{side.subjects.map((s) => (
<Badge
key={s}
variant="outline"
className="text-[0.7rem] bg-gold-wash text-gold-deep border-gold/40"
>
{s}
</Badge>
))}
</div>
)}
{side.sections.length > 0 && (
<div>
<h4 className="text-[0.72rem] uppercase tracking-wider text-gold-deep font-semibold mb-1.5">
חלוקה לחלקים
</h4>
<ul className="space-y-1 text-[0.78rem]">
{side.sections.map((sec) => (
<li key={sec.type} className="flex items-center justify-between gap-2">
<span className="text-ink-soft truncate">{sec.type}</span>
<span className="text-ink-muted tabular-nums shrink-0">
{(sec.chars / 1000).toFixed(1)}K
</span>
</li>
))}
</ul>
</div>
)}
<p className="text-[0.78rem] text-ink-muted">
דפוסי סגנון שנמצאו: <span className="text-navy font-semibold tabular-nums">{side.patterns_count}</span>
</p>
</CardContent>
</Card>
);
}
function PatternList({
title,
items,
tone,
}: {
title: string;
items: PatternEntry[];
tone: "shared" | "a" | "b";
}) {
const toneClass =
tone === "shared"
? "bg-success-bg border-success/40"
: tone === "a"
? "bg-info-bg border-info/40"
: "bg-gold-wash border-gold/40";
const toneHeading =
tone === "shared"
? "text-success"
: tone === "a"
? "text-info"
: "text-gold-deep";
return (
<Card className={`${toneClass} shadow-sm`}>
<CardContent className="px-5 py-4">
<h4 className={`${toneHeading} text-sm font-semibold mb-3 flex items-center gap-2`}>
{title}
<span className="text-[0.7rem] tabular-nums opacity-70">{items.length}</span>
</h4>
{items.length === 0 ? (
<p className="text-ink-muted text-[0.78rem]"></p>
) : (
<ul className="space-y-1.5 text-[0.78rem] max-h-60 overflow-y-auto">
{items.slice(0, 20).map((p) => (
<li key={p.id} className="text-ink leading-relaxed">
{p.text}
</li>
))}
</ul>
)}
</CardContent>
</Card>
);
}
export function ComparePanel() {
const { data: corpus, isPending } = useCorpus();
const [a, setA] = useState<string | null>(null);
const [b, setB] = useState<string | null>(null);
const cmp = useCompare(a, b);
return (
<div className="space-y-6">
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-5 py-4">
<h3 className="text-navy text-base mb-3">בחר שתי החלטות להשוואה</h3>
<div className="grid gap-3 md:grid-cols-2">
{(["a", "b"] as const).map((slot) => (
<div key={slot}>
<label className="block text-[0.78rem] font-medium text-navy mb-1">
{slot === "a" ? "החלטה א" : "החלטה ב"}
</label>
<Select
disabled={isPending}
value={(slot === "a" ? a : b) ?? ""}
onValueChange={(v) => (slot === "a" ? setA(v) : setB(v))}
dir="rtl"
>
<SelectTrigger>
<SelectValue placeholder={isPending ? "טוען…" : "בחר החלטה"} />
</SelectTrigger>
<SelectContent className="max-h-[300px]">
{corpus?.map((c) => (
<SelectItem key={c.id} value={c.id}>
{c.decision_number || "—"}
{c.decision_date ? ` · ${c.decision_date}` : ""}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
))}
</div>
</CardContent>
</Card>
{a && b && a === b && (
<p className="text-ink-muted text-sm text-center">בחר שתי החלטות שונות</p>
)}
{cmp.error && (
<Card className="bg-danger-bg border-danger/40">
<CardContent className="px-6 py-5 text-danger text-center">
{cmp.error.message}
</CardContent>
</Card>
)}
{cmp.isPending && a && b && a !== b && (
<Skeleton className="h-60 w-full" />
)}
{cmp.data && (
<>
<div className="grid gap-4 md:grid-cols-2">
<SideColumn side={cmp.data.a} />
<SideColumn side={cmp.data.b} />
</div>
<div className="grid gap-4 md:grid-cols-3">
<PatternList title="דפוסים משותפים" items={cmp.data.shared} tone="shared" />
<PatternList title="רק בהחלטה א" items={cmp.data.only_a} tone="a" />
<PatternList title="רק בהחלטה ב" items={cmp.data.only_b} tone="b" />
</div>
</>
)}
</div>
);
}

View File

@@ -0,0 +1,139 @@
"use client";
import { Trash2 } 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";
/*
* Corpus tab: table of all decisions currently in the style corpus, with a
* single destructive action (remove from corpus). Uses browser confirm() for
* the confirmation — a full shadcn AlertDialog would be overkill for an
* admin-only destructive action with a server-side safety net.
*/
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 }: { item: CorpusDecision }) {
const del = useDeleteCorpusEntry();
const onDelete = async () => {
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 (
<TableRow className="border-rule hover:bg-gold-wash/30">
<TableCell className="font-semibold text-navy tabular-nums">
{item.decision_number || "—"}
</TableCell>
<TableCell className="text-ink-muted tabular-nums">
{formatDate(item.decision_date)}
</TableCell>
<TableCell>
{item.subject_categories.length === 0 ? (
<span className="text-ink-light"></span>
) : (
<div className="flex flex-wrap gap-1">
{item.subject_categories.map((s) => (
<Badge
key={s}
variant="outline"
className="text-[0.7rem] bg-gold-wash text-gold-deep border-gold/40"
>
{s}
</Badge>
))}
</div>
)}
</TableCell>
<TableCell className="text-ink-soft tabular-nums">
{formatChars(item.chars)}
</TableCell>
<TableCell className="text-ink-muted tabular-nums text-[0.78rem]">
{formatDate(item.created_at)}
</TableCell>
<TableCell className="text-end">
<Button
variant="ghost"
size="sm"
onClick={onDelete}
disabled={del.isPending}
className="text-danger hover:text-danger hover:bg-danger-bg"
>
<Trash2 className="w-4 h-4" />
</Button>
</TableCell>
</TableRow>
);
}
export function CorpusPanel() {
const { data, isPending, error } = useCorpus();
if (error) {
return (
<div className="rounded bg-danger-bg border border-danger/40 px-6 py-5 text-danger text-center">
{error.message}
</div>
);
}
return (
<div className="rounded-lg border border-rule bg-surface shadow-sm overflow-hidden">
<Table>
<TableHeader className="bg-rule-soft/60">
<TableRow className="border-rule">
<TableHead className="text-navy text-right">מס׳ החלטה</TableHead>
<TableHead className="text-navy text-right">תאריך</TableHead>
<TableHead className="text-navy text-right">נושאים</TableHead>
<TableHead className="text-navy text-right">תווים</TableHead>
<TableHead className="text-navy text-right">נוסף בתאריך</TableHead>
<TableHead className="text-navy" />
</TableRow>
</TableHeader>
<TableBody>
{isPending ? (
[...Array(4)].map((_, i) => (
<TableRow key={i} className="border-rule">
{[...Array(6)].map((_, j) => (
<TableCell key={j}>
<Skeleton className="h-4 w-24" />
</TableCell>
))}
</TableRow>
))
) : data?.length === 0 ? (
<TableRow>
<TableCell colSpan={6} className="text-center text-ink-muted py-12">
הקורפוס ריק
</TableCell>
</TableRow>
) : (
data?.map((item) => <Row key={item.id} item={item} />)
)}
</TableBody>
</Table>
</div>
);
}

View File

@@ -0,0 +1,195 @@
"use client";
import { Card, CardContent } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { SubjectDonut } from "@/components/training/subject-donut";
import { useStyleReport } from "@/lib/api/training";
function KPICard({
label,
value,
caption,
}: {
label: string;
value: string;
caption?: string;
}) {
return (
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-5 py-4 flex flex-col gap-0.5">
<span className="text-[0.72rem] uppercase tracking-[0.08em] text-ink-muted">
{label}
</span>
<span className="font-display text-[2rem] font-black leading-none text-navy">
{value}
</span>
{caption && (
<span className="text-[0.78rem] text-ink-muted mt-1">{caption}</span>
)}
</CardContent>
</Card>
);
}
export function StyleReportPanel() {
const { data, isPending, error } = useStyleReport();
if (error) {
return (
<Card className="bg-danger-bg border-danger/40">
<CardContent className="px-6 py-5 text-center text-danger">
{error.message}
</CardContent>
</Card>
);
}
if (isPending || !data) {
return (
<div className="space-y-4">
<Skeleton className="h-24 w-full" />
<Skeleton className="h-40 w-full" />
</div>
);
}
const c = data.corpus;
const dateRange =
c.date_range[0] && c.date_range[1]
? `${c.date_range[0]} ${c.date_range[1]}`
: undefined;
const total = c.decision_count;
const totalSubjects = c.subject_distribution.reduce((a, b) => a + b.count, 0);
return (
<div className="space-y-6">
{/* Headline */}
<Card className="bg-gold-wash border-gold/40 shadow-sm">
<CardContent className="px-6 py-4">
<p className="font-display text-gold-deep text-lg font-semibold leading-snug">
{c.headline}
</p>
</CardContent>
</Card>
{/* KPIs */}
<div className="grid gap-4 grid-cols-2 lg:grid-cols-4">
<KPICard label="החלטות בקורפוס" value={String(c.decision_count)} />
<KPICard
label="סך תווים"
value={`${(c.total_chars / 1000).toFixed(0)}K`}
/>
<KPICard
label="ממוצע להחלטה"
value={`${(c.avg_chars / 1000).toFixed(1)}K`}
/>
<KPICard
label="דפוסי סגנון"
value={String(data.signature_phrases.items.length)}
caption={`מתוך ${data.contribution.total_patterns} שחולצו`}
/>
</div>
{/* Subjects + anatomy */}
<div className="grid gap-6 lg:grid-cols-2">
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-6 py-5">
<h3 className="text-navy text-lg mb-4">פיזור נושאים</h3>
<SubjectDonut
segments={c.subject_distribution}
total={totalSubjects}
/>
{dateRange && (
<p className="text-[0.72rem] text-ink-muted mt-4">
טווח תאריכים: {dateRange}
</p>
)}
</CardContent>
</Card>
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-6 py-5">
<h3 className="text-navy text-lg mb-1">אנטומיה של החלטה ממוצעת</h3>
{data.anatomy.headline && (
<p className="text-[0.78rem] text-gold-deep mb-4">
{data.anatomy.headline}
</p>
)}
{data.anatomy.sections.length === 0 ? (
<p className="text-ink-muted text-sm">אין נתונים על מבנה</p>
) : (
<ul className="space-y-2.5">
{data.anatomy.sections.map((s) => {
const pct = Math.round(s.pct * 100);
return (
<li key={s.type} className="space-y-1">
<div className="flex items-center justify-between text-[0.78rem]">
<span className="text-ink-soft font-medium">
{s.label}
</span>
<span className="text-ink-muted tabular-nums">
{pct}% · {s.avg_chars.toLocaleString()} תווים
</span>
</div>
<div className="h-2 rounded bg-rule-soft overflow-hidden">
<div
className="h-full bg-gradient-to-l from-gold to-gold-deep"
style={{ width: `${pct}%` }}
/>
</div>
</li>
);
})}
</ul>
)}
</CardContent>
</Card>
</div>
{/* Signature phrases */}
<Card className="bg-surface border-rule shadow-sm">
<CardContent className="px-6 py-5">
<h3 className="text-navy text-lg mb-1">ביטויי חתימה</h3>
{data.signature_phrases.headline && (
<p className="text-[0.78rem] text-gold-deep mb-4">
{data.signature_phrases.headline}
</p>
)}
{data.signature_phrases.items.length === 0 ? (
<p className="text-ink-muted text-sm">אין ביטויים שחולצו עדיין</p>
) : (
<ol className="space-y-2">
{data.signature_phrases.items.slice(0, 12).map((p, i) => (
<li
key={`${p.type}-${i}`}
className="flex items-start gap-3 rounded border border-rule bg-parchment/40 px-3 py-2"
>
<span className="text-[0.7rem] text-ink-muted tabular-nums shrink-0 mt-0.5">
#{i + 1}
</span>
<div className="flex-1 min-w-0">
<p className="text-ink leading-relaxed text-sm">{p.text}</p>
{p.context && (
<p className="text-[0.7rem] text-ink-muted mt-0.5">
{p.context}
</p>
)}
</div>
<span
className="
shrink-0 text-[0.72rem] rounded-full
bg-gold-wash text-gold-deep border border-gold/40
px-2 py-0.5 tabular-nums
"
>
×{p.frequency}
</span>
</li>
))}
</ol>
)}
</CardContent>
</Card>
</div>
);
}

View File

@@ -0,0 +1,72 @@
"use client";
/*
* Corpus subject-distribution donut.
*
* Pure CSS conic-gradient — same recipe as the cases StatusDonut, but
* uses a palette-of-gold instead of a status-tone palette. Ported from
* legal-ai/web/static/index.html `renderHero`.
*/
const DONUT_COLORS = [
"var(--color-navy)",
"var(--color-gold)",
"var(--color-info)",
"var(--color-warn)",
"var(--color-success)",
"var(--color-ink-muted)",
"var(--color-gold-deep)",
];
export function SubjectDonut({
segments,
total,
}: {
segments: Array<{ label: string; count: number }>;
total: number;
}) {
let pct = 0;
const parts = segments.map((s, i) => {
const start = total === 0 ? 0 : (pct / total) * 360;
pct += s.count;
const end = total === 0 ? 360 : (pct / total) * 360;
return { ...s, start, end, color: DONUT_COLORS[i % DONUT_COLORS.length] };
});
const background =
total === 0
? "conic-gradient(var(--color-rule-soft) 0deg 360deg)"
: `conic-gradient(${parts
.map((p) => `${p.color} ${p.start}deg ${p.end}deg`)
.join(", ")})`;
return (
<div className="flex items-center gap-6">
<div
className="relative w-[140px] h-[140px] rounded-full shadow-sm shrink-0"
style={{ background }}
aria-label="פיזור נושאים בקורפוס"
>
<div className="absolute inset-[18px] bg-surface rounded-full flex flex-col items-center justify-center">
<span className="font-display text-2xl font-black text-navy leading-none">
{total}
</span>
<span className="text-[0.7rem] text-ink-muted mt-1">החלטות</span>
</div>
</div>
<ul className="flex flex-col gap-1.5 text-sm min-w-0">
{parts.map((p) => (
<li key={p.label} className="flex items-center gap-2">
<span
className="inline-block w-2.5 h-2.5 rounded-full shrink-0"
style={{ background: p.color }}
/>
<span className="text-ink-soft truncate">{p.label}</span>
<span className="text-ink-muted tabular-nums ms-1">{p.count}</span>
</li>
))}
</ul>
</div>
);
}