import React from 'react'; import { Container, Typography, Box, Card, CardContent, CardActions, Button, List, ListItem, ListItemText, ListItemIcon, Avatar, LinearProgress, IconButton, } from '@mui/material'; import { Add, List as ListIcon, ShoppingCart, CheckCircle, PendingActions, Share, TrendingUp, Refresh, } from '@mui/icons-material'; import { useNavigate } from 'react-router-dom'; import { useQuery } from '@tanstack/react-query'; import { dashboardAPI, listsAPI } from '../../services/api'; import { useAuth } from '../../contexts/AuthContext'; import { formatDistanceToNow } from 'date-fns'; import { ListsResponse } from '../../types'; const DashboardPage: React.FC = () => { const navigate = useNavigate(); const { user } = useAuth(); // Fetch dashboard stats const { data: statsData, isLoading: statsLoading, refetch: refetchStats } = useQuery({ queryKey: ['dashboard', 'stats'], queryFn: () => dashboardAPI.getStats(), }); // Fetch recent lists const { data: listsData, isLoading: listsLoading } = useQuery({ queryKey: ['lists', 'recent'], queryFn: () => listsAPI.getLists({ limit: 5, sortBy: 'updatedAt', sortOrder: 'desc' }).then(response => response.data), }); // Fetch recent activity const { data: activityData, isLoading: activityLoading } = useQuery({ queryKey: ['dashboard', 'activity'], queryFn: () => dashboardAPI.getRecentActivity({ limit: 10 }), }); const stats = statsData?.data?.data; const recentLists = listsData?.data?.lists || []; const recentActivity = activityData?.data?.data?.activities || []; const completionRate = stats ? Math.round((stats.completedItems / stats.totalItems) * 100) || 0 : 0; return ( {/* Welcome Section */} Tekrar hoş geldin, {user?.firstName}! 👋 Bugün alışveriş listelerinizde neler oluyor, işte özet. {/* Stats Cards */} Toplam Liste {statsLoading ? '-' : stats?.totalLists || 0} Toplam Ürün {statsLoading ? '-' : stats?.totalItems || 0} Tamamlanan {statsLoading ? '-' : stats?.completedItems || 0} Bekleyen {statsLoading ? '-' : stats?.pendingItems || 0} {/* Completion Rate */} Alışveriş İlerlemesi refetchStats()}> {completionRate}% tamamlanma oranı {stats?.totalItems || 0} üründen {stats?.completedItems || 0} tanesi tamamlandı {/* Quick Actions */} Hızlı İşlemler {/* Recent Lists */} Son Listeler {listsLoading ? ( Yükleniyor... ) : recentLists.length === 0 ? ( Henüz liste yok. İlk alışveriş listenizi oluşturun! ) : ( {recentLists.map((list: any) => ( navigate(`/lists/${list.id}`)} sx={{ px: 0, cursor: 'pointer' }} > {list.isShared ? : } {list._count?.items || 0} ürün {list.isShared && ( Paylaşılan )} } /> ))} )} {/* Recent Activity */} Son Aktiviteler {activityLoading ? ( Yükleniyor... ) : recentActivity.length === 0 ? ( Son aktivite yok. ) : ( {recentActivity.slice(0, 5).map((activity: any) => ( {activity.user?.firstName?.[0] || '?'} ))} )} ); }; export default DashboardPage;