source: chapterx-frontend/src/pages/writer/WriterDashboard.tsx@ acf690c

main
Last change on this file since acf690c was b62cefc, checked in by kikisrbinoska <srbinoskakristina07@…>, 4 months ago

Added frontend and imporoved AI Suggestions service

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