import React, { useState } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { ArrowLeft, BookOpen, Users, Calendar, Plus, BookmarkPlus } from 'lucide-react' import { useStoryStore } from '../../store/storyStore' import { useAuthStore } from '../../store/authStore' import { useUIStore } from '../../store/uiStore' import { Button } from '../../components/ui/Button' import { GenreBadge } from '../../components/ui/Badge' import { Avatar } from '../../components/ui/Avatar' import { ChapterList } from '../../components/story/ChapterList' import { CommentSection } from '../../components/story/CommentSection' import { LikeButton } from '../../components/story/LikeButton' import { Modal } from '../../components/ui/Modal' import { getGenreGradient } from '../../components/story/GenreBadge' import { ReadingListItem } from '../../types' export const StoryDetailPage: React.FC = () => { const { id } = useParams<{ id: string }>() const navigate = useNavigate() const { stories, chapters, collaborations, readingLists, addStoryToList, createReadingList } = useStoryStore() const { currentUser } = useAuthStore() const { addToast } = useUIStore() const [listModalOpen, setListModalOpen] = useState(false) const [newListName, setNewListName] = useState('') const [liveLikes, setLiveLikes] = useState(null) const [liveComments, setLiveComments] = useState(null) const story = stories.find(s => s.story_id === Number(id)) if (!story) { return (

Story not found

) } const storyChapters = chapters.filter(c => c.story_id === story.story_id) const storyCollabs = collaborations.filter(c => c.story_id === story.story_id) const gradient = getGenreGradient(story.genres[0]) const myLists = currentUser ? readingLists.filter(l => l.user_id === currentUser.user_id) : [] const handleAddToList = async (listId: number) => { const list = readingLists.find(l => l.list_id === listId) if (!list) return if (list.stories.some(s => s.story_id === story.story_id)) { addToast('Already in this list', 'info') return } const item: ReadingListItem = { item_id: Date.now(), list_id: listId, story_id: story.story_id, story_title: story.title, author_username: story.author_username, added_at: new Date().toISOString(), genres: story.genres, } try { await addStoryToList(listId, item) addToast(`Added to "${list.name}"!`) } catch (err: any) { const msg = err?.response?.data?.message || '' if (msg.includes('already') || msg.includes('duplicate') || err?.response?.status === 400) { addToast('Already in this list', 'info') } else { addToast('Failed to add to list.', 'error') } } setListModalOpen(false) } const handleCreateList = async () => { if (!newListName.trim() || !currentUser) return const newList = { list_id: Date.now(), user_id: currentUser.user_id, username: currentUser.username, name: newListName.trim(), is_public: false, created_at: new Date().toISOString(), stories: [], } try { const realListId = await createReadingList(newList) await addStoryToList(realListId, { item_id: Date.now() + 1, list_id: realListId, story_id: story.story_id, story_title: story.title, author_username: story.author_username, added_at: new Date().toISOString(), genres: story.genres, }) addToast(`Created "${newListName}" and added story!`) } catch { addToast('Failed to create list.', 'error') } setNewListName('') setListModalOpen(false) } const isOwner = currentUser?.user_id === story.user_id const isCollaborator = currentUser ? storyCollabs.some(c => c.user_id === currentUser.user_id) : false return (
{/* Back */} {/* Hero */}
{story.cover_image && ( {story.title} )}
{story.genres.map(g => )} {story.mature_content && ( 18+ )}

{story.title}

{story.short_description}

{story.author_username}
{story.total_chapters} chapters
{new Date(story.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
{/* Main content */}
{/* Action bar */}
{currentUser && ( )} {(isOwner || isCollaborator) && ( )}
{/* Story content */} {story.content && (

{story.content}

)} {/* Chapters */}

Chapters

{(isOwner || isCollaborator) && ( )}
{/* Comments */}
{/* Sidebar */}
{/* Story info */}

Story Info

Status {story.status}
Chapters {story.total_chapters}
Likes {(liveLikes ?? story.total_likes).toLocaleString()}
Comments {liveComments ?? story.total_comments}
Published {new Date(story.created_at).toLocaleDateString()}
{/* Collaborators */} {storyCollabs.length > 0 && (

Collaborators

{storyCollabs.map(c => (

@{c.username}

{c.role}

))}
)}
{/* Reading list modal */} setListModalOpen(false)} title="Save to Reading List">
{myLists.length > 0 ? ( <>

Select a list:

{myLists.map(list => ( ))}

Or create a new list:

setNewListName(e.target.value)} placeholder="List name..." className="flex-1 px-3 py-2 bg-slate-800 border border-slate-700 rounded-xl text-white text-sm placeholder-slate-500 focus:outline-none focus:border-indigo-500" />
) : (

You don't have any reading lists yet. Create one:

setNewListName(e.target.value)} placeholder="My Favorites..." className="flex-1 px-3 py-2 bg-slate-800 border border-slate-700 rounded-xl text-white text-sm placeholder-slate-500 focus:outline-none focus:border-indigo-500" />
)}
) }