'use client' import { useSession } from 'next-auth/react' import { useRouter } from 'next/navigation' import { useEffect, useState, useCallback } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Button } from '@/components/ui/button' import { DollarSign, Users, Package, TrendingUp, ArrowRight, Gift, Wallet, ShoppingCart } from 'lucide-react' import { motion } from 'framer-motion' import Link from 'next/link' import { toast } from 'sonner' interface DashboardStats { totalEarnings: number pendingCommissions: number totalReferrals: number activeReferrals: number currentBalance: number totalOrders: number thisMonthEarnings: number recentCommissions: { id: string amount: number level: number type: string status: string createdAt: string fromUser: { name: string email: string } }[] recentOrders: { id: string total: number status: string createdAt: string orderItems: { quantity: number product: { name: string } }[] }[] } export default function DashboardPage() { const { data: session, status } = useSession() const router = useRouter() const [stats, setStats] = useState(null) const [loading, setLoading] = useState(true) const fetchDashboardStats = useCallback(async () => { try { const response = await fetch('/api/dashboard') if (!response.ok) { if (response.status === 401) { router.push('/auth/signin?callbackUrl=/dashboard') return } throw new Error('Failed to fetch dashboard stats') } const data = await response.json() setStats(data.stats) } catch (error) { console.error('Error fetching dashboard stats:', error) toast.error('Failed to load dashboard data') } finally { setLoading(false) } }, [router]) useEffect(() => { if (status === 'unauthenticated') { router.push('/auth/signin?callbackUrl=/dashboard') return } if (status === 'authenticated') { fetchDashboardStats() } }, [status, router, fetchDashboardStats]) // Show loading spinner while checking authentication if (status === 'loading' || loading) { return (
) } // Redirect handled in useEffect, show nothing while redirecting if (status === 'unauthenticated') { return null } return (
{/* Header */}

Welcome back, {session?.user?.name}!

Here's your dashboard overview

{/* Stats Grid */} {stats && (

Total Earnings

₹{stats.totalEarnings.toFixed(2)}

₹{stats.thisMonthEarnings.toFixed(2)} this month

Current Balance

₹{stats.currentBalance.toFixed(2)}

₹{stats.pendingCommissions.toFixed(2)} pending

Total Referrals

{stats.totalReferrals}

{stats.activeReferrals} active

Total Orders

{stats.totalOrders}

Your purchases

)} {/* Quick Actions */}

View Commissions

Track your earnings

My Referrals

Manage your network

Request Payout

Withdraw earnings

{/* Recent Activity */} {stats && (
{/* Recent Commissions */}
Recent Commissions
{stats.recentCommissions.length === 0 ? (

No commissions yet

) : (
{stats.recentCommissions.slice(0, 5).map((commission) => (

₹{commission.amount.toFixed(2)}

From {commission.fromUser.name} • Level {commission.level}

{commission.status}
))}
)}
{/* Recent Orders */}
Recent Orders
{stats.recentOrders.length === 0 ? (

No orders yet

) : (
{stats.recentOrders.slice(0, 5).map((order) => (

₹{order.total.toFixed(2)}

{order.orderItems.length} items • {new Date(order.createdAt).toLocaleDateString()}

{order.status}
))}
)}
)}
) }