import { useState, useEffect, useRef } from 'react' import { FileText, Save, RefreshCw, AlertTriangle, Check, X } from 'lucide-react' import { getOpenTTDConfig, saveOpenTTDConfig } from '../api' export default function OpenTTDConfigEditor({ token }) { const [content, setContent] = useState('') const [originalContent, setOriginalContent] = useState('') const [loading, setLoading] = useState(true) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(null) const [hasChanges, setHasChanges] = useState(false) const textareaRef = useRef(null) const highlightRef = useRef(null) useEffect(() => { loadConfig() }, [token]) useEffect(() => { setHasChanges(content !== originalContent) }, [content, originalContent]) const handleScroll = () => { if (highlightRef.current && textareaRef.current) { highlightRef.current.scrollTop = textareaRef.current.scrollTop highlightRef.current.scrollLeft = textareaRef.current.scrollLeft } } async function loadConfig() { setLoading(true) setError(null) try { const data = await getOpenTTDConfig(token) setContent(data.content) setOriginalContent(data.content) } catch (err) { setError('Fehler beim Laden der Config: ' + err.message) } finally { setLoading(false) } } async function handleSave() { if (!hasChanges) return setSaving(true) setError(null) setSuccess(null) try { await saveOpenTTDConfig(token, content) setOriginalContent(content) setSuccess('Config gespeichert! Server-Neustart erforderlich.') setTimeout(() => setSuccess(null), 5000) } catch (err) { setError('Fehler beim Speichern: ' + err.message) } finally { setSaving(false) } } function handleDiscard() { setContent(originalContent) setError(null) setSuccess(null) } function highlightSyntax(text) { if (!text) return '' const lines = text.split('\n') return lines.map((line) => { let highlighted = line .replace(/&/g, '&') .replace(//g, '>') // Section headers [section] if (line.trim().startsWith('[') && line.trim().endsWith(']')) { highlighted = `${highlighted}` } // Comments (;) else if (line.trim().startsWith(';')) { highlighted = `${highlighted}` } // key = value else if (line.includes('=')) { const idx = line.indexOf('=') const key = highlighted.substring(0, idx) const value = highlighted.substring(idx + 1) // Color numbers, true/false, and quoted strings let coloredValue = value .replace(/\b(true|false)\b/gi, '$1') .replace(/\b(\d+)\b/g, '$1') highlighted = `${key}=${coloredValue}` } return highlighted }).join('\n') } if (loading) { return (
Lade Config...
) } return (
{/* Header */}
openttd.cfg - Server-Einstellungen
{/* Error/Success messages */} {error && (
{error}
)} {success && (
{success}
)} {/* Editor */}