import { useEffect, useState, useCallback } from 'react' import { useParams } from 'react-router-dom' import { apiFetch } from '@core/lib/api' import { useAppPath } from '@core/hooks/useAppPath' import { Card, CardContent, CardHeader, CardTitle } from '@core/components/ui/card' import { Button } from '@core/components/ui/button' import { Badge } from '@core/components/ui/badge' import { Skeleton } from '@core/components/ui/skeleton' import { Textarea } from '@core/components/ui/textarea' import { Label } from '@core/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@core/components/ui/select' import { Tabs, TabsContent, TabsList, TabsTrigger, } from '@core/components/ui/tabs' import { CheckCircle2, XCircle, AlertTriangle, ChevronRight, ArrowRight, RefreshCw, Save, Zap, FileCode2, Hash, TestTube2, } from 'lucide-react' // ─── TYPES ──────────────────────────────────────────────────────────────────── interface ChunkContract { id: string chunk_name: string file_path: string line_start: number line_end: number chunk_type: string macro: string micro: string input_spec: string output_spec: string side_effects: string stability: string test_unit_path: string | null test_e2e_path: string | null doc_confidence: string boundary_confidence: string stored_hash: string | null hash_status: string is_dead_code: boolean callers: { chunk_id: string; chunk_name: string; file_path: string }[] callees: { chunk_id: string; chunk_name: string; file_path: string }[] } interface EmbeddingStatus { coverage_pct: number embedded_chunks: number total_chunks: number } // ─── CONFIDENCE / STATUS DISPLAY ───────────────────────────────────────────── const DOC_CONFIDENCE_CONFIG: Record = { human_verified: { label: 'Human Verified', color: 'text-green-600', icon: CheckCircle2 }, ai_generated: { label: 'AI Generated', color: 'text-blue-600', icon: Zap }, unverified_legacy: { label: 'Unverified', color: 'text-amber-600', icon: AlertTriangle }, } const STABILITY_BADGE: Record = { stable: 'default', internal: 'secondary', experimental: 'outline', } function HashStatusBadge({ status }: { status: string }) { if (status === 'current') return ( Current ) if (status === 'drift_detected') return ( Drift Detected ) return ( Unknown ) } // ─── CHUNK LINK CARD ────────────────────────────────────────────────────────── function ChunkLinkCard({ chunk, label, projectId, }: { chunk: { chunk_id: string; chunk_name: string; file_path: string } label: string projectId: string }) { const appPath = useAppPath() return (
{chunk.chunk_name}
{chunk.file_path}
) } // ─── CONTRACT EDITOR ────────────────────────────────────────────────────────── function ContractEditor({ contract, projectId, onSave, }: { contract: ChunkContract projectId: string onSave: (updated: Partial) => void }) { const [form, setForm] = useState({ macro: contract.macro || '', micro: contract.micro || '', input_spec: contract.input_spec || '', output_spec: contract.output_spec || '', side_effects: contract.side_effects || '', stability: contract.stability || 'internal', test_unit_path: contract.test_unit_path || '', test_e2e_path: contract.test_e2e_path || '', doc_confidence: contract.doc_confidence || 'unverified_legacy', }) const [saving, setSaving] = useState(false) const [saved, setSaved] = useState(false) const handleSave = async () => { setSaving(true) try { const res = await apiFetch(`/api/admin-data?id=${contract.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ entity: 'items', data: { ...form, test_unit_path: form.test_unit_path || null, test_e2e_path: form.test_e2e_path || null, }, }), }) if (res.ok) { setSaved(true) onSave(form) setTimeout(() => setSaved(false), 2000) } } finally { setSaving(false) } } const field = ( key: keyof typeof form, label: string, placeholder: string, rows = 2 ) => (