"use client"; import { useSession } from "next-auth/react"; import { useEffect, useState } 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"; 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 [dashboardData, setDashboardData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { async function fetchDashboardData() { try { setIsLoading(true); setError(null); const response = await fetch("/api/dashboard/stats"); if (!response.ok) { throw new Error("Error al cargar los datos del dashboard"); } const data = await response.json(); setDashboardData(data); } catch (err) { console.error("Dashboard fetch error:", err); setError(err instanceof Error ? err.message : "Error desconocido"); } finally { setIsLoading(false); } } fetchDashboardData(); }, []); const userName = session?.user?.name?.split(" ")[0] || "Usuario"; const today = new Date(); return (
{/* Welcome Section */}

Bienvenido, {userName}

{formatDate(today)} - Panel de administracion

{session?.user?.siteName && (
{session.user.siteName}
)}
{/* 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 */}
); }