import React, { useState, useEffect } from 'react'; import { View, Text, StyleSheet, ScrollView, RefreshControl, TouchableOpacity } from 'react-native'; import { useAuth } from '../hooks/useAuth'; import { transactionService } from '../services/transactionService'; import { Transaction } from '../types'; import { TransactionCard } from '../components/TransactionCard'; export const DashboardScreen = ({ navigation }: any) => { const { user, logout } = useAuth(); const [transactions, setTransactions] = useState([]); const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); useEffect(() => { if (!user) return; const unsubscribe = transactionService.subscribeToTransactions( user.uid, (newTransactions) => { setTransactions(newTransactions); setLoading(false); setRefreshing(false); } ); return () => unsubscribe(); }, [user]); const onRefresh = () => { setRefreshing(true); }; const getCurrentMonthStats = () => { const now = new Date(); const currentMonth = now.getMonth(); const currentYear = now.getFullYear(); const monthlyTransactions = transactions.filter((t) => { const transactionDate = new Date(t.date); return ( transactionDate.getMonth() === currentMonth && transactionDate.getFullYear() === currentYear ); }); const totalIncome = monthlyTransactions .filter((t) => t.type === 'income') .reduce((sum, t) => sum + t.amount, 0); const totalExpenses = monthlyTransactions .filter((t) => t.type === 'expense') .reduce((sum, t) => sum + t.amount, 0); const balance = totalIncome - totalExpenses; return { totalIncome, totalExpenses, balance }; }; const stats = getCurrentMonthStats(); const recentTransactions = transactions.slice(0, 5); const getMonthName = () => { return new Intl.DateTimeFormat('fr-FR', { month: 'long', year: 'numeric' }).format( new Date() ); }; return ( } > Bonjour 👋 {getMonthName()} Déconnexion Solde du mois = 0 ? styles.positiveBalance : styles.negativeBalance ]} > {stats.balance >= 0 ? '+' : ''}{stats.balance.toFixed(2)} € Revenus +{stats.totalIncome.toFixed(2)} € Dépenses -{stats.totalExpenses.toFixed(2)} € Transactions récentes {transactions.length > 5 && ( navigation.navigate('Transactions')}> Voir tout )} {loading ? ( Chargement... ) : recentTransactions.length === 0 ? ( 📊 Aucune transaction Ajoutez votre première transaction pour commencer ) : ( recentTransactions.map((transaction) => ( )) )} navigation.navigate('Transactions', { type: 'expense' })} > ➖ Dépense navigation.navigate('Transactions', { type: 'income' })} > ➕ Revenu ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F8F9FA' }, header: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', padding: 24, paddingTop: 60 }, greeting: { fontSize: 28, fontWeight: 'bold', color: '#333' }, monthLabel: { fontSize: 14, color: '#666', marginTop: 4, textTransform: 'capitalize' }, logoutButton: { padding: 8 }, logoutText: { color: '#4A90E2', fontSize: 14, fontWeight: '600' }, balanceCard: { backgroundColor: '#4A90E2', margin: 24, marginTop: 0, padding: 24, borderRadius: 20, shadowColor: '#000', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.2, shadowRadius: 8, elevation: 5 }, balanceLabel: { fontSize: 14, color: '#FFF', opacity: 0.9, marginBottom: 8 }, balanceAmount: { fontSize: 36, fontWeight: 'bold', marginBottom: 20 }, positiveBalance: { color: '#FFF' }, negativeBalance: { color: '#FFE0E0' }, statsRow: { flexDirection: 'row', justifyContent: 'space-around' }, statItem: { flex: 1, alignItems: 'center' }, statDivider: { width: 1, backgroundColor: '#FFF', opacity: 0.3 }, statLabel: { fontSize: 12, color: '#FFF', opacity: 0.8, marginBottom: 4 }, incomeText: { fontSize: 16, fontWeight: '600', color: '#FFF' }, expenseText: { fontSize: 16, fontWeight: '600', color: '#FFF' }, section: { padding: 24, paddingTop: 0 }, sectionHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 16 }, sectionTitle: { fontSize: 20, fontWeight: 'bold', color: '#333' }, seeAllText: { fontSize: 14, color: '#4A90E2', fontWeight: '600' }, emptyState: { alignItems: 'center', paddingVertical: 40 }, emptyIcon: { fontSize: 48, marginBottom: 12 }, emptyText: { fontSize: 16, color: '#666', textAlign: 'center' }, emptySubtext: { fontSize: 14, color: '#999', textAlign: 'center', marginTop: 8 }, quickActions: { flexDirection: 'row', padding: 24, paddingTop: 0, gap: 12 }, actionButton: { flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', padding: 16, borderRadius: 12, gap: 8 }, addExpenseButton: { backgroundColor: '#FF6B6B' }, addIncomeButton: { backgroundColor: '#52C41A' }, actionIcon: { fontSize: 20 }, actionText: { color: '#FFF', fontSize: 16, fontWeight: '600' } });