'use client' import { useState, useCallback } from 'react' import { DOCUMENT_CATEGORIES, type DocumentCategory } from '@/app/(admin)/ai/ocr-pipeline/types' const KLAUSUR_API = '/klausur-api' interface StepUploadProps { onUploaded: (sessionId: string) => void } export function StepUpload({ onUploaded }: StepUploadProps) { const [dragging, setDragging] = useState(false) const [uploading, setUploading] = useState(false) const [title, setTitle] = useState('') const [category, setCategory] = useState('vokabelseite') const [error, setError] = useState('') const handleUpload = useCallback(async (file: File) => { setUploading(true) setError('') try { const formData = new FormData() formData.append('file', file) if (title.trim()) formData.append('name', title.trim()) const res = await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions`, { method: 'POST', body: formData, }) if (!res.ok) { const data = await res.json().catch(() => ({})) throw new Error(data.detail || `Upload fehlgeschlagen (${res.status})`) } const data = await res.json() const sid = data.session_id || data.id // Set category if (category) { await fetch(`${KLAUSUR_API}/api/v1/ocr-pipeline/sessions/${sid}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ document_category: category }), }) } onUploaded(sid) } catch (e) { setError(e instanceof Error ? e.message : String(e)) } finally { setUploading(false) } }, [title, category, onUploaded]) const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault() setDragging(false) const file = e.dataTransfer.files[0] if (file) handleUpload(file) }, [handleUpload]) const handleFileSelect = useCallback((e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) handleUpload(file) }, [handleUpload]) return (
{/* Title input */}
setTitle(e.target.value)} placeholder="z.B. Vokabeln Unit 3" className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-sm" />
{/* Category selector */}
{DOCUMENT_CATEGORIES.map(cat => ( ))}
{/* Drop zone */}
{ e.preventDefault(); setDragging(true) }} onDragLeave={() => setDragging(false)} onDrop={handleDrop} className={`border-2 border-dashed rounded-xl p-12 text-center transition-colors ${ dragging ? 'border-teal-400 bg-teal-50 dark:bg-teal-900/20' : 'border-gray-300 dark:border-gray-600 hover:border-gray-400' }`} > {uploading ? (
Wird hochgeladen...
) : ( <>
📤
Bild oder PDF hierher ziehen
)}
{error && (
{error}
)}
) }