import React from 'react'
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
} from 'recharts'
import { Heart, MessageCircle, BookOpen, Eye } 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)
const totalViews = stories.reduce((a, s) => a + s.total_views, 0)
const sortedPublished = [...published].sort(
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
)
const likesData = sortedPublished.map(s => ({
date: new Date(s.created_at).toLocaleDateString('en-US', { month: 'short', year: '2-digit' }),
likes: s.total_likes,
story: s.title,
}))
const viewsData = sortedPublished.map(s => ({
date: new Date(s.created_at).toLocaleDateString('en-US', { month: 'short', year: '2-digit' }),
views: s.total_views,
story: s.title,
}))
if (published.length === 0) {
return (
No published stories yet — analytics will appear here once you publish.
)
}
return (
{/* Stat cards */}
} label="Total Views" value={totalViews} color="bg-cyan-500/20" />
} 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" />
{/* Views chart */}
Views per Story
{viewsData.length > 0 ? (
} />
) : (
No views yet
)}
{/* 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_views.toLocaleString()}
{s.total_likes}
{s.total_comments}
))}
)}
)
}