import { useState, useEffect } from 'react' import { api } from '../utils/api' import PromptGenerator from './PromptGenerator' export default function PromptEditModal({ prompt, onSave, onClose }) { const [name, setName] = useState('') const [slug, setSlug] = useState('') const [displayName, setDisplayName] = useState('') const [description, setDescription] = useState('') const [category, setCategory] = useState('ganzheitlich') const [template, setTemplate] = useState('') const [active, setActive] = useState(true) const [preview, setPreview] = useState(null) const [unknownPlaceholders, setUnknownPlaceholders] = useState([]) const [showGenerator, setShowGenerator] = useState(false) const [showPlaceholders, setShowPlaceholders] = useState(false) const [placeholders, setPlaceholders] = useState({}) const [placeholderFilter, setPlaceholderFilter] = useState('') const [templateRef, setTemplateRef] = useState(null) const [optimization, setOptimization] = useState(null) const [loading, setLoading] = useState(false) const [saving, setSaving] = useState(false) const categories = [ { id: 'körper', label: 'Körper' }, { id: 'ernährung', label: 'Ernährung' }, { id: 'training', label: 'Training' }, { id: 'schlaf', label: 'Schlaf' }, { id: 'vitalwerte', label: 'Vitalwerte' }, { id: 'ziele', label: 'Ziele' }, { id: 'ganzheitlich', label: 'Ganzheitlich' } ] useEffect(() => { if (prompt) { setName(prompt.name || '') setSlug(prompt.slug || '') setDisplayName(prompt.display_name || '') setDescription(prompt.description || '') setCategory(prompt.category || 'ganzheitlich') setTemplate(prompt.template || '') setActive(prompt.active ?? true) } }, [prompt]) const handlePreview = async () => { try { setLoading(true) const result = await api.previewPrompt(template) setPreview(result.resolved) setUnknownPlaceholders(result.unknown_placeholders || []) } catch (e) { alert('Fehler bei Vorschau: ' + e.message) } finally { setLoading(false) } } const handleOptimize = async () => { if (!prompt?.id) { alert('Prompt muss erst gespeichert werden bevor er optimiert werden kann') return } try { setLoading(true) const result = await api.optimizePrompt(prompt.id) setOptimization(result) } catch (e) { alert('Fehler bei Optimierung: ' + e.message) } finally { setLoading(false) } } const handleApplyOptimized = () => { if (optimization?.optimized_prompt) { setTemplate(optimization.optimized_prompt) setOptimization(null) } } const handleSave = async () => { if (!name.trim()) { alert('Bitte Titel eingeben') return } if (!template.trim()) { alert('Bitte Template eingeben') return } try { setSaving(true) if (prompt?.id) { // Update existing await api.updatePrompt(prompt.id, { name, display_name: displayName || null, description, category, template, active }) } else { // Create new if (!slug.trim()) { alert('Bitte Slug eingeben') return } await api.createPrompt({ name, slug, display_name: displayName || null, description, category, template, active }) } onSave() } catch (e) { alert('Fehler beim Speichern: ' + e.message) } finally { setSaving(false) } } const handleGeneratorResult = (generated) => { setName(generated.suggested_title) setCategory(generated.suggested_category) setTemplate(generated.template) setShowGenerator(false) // Auto-generate slug if new prompt if (!prompt?.id) { const autoSlug = generated.suggested_title .toLowerCase() .replace(/[^a-z0-9]+/g, '_') .replace(/^_+|_+$/g, '') setSlug(autoSlug) } } const loadPlaceholders = async () => { try { const data = await api.listPlaceholders() setPlaceholders(data) setShowPlaceholders(true) setPlaceholderFilter('') } catch (e) { alert('Fehler beim Laden der Platzhalter: ' + e.message) } } const insertPlaceholder = (key) => { if (!templateRef) { // Fallback: append at end setTemplate(prev => prev + ` {{${key}}}`) } else { // Insert at cursor position const start = templateRef.selectionStart const end = templateRef.selectionEnd const text = template const before = text.substring(0, start) const after = text.substring(end) const inserted = `{{${key}}}` setTemplate(before + inserted + after) // Set cursor position after inserted placeholder setTimeout(() => { templateRef.selectionStart = templateRef.selectionEnd = start + inserted.length templateRef.focus() }, 0) } setShowPlaceholders(false) } return (
{preview}
{optimization.changes_summary}
{template}
{optimization.optimized_prompt}