import React, { useState } from 'react'
import { useParams, useNavigate } from 'react-router-dom'
import { ArrowLeft, BookOpen, Eye, 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 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 = (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,
}
addStoryToList(listId, item)
addToast(`Added to "${list.name}"!`)
setListModalOpen(false)
}
const handleCreateList = () => {
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: [{
item_id: Date.now() + 1,
list_id: Date.now(),
story_id: story.story_id,
story_title: story.title,
author_username: story.author_username,
added_at: new Date().toISOString(),
genres: story.genres,
}],
}
createReadingList(newList)
addToast(`Created "${newListName}" and added story!`)
setNewListName('')
setListModalOpen(false)
}
const isOwner = currentUser?.user_id === story.user_id
return (
{/* Back */}
{/* Hero */}
{story.genres.map(g => )}
{story.mature_content && (
18+
)}
{story.title}
{story.short_description}
{story.total_views.toLocaleString()} views
{story.total_chapters} chapters
{new Date(story.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
{/* Main content */}
{/* Action bar */}
{currentUser && (
)}
{isOwner && (
)}
{/* Chapters */}
Chapters
{isOwner && (
)}
{/* Comments */}
{/* Sidebar */}
{/* Story info */}
Story Info
Status
{story.status}
Chapters
{story.total_chapters}
Likes
{story.total_likes.toLocaleString()}
Views
{story.total_views.toLocaleString()}
Comments
{story.total_comments}
Published
{new Date(story.created_at).toLocaleDateString()}
{/* Collaborators */}
{storyCollabs.length > 0 && (
Collaborators
{storyCollabs.map(c => (
))}
)}
{/* Reading list modal */}
setListModalOpen(false)} title="Save to Reading List">
{myLists.length > 0 ? (
<>
Select a list:
{myLists.map(list => (
))}
>
) : (
)}
)
}