"use client"; import { useSession } from "next-auth/react"; import { useEffect, useState, useCallback } from "react"; import { formatCurrency, formatDate } from "@/lib/utils"; import { StatCard, StatCardSkeleton } from "@/components/dashboard/stat-card"; import { OccupancyChart, OccupancyChartSkeleton } from "@/components/dashboard/occupancy-chart"; import { RecentBookings, RecentBookingsSkeleton } from "@/components/dashboard/recent-bookings"; import { QuickActions } from "@/components/dashboard/quick-actions"; import { useSite } from "@/contexts/site-context"; interface DashboardStats { todayBookings: number; todayRevenue: number; occupancyRate: number; activeMembers: number; pendingBookings: number; upcomingTournaments: number; } interface CourtOccupancy { courtId: string; courtName: string; availableHours: number; bookedHours: number; occupancyPercent: number; } interface RecentBooking { id: string; startTime: string; endTime: string; status: string; court: { id: string; name: string; }; client: { id: string; name: string; } | null; } interface DashboardData { stats: DashboardStats; courtOccupancy: CourtOccupancy[]; recentBookings: RecentBooking[]; date: string; } export default function DashboardPage() { const { data: session } = useSession(); const { selectedSiteId, selectedSite } = useSite(); const [dashboardData, setDashboardData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const fetchDashboardData = useCallback(async () => { try { setIsLoading(true); setError(null); const url = selectedSiteId ? `/api/dashboard/stats?siteId=${selectedSiteId}` : "/api/dashboard/stats"; const response = await fetch(url); if (!response.ok) { throw new Error("Error loading dashboard data"); } const data = await response.json(); setDashboardData(data); } catch (err) { console.error("Dashboard fetch error:", err); setError(err instanceof Error ? err.message : "Unknown error"); } finally { setIsLoading(false); } }, [selectedSiteId]); useEffect(() => { fetchDashboardData(); }, [fetchDashboardData]); const userName = session?.user?.name?.split(" ")[0] || "User"; const today = new Date(); return (
{/* Welcome Section */}

Welcome, {userName}

{formatDate(today)} - Admin panel

{selectedSite && (
Showing: {selectedSite.name}
)}
{/* Error State */} {error && (
{error}
)} {/* Stats Cards Row */}
{isLoading ? ( <> ) : dashboardData ? ( <> } /> } /> } /> } /> ) : null}
{/* Secondary Stats Row */} {!isLoading && dashboardData && (
} /> } />
)} {/* Two Column Layout: Occupancy + Recent Bookings */}
{isLoading ? ( <> ) : dashboardData ? ( <> ) : null}
{/* Quick Actions */}
); }