'use client' import { useCallback, useEffect, useRef, useState } from 'react' import type { GridResult, GridCell, WordEntry, WordGroundTruth } from '@/app/(admin)/ai/ocr-pipeline/types' const KLAUSUR_API = '/klausur-api' /** Render text with \n as line breaks */ function MultilineText({ text }: { text: string }) { if (!text) return const lines = text.split('\n') if (lines.length === 1) return <>{text} return <>{lines.map((line, i) => ( {line}{i < lines.length - 1 &&
}
))} } /** Column type → human-readable header */ function colTypeLabel(colType: string): string { const labels: Record = { column_en: 'English', column_de: 'Deutsch', column_example: 'Example', column_text: 'Text', column_marker: 'Marker', page_ref: 'Seite', } return labels[colType] || colType.replace('column_', '') } /** Column type → color class */ function colTypeColor(colType: string): string { const colors: Record = { column_en: 'text-blue-600 dark:text-blue-400', column_de: 'text-green-600 dark:text-green-400', column_example: 'text-orange-600 dark:text-orange-400', column_text: 'text-purple-600 dark:text-purple-400', column_marker: 'text-gray-500 dark:text-gray-400', } return colors[colType] || 'text-gray-600 dark:text-gray-400' } interface StepWordRecognitionProps { sessionId: string | null onNext: () => void goToStep: (step: number) => void /** Skip _heal_row_gaps in cell grid (better overlay positioning) */ skipHealGaps?: boolean } export function StepWordRecognition({ sessionId, onNext, goToStep, skipHealGaps = false }: StepWordRecognitionProps) { const [gridResult, setGridResult] = useState(null) const [detecting, setDetecting] = useState(false) const [error, setError] = useState(null) const [gtNotes, setGtNotes] = useState('') const [gtSaved, setGtSaved] = useState(false) // Step-through labeling state const [activeIndex, setActiveIndex] = useState(0) const [editedEntries, setEditedEntries] = useState([]) const [editedCells, setEditedCells] = useState([]) const [mode, setMode] = useState<'overview' | 'labeling'>('overview') const [ocrEngine, setOcrEngine] = useState<'auto' | 'tesseract' | 'rapid' | 'paddle'>('auto') const [usedEngine, setUsedEngine] = useState('') const [pronunciation, setPronunciation] = useState<'british' | 'american'>('british') const [gridMethod, setGridMethod] = useState<'v2' | 'words_first'>('v2') // Streaming progress state const [streamProgress, setStreamProgress] = useState<{ current: number; total: number } | null>(null) const enRef = useRef(null) const tableEndRef = useRef(null) const isVocab = gridResult?.layout === 'vocab' useEffect(() => { if (!sessionId) return // Always run fresh detection — word-lookup is fast (~0.03s) // and avoids stale cached results from previous pipeline versions. runAutoDetection() // eslint-disable-next-line react-hooks/exhaustive-deps }, [sessionId]) const applyGridResult = (data: GridResult) => { setGridResult(data) setUsedEngine(data.ocr_engine || '') if (data.layout === 'vocab' && data.entries) { initEntries(data.entries) } if (data.cells) { setEditedCells(data.cells.map(c => ({ ...c, status: c.status || 'pending' }))) } } const initEntries = (entries: WordEntry[]) => { setEditedEntries(entries.map(e => ({ ...e, status: e.status || 'pending' }))) setActiveIndex(0) } const runAutoDetection = useCallback(async (engine?: string) => { if (!sessionId) return const eng = engine || ocrEngine setDetecting(true) setError(null) setStreamProgress(null) setEditedCells([]) setEditedEntries([]) setGridResult(null) try { // Retry once if initial request fails (e.g. after container restart, // session cache may not be warm yet when navigating via wizard) let res: Response | null = null for (let attempt = 0; attempt < 2; attempt++) { res = await fetch( `${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/words?stream=${gridMethod === 'v2' ? 'true' : 'false'}&engine=${eng}&pronunciation=${pronunciation}${skipHealGaps ? '&skip_heal_gaps=true' : ''}&grid_method=${gridMethod}`, { method: 'POST' }, ) if (res.ok) break if (attempt === 0 && (res.status === 400 || res.status === 404)) { // Wait briefly for cache to warm up, then retry await new Promise(r => setTimeout(r, 2000)) continue } break } if (!res || !res.ok) { const err = await res?.json().catch(() => ({ detail: res?.statusText })) || { detail: 'Worterkennung fehlgeschlagen' } throw new Error(err.detail || 'Worterkennung fehlgeschlagen') } // words_first returns plain JSON (no streaming) if (gridMethod === 'words_first') { const data = await res.json() as GridResult applyGridResult(data) return } const reader = res.body!.getReader() const decoder = new TextDecoder() let buffer = '' let streamLayout: string | null = null let streamColumnsUsed: GridResult['columns_used'] = [] let streamGridShape: GridResult['grid_shape'] | null = null let streamCells: GridCell[] = [] while (true) { const { done, value } = await reader.read() if (done) break buffer += decoder.decode(value, { stream: true }) // Parse SSE events (separated by \n\n) while (buffer.includes('\n\n')) { const idx = buffer.indexOf('\n\n') const chunk = buffer.slice(0, idx).trim() buffer = buffer.slice(idx + 2) if (!chunk.startsWith('data: ')) continue const dataStr = chunk.slice(6) // strip "data: " let event: any try { event = JSON.parse(dataStr) } catch { continue } if (event.type === 'meta') { streamLayout = event.layout || 'generic' streamGridShape = event.grid_shape || null // Show partial grid result so UI renders structure setGridResult(prev => ({ ...prev, layout: event.layout || 'generic', grid_shape: event.grid_shape, columns_used: [], cells: [], summary: { total_cells: event.grid_shape?.total_cells || 0, non_empty_cells: 0, low_confidence: 0 }, duration_seconds: 0, ocr_engine: '', } as GridResult)) } if (event.type === 'columns') { streamColumnsUsed = event.columns_used || [] setGridResult(prev => prev ? { ...prev, columns_used: streamColumnsUsed } : prev) } if (event.type === 'cell') { const cell: GridCell = { ...event.cell, status: 'pending' } streamCells = [...streamCells, cell] setEditedCells(streamCells) setStreamProgress(event.progress) // Auto-scroll table to bottom setTimeout(() => tableEndRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }), 16) } if (event.type === 'complete') { // Build final GridResult const finalResult: GridResult = { cells: streamCells, grid_shape: streamGridShape || { rows: 0, cols: 0, total_cells: streamCells.length }, columns_used: streamColumnsUsed, layout: streamLayout || 'generic', image_width: 0, image_height: 0, duration_seconds: event.duration_seconds || 0, ocr_engine: event.ocr_engine || '', summary: event.summary || {}, } // If vocab: apply post-processed entries from complete event if (event.vocab_entries) { finalResult.entries = event.vocab_entries finalResult.vocab_entries = event.vocab_entries finalResult.entry_count = event.vocab_entries.length } applyGridResult(finalResult) setUsedEngine(event.ocr_engine || '') setStreamProgress(null) } } } } catch (e) { setError(e instanceof Error ? e.message : 'Unbekannter Fehler') } finally { setDetecting(false) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [sessionId, ocrEngine, pronunciation, gridMethod]) const handleGroundTruth = useCallback(async (isCorrect: boolean) => { if (!sessionId) return const gt: WordGroundTruth = { is_correct: isCorrect, corrected_entries: isCorrect ? undefined : (isVocab ? editedEntries : undefined), notes: gtNotes || undefined, } try { await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/ground-truth/words`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(gt), }) setGtSaved(true) } catch (e) { console.error('Ground truth save failed:', e) } }, [sessionId, gtNotes, editedEntries, isVocab]) // Vocab mode: update entry field const updateEntry = (index: number, field: 'english' | 'german' | 'example', value: string) => { setEditedEntries(prev => prev.map((e, i) => i === index ? { ...e, [field]: value, status: 'edited' as const } : e )) } // Generic mode: update cell text const updateCell = (cellId: string, value: string) => { setEditedCells(prev => prev.map(c => c.cell_id === cellId ? { ...c, text: value, status: 'edited' as const } : c )) } // Step-through: confirm current row (always cell-based) const confirmEntry = () => { const rowCells = getRowCells(activeIndex) const cellIds = new Set(rowCells.map(c => c.cell_id)) setEditedCells(prev => prev.map(c => cellIds.has(c.cell_id) ? { ...c, status: c.status === 'edited' ? 'edited' : 'confirmed' } : c )) const maxIdx = getUniqueRowCount() - 1 if (activeIndex < maxIdx) { setActiveIndex(activeIndex + 1) } } // Step-through: skip current row const skipEntry = () => { const rowCells = getRowCells(activeIndex) const cellIds = new Set(rowCells.map(c => c.cell_id)) setEditedCells(prev => prev.map(c => cellIds.has(c.cell_id) ? { ...c, status: 'skipped' as const } : c )) const maxIdx = getUniqueRowCount() - 1 if (activeIndex < maxIdx) { setActiveIndex(activeIndex + 1) } } // Helper: get unique row indices from cells const getUniqueRowCount = () => { if (!editedCells.length) return 0 return new Set(editedCells.map(c => c.row_index)).size } // Helper: get cells for a given row index (by position in sorted unique rows) const getRowCells = (rowPosition: number) => { const uniqueRows = [...new Set(editedCells.map(c => c.row_index))].sort((a, b) => a - b) const rowIdx = uniqueRows[rowPosition] return editedCells.filter(c => c.row_index === rowIdx) } // Focus english input when active entry changes in labeling mode useEffect(() => { if (mode === 'labeling' && enRef.current) { enRef.current.focus() } }, [activeIndex, mode]) // Keyboard shortcuts in labeling mode useEffect(() => { if (mode !== 'labeling') return const handler = (e: KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault() confirmEntry() } else if (e.key === 'ArrowDown' && e.ctrlKey) { e.preventDefault() skipEntry() } else if (e.key === 'ArrowUp' && e.ctrlKey) { e.preventDefault() if (activeIndex > 0) setActiveIndex(activeIndex - 1) } } window.addEventListener('keydown', handler) return () => window.removeEventListener('keydown', handler) // eslint-disable-next-line react-hooks/exhaustive-deps }, [mode, activeIndex, editedEntries, editedCells]) if (!sessionId) { return (
🔤

Schritt 5: Worterkennung

Bitte zuerst Schritte 1-4 abschliessen.

) } const overlayUrl = `${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/image/words-overlay` const dewarpedUrl = `${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/image/cropped` const confColor = (conf: number) => { if (conf >= 70) return 'text-green-600 dark:text-green-400' if (conf >= 50) return 'text-yellow-600 dark:text-yellow-400' return 'text-red-600 dark:text-red-400' } const statusBadge = (status?: string) => { const map: Record = { pending: 'bg-gray-100 dark:bg-gray-700 text-gray-500', confirmed: 'bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-400', edited: 'bg-blue-100 dark:bg-blue-900/30 text-blue-700 dark:text-blue-400', skipped: 'bg-orange-100 dark:bg-orange-900/30 text-orange-700 dark:text-orange-400', } return map[status || 'pending'] || map.pending } const summary = gridResult?.summary const columnsUsed = gridResult?.columns_used || [] const gridShape = gridResult?.grid_shape // Counts for labeling progress (always cell-based) const confirmedRowIds = new Set( editedCells.filter(c => c.status === 'confirmed' || c.status === 'edited').map(c => c.row_index) ) const confirmedCount = confirmedRowIds.size const totalCount = getUniqueRowCount() // Group cells by row for generic table display const cellsByRow: Map = new Map() for (const cell of editedCells) { const existing = cellsByRow.get(cell.row_index) || [] existing.push(cell) cellsByRow.set(cell.row_index, existing) } const sortedRowIndices = [...cellsByRow.keys()].sort((a, b) => a - b) return (
{/* Loading with streaming progress */} {detecting && (
{streamProgress ? `Zelle ${streamProgress.current}/${streamProgress.total} erkannt...` : 'Worterkennung startet...'}
{streamProgress && streamProgress.total > 0 && (
)}
)} {/* Layout badge + Mode toggle */} {gridResult && (
{/* Layout badge */} {isVocab ? 'Vokabel-Layout' : 'Generisch'} {gridShape && ( {gridShape.rows}×{gridShape.cols} = {gridShape.total_cells} Zellen )}
)} {/* Overview mode */} {mode === 'overview' && ( <> {/* Images: overlay vs clean */}
Mit Grid-Overlay
{gridResult ? ( // eslint-disable-next-line @next/next/no-img-element Wort-Overlay ) : (
{detecting ? 'Erkenne Woerter...' : 'Keine Daten'}
)}
Entzerrtes Bild
{/* eslint-disable-next-line @next/next/no-img-element */} Entzerrt
{/* Result summary (only after streaming completes) */} {gridResult && summary && !detecting && (

Ergebnis: {summary.non_empty_cells}/{summary.total_cells} Zellen mit Text ({sortedRowIndices.length} Zeilen, {columnsUsed.length} Spalten)

{gridResult.duration_seconds}s
{/* Summary badges */}
Zellen: {summary.non_empty_cells}/{summary.total_cells} {columnsUsed.map((col, i) => ( C{col.index}: {colTypeLabel(col.type)} ))} {summary.low_confidence > 0 && ( Unsicher: {summary.low_confidence} )}
{/* Entry/Cell table */}
{/* Unified dynamic table — columns driven by columns_used */} {columnsUsed.map((col, i) => ( ))} {sortedRowIndices.map((rowIdx, posIdx) => { const rowCells = cellsByRow.get(rowIdx) || [] const avgConf = rowCells.length ? Math.round(rowCells.reduce((s, c) => s + c.confidence, 0) / rowCells.length) : 0 return ( { setActiveIndex(posIdx); setMode('labeling') }} > {columnsUsed.map((col) => { const cell = rowCells.find(c => c.col_index === col.index) return ( ) })} ) })}
Zeile {colTypeLabel(col.type)} Conf
R{String(rowIdx).padStart(2, '0')} {avgConf}%
)} {/* Streaming cell table (shown while detecting, before complete) */} {detecting && editedCells.length > 0 && !gridResult?.summary?.non_empty_cells && (

Live: {editedCells.length} Zellen erkannt...

{columnsUsed.map((col, i) => ( ))} {(() => { const liveByRow: Map = new Map() for (const cell of editedCells) { const existing = liveByRow.get(cell.row_index) || [] existing.push(cell) liveByRow.set(cell.row_index, existing) } const liveSorted = [...liveByRow.keys()].sort((a, b) => a - b) return liveSorted.map(rowIdx => { const rowCells = liveByRow.get(rowIdx) || [] const avgConf = rowCells.length ? Math.round(rowCells.reduce((s, c) => s + c.confidence, 0) / rowCells.length) : 0 return ( {columnsUsed.map((col) => { const cell = rowCells.find(c => c.col_index === col.index) return ( ) })} ) }) })()}
Zelle {colTypeLabel(col.type)} Conf
R{String(rowIdx).padStart(2, '0')} {avgConf}%
)} )} {/* Labeling mode */} {mode === 'labeling' && editedCells.length > 0 && (
{/* Left 2/3: Image with highlighted active row */}
Zeile {activeIndex + 1} von {getUniqueRowCount()}
{/* eslint-disable-next-line @next/next/no-img-element */} Wort-Overlay {/* Highlight overlay for active row */} {(() => { const rowCells = getRowCells(activeIndex) return rowCells.map(cell => (
)) })()}
{/* Right 1/3: Editable fields */}
{/* Navigation */}
{activeIndex + 1} / {getUniqueRowCount()}
{/* Status badge */}
{(() => { const rowCells = getRowCells(activeIndex) const avgConf = rowCells.length ? Math.round(rowCells.reduce((s, c) => s + c.confidence, 0) / rowCells.length) : 0 return ( {avgConf}% Konfidenz ) })()}
{/* Editable fields — one per column, driven by columns_used */}
{(() => { const rowCells = getRowCells(activeIndex) return columnsUsed.map((col, colIdx) => { const cell = rowCells.find(c => c.col_index === col.index) if (!cell) return null return (
{cell.cell_id}
{/* Cell crop */}