import React from 'react'; import { View, FlatList, StyleSheet, TouchableOpacity, Text, Dimensions, Image, ActivityIndicator } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { useFiles, useFileImage } from '../hooks/useFiles'; import { FileItem } from '../types'; const NUM_COLUMNS = 3; const SCREEN_WIDTH = Dimensions.get('window').width; const ITEM_SIZE = (SCREEN_WIDTH - 16 * 2 - (NUM_COLUMNS - 1) * 6) / NUM_COLUMNS; type RootStackParamList = { Home: undefined; Upload: undefined; Scan: undefined; Search: undefined; }; type NavigationProp = NativeStackNavigationProp; type GroupedFiles = Record; function parseBackendDate(dateStr: string): Date | null { if (!dateStr) return null; const match = dateStr.match(/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})/); if (!match) return new Date(dateStr); return new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]), Number(match[4]), Number(match[5]), Number(match[6])); } function groupByDate(files: FileItem[]): GroupedFiles { const groups: GroupedFiles = {}; for (const file of files) { const d = parseBackendDate(file.createdAt); if (!d) continue; const key = d.toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', }); if (!groups[key]) groups[key] = []; groups[key].push(file); } return groups; } function formatDateLabel(key: string): string { const d = new Date(); const today = d.toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); d.setDate(d.getDate() - 1); const yesterday = d.toLocaleDateString('fr-FR', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }); if (key === today) return "Aujourd'hui"; if (key === yesterday) return 'Hier'; return key.charAt(0).toUpperCase() + key.slice(1); } export function HomeScreen() { const navigation = useNavigation(); const { data, isLoading, error } = useFiles(); if (isLoading) { return ( Chargement... ); } if (error) { return ( Erreur de chargement ); } const files = data?.data ?? []; const groups = groupByDate(files); const groupKeys = Object.keys(groups); return ( item} contentContainerStyle={styles.list} renderItem={({ item: dateKey }) => { const groupFiles = groups[dateKey]; return ( {formatDateLabel(dateKey)} {groupFiles.map((file) => ( ))} ); }} /> navigation.navigate('Upload')} > Upload navigation.navigate('Scan')} > Scan navigation.navigate('Search')} > Recherche ); } function FileGridItem({ file }: { file: FileItem }) { const { data, isLoading } = useFileImage(file.id); const uri = data?.data?.url; return ( {isLoading ? ( ) : uri ? ( ) : ( PDF )} {file.name} ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', }, center: { flex: 1, justifyContent: 'center', alignItems: 'center', }, infoText: { fontSize: 16, color: '#666', }, list: { padding: 16, paddingBottom: 80, }, section: { marginBottom: 24, }, sectionTitle: { fontSize: 18, fontWeight: '700', marginBottom: 12, color: '#333', }, grid: { flexDirection: 'row', flexWrap: 'wrap', gap: 6, }, gridItem: { width: ITEM_SIZE, }, thumb: { width: ITEM_SIZE, height: ITEM_SIZE, borderRadius: 6, backgroundColor: '#e0e0e0', }, placeholder: { width: ITEM_SIZE, height: ITEM_SIZE, borderRadius: 6, backgroundColor: '#e3f2fd', justifyContent: 'center', alignItems: 'center', }, placeholderText: { fontSize: 12, color: '#1976D2', fontWeight: '600', }, fileName: { fontSize: 11, color: '#666', marginTop: 4, textAlign: 'center', }, bottomNav: { flexDirection: 'row', justifyContent: 'space-around', paddingVertical: 12, backgroundColor: '#fff', borderTopWidth: 1, borderTopColor: '#e0e0e0', }, navButton: { padding: 8, }, navText: { fontSize: 16, color: '#1976D2', }, });