From 11d86f94646b841c9d18e4a0b05bf8c4475ec638 Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 16 Apr 2025 10:34:31 +0200 Subject: [PATCH] mail page imap connection mime 5 bis rest 16 login page 30 --- components/email.tsx | 61 +++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/components/email.tsx b/components/email.tsx index 1c68435..5e49416 100644 --- a/components/email.tsx +++ b/components/email.tsx @@ -42,59 +42,38 @@ export function Email() { const response = await fetch('/api/mail'); console.log('Response status:', response.status); - // First, get the raw text of the response - const rawText = await response.text(); - console.log('Raw response text:', rawText); - - // Try to parse it as JSON - let data; - try { - data = JSON.parse(rawText); - console.log('Parsed data type:', typeof data); - console.log('Is Array?', Array.isArray(data)); - console.log('Data:', data); - } catch (parseError) { - console.error('JSON parse error:', parseError); - throw new Error('Invalid JSON response from server'); - } - if (!response.ok) { if (response.status === 401) { signIn(); return; } - throw new Error(data.error || 'Failed to fetch emails'); + const errorData = await response.json(); + throw new Error(errorData.error || 'Failed to fetch emails'); } - // Ensure we have an array to work with - let emailsToProcess = []; - if (Array.isArray(data)) { - emailsToProcess = data; - } else if (data && Array.isArray(data.emails)) { - emailsToProcess = data.emails; - } else { - console.error('Unexpected data structure:', data); + // Get the response data + const data = await response.json(); + console.log('Received data:', data); + + // Check if data has the expected structure + if (!data || !Array.isArray(data.emails)) { + console.error('Invalid response format:', data); throw new Error('Invalid response format: no emails array found'); } - console.log('Emails to process:', emailsToProcess); - // Transform the emails - const transformedEmails = emailsToProcess.map((email) => { - console.log('Processing email:', email); - return { - id: String(email.id || ''), - subject: email.subject || '(No subject)', - sender: { - name: extractSenderName(email.from || ''), - email: extractEmailAddress(email.from || '') - }, - date: email.date || new Date().toISOString(), - isUnread: !email.read - }; - }); + const transformedEmails = data.emails.map((email) => ({ + id: String(email.id || ''), + subject: email.subject || '(No subject)', + sender: { + name: extractSenderName(email.from || ''), + email: extractEmailAddress(email.from || '') + }, + date: email.date || new Date().toISOString(), + isUnread: !email.read + })); - console.log('Final transformed emails:', transformedEmails); + console.log('Transformed emails:', transformedEmails); setEmails(transformedEmails); setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');