'use client'; import React, { useState } from 'react'; import { Loader2, Mail, Search, X } from 'lucide-react'; import { Email } from '@/hooks/use-courrier'; import EmailListItem from './EmailListItem'; import EmailListHeader from './EmailListHeader'; import BulkActionsToolbar from './BulkActionsToolbar'; import { Input } from '@/components/ui/input'; interface EmailListProps { emails: Email[]; selectedEmailIds: string[]; selectedEmail: Email | null; currentFolder: string; isLoading: boolean; totalEmails: number; hasMoreEmails: boolean; onSelectEmail: (emailId: string, accountId: string, folder: string) => void; onToggleSelect: (emailId: string) => void; onToggleSelectAll: () => void; onBulkAction: (action: 'delete' | 'mark-read' | 'mark-unread' | 'archive') => void; onToggleStarred: (emailId: string) => void; onLoadMore: () => void; onSearch?: (query: string) => void; } export default function EmailList({ emails, selectedEmailIds, selectedEmail, currentFolder, isLoading, totalEmails, hasMoreEmails, onSelectEmail, onToggleSelect, onToggleSelectAll, onBulkAction, onToggleStarred, onLoadMore, onSearch }: EmailListProps) { const [scrollPosition, setScrollPosition] = useState(0); const [searchQuery, setSearchQuery] = useState(''); // Handle scroll to detect when user reaches the bottom const handleScroll = (event: React.UIEvent) => { const target = event.target as HTMLDivElement; const { scrollTop, scrollHeight, clientHeight } = target; setScrollPosition(scrollTop); // Calculate how close to the bottom we are (in pixels) const distanceToBottom = scrollHeight - scrollTop - clientHeight; // DEBUG: Log scroll positions console.log(`[DEBUG] Scroll metrics - Distance to bottom: ${distanceToBottom}px, scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`); // CRITICAL FIX: Much more aggressive threshold - load more when within 500px of bottom // Also add double-check with percentage to handle all screen sizes const scrollPercentage = (scrollTop + clientHeight) / scrollHeight; // DEBUG: Log scroll percentage and conditions console.log(`[DEBUG] Scroll percentage: ${Math.round(scrollPercentage * 100)}%, hasMoreEmails: ${hasMoreEmails}, isLoading: ${isLoading}`); // Trigger loading when within 500px OR at 80% of the scroll distance if ((distanceToBottom < 500 || scrollPercentage > 0.8) && hasMoreEmails && !isLoading) { console.log(`[DEBUG-TRIGGER] Loading more emails - distance: ${distanceToBottom}px, percentage: ${Math.round(scrollPercentage * 100)}%`); onLoadMore(); } else if ((distanceToBottom < 500 || scrollPercentage > 0.8) && hasMoreEmails && isLoading) { console.log(`[DEBUG-BLOCKED] Not loading more emails because isLoading is true`); } else if ((distanceToBottom < 500 || scrollPercentage > 0.8) && !hasMoreEmails) { console.log(`[DEBUG-BLOCKED] Not loading more emails because hasMoreEmails is false`); } }; // Handle search const handleSearch = (e: React.FormEvent) => { e.preventDefault(); onSearch?.(searchQuery); }; const clearSearch = () => { setSearchQuery(''); onSearch?.(''); }; // Render loading state if (isLoading && emails.length === 0) { return (
); } // Render empty state if (emails.length === 0) { return (

{searchQuery ? 'No emails match your search' : currentFolder === 'INBOX' ? "Your inbox is empty. You're all caught up!" : 'No emails in this folder'}

); } // Are all emails selected const allSelected = selectedEmailIds.length === emails.length && emails.length > 0; // Are some (but not all) emails selected const someSelected = selectedEmailIds.length > 0 && selectedEmailIds.length < emails.length; return (
{/* Sticky toolbar - always visible at the top when emails are selected */} {selectedEmailIds.length > 0 && (
)} {/* Search header */}
setSearchQuery(e.target.value)} /> {searchQuery && ( )}
{emails.map((email) => ( onSelectEmail(email.id, email.accountId || '', email.folder || '')} onToggleSelect={(e: React.MouseEvent) => { e.stopPropagation(); onToggleSelect(email.id); }} onToggleStarred={(e: React.MouseEvent) => { e.stopPropagation(); onToggleStarred(email.id); }} /> ))} {isLoading && emails.length > 0 && (
)}
); }