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

main
Last change on this file since 99c1e45 was 99c1e45, checked in by kikisrbinoska <srbinoskakristina07@…>, 11 days ago

Fixed writer section and admin management

  • Property mode set to 100644
File size: 7.6 KB
Line 
1import React, { useEffect } from 'react'
2import { useNavigate } from 'react-router-dom'
3import { Plus, BookOpen, 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, collaborations, fetchStories } = useStoryStore()
25 const { notifications } = useNotificationStore()
26
27 useEffect(() => { fetchStories() }, [])
28
29 if (!currentUser) return null
30
31 const ownedStories = stories.filter(s => s.user_id === currentUser.user_id)
32 const collabStoryIds = new Set(collaborations.filter(c => c.user_id === currentUser.user_id).map(c => c.story_id))
33 const collabStories = stories.filter(s => collabStoryIds.has(s.story_id))
34 const myStories = ownedStories
35 const allDashboardStories = [...ownedStories, ...collabStories]
36 const published = myStories.filter(s => s.status === 'published')
37 const drafts = myStories.filter(s => s.status === 'draft')
38 const totalLikes = myStories.reduce((acc, s) => acc + s.total_likes, 0)
39
40 const recentNotifs = notifications.slice(0, 5)
41
42 return (
43 <div className="max-w-7xl mx-auto px-4 py-8">
44 {/* Header */}
45 <div className="flex items-center justify-between mb-8">
46 <div>
47 <h1 className="font-serif text-3xl font-bold text-white">
48 Welcome back, {currentUser.name}!
49 </h1>
50 <p className="text-slate-400 mt-1">Here's how your stories are performing</p>
51 </div>
52 <Button onClick={() => navigate('/writer/create-story')}>
53 <Plus size={16} />
54 New Story
55 </Button>
56 </div>
57
58 {/* Stats */}
59 <div className="grid grid-cols-2 lg:grid-cols-3 gap-4 mb-8">
60 {[
61 { icon: <BookOpen size={20} className="text-indigo-300" />, label: 'Total Stories', value: myStories.length, sub: `${published.length} published`, color: 'bg-indigo-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"><Heart size={11} /> {story.total_likes}</span>
111 <span className="flex items-center gap-1"><MessageCircle size={11} /> {story.total_comments}</span>
112 <span>{story.total_chapters} chapters</span>
113 </div>
114 </div>
115 <div className="flex gap-2 flex-shrink-0">
116 <Button size="sm" variant="ghost" onClick={() => navigate(`/story/${story.story_id}`)}>View</Button>
117 <Button size="sm" variant="secondary" onClick={() => navigate(`/writer/edit-story/${story.story_id}`)}>Edit</Button>
118 </div>
119 </div>
120 )
121 })}
122 </div>
123 )}
124 </div>
125
126 {/* Recent notifications */}
127 <div>
128 <div className="flex items-center gap-2 mb-4">
129 <Bell size={16} className="text-amber-400" />
130 <h2 className="font-serif text-xl font-bold text-white">Recent Activity</h2>
131 </div>
132 <div className="space-y-2">
133 {recentNotifs.length === 0 ? (
134 <div className="text-center py-8 text-slate-500 text-sm">No recent activity</div>
135 ) : (
136 recentNotifs.map(n => (
137 <div key={n.notification_id} className={`p-3 rounded-xl border text-sm ${
138 !n.is_read ? 'bg-indigo-500/5 border-indigo-500/20' : 'bg-slate-800 border-slate-700'
139 }`}>
140 <p className="text-white text-xs font-medium">{n.title}</p>
141 <p className="text-slate-400 text-xs mt-0.5 line-clamp-2">{n.message}</p>
142 <p className="text-slate-600 text-xs mt-1">{timeAgo(n.created_at)}</p>
143 </div>
144 ))
145 )}
146 </div>
147 </div>
148 </div>
149
150 {/* Analytics */}
151 {myStories.some(s => s.status === 'published') && (
152 <div className="mt-10">
153 <div className="flex items-center gap-2 mb-6">
154 <TrendingUp size={18} className="text-indigo-400" />
155 <h2 className="font-serif text-xl font-bold text-white">Analytics</h2>
156 </div>
157 <StoryAnalytics stories={myStories} />
158 </div>
159 )}
160 </div>
161 )
162}
Note: See TracBrowser for help on using the repository browser.