import React, { useMemo } from 'react'; import { useTranslation } from 'react-i18next'; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, LineChart, Line } from 'recharts'; import { TrendingUp, TrendingDown, Minus } from 'lucide-react'; import { useServices } from '../hooks/useServices'; import { useResources } from '../hooks/useResources'; import { useAppointments } from '../hooks/useAppointments'; import { useCustomers } from '../hooks/useCustomers'; interface Metric { label: string; value: string; trend: 'up' | 'down' | 'neutral'; change: string; } const Dashboard: React.FC = () => { const { t } = useTranslation(); const { data: services, isLoading: servicesLoading } = useServices(); const { data: resources, isLoading: resourcesLoading } = useResources(); const { data: appointments, isLoading: appointmentsLoading } = useAppointments(); const { data: customers, isLoading: customersLoading } = useCustomers(); const isLoading = servicesLoading || resourcesLoading || appointmentsLoading || customersLoading; // Calculate metrics from real data const metrics: Metric[] = useMemo(() => { if (!appointments || !customers || !services || !resources) { return [ { label: t('dashboard.totalAppointments'), value: '0', trend: 'neutral', change: '0%' }, { label: t('customers.title'), value: '0', trend: 'neutral', change: '0%' }, { label: t('services.title'), value: '0', trend: 'neutral', change: '0%' }, { label: t('resources.title'), value: '0', trend: 'neutral', change: '0%' }, ]; } const activeCustomers = customers.filter(c => c.status === 'Active').length; return [ { label: t('dashboard.totalAppointments'), value: appointments.length.toString(), trend: 'up', change: '+12%' }, { label: t('customers.title'), value: activeCustomers.toString(), trend: 'up', change: '+8%' }, { label: t('services.title'), value: services.length.toString(), trend: 'neutral', change: '0%' }, { label: t('resources.title'), value: resources.length.toString(), trend: 'up', change: '+3%' }, ]; }, [appointments, customers, services, resources, t]); // Calculate weekly data from appointments const weeklyData = useMemo(() => { if (!appointments) { return [ { name: 'Mon', revenue: 0, appointments: 0 }, { name: 'Tue', revenue: 0, appointments: 0 }, { name: 'Wed', revenue: 0, appointments: 0 }, { name: 'Thu', revenue: 0, appointments: 0 }, { name: 'Fri', revenue: 0, appointments: 0 }, { name: 'Sat', revenue: 0, appointments: 0 }, { name: 'Sun', revenue: 0, appointments: 0 }, ]; } // Group appointments by day of week const dayMap: { [key: string]: { revenue: number; count: number } } = { 'Mon': { revenue: 0, count: 0 }, 'Tue': { revenue: 0, count: 0 }, 'Wed': { revenue: 0, count: 0 }, 'Thu': { revenue: 0, count: 0 }, 'Fri': { revenue: 0, count: 0 }, 'Sat': { revenue: 0, count: 0 }, 'Sun': { revenue: 0, count: 0 }, }; appointments.forEach(appt => { const date = new Date(appt.startTime); const dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; const dayName = dayNames[date.getDay()]; dayMap[dayName].count++; // Use price from appointment or default to 0 dayMap[dayName].revenue += appt.price || 0; }); return [ { name: 'Mon', revenue: dayMap['Mon'].revenue, appointments: dayMap['Mon'].count }, { name: 'Tue', revenue: dayMap['Tue'].revenue, appointments: dayMap['Tue'].count }, { name: 'Wed', revenue: dayMap['Wed'].revenue, appointments: dayMap['Wed'].count }, { name: 'Thu', revenue: dayMap['Thu'].revenue, appointments: dayMap['Thu'].count }, { name: 'Fri', revenue: dayMap['Fri'].revenue, appointments: dayMap['Fri'].count }, { name: 'Sat', revenue: dayMap['Sat'].revenue, appointments: dayMap['Sat'].count }, { name: 'Sun', revenue: dayMap['Sun'].revenue, appointments: dayMap['Sun'].count }, ]; }, [appointments]); if (isLoading) { return (
{t('common.loading')}
{t('dashboard.todayOverview')}
{metric.label}