import { useEffect, useState } from 'react' import Modal from '../../../components/Modal.jsx' import { api } from '../../../api/client.js' export default function WikiEditor({ slug, onClose, onSaved }) { const isEdit = Boolean(slug) const [form, setForm] = useState({ slug: '', title: '', body: '' }) const [loading, setLoading] = useState(isEdit) const [busy, setBusy] = useState(false) const [error, setError] = useState('') useEffect(() => { if (!isEdit) return let active = true api.admin .getWiki(slug) .then((p) => active && setForm({ slug: p.slug, title: p.title, body: p.body || '' })) .catch(() => active && setError('Could not load this page.')) .finally(() => active && setLoading(false)) return () => { active = false } }, [slug, isEdit]) const set = (k) => (e) => setForm((f) => ({ ...f, [k]: e.target.value })) async function save() { if (!form.title.trim()) return setError('Title is required.') if (!isEdit && !/^[a-z0-9-]+$/.test(form.slug)) return setError('Slug must be lowercase letters, numbers, and dashes.') setBusy(true) setError('') try { if (isEdit) await api.admin.updateWiki(slug, { title: form.title.trim(), body: form.body }) else await api.admin.createWiki({ slug: form.slug, title: form.title.trim(), body: form.body }) onSaved() } catch (err) { setError(err.message || 'Could not save.') setBusy(false) } } async function remove() { if (!confirm('Delete this wiki page?')) return setBusy(true) try { await api.admin.deleteWiki(slug) onSaved() } catch (err) { setError(err.message || 'Could not delete.') setBusy(false) } } return ( {isEdit && ( )} } > {loading ? ( ) : (
{error &&

{error}

}