'use client' import { useState, useEffect } from 'react' import { LeadStats, Lead } from '@/lib/db' import StatsCards from '@/components/StatsCards' import LeadsTable from '@/components/LeadsTable' import ThemeToggle from '@/components/ThemeToggle' import LeadsChart from '@/components/LeadsChart' export default function DashboardPage() { const [stats, setStats] = useState(null) const [leadsData, setLeadsData] = useState<{ leads: Lead[], pagination: any } | null>(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { const fetchData = async () => { try { const [statsResponse, leadsResponse] = await Promise.all([ fetch('/api/stats'), fetch('/api/leads') ]) if (!statsResponse.ok || !leadsResponse.ok) { throw new Error('Failed to fetch data') } const statsData = await statsResponse.json() const leadsData = await leadsResponse.json() setStats(statsData) setLeadsData(leadsData) } catch (error) { console.error('Failed to fetch dashboard data:', error) setError('Failed to load dashboard data. Please try again.') } finally { setLoading(false) } } fetchData() }, []) if (loading) { return (

Loading dashboard...

) } if (error) { return (

{error}

) } if (!stats || !leadsData) { return (

No data available

) } return (
{/* Theme Toggle */} {/* Header */}

CHORUS Dashboard

Lead management and analytics for CHORUS Services

{/* Stats Cards */} {/* Lead Trends Chart */} {/* Source Breakdown */} {stats.by_source.length > 0 && (

Lead Sources

{stats.by_source.map((source) => (
{source.lead_source.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase())} {source.count}
))}
)} {/* Leads Table */}
) }