import { Metadata } from 'next'; import { db } from '@/lib/db'; import { auth } from '@/lib/auth'; import { redirect } from 'next/navigation'; import { getCachedBunnyStorageStats, getCachedTotalStorage } from '@/lib/admin-stats'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Users, Folder, Video, MessageSquare, Mic, HardDrive, Image as ImageIcon, Film, MessageSquareQuote, Star } from 'lucide-react'; export const metadata: Metadata = { title: 'Admin Dashboard | OpenFrame', description: 'Admin overview dashboard', }; function formatBytes(bytes: number, decimals = 2) { if (bytes < 0) return 'Error Fetching'; if (!+bytes) return '0 Bytes'; const k = 1000; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`; } export default async function AdminDashboardPage() { const session = await auth(); if (!session?.user?.isAdmin) { redirect('/'); } const userFeedbackDelegate = (db as unknown as { userFeedback?: { count: (args?: unknown) => Promise }; }).userFeedback; // 1. Database Stats const [ totalUsers, totalProjects, totalVideos, totalComments, totalVoiceComments, totalImageComments, ] = await Promise.all([ db.user.count(), db.project.count(), db.video.count(), db.comment.count(), db.comment.count({ where: { voiceUrl: { not: null } }, }), db.comment.count({ where: { imageUrl: { not: null } }, }), ]); let totalFeedback = 0; let totalReviews = 0; if (userFeedbackDelegate) { try { [totalFeedback, totalReviews] = await Promise.all([ userFeedbackDelegate.count({ where: { type: 'FEEDBACK' }, }), userFeedbackDelegate.count({ where: { type: 'REVIEW' }, }), ]); } catch (error) { console.error('Failed to fetch feedback stats:', error); } } // 2. Storage Stats (Cached) const [totalStorageBytes, bunnyStorageStats] = await Promise.all([ getCachedTotalStorage(), getCachedBunnyStorageStats(), ]); return (

Dashboard Overview

Total Users
{totalUsers}
Workspaces & Projects
{totalProjects}

Total active projects on the platform

Active Videos
{totalVideos}
Total Comments
{totalComments}
Voice Recordings
{totalVoiceComments}
Image Attachments
{totalImageComments}
Feedback Submissions
{totalFeedback}
Review Submissions
{totalReviews}
Cloudflare R2 Storage
{formatBytes(totalStorageBytes)}
Bunny Stream Storage
{formatBytes(bunnyStorageStats.totalBytes)}
); }