'use client' import { useCallback, useEffect, useState } from 'react' import { OverlayReconstruction } from './OverlayReconstruction' const KLAUSUR_API = '/klausur-api' type Phase = 'idle' | 'running' | 'overlay' interface PaddleDirectStepProps { sessionId: string | null onNext: () => void /** Backend endpoint suffix, default: 'paddle-direct' */ endpoint?: string /** Title shown in idle state */ title?: string /** Description shown in idle state */ description?: string /** Icon shown in idle state */ icon?: string /** Button label */ buttonLabel?: string /** Running label */ runningLabel?: string /** OCR engine key to check for auto-detect */ engineKey?: string } export function PaddleDirectStep({ sessionId, onNext, endpoint = 'paddle-direct', title = 'PP-OCRv5 Direct', description = 'PP-OCRv5 (lokal via RapidOCR) erkennt alle Woerter direkt auf dem Originalbild — ohne Begradigung, Entzerrung oder Zuschnitt.', icon = '⚡', buttonLabel = 'PP-OCRv5 starten', runningLabel = 'PP-OCRv5 laeuft...', engineKey = 'paddle_direct', }: PaddleDirectStepProps) { const [phase, setPhase] = useState('idle') const [error, setError] = useState(null) const [stats, setStats] = useState<{ cells: number; rows: number; duration: number } | null>(null) // Auto-detect: if session already has matching word_result → show overlay useEffect(() => { if (!sessionId) return let cancelled = false ;(async () => { try { const res = await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}`) if (!res.ok || cancelled) return const data = await res.json() if (data.word_result?.ocr_engine === engineKey) { setPhase('overlay') } } catch { // ignore } })() return () => { cancelled = true } }, [sessionId, engineKey]) const runOcr = useCallback(async () => { if (!sessionId) return setPhase('running') setError(null) try { const res = await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/${endpoint}`, { method: 'POST', }) if (!res.ok) { const data = await res.json().catch(() => ({})) throw new Error(data.detail || `HTTP ${res.status}`) } const data = await res.json() setStats({ cells: data.summary?.total_cells || 0, rows: data.grid_shape?.rows || 0, duration: data.duration_seconds || 0, }) setPhase('overlay') } catch (e: unknown) { setError(e instanceof Error ? e.message : 'Unbekannter Fehler') setPhase('idle') } }, [sessionId, endpoint]) if (!sessionId) { return (
Bitte zuerst ein Bild hochladen.
) } if (phase === 'overlay') { return (
{stats && (
{stats.cells} Woerter erkannt {stats.rows} Zeilen {stats.duration.toFixed(1)}s
)}
) } return (
{phase === 'running' ? ( <>

{runningLabel}

Bild wird analysiert (ca. 5-30s)

) : ( <>
{icon}

{title}

{description}

{error && (
{error}
)} )}
) }