Changeset 99c1e45 for chapterx-frontend/src/components
- Timestamp:
- 06/24/26 16:28:50 (12 days ago)
- Branches:
- main
- Children:
- a8f4a2d
- Parents:
- 0b502c2
- Location:
- chapterx-frontend/src/components
- Files:
-
- 5 edited
-
admin/PlatformStats.tsx (modified) (1 diff)
-
admin/UserTable.tsx (modified) (4 diffs)
-
story/LikeButton.tsx (modified) (3 diffs)
-
ui/StoryCard.tsx (modified) (2 diffs)
-
writer/StoryAnalytics.tsx (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
chapterx-frontend/src/components/admin/PlatformStats.tsx
r0b502c2 r99c1e45 1 import React from 'react'2 import { Users, BookOpen, FileText, Heart, Eye, MessageCircle } from 'lucide-react'1 import React, { useEffect } from 'react' 2 import { Users, BookOpen, Heart, MessageCircle } from 'lucide-react' 3 3 import { useAuthStore } from '../../store/authStore' 4 4 import { useStoryStore } from '../../store/storyStore' 5 5 6 6 export const PlatformStats: React.FC = () => { 7 const { allUsers } = useAuthStore()8 const { stories, comments } = useStoryStore()7 const { allUsers, fetchAllUsers } = useAuthStore() 8 const { stories, fetchStories } = useStoryStore() 9 9 10 const totalViews = stories.reduce((acc, s) => acc + s.total_views, 0) 10 useEffect(() => { fetchStories(); fetchAllUsers() }, []) 11 11 12 const totalLikes = stories.reduce((acc, s) => acc + s.total_likes, 0) 13 const totalComments = stories.reduce((acc, s) => acc + s.total_comments, 0) 12 14 const published = stories.filter(s => s.status === 'published').length 13 15 14 16 const stats = [ 15 { icon: <Users size={24} className="text-blue-300" />, label: 'Total Users', value: allUsers.length, color: 'bg-blue-500/20', change: '+12 this month'},17 { icon: <Users size={24} className="text-blue-300" />, label: 'Total Users', value: allUsers.length, color: 'bg-blue-500/20', change: `${allUsers.filter(u => u.role === 'writer').length} writers` }, 16 18 { icon: <BookOpen size={24} className="text-violet-300" />, label: 'Total Stories', value: stories.length, color: 'bg-violet-500/20', change: `${published} published` }, 17 { icon: <FileText size={24} className="text-emerald-300" />, label: 'Comments', value: comments.length, color: 'bg-emerald-500/20', change: 'Platform-wide' },18 19 { icon: <Heart size={24} className="text-rose-300" />, label: 'Total Likes', value: totalLikes.toLocaleString(), color: 'bg-rose-500/20', change: 'Across all stories' }, 19 { icon: <Eye size={24} className="text-amber-300" />, label: 'Total Views', value: totalViews.toLocaleString(), color: 'bg-amber-500/20', change: 'All time' }, 20 { icon: <MessageCircle size={24} className="text-cyan-300" />, label: 'Writers', value: allUsers.filter(u => u.role === 'writer').length, color: 'bg-cyan-500/20', change: 'Active creators' }, 20 { icon: <MessageCircle size={24} className="text-emerald-300" />, label: 'Total Comments', value: totalComments.toLocaleString(), color: 'bg-emerald-500/20', change: 'Platform-wide' }, 21 21 ] 22 22 23 23 return ( 24 <div className="grid grid-cols-2 md:grid-cols- 3gap-4">24 <div className="grid grid-cols-2 md:grid-cols-4 gap-4"> 25 25 {stats.map(stat => ( 26 26 <div key={stat.label} className="bg-slate-800 border border-slate-700 rounded-2xl p-6"> -
chapterx-frontend/src/components/admin/UserTable.tsx
r0b502c2 r99c1e45 1 1 import React, { useState } from 'react' 2 2 import { Search, Shield, UserX, UserCheck } from 'lucide-react' 3 import axios from 'axios' 3 4 import { useAuthStore } from '../../store/authStore' 4 import { useNotificationStore } from '../../store/notificationStore'5 5 import { useUIStore } from '../../store/uiStore' 6 6 import { User, UserRole } from '../../types' … … 10 10 import { Modal } from '../ui/Modal' 11 11 12 const API = 'https://localhost:7125/api' 13 12 14 export const UserTable: React.FC = () => { 13 const { allUsers, updateUserRole, currentUser } = useAuthStore() 14 const { addNotification } = useNotificationStore() 15 const { allUsers, updateUserRole, currentUser, token } = useAuthStore() 15 16 const { addToast } = useUIStore() 16 17 const [search, setSearch] = useState('') 17 18 const [confirmUser, setConfirmUser] = useState<User | null>(null) 18 19 const [confirmAction, setConfirmAction] = useState<'promote' | 'demote' | null>(null) 20 const [loading, setLoading] = useState(false) 21 22 const authHeaders = token ? { Authorization: `Bearer ${token}` } : {} 19 23 20 24 const filtered = allUsers.filter( … … 25 29 ) 26 30 27 const handlePromote = (user: User) => { 28 const newRole: UserRole = user.role === 'regular' ? 'writer' : user.role === 'writer' ? 'admin' : 'admin' 29 updateUserRole(user.user_id, newRole) 30 addNotification({ 31 user_id: user.user_id, 32 type: 'system', 33 title: 'Role Updated', 34 message: `Your account has been promoted to ${newRole}.`, 35 }) 36 addToast(`${user.username} promoted to ${newRole}`) 37 setConfirmUser(null) 31 const handlePromote = async (user: User) => { 32 setLoading(true) 33 try { 34 await axios.post(`${API}/admins`, { userId: user.user_id }, { headers: authHeaders }) 35 updateUserRole(user.user_id, 'admin') 36 addToast(`${user.username} promoted to admin`) 37 } catch (err: any) { 38 addToast(err?.response?.data?.message || 'Failed to promote user.', 'error') 39 } finally { 40 setLoading(false) 41 setConfirmUser(null) 42 } 38 43 } 39 44 40 const handleDemote = (user: User) => { 41 const newRole: UserRole = user.role === 'admin' ? 'writer' : 'regular' 42 updateUserRole(user.user_id, newRole) 43 addToast(`${user.username} role changed to ${newRole}`, 'info') 44 setConfirmUser(null) 45 const handleDemote = async (user: User) => { 46 setLoading(true) 47 try { 48 await axios.delete(`${API}/admins/${user.user_id}`, { headers: authHeaders }) 49 updateUserRole(user.user_id, 'writer') 50 addToast(`${user.username} removed from admin`, 'info') 51 } catch (err: any) { 52 addToast(err?.response?.data?.message || 'Failed to demote user.', 'error') 53 } finally { 54 setLoading(false) 55 setConfirmUser(null) 56 } 45 57 } 46 58 … … 140 152 variant={confirmAction === 'promote' ? 'primary' : 'danger'} 141 153 className="flex-1" 154 loading={loading} 142 155 onClick={() => confirmAction === 'promote' ? handlePromote(confirmUser) : handleDemote(confirmUser)} 143 156 > -
chapterx-frontend/src/components/story/LikeButton.tsx
r0b502c2 r99c1e45 9 9 const API = 'https://localhost:7125/api' 10 10 11 function getAuthHeaders() {12 try {13 const token = JSON.parse(localStorage.getItem('chapterx-auth') || '{}')?.state?.token14 return token ? { Authorization: `Bearer ${token}` } : {}15 } catch { return {} }16 }17 18 11 interface LikeButtonProps { 19 12 storyId: number … … 25 18 export const LikeButton: React.FC<LikeButtonProps> = ({ storyId, authorUserId, totalLikes, onCountChange }) => { 26 19 const navigate = useNavigate() 27 const { currentUser } = useAuthStore()20 const { currentUser, token } = useAuthStore() 28 21 const { addNotification } = useNotificationStore() 22 const authHeaders = token ? { Authorization: `Bearer ${token}` } : {} 29 23 const { addToast } = useUIStore() 30 24 const [liked, setLiked] = useState(false) … … 55 49 try { 56 50 if (liked) { 57 await axios.delete(`${API}/likes/user/${currentUser.user_id}/story/${storyId}`, { headers: getAuthHeaders()})51 await axios.delete(`${API}/likes/user/${currentUser.user_id}/story/${storyId}`, { headers: authHeaders }) 58 52 setLiked(false) 59 53 setCount(c => { const n = c - 1; onCountChange?.(n); return n }) 60 54 addToast('Removed from likes', 'info') 61 55 } else { 62 await axios.post(`${API}/likes`, { userId: currentUser.user_id, storyId }, { headers: getAuthHeaders()})56 await axios.post(`${API}/likes`, { userId: currentUser.user_id, storyId }, { headers: authHeaders }) 63 57 setLiked(true) 64 58 setCount(c => { const n = c + 1; onCountChange?.(n); return n }) -
chapterx-frontend/src/components/ui/StoryCard.tsx
r0b502c2 r99c1e45 1 1 import React from 'react' 2 2 import { useNavigate } from 'react-router-dom' 3 import { Heart, MessageCircle, Eye,Lock } from 'lucide-react'3 import { Heart, MessageCircle, Lock } from 'lucide-react' 4 4 import { Story } from '../../types' 5 5 import { useAuthStore } from '../../store/authStore' … … 86 86 {story.total_comments} 87 87 </span> 88 <span className="flex items-center gap-1 ml-auto">89 <Eye size={12} />90 {story.total_views.toLocaleString()}91 </span>92 88 </div> 93 89 </div> -
chapterx-frontend/src/components/writer/StoryAnalytics.tsx
r0b502c2 r99c1e45 1 1 import React from 'react' 2 2 import { 3 LineChart,4 Line,5 3 BarChart, 6 4 Bar, … … 11 9 ResponsiveContainer, 12 10 } from 'recharts' 13 import { Eye, Heart, MessageCircle, TrendingUp, Clock, BarChart2 } from 'lucide-react' 14 import { mockAnalytics } from '../../data/mockData' 11 import { Heart, MessageCircle, BookOpen } from 'lucide-react' 12 import { Story } from '../../types' 13 14 interface Props { 15 stories: Story[] 16 } 15 17 16 18 const StatCard: React.FC<{ icon: React.ReactNode; label: string; value: string | number; color: string }> = ({ 17 19 icon, label, value, color, 18 20 }) => ( 19 <div className= {`p-4 bg-slate-800 rounded-xl border border-slate-700`}>21 <div className="p-4 bg-slate-800 rounded-xl border border-slate-700"> 20 22 <div className={`w-10 h-10 rounded-xl ${color} flex items-center justify-center mb-3`}> 21 23 {icon} 22 24 </div> 23 <p className="text-2xl font-bold text-white">{ value.toLocaleString()}</p>25 <p className="text-2xl font-bold text-white">{typeof value === 'number' ? value.toLocaleString() : value}</p> 24 26 <p className="text-slate-400 text-sm mt-0.5">{label}</p> 25 27 </div> … … 42 44 } 43 45 44 export const StoryAnalytics: React.FC = () => { 45 const analytics = mockAnalytics 46 const viewsData = analytics.views_over_time.filter((_, i) => i % 5 === 0) 47 const likesData = analytics.likes_over_time.filter((_, i) => i % 5 === 0) 46 export const StoryAnalytics: React.FC<Props> = ({ stories }) => { 47 const published = stories.filter(s => s.status === 'published') 48 49 const totalLikes = stories.reduce((a, s) => a + s.total_likes, 0) 50 const totalComments = stories.reduce((a, s) => a + s.total_comments, 0) 51 const totalChapters = stories.reduce((a, s) => a + s.total_chapters, 0) 52 53 // Likes per story (sorted by created_at) 54 const likesData = [...published] 55 .sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()) 56 .map(s => ({ 57 date: new Date(s.created_at).toLocaleDateString('en-US', { month: 'short', year: '2-digit' }), 58 likes: s.total_likes, 59 story: s.title, 60 })) 61 62 if (published.length === 0) { 63 return ( 64 <div className="text-center py-12 text-slate-500"> 65 <p>No published stories yet — analytics will appear here once you publish.</p> 66 </div> 67 ) 68 } 48 69 49 70 return ( 50 71 <div className="space-y-6"> 51 72 {/* Stat cards */} 52 <div className="grid grid-cols-2 lg:grid-cols-5 gap-4"> 53 <StatCard icon={<Eye size={18} className="text-blue-300" />} label="Total Views" value={analytics.total_views} color="bg-blue-500/20" /> 54 <StatCard icon={<Heart size={18} className="text-rose-300" />} label="Total Likes" value={analytics.total_likes} color="bg-rose-500/20" /> 55 <StatCard icon={<MessageCircle size={18} className="text-violet-300" />} label="Comments" value={analytics.total_comments} color="bg-violet-500/20" /> 56 <StatCard icon={<Clock size={18} className="text-amber-300" />} label="Avg Read (min)" value={analytics.avg_read_time} color="bg-amber-500/20" /> 57 <StatCard icon={<BarChart2 size={18} className="text-emerald-300" />} label="Completion %" value={`${analytics.completion_rate}%`} color="bg-emerald-500/20" /> 58 </div> 59 60 {/* Views chart */} 61 <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6"> 62 <div className="flex items-center gap-2 mb-4"> 63 <TrendingUp size={16} className="text-blue-400" /> 64 <h3 className="text-white font-semibold">Views Over Time</h3> 65 </div> 66 <ResponsiveContainer width="100%" height={200}> 67 <LineChart data={viewsData}> 68 <CartesianGrid strokeDasharray="3 3" stroke="#334155" /> 69 <XAxis dataKey="date" tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 70 <YAxis tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 71 <Tooltip content={<CustomTooltip />} /> 72 <Line type="monotone" dataKey="views" stroke="#6366f1" strokeWidth={2} dot={false} name="Views" /> 73 </LineChart> 74 </ResponsiveContainer> 73 <div className="grid grid-cols-2 lg:grid-cols-3 gap-4"> 74 <StatCard icon={<Heart size={18} className="text-rose-300" />} label="Total Likes" value={totalLikes} color="bg-rose-500/20" /> 75 <StatCard icon={<MessageCircle size={18} className="text-violet-300" />} label="Total Comments" value={totalComments} color="bg-violet-500/20" /> 76 <StatCard icon={<BookOpen size={18} className="text-emerald-300" />} label="Total Chapters" value={totalChapters} color="bg-emerald-500/20" /> 75 77 </div> 76 78 … … 79 81 <div className="flex items-center gap-2 mb-4"> 80 82 <Heart size={16} className="text-rose-400" /> 81 <h3 className="text-white font-semibold">Likes Over Time</h3>83 <h3 className="text-white font-semibold">Likes per Story</h3> 82 84 </div> 83 <ResponsiveContainer width="100%" height={200}> 84 <BarChart data={likesData}> 85 <CartesianGrid strokeDasharray="3 3" stroke="#334155" /> 86 <XAxis dataKey="date" tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 87 <YAxis tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 88 <Tooltip content={<CustomTooltip />} /> 89 <Bar dataKey="likes" fill="#f43f5e" radius={[4, 4, 0, 0]} name="Likes" /> 90 </BarChart> 91 </ResponsiveContainer> 85 {likesData.length > 0 ? ( 86 <ResponsiveContainer width="100%" height={200}> 87 <BarChart data={likesData}> 88 <CartesianGrid strokeDasharray="3 3" stroke="#334155" /> 89 <XAxis dataKey="date" tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 90 <YAxis tick={{ fill: '#64748b', fontSize: 11 }} tickLine={false} axisLine={false} /> 91 <Tooltip content={<CustomTooltip />} /> 92 <Bar dataKey="likes" fill="#f43f5e" radius={[4, 4, 0, 0]} name="Likes" /> 93 </BarChart> 94 </ResponsiveContainer> 95 ) : ( 96 <p className="text-slate-500 text-sm text-center py-8">No likes data yet</p> 97 )} 92 98 </div> 99 100 {/* Per-story breakdown */} 101 {published.length > 1 && ( 102 <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6"> 103 <h3 className="text-white font-semibold mb-4">Story Breakdown</h3> 104 <div className="space-y-3"> 105 {[...published] 106 .sort((a, b) => b.total_likes - a.total_likes) 107 .map(s => ( 108 <div key={s.story_id} className="flex items-center justify-between text-sm"> 109 <span className="text-slate-300 truncate max-w-xs">{s.title}</span> 110 <div className="flex items-center gap-4 text-slate-400 flex-shrink-0"> 111 <span className="flex items-center gap-1"><Heart size={12} /> {s.total_likes}</span> 112 <span className="flex items-center gap-1"><MessageCircle size={12} /> {s.total_comments}</span> 113 </div> 114 </div> 115 ))} 116 </div> 117 </div> 118 )} 93 119 </div> 94 120 )
Note:
See TracChangeset
for help on using the changeset viewer.
