import React, { useState, useEffect } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { BookOpen, Heart, Calendar, MessageCircle } from 'lucide-react' import { useAuthStore } from '../../store/authStore' import { useStoryStore } from '../../store/storyStore' import { useUIStore } from '../../store/uiStore' import { Avatar } from '../../components/ui/Avatar' import { RoleBadge, StatusBadge } from '../../components/ui/Badge' import { StoryCard } from '../../components/ui/StoryCard' import { GenreBadge } from '../../components/ui/Badge' import { Modal } from '../../components/ui/Modal' import { Button } from '../../components/ui/Button' type Tab = 'stories' | 'about' export const ProfilePage: React.FC = () => { const { username } = useParams<{ username: string }>() const navigate = useNavigate() const { allUsers, currentUser, fetchAllUsers, updateUser } = useAuthStore() const { stories, fetchStories } = useStoryStore() const { addToast } = useUIStore() const [tab, setTab] = useState('stories') const [loading, setLoading] = useState(false) const [editOpen, setEditOpen] = useState(false) const [editForm, setEditForm] = useState({ username: '', email: '', name: '', surname: '' }) const [saving, setSaving] = useState(false) useEffect(() => { fetchStories() if (allUsers.length === 0) { setLoading(true) fetchAllUsers().finally(() => setLoading(false)) } }, []) const user = allUsers.find(u => u.username === username) const openEdit = () => { if (!user) return setEditForm({ username: user.username, email: user.email, name: user.name, surname: user.surname }) setEditOpen(true) } const handleEditSave = async () => { if (!user) return setSaving(true) try { await updateUser(user.user_id, editForm) addToast('Profile updated successfully!') setEditOpen(false) navigate(`/profile/${editForm.username}`, { replace: true }) } catch (err: any) { addToast(err.response?.data?.message ?? 'Failed to update profile.', 'error') } finally { setSaving(false) } } if (loading) { return (

Loading profile...

) } if (!user) { return (

User not found

) } const userStories = stories.filter(s => s.user_id === user.user_id && s.status === 'published') const totalLikes = userStories.reduce((acc, s) => acc + s.total_likes, 0) const totalComments = userStories.reduce((acc, s) => acc + s.total_comments, 0) const allGenres = [...new Set(userStories.flatMap(s => s.genres))] return (
{/* Profile header */}
{/* Cover */}
{/* Avatar + info */}
{currentUser?.user_id === user.user_id && ( )}
{/* User info */}

{user.name} {user.surname}

@{user.username}

{user.bio &&

{user.bio}

}
Joined {new Date(user.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
{/* Stats */}
{[ { icon: , value: userStories.length, label: 'Stories' }, { icon: , value: totalLikes.toLocaleString(), label: 'Likes' }, { icon: , value: totalComments, label: 'Comments' }, ].map(s => (
{s.icon}

{s.value}

{s.label}

))}
{/* Tabs */}
{(['stories', 'about'] as Tab[]).map(t => ( ))}
{/* Tab content */} {tab === 'stories' ? (
{userStories.length === 0 ? (

No published stories yet.

) : (
{userStories.map(story => ( ))}
)}
) : (
{allGenres.length > 0 && (

Writes In

{allGenres.map(g => )}
)}

Stats

Role
Member since {new Date(user.created_at).toLocaleDateString()}
Published stories {userStories.length}
)} setEditOpen(false)} title="Edit Profile">
{[ { label: 'First Name', key: 'name' }, { label: 'Last Name', key: 'surname' }, { label: 'Username', key: 'username' }, { label: 'Email', key: 'email' }, ].map(({ label, key }) => (
setEditForm(p => ({ ...p, [key]: e.target.value }))} className="w-full px-4 py-2.5 bg-slate-800 border border-slate-700 rounded-xl text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500" />
))}
) }