import React, { useEffect, useState } from "react"; import { Briefcase, Calendar, CheckSquare, DollarSign, Grid3x3, TrendingUp, Users } from "lucide-react"; import { Button } from "@/components/ui/button"; import Layout from "@/components/layout/layout"; import StatsCard from "@/components/dashboard/stats-card"; import ChartCard from "@/components/dashboard/chart-card"; import WorkFocus from "@/components/dashboard/work-focus"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { mockDashboardStats, mockRevenueData, mockSalesData } from "@/lib/data"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { useAuth } from "@/lib/auth-context"; import { useNavigate } from "react-router-dom"; import { leaveApi } from "@/services/leaveApi"; import { employeeApi } from "@/services/employeeApi"; import { Leave } from "@/types"; const Dashboard = () => { const { userData } = useAuth(); const navigate = useNavigate(); const [leavesToday, setLeavesToday] = useState([]); const [employees, setEmployees] = useState([]); const [loading, setLoading] = useState(true); // Redirect client users to production-bugs page useEffect(() => { if (userData?.roleName?.toLowerCase() === "client") { navigate("/production-bugs", { state: { from: "dashboard" } }); } }, [userData, navigate]); // Fetch employees on leave today useEffect(() => { const fetchLeavesAndEmployees = async () => { try { setLoading(true); const [leavesData, employeesData] = await Promise.all([ leaveApi.getAllLeavesToday(), employeeApi.getAllForAdmin() ]); setLeavesToday(leavesData); setEmployees(employeesData); } catch (error) { console.error('Error fetching leaves or employees:', error); } finally { setLoading(false); } }; fetchLeavesAndEmployees(); }, []); // Get employee details by ID const getEmployeeById = (employeeId: number) => { return employees.find(emp => emp.id === employeeId); }; // Get initials from name const getInitials = (firstName: string, lastName: string) => { return `${firstName?.charAt(0) || ''}${lastName?.charAt(0) || ''}`.toUpperCase(); }; // Format date const formatDate = (dateString: string) => { const date = new Date(dateString); return date.toLocaleDateString('en-US', { day: 'numeric', month: 'short', year: 'numeric' }); }; // Get badge variant based on status const getStatusBadgeVariant = (status: string) => { switch (status) { case 'Approved': return 'default'; case 'Pending': return 'outline'; case 'Rejected': return 'destructive'; default: return 'outline'; } }; // Get badge color classes const getStatusBadgeClass = (status: string) => { switch (status) { case 'Approved': return 'bg-green-50 text-green-500 border-green-200'; case 'Pending': return 'bg-yellow-50 text-yellow-600 border-yellow-200'; case 'Rejected': return 'bg-red-50 text-red-500 border-red-200'; default: return 'bg-gray-50 text-gray-500 border-gray-200'; } }; // Format the revenue data for charts const revenueChartData = [mockRevenueData]; const salesChartData = [ mockSalesData.filter((_, i) => i < 9), mockSalesData.filter((_, i) => i >= 9), ]; return (

Dashboard

Welcome back! Here's an overview of your projects and team performance.

New Employees +20% from last month
10
Overall Employees: 218
Earnings +35.3% from previous month
$142,500
Previous Month: $105,400
Expenses -5.6% from previous month
$58,500
Previous Month: $62,000
Profit +70% from previous month
$84,000
Previous Month: $49,400
Task Statistics
365
Total Tasks
75
Completed Tasks
Completed Tasks 156
In Progress Tasks 75
On Hold Tasks 21
Pending Tasks 32
Review Tasks 5
Today Absent {leavesToday.length > 2 && ( )} {loading ? (
Loading...
) : leavesToday.length === 0 ? (
No employees on leave today
) : (
{leavesToday.map((leave) => { const employee = getEmployeeById(leave.employeeId); if (!employee) return null; return (
{getInitials(employee.firstName, employee.lastName)}

{employee.firstName} {employee.lastName}

{leave.type || 'Leave'} {leave.halfDay ? '(Half Day)' : ''}

{formatDate(leave.startDate)}

{leave.status}
); })}
)}
{/* Daily Work Focus Component */}
Recent Projects
Project Name Progress Status Action
OM

Office Management

Client: Delta Networks

65%
Active
HA

Hospital Administration

Client: City Medical

40%
Active
PM

Project Management

Client: CreativeTech

25%
Active
); }; export default Dashboard;