| 1 | import React, { useState, useEffect } from 'react'
|
|---|
| 2 | import { useParams, useNavigate } from 'react-router-dom'
|
|---|
| 3 | import { BookOpen, Heart, Users, Calendar, MessageCircle, Eye } from 'lucide-react'
|
|---|
| 4 | import { useAuthStore } from '../../store/authStore'
|
|---|
| 5 | import { useStoryStore } from '../../store/storyStore'
|
|---|
| 6 | import { useUIStore } from '../../store/uiStore'
|
|---|
| 7 | import { Avatar } from '../../components/ui/Avatar'
|
|---|
| 8 | import { RoleBadge, StatusBadge } from '../../components/ui/Badge'
|
|---|
| 9 | import { StoryCard } from '../../components/ui/StoryCard'
|
|---|
| 10 | import { GenreBadge } from '../../components/ui/Badge'
|
|---|
| 11 | import { Modal } from '../../components/ui/Modal'
|
|---|
| 12 | import { Button } from '../../components/ui/Button'
|
|---|
| 13 |
|
|---|
| 14 | type Tab = 'stories' | 'about'
|
|---|
| 15 |
|
|---|
| 16 | export const ProfilePage: React.FC = () => {
|
|---|
| 17 | const { username } = useParams<{ username: string }>()
|
|---|
| 18 | const navigate = useNavigate()
|
|---|
| 19 | const { allUsers, currentUser, fetchAllUsers, updateUser } = useAuthStore()
|
|---|
| 20 | const { stories, comments } = useStoryStore()
|
|---|
| 21 | const { addToast } = useUIStore()
|
|---|
| 22 | const [tab, setTab] = useState<Tab>('stories')
|
|---|
| 23 | const [loading, setLoading] = useState(false)
|
|---|
| 24 | const [editOpen, setEditOpen] = useState(false)
|
|---|
| 25 | const [editForm, setEditForm] = useState({ username: '', email: '', name: '', surname: '' })
|
|---|
| 26 | const [saving, setSaving] = useState(false)
|
|---|
| 27 |
|
|---|
| 28 | useEffect(() => {
|
|---|
| 29 | if (allUsers.length === 0) {
|
|---|
| 30 | setLoading(true)
|
|---|
| 31 | fetchAllUsers().finally(() => setLoading(false))
|
|---|
| 32 | }
|
|---|
| 33 | }, [])
|
|---|
| 34 |
|
|---|
| 35 | const user = allUsers.find(u => u.username === username)
|
|---|
| 36 |
|
|---|
| 37 | const openEdit = () => {
|
|---|
| 38 | if (!user) return
|
|---|
| 39 | setEditForm({ username: user.username, email: user.email, name: user.name, surname: user.surname })
|
|---|
| 40 | setEditOpen(true)
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | const handleEditSave = async () => {
|
|---|
| 44 | if (!user) return
|
|---|
| 45 | setSaving(true)
|
|---|
| 46 | try {
|
|---|
| 47 | await updateUser(user.user_id, editForm)
|
|---|
| 48 | addToast('Profile updated successfully!')
|
|---|
| 49 | setEditOpen(false)
|
|---|
| 50 | navigate(`/profile/${editForm.username}`, { replace: true })
|
|---|
| 51 | } catch (err: any) {
|
|---|
| 52 | addToast(err.response?.data?.message ?? 'Failed to update profile.', 'error')
|
|---|
| 53 | } finally {
|
|---|
| 54 | setSaving(false)
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (loading) {
|
|---|
| 59 | return (
|
|---|
| 60 | <div className="max-w-4xl mx-auto px-4 py-20 text-center">
|
|---|
| 61 | <p className="text-slate-400">Loading profile...</p>
|
|---|
| 62 | </div>
|
|---|
| 63 | )
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (!user) {
|
|---|
| 67 | return (
|
|---|
| 68 | <div className="max-w-4xl mx-auto px-4 py-20 text-center">
|
|---|
| 69 | <h2 className="text-2xl text-white mb-4">User not found</h2>
|
|---|
| 70 | </div>
|
|---|
| 71 | )
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | const userStories = stories.filter(s => s.user_id === user.user_id && s.status === 'published')
|
|---|
| 75 | const totalLikes = userStories.reduce((acc, s) => acc + s.total_likes, 0)
|
|---|
| 76 | const totalViews = userStories.reduce((acc, s) => acc + s.total_views, 0)
|
|---|
| 77 | const userComments = comments.filter(c => c.user_id === user.user_id)
|
|---|
| 78 | const allGenres = [...new Set(userStories.flatMap(s => s.genres))]
|
|---|
| 79 |
|
|---|
| 80 | return (
|
|---|
| 81 | <div className="max-w-5xl mx-auto px-4 py-8">
|
|---|
| 82 | {/* Profile header */}
|
|---|
| 83 | <div className="relative mb-8">
|
|---|
| 84 | {/* Cover */}
|
|---|
| 85 | <div className="h-32 sm:h-48 rounded-2xl bg-gradient-to-br from-indigo-900 via-violet-900 to-purple-900 mb-16 sm:mb-20 overflow-hidden">
|
|---|
| 86 | <div className="absolute inset-0 rounded-2xl opacity-40">
|
|---|
| 87 | <div className="absolute top-4 left-16 w-32 h-32 rounded-full bg-white/10 blur-2xl" />
|
|---|
| 88 | <div className="absolute bottom-2 right-8 w-24 h-24 rounded-full bg-white/10 blur-xl" />
|
|---|
| 89 | </div>
|
|---|
| 90 | </div>
|
|---|
| 91 |
|
|---|
| 92 | {/* Avatar + info */}
|
|---|
| 93 | <div className="absolute bottom-0 left-0 right-0 px-6 flex items-end justify-between">
|
|---|
| 94 | <Avatar name={`${user.name} ${user.surname}`} size="xl" className="ring-4 ring-slate-950" />
|
|---|
| 95 | {currentUser?.user_id === user.user_id && (
|
|---|
| 96 | <button onClick={openEdit} className="mb-2 text-sm text-indigo-400 hover:text-indigo-300 transition-colors">
|
|---|
| 97 | Edit Profile
|
|---|
| 98 | </button>
|
|---|
| 99 | )}
|
|---|
| 100 | </div>
|
|---|
| 101 | </div>
|
|---|
| 102 |
|
|---|
| 103 | {/* User info */}
|
|---|
| 104 | <div className="mb-8">
|
|---|
| 105 | <div className="flex items-center gap-3 mb-1">
|
|---|
| 106 | <h1 className="font-serif text-2xl font-bold text-white">
|
|---|
| 107 | {user.name} {user.surname}
|
|---|
| 108 | </h1>
|
|---|
| 109 | <RoleBadge role={user.role} />
|
|---|
| 110 | </div>
|
|---|
| 111 | <p className="text-slate-400">@{user.username}</p>
|
|---|
| 112 | {user.bio && <p className="text-slate-300 mt-3 max-w-xl">{user.bio}</p>}
|
|---|
| 113 | <div className="flex items-center gap-4 mt-4 flex-wrap">
|
|---|
| 114 | <span className="flex items-center gap-1 text-slate-400 text-sm">
|
|---|
| 115 | <Calendar size={14} />
|
|---|
| 116 | Joined {new Date(user.created_at).toLocaleDateString('en-US', { month: 'long', year: 'numeric' })}
|
|---|
| 117 | </span>
|
|---|
| 118 | <span className="flex items-center gap-1 text-slate-400 text-sm">
|
|---|
| 119 | <Users size={14} />
|
|---|
| 120 | {user.follower_count} followers · {user.following_count} following
|
|---|
| 121 | </span>
|
|---|
| 122 | </div>
|
|---|
| 123 | </div>
|
|---|
| 124 |
|
|---|
| 125 | {/* Stats */}
|
|---|
| 126 | <div className="grid grid-cols-2 sm:grid-cols-4 gap-4 mb-8">
|
|---|
| 127 | {[
|
|---|
| 128 | { icon: <BookOpen size={16} className="text-indigo-400" />, value: userStories.length, label: 'Stories' },
|
|---|
| 129 | { icon: <Heart size={16} className="text-rose-400" />, value: totalLikes.toLocaleString(), label: 'Likes' },
|
|---|
| 130 | { icon: <Eye size={16} className="text-blue-400" />, value: totalViews.toLocaleString(), label: 'Views' },
|
|---|
| 131 | { icon: <MessageCircle size={16} className="text-amber-400" />, value: userComments.length, label: 'Comments' },
|
|---|
| 132 | ].map(s => (
|
|---|
| 133 | <div key={s.label} className="bg-slate-800 border border-slate-700 rounded-xl p-4 text-center">
|
|---|
| 134 | <div className="flex justify-center mb-1">{s.icon}</div>
|
|---|
| 135 | <p className="text-xl font-bold text-white">{s.value}</p>
|
|---|
| 136 | <p className="text-slate-500 text-xs">{s.label}</p>
|
|---|
| 137 | </div>
|
|---|
| 138 | ))}
|
|---|
| 139 | </div>
|
|---|
| 140 |
|
|---|
| 141 | {/* Tabs */}
|
|---|
| 142 | <div className="flex gap-1 p-1 bg-slate-800 rounded-xl mb-6 w-fit">
|
|---|
| 143 | {(['stories', 'about'] as Tab[]).map(t => (
|
|---|
| 144 | <button
|
|---|
| 145 | key={t}
|
|---|
| 146 | onClick={() => setTab(t)}
|
|---|
| 147 | className={`px-4 py-2 rounded-lg text-sm font-medium capitalize transition-colors ${
|
|---|
| 148 | tab === t ? 'bg-indigo-600 text-white' : 'text-slate-400 hover:text-white'
|
|---|
| 149 | }`}
|
|---|
| 150 | >
|
|---|
| 151 | {t}
|
|---|
| 152 | </button>
|
|---|
| 153 | ))}
|
|---|
| 154 | </div>
|
|---|
| 155 |
|
|---|
| 156 | {/* Tab content */}
|
|---|
| 157 | {tab === 'stories' ? (
|
|---|
| 158 | <div>
|
|---|
| 159 | {userStories.length === 0 ? (
|
|---|
| 160 | <div className="text-center py-16 text-slate-500">
|
|---|
| 161 | <BookOpen size={40} className="mx-auto mb-3 opacity-40" />
|
|---|
| 162 | <p>No published stories yet.</p>
|
|---|
| 163 | </div>
|
|---|
| 164 | ) : (
|
|---|
| 165 | <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
|
|---|
| 166 | {userStories.map(story => (
|
|---|
| 167 | <StoryCard key={story.story_id} story={story} />
|
|---|
| 168 | ))}
|
|---|
| 169 | </div>
|
|---|
| 170 | )}
|
|---|
| 171 | </div>
|
|---|
| 172 | ) : (
|
|---|
| 173 | <div className="space-y-6">
|
|---|
| 174 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
|
|---|
| 175 | <h3 className="text-white font-semibold mb-3">About</h3>
|
|---|
| 176 | <p className="text-slate-300">{user.bio || 'No bio provided.'}</p>
|
|---|
| 177 | </div>
|
|---|
| 178 | {allGenres.length > 0 && (
|
|---|
| 179 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
|
|---|
| 180 | <h3 className="text-white font-semibold mb-3">Writes In</h3>
|
|---|
| 181 | <div className="flex flex-wrap gap-2">
|
|---|
| 182 | {allGenres.map(g => <GenreBadge key={g} genre={g} />)}
|
|---|
| 183 | </div>
|
|---|
| 184 | </div>
|
|---|
| 185 | )}
|
|---|
| 186 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
|
|---|
| 187 | <h3 className="text-white font-semibold mb-3">Stats</h3>
|
|---|
| 188 | <div className="space-y-3">
|
|---|
| 189 | <div className="flex justify-between text-sm">
|
|---|
| 190 | <span className="text-slate-400">Role</span>
|
|---|
| 191 | <RoleBadge role={user.role} />
|
|---|
| 192 | </div>
|
|---|
| 193 | <div className="flex justify-between text-sm">
|
|---|
| 194 | <span className="text-slate-400">Member since</span>
|
|---|
| 195 | <span className="text-white">{new Date(user.created_at).toLocaleDateString()}</span>
|
|---|
| 196 | </div>
|
|---|
| 197 | <div className="flex justify-between text-sm">
|
|---|
| 198 | <span className="text-slate-400">Published stories</span>
|
|---|
| 199 | <span className="text-white">{userStories.length}</span>
|
|---|
| 200 | </div>
|
|---|
| 201 | </div>
|
|---|
| 202 | </div>
|
|---|
| 203 | </div>
|
|---|
| 204 | )}
|
|---|
| 205 |
|
|---|
| 206 | <Modal isOpen={editOpen} onClose={() => setEditOpen(false)} title="Edit Profile">
|
|---|
| 207 | <div className="space-y-4">
|
|---|
| 208 | {[
|
|---|
| 209 | { label: 'First Name', key: 'name' },
|
|---|
| 210 | { label: 'Last Name', key: 'surname' },
|
|---|
| 211 | { label: 'Username', key: 'username' },
|
|---|
| 212 | { label: 'Email', key: 'email' },
|
|---|
| 213 | ].map(({ label, key }) => (
|
|---|
| 214 | <div key={key}>
|
|---|
| 215 | <label className="block text-sm text-slate-400 mb-1.5">{label}</label>
|
|---|
| 216 | <input
|
|---|
| 217 | type={key === 'email' ? 'email' : 'text'}
|
|---|
| 218 | value={editForm[key as keyof typeof editForm]}
|
|---|
| 219 | onChange={e => setEditForm(p => ({ ...p, [key]: e.target.value }))}
|
|---|
| 220 | 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"
|
|---|
| 221 | />
|
|---|
| 222 | </div>
|
|---|
| 223 | ))}
|
|---|
| 224 | <div className="flex gap-3 pt-2">
|
|---|
| 225 | <Button variant="ghost" className="flex-1" onClick={() => setEditOpen(false)}>Cancel</Button>
|
|---|
| 226 | <Button className="flex-1" loading={saving} onClick={handleEditSave}>Save Changes</Button>
|
|---|
| 227 | </div>
|
|---|
| 228 | </div>
|
|---|
| 229 | </Modal>
|
|---|
| 230 | </div>
|
|---|
| 231 | )
|
|---|
| 232 | }
|
|---|