"use client" import { useState, useEffect, useRef } from "react" import { PageHeader } from "@/components/shared/page-header" import { StatCard } from "@/components/dashboard/stat-card" import { StatCardSkeleton } from "@/components/dashboard/stat-card-skeleton" import { RecentLeadsTable } from "@/components/dashboard/recent-leads-table" import { LeadStatusChart } from "@/components/dashboard/lead-status-chart" import { LeadsPerMonthChart } from "@/components/dashboard/leads-per-month-chart" import { Users, UserPlus, PhoneCall, Clock, CheckCircle2, TrendingUp, ListFilter, } from "lucide-react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { DashboardStats } from "@/types" import { CrossedLightsabers } from "@/components/dashboard/crossed-lightsabers" import { StarField } from "@/components/dashboard/star-field" import { useWebsiteTheme } from "@/providers/website-theme-provider" export default function DashboardPage() { const { websiteTheme: theme } = useWebsiteTheme() const [period, setPeriod] = useState("6months") const [stats, setStats] = useState(null) const pollingRef = useRef(null) async function fetchStats(p: string) { try { const res = await fetch(`/api/dashboard?period=${p}`) if (res.ok) { const data = await res.json() setStats(data) } } catch { console.warn("Failed to fetch dashboard stats") } } useEffect(() => { fetchStats(period) pollingRef.current = setInterval(() => fetchStats(period), 30000) return () => { if (pollingRef.current) clearInterval(pollingRef.current) } }, [period]) const statCards = stats ? [ { title: "Total Leads", value: stats.totalLeads, icon: Users, description: stats.periodLabel, trend: stats.trends.totalLeads, sparklineField: "total" as const }, { title: "Open Leads", value: stats.openLeads, icon: UserPlus, description: "Needs attention", trend: stats.trends.openLeads, sparklineField: "open" as const }, { title: "Contacted", value: stats.contactedLeads, icon: PhoneCall, description: "In conversation", trend: stats.trends.contactedLeads, sparklineField: "contacted" as const }, { title: "Pending", value: stats.pendingLeads, icon: Clock, description: "Awaiting decision", trend: stats.trends.pendingLeads, sparklineField: "pending" as const }, { title: "Closed", value: stats.closedLeads, icon: CheckCircle2, description: "Won", trend: stats.trends.closedLeads, sparklineField: "closed" as const }, { title: "Conversion Rate", value: `${stats.conversionRate}%`, icon: TrendingUp, description: `${stats.closedLeads} of ${stats.totalLeads} leads`, trend: stats.trends.conversionRate, sparklineField: "closed" as const, conversionRate: stats.conversionRate }, ] : [] return (
{theme === "starforce" && }
Dashboard} description="Overview of your sales pipeline" > {theme === "starforce" && ( Trust the Force, Find your Path )} {theme === "starforce" && }

Pipeline Overview

{stats ? statCards.map((card, i) => ( )) : Array.from({ length: 6 }).map((_, i) => ) }

Analytics

) }