GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add admin dashboard and management features · aidrivencoder/master-git-github@7f21c0b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • app/admin
      • layout.tsx
      • page.tsx
      • subscriptions
        • page.tsx
      • tutorials
        • page.tsx
      • users
        • page.tsx
    • lib/firebase
      • config
        • firebase-admin.ts
      • init
        • adminData.ts
        • initialize-collections.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/app/admin/layout.tsx‎

Copy file name to clipboard
+143Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'use client';
2+
3+
import { useEffect, useState } from 'react';
4+
import { useRouter } from 'next/navigation';
5+
import Link from 'next/link';
6+
import { auth } from '@/lib/firebase/config';
7+
import { onAuthStateChanged } from 'firebase/auth';
8+
import { db } from '@/lib/firebase/config';
9+
import { doc, getDoc } from 'firebase/firestore';
10+
11+
interface NavItem {
12+
label: string;
13+
href: string;
14+
icon: string;
15+
}
16+
17+
const navItems: NavItem[] = [
18+
{
19+
label: 'Overview',
20+
href: '/admin',
21+
icon: '📊'
22+
},
23+
{
24+
label: 'Users',
25+
href: '/admin/users',
26+
icon: '👥'
27+
},
28+
{
29+
label: 'Tutorials',
30+
href: '/admin/tutorials',
31+
icon: '📚'
32+
},
33+
{
34+
label: 'Subscriptions',
35+
href: '/admin/subscriptions',
36+
icon: '💳'
37+
}
38+
];
39+
40+
export default function AdminLayout({
41+
children,
42+
}: {
43+
children: React.ReactNode;
44+
}) {
45+
const [loading, setLoading] = useState(true);
46+
const [isAdmin, setIsAdmin] = useState(false);
47+
const router = useRouter();
48+
49+
useEffect(() => {
50+
const unsubscribe = onAuthStateChanged(auth, async (user) => {
51+
if (!user) {
52+
router.push('/login');
53+
setLoading(false);
54+
return;
55+
}
56+
57+
try {
58+
// Check if user has admin role in Firestore
59+
const userDoc = await getDoc(doc(db, 'users', user.uid));
60+
const userData = userDoc.data();
61+
62+
if (!userData || userData.role !== 'admin') {
63+
router.push('/login');
64+
setLoading(false);
65+
return;
66+
}
67+
68+
setIsAdmin(true);
69+
} catch (error) {
70+
console.error('Error checking admin status:', error);
71+
router.push('/login');
72+
} finally {
73+
setLoading(false);
74+
}
75+
});
76+
77+
return () => unsubscribe();
78+
}, [router]);
79+
80+
if (loading) {
81+
return (
82+
<div className="flex items-center justify-center min-h-screen">
83+
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900"></div>
84+
</div>
85+
);
86+
}
87+
88+
if (!isAdmin) {
89+
return null;
90+
}
91+
92+
return (
93+
<div className="min-h-screen bg-gray-100">
94+
<div className="flex">
95+
{/* Admin Sidebar */}
96+
<div className="w-64 min-h-screen bg-white shadow-lg">
97+
<div className="p-6 border-b">
98+
<h2 className="text-xl font-bold text-gray-800">Admin Dashboard</h2>
99+
</div>
100+
<nav className="mt-6">
101+
{navItems.map((item) => (
102+
<Link
103+
key={item.href}
104+
href={item.href}
105+
className={`flex items-center px-6 py-3 text-gray-700 hover:bg-gray-100 hover:text-gray-900 transition-colors duration-200 ${
106+
typeof window !== 'undefined' && window.location.pathname === item.href
107+
? 'bg-gray-100 text-gray-900'
108+
: ''
109+
}`}
110+
>
111+
<span className="mr-3">{item.icon}</span>
112+
{item.label}
113+
</Link>
114+
))}
115+
</nav>
116+
117+
<div className="absolute bottom-0 w-64 p-6 border-t">
118+
<div className="flex items-center">
119+
<div className="ml-3">
120+
<p className="text-sm font-medium text-gray-700">
121+
{auth.currentUser?.email}
122+
</p>
123+
<button
124+
onClick={() => auth.signOut()}
125+
className="text-sm text-red-600 hover:text-red-800"
126+
>
127+
Sign Out
128+
</button>
129+
</div>
130+
</div>
131+
</div>
132+
</div>
133+
134+
{/* Main Content */}
135+
<div className="flex-1 overflow-auto">
136+
<div className="p-6">
137+
{children}
138+
</div>
139+
</div>
140+
</div>
141+
</div>
142+
);
143+
}
Collapse file

‎src/app/admin/page.tsx‎

Copy file name to clipboard
+174Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.