source: chapterx-frontend/src/components/writer/StoryAnalytics.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: 4.7 KB
Line 
1import React from 'react'
2import {
3 BarChart,
4 Bar,
5 XAxis,
6 YAxis,
7 CartesianGrid,
8 Tooltip,
9 ResponsiveContainer,
10} from 'recharts'
11import { Heart, MessageCircle, BookOpen } from 'lucide-react'
12import { Story } from '../../types'
13
14interface Props {
15 stories: Story[]
16}
17
18const StatCard: React.FC<{ icon: React.ReactNode; label: string; value: string | number; color: string }> = ({
19 icon, label, value, color,
20}) => (
21 <div className="p-4 bg-slate-800 rounded-xl border border-slate-700">
22 <div className={`w-10 h-10 rounded-xl ${color} flex items-center justify-center mb-3`}>
23 {icon}
24 </div>
25 <p className="text-2xl font-bold text-white">{typeof value === 'number' ? value.toLocaleString() : value}</p>
26 <p className="text-slate-400 text-sm mt-0.5">{label}</p>
27 </div>
28)
29
30const CustomTooltip = ({ active, payload, label }: any) => {
31 if (active && payload && payload.length) {
32 return (
33 <div className="bg-slate-800 border border-slate-700 rounded-xl px-3 py-2 text-sm">
34 <p className="text-slate-400 mb-1">{label}</p>
35 {payload.map((p: any) => (
36 <p key={p.dataKey} className="text-white font-medium">
37 {p.name}: {p.value.toLocaleString()}
38 </p>
39 ))}
40 </div>
41 )
42 }
43 return null
44}
45
46export 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 }
69
70 return (
71 <div className="space-y-6">
72 {/* Stat cards */}
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" />
77 </div>
78
79 {/* Likes chart */}
80 <div className="bg-slate-800 border border-slate-700 rounded-2xl p-6">
81 <div className="flex items-center gap-2 mb-4">
82 <Heart size={16} className="text-rose-400" />
83 <h3 className="text-white font-semibold">Likes per Story</h3>
84 </div>
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 )}
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 )}
119 </div>
120 )
121}
Note: See TracBrowser for help on using the repository browser.