import { useQuery } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { Users, Car, Calendar, Plane, Clock } from 'lucide-react'; import { VIP, Driver, ScheduleEvent } from '@/types'; import { formatDateTime } from '@/lib/utils'; interface Flight { id: string; vipId: string; vip?: { name: string; organization: string | null; }; flightNumber: string; flightDate: string; segment: number; departureAirport: string; arrivalAirport: string; scheduledDeparture: string | null; scheduledArrival: string | null; actualDeparture: string | null; actualArrival: string | null; status: string | null; } export function Dashboard() { const { data: vips } = useQuery({ queryKey: ['vips'], queryFn: async () => { const { data } = await api.get('/vips'); return data; }, }); const { data: drivers } = useQuery({ queryKey: ['drivers'], queryFn: async () => { const { data } = await api.get('/drivers'); return data; }, }); const { data: events } = useQuery({ queryKey: ['events'], queryFn: async () => { const { data } = await api.get('/events'); return data; }, }); const { data: flights } = useQuery({ queryKey: ['flights'], queryFn: async () => { const { data } = await api.get('/flights'); return data; }, }); const today = new Date(); today.setHours(0, 0, 0, 0); const tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); const eventsToday = events?.filter((e) => { const eventDate = new Date(e.startTime); return eventDate >= today && eventDate < tomorrow && e.status !== 'CANCELLED'; }).length || 0; const flightsToday = flights?.filter((f) => { const flightDate = new Date(f.flightDate); return flightDate >= today && flightDate < tomorrow; }).length || 0; const upcomingEvents = events ?.filter((e) => e.status === 'SCHEDULED' && new Date(e.startTime) >= today) .sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime()) .slice(0, 5) || []; const upcomingFlights = flights ?.filter((f) => { const flightDate = new Date(f.flightDate); return flightDate >= today && f.status !== 'cancelled'; }) .sort((a, b) => new Date(a.flightDate).getTime() - new Date(b.flightDate).getTime()) .slice(0, 5) || []; const stats = [ { name: 'Total VIPs', value: vips?.length || 0, icon: Users, color: 'bg-blue-500', }, { name: 'Active Drivers', value: drivers?.length || 0, icon: Car, color: 'bg-green-500', }, { name: 'Events Today', value: eventsToday, icon: Clock, color: 'bg-purple-500', }, { name: 'Flights Today', value: flightsToday, icon: Plane, color: 'bg-indigo-500', }, ]; return (

Dashboard

{/* Stats Grid */}
{stats.map((stat) => { const Icon = stat.icon; return (
{stat.name}
{stat.value}
); })}
{/* Recent VIPs */}

Recent VIPs

{vips && vips.length > 0 ? (
{vips.slice(0, 5).map((vip) => ( ))}
Name Organization Arrival Mode Events
{vip.name} {vip.organization || '-'} {vip.arrivalMode} {vip.events?.length || 0}
) : (

No VIPs yet. Add your first VIP to get started.

)}
{/* Upcoming Flights */}

Upcoming Flights

{upcomingFlights.length > 0 ? (
{upcomingFlights.map((flight) => (

{flight.flightNumber}

{flight.vip?.name} • {flight.departureAirport} → {flight.arrivalAirport}

{flight.scheduledDeparture && (

Departs: {formatDateTime(flight.scheduledDeparture)}

)}
{new Date(flight.flightDate).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', })} {flight.status || 'Unknown'}
))}
) : (

No upcoming flights tracked.

)}
{/* Upcoming Events */}

Upcoming Events

{upcomingEvents.length > 0 ? (
{upcomingEvents.map((event) => (

{event.title}

{event.vips && event.vips.length > 0 ? event.vips.map(vip => vip.name).join(', ') : 'No VIPs assigned'} • {event.driver?.name || 'No driver assigned'}

{event.location && (

{event.location}

)}
{formatDateTime(event.startTime)} {event.type}
))}
) : (

No upcoming events scheduled.

)}
); }