'use client' import { useEffect, useState } from 'react' import type { DewarpResult, DewarpDetection, DewarpGroundTruth } from '@/app/(admin)/ai/ocr-pipeline/types' interface DewarpControlsProps { dewarpResult: DewarpResult | null showGrid: boolean onToggleGrid: () => void onManualDewarp: (shearDegrees: number) => void onGroundTruth: (gt: DewarpGroundTruth) => void onNext: () => void isApplying: boolean } const METHOD_LABELS: Record = { vertical_edge: 'A: Vertikale Kanten', projection: 'B: Projektions-Varianz', hough_lines: 'C: Hough-Linien', text_lines: 'D: Textzeilenanalyse', manual: 'Manuell', none: 'Keine Korrektur', } /** Colour for a confidence value (0-1). */ function confColor(conf: number): string { if (conf >= 0.7) return 'text-green-600 dark:text-green-400' if (conf >= 0.5) return 'text-yellow-600 dark:text-yellow-400' return 'text-gray-400' } /** Short confidence bar (visual). */ function ConfBar({ value }: { value: number }) { const pct = Math.round(value * 100) const bg = value >= 0.7 ? 'bg-green-500' : value >= 0.5 ? 'bg-yellow-500' : 'bg-gray-400' return (
{pct}%
) } export function DewarpControls({ dewarpResult, showGrid, onToggleGrid, onManualDewarp, onGroundTruth, onNext, isApplying, }: DewarpControlsProps) { const [manualShear, setManualShear] = useState(0) const [gtFeedback, setGtFeedback] = useState<'correct' | 'incorrect' | null>(null) const [gtNotes, setGtNotes] = useState('') const [gtSaved, setGtSaved] = useState(false) const [showDetails, setShowDetails] = useState(false) // Initialize slider to auto-detected value when result arrives useEffect(() => { if (dewarpResult && dewarpResult.shear_degrees !== undefined) { setManualShear(dewarpResult.shear_degrees) } }, [dewarpResult?.shear_degrees]) const handleGroundTruth = (isCorrect: boolean) => { setGtFeedback(isCorrect ? 'correct' : 'incorrect') if (isCorrect) { onGroundTruth({ is_correct: true }) setGtSaved(true) } } const handleGroundTruthIncorrect = () => { onGroundTruth({ is_correct: false, corrected_shear: manualShear !== 0 ? manualShear : undefined, notes: gtNotes || undefined, }) setGtSaved(true) } const wasRejected = dewarpResult && dewarpResult.method_used === 'none' && (dewarpResult.detections || []).length > 0 const wasApplied = dewarpResult && dewarpResult.method_used !== 'none' && dewarpResult.method_used !== 'manual' const detections = dewarpResult?.detections || [] return (
{/* Summary banner */} {dewarpResult && (
{/* Status line */}
{wasRejected ? '\u26A0\uFE0F' : wasApplied ? '\u2705' : '\u2796'} {wasRejected ? 'Quality Gate: Korrektur verworfen (Projektion nicht verbessert)' : wasApplied ? `Korrektur angewendet: ${dewarpResult.shear_degrees.toFixed(2)}°` : dewarpResult.method_used === 'manual' ? `Manuelle Korrektur: ${dewarpResult.shear_degrees.toFixed(2)}°` : 'Keine Korrektur noetig'}
{/* Key metrics */}
Scherung:{' '} {dewarpResult.shear_degrees.toFixed(2)}°
Methode:{' '} {dewarpResult.method_used.includes('+') ? `Ensemble (${dewarpResult.method_used.split('+').map(m => METHOD_LABELS[m] || m).join(' + ')})` : METHOD_LABELS[dewarpResult.method_used] || dewarpResult.method_used}
Konfidenz:
{/* Toggles row */}
{detections.length > 0 && ( )}
{/* Detailed detections */} {showDetails && detections.length > 0 && (
Einzelne Detektoren:
{detections.map((d: DewarpDetection) => { const isUsed = dewarpResult.method_used.includes(d.method) const aboveThreshold = d.confidence >= 0.5 return (
{isUsed ? '\u2713' : aboveThreshold ? '\u2012' : '\u2717'} {METHOD_LABELS[d.method] || d.method} {d.shear_degrees.toFixed(2)}° {!aboveThreshold && ( (unter Schwelle) )}
) })}
{wasRejected && (
Die Korrektur wurde verworfen, weil die horizontale Projektions-Varianz nach Anwendung nicht besser war als vorher.
)}
)}
)} {/* Manual shear angle slider */} {dewarpResult && (
Scherwinkel (manuell)
-2.0° setManualShear(parseInt(e.target.value) / 100)} className="flex-1 h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700 accent-teal-500" /> +2.0° {manualShear.toFixed(2)}°

Scherung der vertikalen Achse in Grad. Positiv = Spalten nach rechts kippen, negativ = nach links.

)} {/* Ground Truth */} {dewarpResult && (
Spalten vertikal ausgerichtet?

Pruefen ob die Spaltenraender jetzt senkrecht zum Raster stehen.

{!gtSaved ? (
{gtFeedback === 'incorrect' && (