import React, { useState, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { Plus, BookOpen, Lock, Globe, Trash2, X } from 'lucide-react' import { useAuthStore } from '../../store/authStore' import { useStoryStore } from '../../store/storyStore' import { useUIStore } from '../../store/uiStore' import { Button } from '../../components/ui/Button' import { Modal } from '../../components/ui/Modal' import { GenreBadge } from '../../components/ui/Badge' export const ReadingListPage: React.FC = () => { const navigate = useNavigate() const { currentUser } = useAuthStore() const { readingLists, fetchUserReadingLists, createReadingList, deleteReadingList, removeStoryFromList } = useStoryStore() const [fetching, setFetching] = useState(false) const [confirmRemove, setConfirmRemove] = useState<{ listId: number; storyId: number; title: string } | null>(null) useEffect(() => { if (!currentUser) return setFetching(true) fetchUserReadingLists(currentUser.user_id).finally(() => setFetching(false)) }, [currentUser?.user_id]) const { addToast } = useUIStore() const [createOpen, setCreateOpen] = useState(false) const [newListName, setNewListName] = useState('') const [newListDesc, setNewListDesc] = useState('') const [isPublic, setIsPublic] = useState(false) if (!currentUser) { return (

Sign in to view your reading lists

) } const myLists = readingLists.filter(l => l.user_id === currentUser!.user_id) const handleCreate = async () => { if (!newListName.trim()) return try { await createReadingList({ list_id: Date.now(), user_id: currentUser.user_id, username: currentUser.username, name: newListName.trim(), description: newListDesc.trim() || undefined, is_public: isPublic, created_at: new Date().toISOString(), stories: [], }) addToast(`"${newListName}" created!`) } catch (err: any) { addToast(err?.response?.data?.message || 'Failed to create list.', 'error') return } setNewListName('') setNewListDesc('') setIsPublic(false) setCreateOpen(false) } return (
{/* Header */}

My Reading Lists

{myLists.length} list{myLists.length !== 1 ? 's' : ''}

{fetching ? (

Loading your lists...

) : myLists.length === 0 ? (

No reading lists yet

Create your first list to save stories you love.

) : (
{myLists.map(list => (
{/* Header */}

{list.name}

{list.is_public ? ( Public ) : ( Private )}
{list.description && (

{list.description}

)}

{list.stories.length} stories

{/* Stories */}
{list.stories.length === 0 ? (

No stories yet.

) : (
{list.stories.slice(0, 4).map(item => (
by {item.author_username} {item.genres.slice(0, 1).map(g => )}
))} {list.stories.length > 4 && (

+{list.stories.length - 4} more stories

)}
)}
))}
)} {/* Confirm remove modal */} setConfirmRemove(null)} title="Remove Story">

Are you sure you want to remove "{confirmRemove?.title}" from this list?

{/* Create modal */} setCreateOpen(false)} title="Create Reading List">
setNewListName(e.target.value)} placeholder="My Fantasy Favorites..." className="w-full px-4 py-3 bg-slate-800 border border-slate-700 rounded-xl text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500" />