'use client' import { useState, useEffect, useCallback, useRef } from 'react' import { useRouter } from 'next/navigation' import api from '@/services/api' import { getErrorMessage } from '@/utils/error' // ============================================ // TYPES // ============================================ type Period = '7d' | '30d' | '90d' type SortKey = 'views' | 'likes' | 'comments' | 'shares' | 'date' type SortDir = 'asc' | 'desc' interface StatSummary { current: number previous: number changePercent: number } interface ImpressionsPoint { date: string views: number } interface TopPost { id: string title: string views: number likes: number comments: number shares: number date: string } interface LanguageStat { language: string count: number percentage: number } interface LocationStat { country: string count: number } interface ReferralStat { source: 'direct' | 'search' | 'social' | string count: number percentage: number } interface DashboardData { stats: { views: StatSummary likes: StatSummary comments: StatSummary followersGained: StatSummary } impressions: ImpressionsPoint[] topPosts: TopPost[] languages: LanguageStat[] audience: { locations: LocationStat[] referrals: ReferralStat[] } } // ============================================ // HELPERS // ============================================ function formatNum(n: number): string { if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M` if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K` return n.toLocaleString() } function formatDate(iso: string, short = false): string { const d = new Date(iso) if (short) return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) } // ============================================ // STAT CARD // ============================================ interface StatCardProps { title: string value: number changePercent: number } function StatCard({ title, value, changePercent }: StatCardProps) { const positive = changePercent >= 0 const arrow = positive ? '↑' : '↓' const colorClass = positive ? 'text-[var(--accent)]' : 'text-[var(--error)]' const absChange = Math.abs(changePercent).toFixed(1) return (
{title}
{formatNum(value)}
{arrow} {absChange}% vs prior period
{formatDate(tooltip.date, true)}
{tooltip.value.toLocaleString()} views
No language data available
) } const maxCount = Math.max(...data.map((d) => d.count), 1) return (No posts to display
) } return (| Post | handleSort('views')} > Views{indicator('views')} | handleSort('likes')} > Likes{indicator('likes')} | handleSort('comments')} > Comments{indicator('comments')} | handleSort('shares')} > Shares{indicator('shares')} | handleSort('date')} > Date{indicator('date')} |
|---|---|---|---|---|---|
|
{post.title || 'Untitled post'} |
{formatNum(post.views)} | {formatNum(post.likes)} | {formatNum(post.comments)} | {formatNum(post.shares)} | {formatDate(post.date, true)} |
No location data yet
) : (No referral data yet
) : (Post performance and audience insights
{error}
No analytics data available yet. Start posting to see your stats.