import React, { useState, useEffect } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { ArrowLeft, Save, Globe, BookOpen } from 'lucide-react' import { useStoryStore } from '../../store/storyStore' import { useUIStore } from '../../store/uiStore' import { Button } from '../../components/ui/Button' import { AISuggestionPanel } from '../../components/writer/AISuggestionPanel' export const EditChapterPage: React.FC = () => { const { chapterId } = useParams<{ chapterId: string }>() const navigate = useNavigate() const { stories, chapters, updateChapter } = useStoryStore() const { addToast } = useUIStore() const [saving, setSaving] = useState(false) const chapter = chapters.find(c => c.chapter_id === Number(chapterId)) const story = chapter ? stories.find(s => s.story_id === chapter.story_id) : null const [form, setForm] = useState({ title: '', content: '' }) useEffect(() => { if (chapter) { setForm({ title: chapter.title, content: chapter.content }) } }, [chapter?.chapter_id]) if (!chapter || !story) { return (

Chapter not found

) } const wordCount = form.content.trim().split(/\s+/).filter(Boolean).length const handleSave = async (publish?: boolean) => { setSaving(true) await new Promise(r => setTimeout(r, 400)) updateChapter(chapter.chapter_id, { ...form, word_count: wordCount, is_published: publish !== undefined ? publish : chapter.is_published, }) addToast('Chapter saved!') setSaving(false) } return (
{/* Header */}

{story.title} · Chapter {chapter.chapter_number}

Edit Chapter

{!chapter.is_published && ( )}
{/* Editor */}
setForm(f => ({ ...f, title: e.target.value }))} className="w-full px-4 py-3 bg-slate-800 border border-slate-700 rounded-xl text-white focus:outline-none focus:border-indigo-500 font-serif text-lg" />
{wordCount.toLocaleString()} words