diff --git a/app/api/courrier/route.ts b/app/api/courrier/route.ts index d5e5ea6e..1fe59640 100644 --- a/app/api/courrier/route.ts +++ b/app/api/courrier/route.ts @@ -176,11 +176,16 @@ export async function GET(request: Request) { // Function to get mailboxes const getMailboxes = async () => { try { + console.log("Getting list of mailboxes..."); const mailboxes = []; const list = await imapClient.list(); + console.log(`Found ${list.length} mailboxes from IMAP server`); + for (const mailbox of list) { mailboxes.push(mailbox.path); } + + console.log("Available mailboxes:", mailboxes); return mailboxes; } catch (error) { console.error("Error listing mailboxes:", error); diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 3533cb3c..164719c1 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -1893,63 +1893,66 @@ export default function CourrierPage() { loadEmails(); }, [currentView]); + // Create a function to load folders that will be available throughout the component + const loadFolders = async () => { + console.log('[DEBUG] Explicitly loading folders from standalone function...'); + + try { + // Make a specific request just to get folders + const timestamp = Date.now(); // Cache busting + const response = await fetch(`/api/courrier?folder=INBOX&page=1&limit=1&skipCache=true&_t=${timestamp}`, { + headers: { + 'Cache-Control': 'no-cache, no-store, must-revalidate', + 'Pragma': 'no-cache' + } + }); + + if (response.ok) { + const data = await response.json(); + console.log('[DEBUG] Folder response data:', JSON.stringify(data, null, 2)); + + // Check for mailboxes field (from IMAP) and use it if available + const folders = data.mailboxes || data.folders || []; + + if (folders && folders.length > 0) { + console.log('[DEBUG] Successfully loaded folders:', folders); + + setAvailableFolders(folders); + + // Update the mail account with folders + setAccounts(prev => { + console.log('[DEBUG] Updating account with folders:', folders); + return prev.map(account => + account.id === 1 + ? { ...account, folders: folders } + : account + ); + }); + } else { + console.warn('[DEBUG] No folders found in response. Response data:', data); + } + } else { + console.error('[DEBUG] Folder request failed:', response.status); + } + } catch (error) { + console.error('[DEBUG] Error explicitly loading folders:', error); + } + }; + // Improve the folder loading logic with a delay and better reliability useEffect(() => { let isMounted = true; // For cleanup - const loadFolders = async () => { - // Only load if we don't have folders yet and we're not already loading - if ((!accounts[1]?.folders || accounts[1]?.folders?.length === 0) && !loading) { - console.log('Explicitly loading folders with delay...'); - - // Set a small delay to ensure other loading operations have completed - setTimeout(async () => { - if (!isMounted) return; - - setLoading(true); - try { - // Make a specific request just to get folders - const timestamp = Date.now(); // Cache busting - const response = await fetch(`/api/courrier?folder=INBOX&page=1&limit=1&skipCache=true&_t=${timestamp}`, { - headers: { - 'Cache-Control': 'no-cache, no-store, must-revalidate', - 'Pragma': 'no-cache' - } - }); - - if (response.ok) { - const data = await response.json(); - if (data.folders && data.folders.length > 0) { - console.log('Successfully loaded folders:', data.folders); - - if (isMounted) { - setAvailableFolders(data.folders); - - // Update the mail account with folders - setAccounts(prev => prev.map(account => - account.id === 1 - ? { ...account, folders: data.folders } - : account - )); - } - } else { - console.warn('No folders found in response'); - } - } else { - console.error('Folder request failed:', response.status); - } - } catch (error) { - console.error('Error explicitly loading folders:', error); - } finally { - if (isMounted) { - setLoading(false); - } - } - }, 500); // 500ms delay - } - }; - - loadFolders(); + // Only load if we don't have folders yet and we're not already loading + if ((!accounts[1]?.folders || accounts[1]?.folders?.length === 0) && !loading) { + console.log('[DEBUG] Triggering folder load in useEffect...'); + + // Set a small delay to ensure other loading operations have completed + setTimeout(() => { + if (!isMounted) return; + loadFolders(); + }, 500); // 500ms delay + } return () => { isMounted = false; @@ -1959,27 +1962,28 @@ export default function CourrierPage() { // Add a new function to fetch the IMAP credentials email const fetchImapCredentials = async () => { try { - console.log("Fetching IMAP credentials..."); + console.log("[DEBUG] Fetching IMAP credentials..."); const response = await fetch("/api/courrier/credentials", { method: "GET", headers: { "Content-Type": "application/json", + "Cache-Control": "no-cache" }, }); - console.log("IMAP credentials response status:", response.status); + console.log("[DEBUG] IMAP credentials response status:", response.status); if (!response.ok) { const errorData = await response.json().catch(() => ({})); - console.error("Error fetching IMAP credentials:", response.status, errorData); + console.error("[DEBUG] Error fetching IMAP credentials:", response.status, errorData); throw new Error(`Failed to fetch IMAP credentials: ${response.status}`); } const data = await response.json(); - console.log("IMAP credentials data:", data); + console.log("[DEBUG] IMAP credentials data:", data); if (data && data.credentials && data.credentials.email) { - console.log("Setting account with IMAP email:", data.credentials.email); + console.log("[DEBUG] Setting account with IMAP email:", data.credentials.email); setAccounts(prev => prev.map(account => account.id === 1 ? { @@ -1989,11 +1993,17 @@ export default function CourrierPage() { } : account )); + + // After setting the account email, explicitly load folders + setTimeout(() => { + console.log("[DEBUG] Triggering folder load after setting account"); + loadFolders(); + }, 1000); } else { - console.log("No valid IMAP credentials found in response:", data); + console.log("[DEBUG] No valid IMAP credentials found in response:", data); } } catch (error) { - console.error("Error in fetchImapCredentials:", error); + console.error("[DEBUG] Error in fetchImapCredentials:", error); } };