| 1 | import React, { useState } from 'react'
|
|---|
| 2 | import { useParams, useNavigate } from 'react-router-dom'
|
|---|
| 3 | import { ArrowLeft, BookOpen, Eye, Users, Calendar, Plus, BookmarkPlus } from 'lucide-react'
|
|---|
| 4 | import { useStoryStore } from '../../store/storyStore'
|
|---|
| 5 | import { useAuthStore } from '../../store/authStore'
|
|---|
| 6 | import { useUIStore } from '../../store/uiStore'
|
|---|
| 7 | import { Button } from '../../components/ui/Button'
|
|---|
| 8 | import { GenreBadge } from '../../components/ui/Badge'
|
|---|
| 9 | import { Avatar } from '../../components/ui/Avatar'
|
|---|
| 10 | import { ChapterList } from '../../components/story/ChapterList'
|
|---|
| 11 | import { CommentSection } from '../../components/story/CommentSection'
|
|---|
| 12 | import { LikeButton } from '../../components/story/LikeButton'
|
|---|
| 13 | import { Modal } from '../../components/ui/Modal'
|
|---|
| 14 | import { getGenreGradient } from '../../components/story/GenreBadge'
|
|---|
| 15 | import { ReadingListItem } from '../../types'
|
|---|
| 16 |
|
|---|
| 17 | export const StoryDetailPage: React.FC = () => {
|
|---|
| 18 | const { id } = useParams<{ id: string }>()
|
|---|
| 19 | const navigate = useNavigate()
|
|---|
| 20 | const { stories, chapters, collaborations, readingLists, addStoryToList, createReadingList } = useStoryStore()
|
|---|
| 21 | const { currentUser } = useAuthStore()
|
|---|
| 22 | const { addToast } = useUIStore()
|
|---|
| 23 | const [listModalOpen, setListModalOpen] = useState(false)
|
|---|
| 24 | const [newListName, setNewListName] = useState('')
|
|---|
| 25 |
|
|---|
| 26 | const story = stories.find(s => s.story_id === Number(id))
|
|---|
| 27 | if (!story) {
|
|---|
| 28 | return (
|
|---|
| 29 | <div className="max-w-4xl mx-auto px-4 py-20 text-center">
|
|---|
| 30 | <h2 className="text-2xl text-white mb-4">Story not found</h2>
|
|---|
| 31 | <Button onClick={() => navigate('/browse')}>Browse Stories</Button>
|
|---|
| 32 | </div>
|
|---|
| 33 | )
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | const storyChapters = chapters.filter(c => c.story_id === story.story_id)
|
|---|
| 37 | const storyCollabs = collaborations.filter(c => c.story_id === story.story_id)
|
|---|
| 38 | const gradient = getGenreGradient(story.genres[0])
|
|---|
| 39 | const myLists = currentUser ? readingLists.filter(l => l.user_id === currentUser.user_id) : []
|
|---|
| 40 |
|
|---|
| 41 | const handleAddToList = (listId: number) => {
|
|---|
| 42 | const list = readingLists.find(l => l.list_id === listId)
|
|---|
| 43 | if (!list) return
|
|---|
| 44 | if (list.stories.some(s => s.story_id === story.story_id)) {
|
|---|
| 45 | addToast('Already in this list', 'info')
|
|---|
| 46 | return
|
|---|
| 47 | }
|
|---|
| 48 | const item: ReadingListItem = {
|
|---|
| 49 | item_id: Date.now(),
|
|---|
| 50 | list_id: listId,
|
|---|
| 51 | story_id: story.story_id,
|
|---|
| 52 | story_title: story.title,
|
|---|
| 53 | author_username: story.author_username,
|
|---|
| 54 | added_at: new Date().toISOString(),
|
|---|
| 55 | genres: story.genres,
|
|---|
| 56 | }
|
|---|
| 57 | addStoryToList(listId, item)
|
|---|
| 58 | addToast(`Added to "${list.name}"!`)
|
|---|
| 59 | setListModalOpen(false)
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | const handleCreateList = () => {
|
|---|
| 63 | if (!newListName.trim() || !currentUser) return
|
|---|
| 64 | const newList = {
|
|---|
| 65 | list_id: Date.now(),
|
|---|
| 66 | user_id: currentUser.user_id,
|
|---|
| 67 | username: currentUser.username,
|
|---|
| 68 | name: newListName.trim(),
|
|---|
| 69 | is_public: false,
|
|---|
| 70 | created_at: new Date().toISOString(),
|
|---|
| 71 | stories: [{
|
|---|
| 72 | item_id: Date.now() + 1,
|
|---|
| 73 | list_id: Date.now(),
|
|---|
| 74 | story_id: story.story_id,
|
|---|
| 75 | story_title: story.title,
|
|---|
| 76 | author_username: story.author_username,
|
|---|
| 77 | added_at: new Date().toISOString(),
|
|---|
| 78 | genres: story.genres,
|
|---|
| 79 | }],
|
|---|
| 80 | }
|
|---|
| 81 | createReadingList(newList)
|
|---|
| 82 | addToast(`Created "${newListName}" and added story!`)
|
|---|
| 83 | setNewListName('')
|
|---|
| 84 | setListModalOpen(false)
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | const isOwner = currentUser?.user_id === story.user_id
|
|---|
| 88 |
|
|---|
| 89 | return (
|
|---|
| 90 | <div className="max-w-5xl mx-auto px-4 py-8">
|
|---|
| 91 | {/* Back */}
|
|---|
| 92 | <button
|
|---|
| 93 | onClick={() => navigate(-1)}
|
|---|
| 94 | className="flex items-center gap-2 text-slate-400 hover:text-white mb-6 text-sm transition-colors"
|
|---|
| 95 | >
|
|---|
| 96 | <ArrowLeft size={16} />
|
|---|
| 97 | Back
|
|---|
| 98 | </button>
|
|---|
| 99 |
|
|---|
| 100 | {/* Hero */}
|
|---|
| 101 | <div className={`relative rounded-2xl overflow-hidden mb-8 bg-gradient-to-br ${gradient}`}>
|
|---|
| 102 | <div className="absolute inset-0 bg-gradient-to-t from-slate-950/90 via-slate-950/30 to-transparent" />
|
|---|
| 103 | <div className="relative p-8 sm:p-12">
|
|---|
| 104 | <div className="flex flex-wrap gap-2 mb-4">
|
|---|
| 105 | {story.genres.map(g => <GenreBadge key={g} genre={g} />)}
|
|---|
| 106 | {story.mature_content && (
|
|---|
| 107 | <span className="px-2 py-0.5 text-xs font-medium rounded-full border bg-rose-500/20 text-rose-400 border-rose-500/30">
|
|---|
| 108 | 18+
|
|---|
| 109 | </span>
|
|---|
| 110 | )}
|
|---|
| 111 | </div>
|
|---|
| 112 | <h1 className="font-serif text-3xl sm:text-4xl font-bold text-white mb-3">{story.title}</h1>
|
|---|
| 113 | <p className="text-slate-300 text-lg mb-6 leading-relaxed max-w-2xl">{story.short_description}</p>
|
|---|
| 114 |
|
|---|
| 115 | <div className="flex items-center gap-4 flex-wrap">
|
|---|
| 116 | <div className="flex items-center gap-2">
|
|---|
| 117 | <Avatar name={story.author_username} size="sm" />
|
|---|
| 118 | <span className="text-white text-sm font-medium">{story.author_username}</span>
|
|---|
| 119 | </div>
|
|---|
| 120 | <div className="flex items-center gap-1 text-slate-400 text-sm">
|
|---|
| 121 | <Eye size={14} />
|
|---|
| 122 | {story.total_views.toLocaleString()} views
|
|---|
| 123 | </div>
|
|---|
| 124 | <div className="flex items-center gap-1 text-slate-400 text-sm">
|
|---|
| 125 | <BookOpen size={14} />
|
|---|
| 126 | {story.total_chapters} chapters
|
|---|
| 127 | </div>
|
|---|
| 128 | <div className="flex items-center gap-1 text-slate-400 text-sm">
|
|---|
| 129 | <Calendar size={14} />
|
|---|
| 130 | {new Date(story.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
|
|---|
| 131 | </div>
|
|---|
| 132 | </div>
|
|---|
| 133 | </div>
|
|---|
| 134 | </div>
|
|---|
| 135 |
|
|---|
| 136 | <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
|---|
| 137 | {/* Main content */}
|
|---|
| 138 | <div className="lg:col-span-2 space-y-8">
|
|---|
| 139 | {/* Action bar */}
|
|---|
| 140 | <div className="flex items-center gap-3 flex-wrap">
|
|---|
| 141 | <LikeButton storyId={story.story_id} authorUserId={story.user_id} totalLikes={story.total_likes} />
|
|---|
| 142 | {currentUser && (
|
|---|
| 143 | <Button variant="secondary" size="sm" onClick={() => setListModalOpen(true)}>
|
|---|
| 144 | <BookmarkPlus size={14} />
|
|---|
| 145 | Save to List
|
|---|
| 146 | </Button>
|
|---|
| 147 | )}
|
|---|
| 148 | {isOwner && (
|
|---|
| 149 | <Button variant="ghost" size="sm" onClick={() => navigate(`/writer/edit-story/${story.story_id}`)}>
|
|---|
| 150 | Edit Story
|
|---|
| 151 | </Button>
|
|---|
| 152 | )}
|
|---|
| 153 | </div>
|
|---|
| 154 |
|
|---|
| 155 | {/* Chapters */}
|
|---|
| 156 | <div>
|
|---|
| 157 | <div className="flex items-center justify-between mb-4">
|
|---|
| 158 | <h2 className="font-serif text-xl font-bold text-white">Chapters</h2>
|
|---|
| 159 | {isOwner && (
|
|---|
| 160 | <Button size="sm" onClick={() => navigate(`/writer/create-chapter/${story.story_id}`)}>
|
|---|
| 161 | <Plus size={14} />
|
|---|
| 162 | Add Chapter
|
|---|
| 163 | </Button>
|
|---|
| 164 | )}
|
|---|
| 165 | </div>
|
|---|
| 166 | <ChapterList chapters={storyChapters} storyId={story.story_id} />
|
|---|
| 167 | </div>
|
|---|
| 168 |
|
|---|
| 169 | {/* Comments */}
|
|---|
| 170 | <div className="border-t border-slate-700 pt-8">
|
|---|
| 171 | <CommentSection storyId={story.story_id} authorUserId={story.user_id} />
|
|---|
| 172 | </div>
|
|---|
| 173 | </div>
|
|---|
| 174 |
|
|---|
| 175 | {/* Sidebar */}
|
|---|
| 176 | <div className="space-y-6">
|
|---|
| 177 | {/* Story info */}
|
|---|
| 178 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-5">
|
|---|
| 179 | <h3 className="text-white font-semibold mb-4">Story Info</h3>
|
|---|
| 180 | <div className="space-y-3 text-sm">
|
|---|
| 181 | <div className="flex justify-between">
|
|---|
| 182 | <span className="text-slate-400">Status</span>
|
|---|
| 183 | <span className="text-white capitalize">{story.status}</span>
|
|---|
| 184 | </div>
|
|---|
| 185 | <div className="flex justify-between">
|
|---|
| 186 | <span className="text-slate-400">Chapters</span>
|
|---|
| 187 | <span className="text-white">{story.total_chapters}</span>
|
|---|
| 188 | </div>
|
|---|
| 189 | <div className="flex justify-between">
|
|---|
| 190 | <span className="text-slate-400">Likes</span>
|
|---|
| 191 | <span className="text-white">{story.total_likes.toLocaleString()}</span>
|
|---|
| 192 | </div>
|
|---|
| 193 | <div className="flex justify-between">
|
|---|
| 194 | <span className="text-slate-400">Views</span>
|
|---|
| 195 | <span className="text-white">{story.total_views.toLocaleString()}</span>
|
|---|
| 196 | </div>
|
|---|
| 197 | <div className="flex justify-between">
|
|---|
| 198 | <span className="text-slate-400">Comments</span>
|
|---|
| 199 | <span className="text-white">{story.total_comments}</span>
|
|---|
| 200 | </div>
|
|---|
| 201 | <div className="flex justify-between">
|
|---|
| 202 | <span className="text-slate-400">Published</span>
|
|---|
| 203 | <span className="text-white">{new Date(story.created_at).toLocaleDateString()}</span>
|
|---|
| 204 | </div>
|
|---|
| 205 | </div>
|
|---|
| 206 | </div>
|
|---|
| 207 |
|
|---|
| 208 | {/* Collaborators */}
|
|---|
| 209 | {storyCollabs.length > 0 && (
|
|---|
| 210 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-5">
|
|---|
| 211 | <div className="flex items-center gap-2 mb-4">
|
|---|
| 212 | <Users size={16} className="text-violet-400" />
|
|---|
| 213 | <h3 className="text-white font-semibold">Collaborators</h3>
|
|---|
| 214 | </div>
|
|---|
| 215 | <div className="space-y-3">
|
|---|
| 216 | {storyCollabs.map(c => (
|
|---|
| 217 | <div key={c.collab_id} className="flex items-center gap-2">
|
|---|
| 218 | <Avatar name={c.name} size="sm" />
|
|---|
| 219 | <div>
|
|---|
| 220 | <p className="text-white text-sm">@{c.username}</p>
|
|---|
| 221 | <p className="text-slate-500 text-xs">{c.role}</p>
|
|---|
| 222 | </div>
|
|---|
| 223 | </div>
|
|---|
| 224 | ))}
|
|---|
| 225 | </div>
|
|---|
| 226 | </div>
|
|---|
| 227 | )}
|
|---|
| 228 | </div>
|
|---|
| 229 | </div>
|
|---|
| 230 |
|
|---|
| 231 | {/* Reading list modal */}
|
|---|
| 232 | <Modal isOpen={listModalOpen} onClose={() => setListModalOpen(false)} title="Save to Reading List">
|
|---|
| 233 | <div className="space-y-3">
|
|---|
| 234 | {myLists.length > 0 ? (
|
|---|
| 235 | <>
|
|---|
| 236 | <p className="text-slate-400 text-sm mb-3">Select a list:</p>
|
|---|
| 237 | {myLists.map(list => (
|
|---|
| 238 | <button
|
|---|
| 239 | key={list.list_id}
|
|---|
| 240 | onClick={() => handleAddToList(list.list_id)}
|
|---|
| 241 | className="w-full flex items-center justify-between p-3 bg-slate-800 rounded-xl border border-slate-700 hover:border-indigo-500/50 transition-colors"
|
|---|
| 242 | >
|
|---|
| 243 | <span className="text-white text-sm">{list.name}</span>
|
|---|
| 244 | <span className="text-slate-500 text-xs">{list.stories.length} stories</span>
|
|---|
| 245 | </button>
|
|---|
| 246 | ))}
|
|---|
| 247 | <div className="border-t border-slate-700 pt-3">
|
|---|
| 248 | <p className="text-slate-400 text-sm mb-2">Or create a new list:</p>
|
|---|
| 249 | <div className="flex gap-2">
|
|---|
| 250 | <input
|
|---|
| 251 | value={newListName}
|
|---|
| 252 | onChange={e => setNewListName(e.target.value)}
|
|---|
| 253 | placeholder="List name..."
|
|---|
| 254 | 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"
|
|---|
| 255 | />
|
|---|
| 256 | <Button size="sm" onClick={handleCreateList} disabled={!newListName.trim()}>
|
|---|
| 257 | Create
|
|---|
| 258 | </Button>
|
|---|
| 259 | </div>
|
|---|
| 260 | </div>
|
|---|
| 261 | </>
|
|---|
| 262 | ) : (
|
|---|
| 263 | <div>
|
|---|
| 264 | <p className="text-slate-400 text-sm mb-3">You don't have any reading lists yet. Create one:</p>
|
|---|
| 265 | <div className="flex gap-2">
|
|---|
| 266 | <input
|
|---|
| 267 | value={newListName}
|
|---|
| 268 | onChange={e => setNewListName(e.target.value)}
|
|---|
| 269 | placeholder="My Favorites..."
|
|---|
| 270 | 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"
|
|---|
| 271 | />
|
|---|
| 272 | <Button size="sm" onClick={handleCreateList} disabled={!newListName.trim()}>
|
|---|
| 273 | Create
|
|---|
| 274 | </Button>
|
|---|
| 275 | </div>
|
|---|
| 276 | </div>
|
|---|
| 277 | )}
|
|---|
| 278 | </div>
|
|---|
| 279 | </Modal>
|
|---|
| 280 | </div>
|
|---|
| 281 | )
|
|---|
| 282 | }
|
|---|