|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { db } from '@/lib/firebase/config'; |
| 5 | +import { collection, getDocs, query, where, QueryDocumentSnapshot } from 'firebase/firestore'; |
| 6 | +import { FirebaseUser } from '@/lib/firebase/schema/users'; |
| 7 | +import { Tutorial } from '@/lib/firebase/schema/tutorials'; |
| 8 | +import { Subscription } from '@/lib/firebase/schema/subscriptions'; |
| 9 | + |
| 10 | +interface AdminStats { |
| 11 | + totalUsers: number; |
| 12 | + premiumUsers: number; |
| 13 | + totalTutorials: number; |
| 14 | + premiumTutorials: number; |
| 15 | + activeSubscriptions: number; |
| 16 | + recentUsers: FirebaseUser[]; |
| 17 | +} |
| 18 | + |
| 19 | +async function getAdminStats(): Promise<AdminStats> { |
| 20 | + try { |
| 21 | + // Fetch users |
| 22 | + const usersSnapshot = await getDocs(collection(db, 'users')); |
| 23 | + const users = usersSnapshot.docs.map((doc: QueryDocumentSnapshot) => { |
| 24 | + const data = doc.data(); |
| 25 | + return { |
| 26 | + id: doc.id, |
| 27 | + ...data |
| 28 | + } as FirebaseUser; |
| 29 | + }); |
| 30 | + |
| 31 | + // Fetch tutorials |
| 32 | + const tutorialsSnapshot = await getDocs(collection(db, 'tutorials')); |
| 33 | + const tutorials = tutorialsSnapshot.docs.map((doc: QueryDocumentSnapshot) => { |
| 34 | + const data = doc.data(); |
| 35 | + return { |
| 36 | + id: doc.id, |
| 37 | + ...data |
| 38 | + } as Tutorial; |
| 39 | + }); |
| 40 | + |
| 41 | + // Fetch active subscriptions |
| 42 | + const subscriptionsQuery = query( |
| 43 | + collection(db, 'subscriptions'), |
| 44 | + where('status', '==', 'active') |
| 45 | + ); |
| 46 | + const subscriptionsSnapshot = await getDocs(subscriptionsQuery); |
| 47 | + const subscriptions = subscriptionsSnapshot.docs.map((doc: QueryDocumentSnapshot) => { |
| 48 | + const data = doc.data(); |
| 49 | + return { |
| 50 | + id: doc.id, |
| 51 | + ...data |
| 52 | + } as unknown as Subscription; |
| 53 | + }); |
| 54 | + |
| 55 | + return { |
| 56 | + totalUsers: usersSnapshot.size, |
| 57 | + premiumUsers: users.filter(user => user.subscription?.tier === 'premium').length, |
| 58 | + totalTutorials: tutorials.length, |
| 59 | + premiumTutorials: tutorials.filter(tutorial => tutorial.isPremium).length, |
| 60 | + activeSubscriptions: subscriptions.length, |
| 61 | + recentUsers: users |
| 62 | + .sort((a, b) => b.createdAt.toMillis() - a.createdAt.toMillis()) |
| 63 | + .slice(0, 5) |
| 64 | + }; |
| 65 | + } catch (error) { |
| 66 | + console.error('Error fetching admin stats:', error); |
| 67 | + return { |
| 68 | + totalUsers: 0, |
| 69 | + premiumUsers: 0, |
| 70 | + totalTutorials: 0, |
| 71 | + premiumTutorials: 0, |
| 72 | + activeSubscriptions: 0, |
| 73 | + recentUsers: [] |
| 74 | + }; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +export default function AdminDashboard() { |
| 79 | + const [stats, setStats] = useState<AdminStats>({ |
| 80 | + totalUsers: 0, |
| 81 | + premiumUsers: 0, |
| 82 | + totalTutorials: 0, |
| 83 | + premiumTutorials: 0, |
| 84 | + activeSubscriptions: 0, |
| 85 | + recentUsers: [] |
| 86 | + }); |
| 87 | + const [loading, setLoading] = useState(true); |
| 88 | + |
| 89 | + useEffect(() => { |
| 90 | + const fetchStats = async () => { |
| 91 | + try { |
| 92 | + const adminStats = await getAdminStats(); |
| 93 | + setStats(adminStats); |
| 94 | + } catch (error) { |
| 95 | + console.error('Error fetching admin stats:', error); |
| 96 | + } finally { |
| 97 | + setLoading(false); |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + fetchStats(); |
| 102 | + }, []); |
| 103 | + |
| 104 | + if (loading) { |
| 105 | + return ( |
| 106 | + <div className="flex items-center justify-center min-h-screen"> |
| 107 | + <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900"></div> |
| 108 | + </div> |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + return ( |
| 113 | + <div> |
| 114 | + <h1 className="text-2xl font-bold mb-8">Dashboard Overview</h1> |
| 115 | + |
| 116 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8"> |
| 117 | + {/* Stats Cards */} |
| 118 | + <div className="bg-white p-6 rounded-lg shadow"> |
| 119 | + <h3 className="text-gray-500 text-sm">Total Users</h3> |
| 120 | + <p className="text-3xl font-bold">{stats.totalUsers}</p> |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="bg-white p-6 rounded-lg shadow"> |
| 124 | + <h3 className="text-gray-500 text-sm">Premium Users</h3> |
| 125 | + <p className="text-3xl font-bold">{stats.premiumUsers}</p> |
| 126 | + </div> |
| 127 | + |
| 128 | + <div className="bg-white p-6 rounded-lg shadow"> |
| 129 | + <h3 className="text-gray-500 text-sm">Total Tutorials</h3> |
| 130 | + <div> |
| 131 | + <p className="text-3xl font-bold">{stats.totalTutorials}</p> |
| 132 | + <p className="text-sm text-gray-500"> |
| 133 | + {stats.premiumTutorials} Premium |
| 134 | + </p> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + |
| 138 | + <div className="bg-white p-6 rounded-lg shadow"> |
| 139 | + <h3 className="text-gray-500 text-sm">Active Subscriptions</h3> |
| 140 | + <p className="text-3xl font-bold">{stats.activeSubscriptions}</p> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + {/* Recent Users */} |
| 145 | + <div className="bg-white rounded-lg shadow p-6"> |
| 146 | + <h2 className="text-xl font-semibold mb-4">Recent Users</h2> |
| 147 | + <div className="overflow-x-auto"> |
| 148 | + <table className="min-w-full"> |
| 149 | + <thead> |
| 150 | + <tr className="border-b"> |
| 151 | + <th className="text-left py-2">Name</th> |
| 152 | + <th className="text-left py-2">Email</th> |
| 153 | + <th className="text-left py-2">Subscription</th> |
| 154 | + <th className="text-left py-2">Joined</th> |
| 155 | + </tr> |
| 156 | + </thead> |
| 157 | + <tbody> |
| 158 | + {stats.recentUsers.map((user) => ( |
| 159 | + <tr key={user.id} className="border-b"> |
| 160 | + <td className="py-2">{user.displayName}</td> |
| 161 | + <td className="py-2">{user.email}</td> |
| 162 | + <td className="py-2">{user.subscription.tier}</td> |
| 163 | + <td className="py-2"> |
| 164 | + {new Date(user.createdAt.toMillis()).toLocaleDateString()} |
| 165 | + </td> |
| 166 | + </tr> |
| 167 | + ))} |
| 168 | + </tbody> |
| 169 | + </table> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +} |