'use client' import { useState, useEffect } from 'react' const KLAUSUR_API = '/klausur-api' interface StepGridBuildProps { sessionId: string | null onNext: () => void } /** * Step 9: Grid Build. * Triggers the build-grid endpoint and shows progress. */ export function StepGridBuild({ sessionId, onNext }: StepGridBuildProps) { const [building, setBuilding] = useState(false) const [result, setResult] = useState<{ rows: number; cols: number; cells: number } | null>(null) const [error, setError] = useState('') const [autoTriggered, setAutoTriggered] = useState(false) useEffect(() => { if (!sessionId || autoTriggered) return // Check if grid already exists checkExistingGrid() // eslint-disable-next-line react-hooks/exhaustive-deps }, [sessionId]) const checkExistingGrid = async () => { if (!sessionId) return try { const res = await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/grid-editor`) if (res.ok) { const data = await res.json() if (data.grid_shape) { setResult({ rows: data.grid_shape.rows, cols: data.grid_shape.cols, cells: data.grid_shape.total_cells }) return } } } catch { /* no existing grid */ } // Auto-trigger build setAutoTriggered(true) buildGrid() } const buildGrid = async () => { if (!sessionId) return setBuilding(true) setError('') try { const res = await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sessionId}/build-grid`, { method: 'POST', }) if (!res.ok) { const data = await res.json().catch(() => ({})) throw new Error(data.detail || `Grid-Build fehlgeschlagen (${res.status})`) } const data = await res.json() const shape = data.grid_shape || { rows: 0, cols: 0, total_cells: 0 } setResult({ rows: shape.rows, cols: shape.cols, cells: shape.total_cells }) } catch (e) { setError(e instanceof Error ? e.message : String(e)) } finally { setBuilding(false) } } return (
{building && (
Grid wird aufgebaut...
)} {result && (
Grid erstellt: {result.rows} Zeilen, {result.cols} Spalten, {result.cells} Zellen
)} {error && (
{error}
)}
) }