| 1 | import React from 'react'
|
|---|
| 2 | import { useNavigate } from 'react-router-dom'
|
|---|
| 3 | import { Plus, BookOpen, Eye, Heart, MessageCircle, TrendingUp, Bell } from 'lucide-react'
|
|---|
| 4 | import { useAuthStore } from '../../store/authStore'
|
|---|
| 5 | import { useStoryStore } from '../../store/storyStore'
|
|---|
| 6 | import { useNotificationStore } from '../../store/notificationStore'
|
|---|
| 7 | import { Button } from '../../components/ui/Button'
|
|---|
| 8 | import { StatusBadge } from '../../components/ui/Badge'
|
|---|
| 9 | import { StoryAnalytics } from '../../components/writer/StoryAnalytics'
|
|---|
| 10 |
|
|---|
| 11 | function timeAgo(str: string): string {
|
|---|
| 12 | const diff = Date.now() - new Date(str).getTime()
|
|---|
| 13 | const m = Math.floor(diff / 60000)
|
|---|
| 14 | if (m < 1) return 'just now'
|
|---|
| 15 | if (m < 60) return `${m}m ago`
|
|---|
| 16 | const h = Math.floor(m / 60)
|
|---|
| 17 | if (h < 24) return `${h}h ago`
|
|---|
| 18 | return `${Math.floor(h / 24)}d ago`
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | export const WriterDashboard: React.FC = () => {
|
|---|
| 22 | const navigate = useNavigate()
|
|---|
| 23 | const { currentUser } = useAuthStore()
|
|---|
| 24 | const { stories, collaborations } = useStoryStore()
|
|---|
| 25 | const { notifications } = useNotificationStore()
|
|---|
| 26 |
|
|---|
| 27 | if (!currentUser) return null
|
|---|
| 28 |
|
|---|
| 29 | const ownedStories = stories.filter(s => s.user_id === currentUser.user_id)
|
|---|
| 30 | const collabStoryIds = new Set(collaborations.filter(c => c.user_id === currentUser.user_id).map(c => c.story_id))
|
|---|
| 31 | const collabStories = stories.filter(s => collabStoryIds.has(s.story_id))
|
|---|
| 32 | const myStories = ownedStories
|
|---|
| 33 | const allDashboardStories = [...ownedStories, ...collabStories]
|
|---|
| 34 | const published = myStories.filter(s => s.status === 'published')
|
|---|
| 35 | const drafts = myStories.filter(s => s.status === 'draft')
|
|---|
| 36 | const totalViews = myStories.reduce((acc, s) => acc + s.total_views, 0)
|
|---|
| 37 | const totalLikes = myStories.reduce((acc, s) => acc + s.total_likes, 0)
|
|---|
| 38 |
|
|---|
| 39 | const recentNotifs = notifications.slice(0, 5)
|
|---|
| 40 |
|
|---|
| 41 | return (
|
|---|
| 42 | <div className="max-w-7xl mx-auto px-4 py-8">
|
|---|
| 43 | {/* Header */}
|
|---|
| 44 | <div className="flex items-center justify-between mb-8">
|
|---|
| 45 | <div>
|
|---|
| 46 | <h1 className="font-serif text-3xl font-bold text-white">
|
|---|
| 47 | Welcome back, {currentUser.name}!
|
|---|
| 48 | </h1>
|
|---|
| 49 | <p className="text-slate-400 mt-1">Here's how your stories are performing</p>
|
|---|
| 50 | </div>
|
|---|
| 51 | <Button onClick={() => navigate('/writer/create-story')}>
|
|---|
| 52 | <Plus size={16} />
|
|---|
| 53 | New Story
|
|---|
| 54 | </Button>
|
|---|
| 55 | </div>
|
|---|
| 56 |
|
|---|
| 57 | {/* Stats */}
|
|---|
| 58 | <div className="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
|
|---|
| 59 | {[
|
|---|
| 60 | { icon: <BookOpen size={20} className="text-indigo-300" />, label: 'Total Stories', value: myStories.length, sub: `${published.length} published`, color: 'bg-indigo-500/20' },
|
|---|
| 61 | { icon: <Eye size={20} className="text-blue-300" />, label: 'Total Views', value: totalViews.toLocaleString(), sub: 'All time', color: 'bg-blue-500/20' },
|
|---|
| 62 | { icon: <Heart size={20} className="text-rose-300" />, label: 'Total Likes', value: totalLikes.toLocaleString(), sub: 'Across all stories', color: 'bg-rose-500/20' },
|
|---|
| 63 | { icon: <TrendingUp size={20} className="text-emerald-300" />, label: 'Drafts', value: drafts.length, sub: 'In progress', color: 'bg-emerald-500/20' },
|
|---|
| 64 | ].map(s => (
|
|---|
| 65 | <div key={s.label} className="bg-slate-800 border border-slate-700 rounded-2xl p-5">
|
|---|
| 66 | <div className={`w-10 h-10 rounded-xl ${s.color} flex items-center justify-center mb-3`}>
|
|---|
| 67 | {s.icon}
|
|---|
| 68 | </div>
|
|---|
| 69 | <p className="text-2xl font-bold text-white">{s.value}</p>
|
|---|
| 70 | <p className="text-slate-400 text-sm mt-0.5">{s.label}</p>
|
|---|
| 71 | <p className="text-slate-600 text-xs mt-0.5">{s.sub}</p>
|
|---|
| 72 | </div>
|
|---|
| 73 | ))}
|
|---|
| 74 | </div>
|
|---|
| 75 |
|
|---|
| 76 | <div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
|---|
| 77 | {/* My stories */}
|
|---|
| 78 | <div className="lg:col-span-2">
|
|---|
| 79 | <div className="flex items-center justify-between mb-4">
|
|---|
| 80 | <h2 className="font-serif text-xl font-bold text-white">My Stories</h2>
|
|---|
| 81 | </div>
|
|---|
| 82 | {allDashboardStories.length === 0 ? (
|
|---|
| 83 | <div className="bg-slate-800 border border-slate-700 rounded-2xl p-12 text-center">
|
|---|
| 84 | <BookOpen size={40} className="mx-auto mb-4 text-slate-600" />
|
|---|
| 85 | <h3 className="text-white font-medium mb-2">No stories yet</h3>
|
|---|
| 86 | <p className="text-slate-500 text-sm mb-4">Start your writing journey today!</p>
|
|---|
| 87 | <Button onClick={() => navigate('/writer/create-story')}>
|
|---|
| 88 | <Plus size={14} />
|
|---|
| 89 | Create Your First Story
|
|---|
| 90 | </Button>
|
|---|
| 91 | </div>
|
|---|
| 92 | ) : (
|
|---|
| 93 | <div className="space-y-3">
|
|---|
| 94 | {allDashboardStories.map(story => {
|
|---|
| 95 | const isCollab = collabStoryIds.has(story.story_id)
|
|---|
| 96 | return (
|
|---|
| 97 | <div key={story.story_id} className="flex items-center gap-4 p-4 bg-slate-800 border border-slate-700 rounded-xl hover:border-indigo-500/40 transition-colors">
|
|---|
| 98 | <div className="flex-1 min-w-0">
|
|---|
| 99 | <div className="flex items-center gap-2 mb-1">
|
|---|
| 100 | <h3 className="text-white font-medium text-sm truncate">{story.title}</h3>
|
|---|
| 101 | <StatusBadge status={story.status} />
|
|---|
| 102 | {isCollab && (
|
|---|
| 103 | <span className="px-2 py-0.5 text-xs font-medium rounded-full bg-violet-500/20 text-violet-400 border border-violet-500/30">
|
|---|
| 104 | Collaborator
|
|---|
| 105 | </span>
|
|---|
| 106 | )}
|
|---|
| 107 | </div>
|
|---|
| 108 | <div className="flex items-center gap-3 text-slate-500 text-xs">
|
|---|
| 109 | {isCollab && <span className="text-slate-500">by {story.author_username}</span>}
|
|---|
| 110 | <span className="flex items-center gap-1"><Eye size={11} /> {story.total_views.toLocaleString()}</span>
|
|---|
| 111 | <span className="flex items-center gap-1"><Heart size={11} /> {story.total_likes}</span>
|
|---|
| 112 | <span className="flex items-center gap-1"><MessageCircle size={11} /> {story.total_comments}</span>
|
|---|
| 113 | <span>{story.total_chapters} chapters</span>
|
|---|
| 114 | </div>
|
|---|
| 115 | </div>
|
|---|
| 116 | <div className="flex gap-2 flex-shrink-0">
|
|---|
| 117 | <Button size="sm" variant="ghost" onClick={() => navigate(`/story/${story.story_id}`)}>View</Button>
|
|---|
| 118 | <Button size="sm" variant="secondary" onClick={() => navigate(`/writer/edit-story/${story.story_id}`)}>Edit</Button>
|
|---|
| 119 | </div>
|
|---|
| 120 | </div>
|
|---|
| 121 | )
|
|---|
| 122 | })}
|
|---|
| 123 | </div>
|
|---|
| 124 | )}
|
|---|
| 125 | </div>
|
|---|
| 126 |
|
|---|
| 127 | {/* Recent notifications */}
|
|---|
| 128 | <div>
|
|---|
| 129 | <div className="flex items-center gap-2 mb-4">
|
|---|
| 130 | <Bell size={16} className="text-amber-400" />
|
|---|
| 131 | <h2 className="font-serif text-xl font-bold text-white">Recent Activity</h2>
|
|---|
| 132 | </div>
|
|---|
| 133 | <div className="space-y-2">
|
|---|
| 134 | {recentNotifs.length === 0 ? (
|
|---|
| 135 | <div className="text-center py-8 text-slate-500 text-sm">No recent activity</div>
|
|---|
| 136 | ) : (
|
|---|
| 137 | recentNotifs.map(n => (
|
|---|
| 138 | <div key={n.notification_id} className={`p-3 rounded-xl border text-sm ${
|
|---|
| 139 | !n.is_read ? 'bg-indigo-500/5 border-indigo-500/20' : 'bg-slate-800 border-slate-700'
|
|---|
| 140 | }`}>
|
|---|
| 141 | <p className="text-white text-xs font-medium">{n.title}</p>
|
|---|
| 142 | <p className="text-slate-400 text-xs mt-0.5 line-clamp-2">{n.message}</p>
|
|---|
| 143 | <p className="text-slate-600 text-xs mt-1">{timeAgo(n.created_at)}</p>
|
|---|
| 144 | </div>
|
|---|
| 145 | ))
|
|---|
| 146 | )}
|
|---|
| 147 | </div>
|
|---|
| 148 | </div>
|
|---|
| 149 | </div>
|
|---|
| 150 |
|
|---|
| 151 | {/* Analytics */}
|
|---|
| 152 | {myStories.some(s => s.status === 'published') && (
|
|---|
| 153 | <div className="mt-10">
|
|---|
| 154 | <div className="flex items-center gap-2 mb-6">
|
|---|
| 155 | <TrendingUp size={18} className="text-indigo-400" />
|
|---|
| 156 | <h2 className="font-serif text-xl font-bold text-white">Analytics</h2>
|
|---|
| 157 | <span className="text-slate-500 text-sm">(Story: The Chronicles of Eldoria)</span>
|
|---|
| 158 | </div>
|
|---|
| 159 | <StoryAnalytics />
|
|---|
| 160 | </div>
|
|---|
| 161 | )}
|
|---|
| 162 | </div>
|
|---|
| 163 | )
|
|---|
| 164 | }
|
|---|