'use client' import { useState, useEffect, useCallback } from 'react' import { useTheme } from '@/lib/ThemeContext' import { useLanguage } from '@/lib/LanguageContext' import { useWorksheet } from '@/lib/worksheet-editor/WorksheetContext' interface Session { id: string name: string description?: string vocabulary_count: number page_count: number status: string created_at?: string } interface DocumentImporterProps { isOpen: boolean onClose: () => void } export function DocumentImporter({ isOpen, onClose }: DocumentImporterProps) { const { isDark } = useTheme() const { t } = useLanguage() const { canvas, saveToHistory } = useWorksheet() const [sessions, setSessions] = useState([]) const [selectedSession, setSelectedSession] = useState(null) const [selectedPage, setSelectedPage] = useState(1) const [isLoading, setIsLoading] = useState(false) const [isImporting, setIsImporting] = useState(false) const [error, setError] = useState(null) const [includeImages, setIncludeImages] = useState(true) // Load available sessions const loadSessions = useCallback(async () => { setIsLoading(true) setError(null) try { const { hostname, protocol } = window.location const apiBase = hostname === 'localhost' ? 'http://localhost:8086' : `${protocol}//${hostname}:8086` const response = await fetch(`${apiBase}/api/v1/worksheet/sessions/available`) if (!response.ok) { throw new Error(`HTTP ${response.status}`) } const data = await response.json() setSessions(data.sessions || []) } catch (err) { console.error('Failed to load sessions:', err) setError('Konnte Sessions nicht laden. Ist der Server erreichbar?') } finally { setIsLoading(false) } }, []) useEffect(() => { if (isOpen) { loadSessions() } }, [isOpen, loadSessions]) // Handle import const handleImport = async () => { if (!selectedSession || !canvas) return setIsImporting(true) setError(null) try { const { hostname, protocol } = window.location const apiBase = hostname === 'localhost' ? 'http://localhost:8086' : `${protocol}//${hostname}:8086` const response = await fetch(`${apiBase}/api/v1/worksheet/reconstruct-from-session`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ session_id: selectedSession.id, page_number: selectedPage, include_images: includeImages, regenerate_graphics: false }) }) if (!response.ok) { const errorData = await response.json().catch(() => ({ detail: 'Unknown error' })) throw new Error(errorData.detail || `HTTP ${response.status}`) } const result = await response.json() // Load canvas JSON if (result.canvas_json) { const canvasData = JSON.parse(result.canvas_json) // Clear current canvas and load new content canvas.clear() canvas.loadFromJSON(canvasData, () => { canvas.renderAll() saveToHistory(`Imported: ${selectedSession.name} Page ${selectedPage}`) }) // Close modal onClose() } } catch (err) { console.error('Import failed:', err) setError(err instanceof Error ? err.message : 'Import fehlgeschlagen') } finally { setIsImporting(false) } } if (!isOpen) return null // Glassmorphism styles const overlayStyle = 'fixed inset-0 bg-black/50 backdrop-blur-sm z-50' const modalStyle = isDark ? 'backdrop-blur-xl bg-white/10 border border-white/20' : 'backdrop-blur-xl bg-white/90 border border-black/10 shadow-2xl' const cardStyle = (selected: boolean) => isDark ? selected ? 'bg-purple-500/30 border-purple-400/50' : 'bg-white/5 border-white/10 hover:bg-white/10' : selected ? 'bg-purple-100 border-purple-300' : 'bg-white/50 border-slate-200 hover:bg-slate-50' const labelStyle = isDark ? 'text-white/70' : 'text-slate-600' return (
e.stopPropagation()} > {/* Header */}

Dokument importieren

Rekonstruiere ein Arbeitsblatt aus einer Vokabel-Session

{/* Content */}
{/* Loading State */} {isLoading && (
)} {/* Error State */} {error && (
{error}
)} {/* No Sessions */} {!isLoading && sessions.length === 0 && (

Keine Sessions gefunden

Verarbeite zuerst ein Dokument im Vokabel-Arbeitsblatt Generator

)} {/* Session List */} {!isLoading && sessions.length > 0 && (
{sessions.map((session) => ( ))}
)} {/* Page Selection */} {selectedSession && selectedSession.page_count > 1 && (
{Array.from({ length: selectedSession.page_count }, (_, i) => i + 1).map((page) => ( ))}
)} {/* Options */} {selectedSession && (
)}
{/* Footer */}
) }