import React from 'react' import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from 'recharts' import { Heart, MessageCircle, BookOpen } from 'lucide-react' import { Story } from '../../types' interface Props { stories: Story[] } const StatCard: React.FC<{ icon: React.ReactNode; label: string; value: string | number; color: string }> = ({ icon, label, value, color, }) => (
{icon}

{typeof value === 'number' ? value.toLocaleString() : value}

{label}

) const CustomTooltip = ({ active, payload, label }: any) => { if (active && payload && payload.length) { return (

{label}

{payload.map((p: any) => (

{p.name}: {p.value.toLocaleString()}

))}
) } return null } export const StoryAnalytics: React.FC = ({ stories }) => { const published = stories.filter(s => s.status === 'published') const totalLikes = stories.reduce((a, s) => a + s.total_likes, 0) const totalComments = stories.reduce((a, s) => a + s.total_comments, 0) const totalChapters = stories.reduce((a, s) => a + s.total_chapters, 0) // Likes per story (sorted by created_at) const likesData = [...published] .sort((a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()) .map(s => ({ date: new Date(s.created_at).toLocaleDateString('en-US', { month: 'short', year: '2-digit' }), likes: s.total_likes, story: s.title, })) if (published.length === 0) { return (

No published stories yet — analytics will appear here once you publish.

) } return (
{/* Stat cards */}
} label="Total Likes" value={totalLikes} color="bg-rose-500/20" /> } label="Total Comments" value={totalComments} color="bg-violet-500/20" /> } label="Total Chapters" value={totalChapters} color="bg-emerald-500/20" />
{/* Likes chart */}

Likes per Story

{likesData.length > 0 ? ( } /> ) : (

No likes data yet

)}
{/* Per-story breakdown */} {published.length > 1 && (

Story Breakdown

{[...published] .sort((a, b) => b.total_likes - a.total_likes) .map(s => (
{s.title}
{s.total_likes} {s.total_comments}
))}
)}
) }