'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; previewMode?: boolean; } export default function EmailList({ emails, selectedEmailIds, selectedEmail, currentFolder, isLoading, totalEmails, hasMoreEmails, onSelectEmail, onToggleSelect, onToggleSelectAll, onBulkAction, onToggleStarred, onLoadMore, onSearch, previewMode = false }: EmailListProps) { const [scrollPosition, setScrollPosition] = useState(0); const [searchQuery, setSearchQuery] = useState(''); // Handle scroll to detect when user is near the bottom const handleScroll = (e: React.UIEvent) => { const target = e.target as HTMLDivElement; const scrollTop = target.scrollTop; setScrollPosition(scrollTop); // Check if we're near the bottom const scrollHeight = target.scrollHeight; const clientHeight = target.clientHeight; const scrollPosition = scrollTop + clientHeight; // If we're within 100px of the bottom, load more if (scrollHeight - scrollPosition < 100 && !isLoading && hasMoreEmails) { onLoadMore(); } }; // Handle search form submission const handleSearch = (e: React.FormEvent) => { e.preventDefault(); if (onSearch && searchQuery.trim()) { onSearch(searchQuery.trim()); } }; // Clear search const clearSearch = () => { setSearchQuery(''); if (onSearch) { 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 (
{/* Search header */}
setSearchQuery(e.target.value)} /> {searchQuery && ( )}
{/* Only show selection UI if not in preview mode */} {!previewMode ? ( ) : (

Messages

{totalEmails} {totalEmails === 1 ? 'email' : 'emails'}
)}
{/* Only show bulk actions if not in preview mode and there are selected emails */} {!previewMode && selectedEmailIds.length > 0 && ( )}
{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); }} previewMode={previewMode} /> ))} {isLoading && emails.length > 0 && (
)}
); }