mail page imap connection mime 5 bis rest 16 login page 31
This commit is contained in:
parent
11d86f9464
commit
a4f4ebd010
@ -51,36 +51,56 @@ export function Email() {
|
|||||||
throw new Error(errorData.error || 'Failed to fetch emails');
|
throw new Error(errorData.error || 'Failed to fetch emails');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the response data
|
const rawText = await response.text();
|
||||||
const data = await response.json();
|
console.log('Raw API Response:', rawText);
|
||||||
console.log('Received data:', data);
|
|
||||||
|
|
||||||
// Check if data has the expected structure
|
let data;
|
||||||
if (!data || !Array.isArray(data.emails)) {
|
try {
|
||||||
console.error('Invalid response format:', data);
|
data = JSON.parse(rawText);
|
||||||
throw new Error('Invalid response format: no emails array found');
|
console.log('Parsed API Response:', data);
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error('JSON Parse Error:', parseError);
|
||||||
|
throw new Error('Invalid JSON response from server');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform the emails
|
if (data.error) {
|
||||||
const transformedEmails = data.emails.map((email) => ({
|
throw new Error(data.error);
|
||||||
id: String(email.id || ''),
|
}
|
||||||
|
|
||||||
|
let emailsArray = [];
|
||||||
|
if (Array.isArray(data)) {
|
||||||
|
emailsArray = data;
|
||||||
|
} else if (data.messages && Array.isArray(data.messages)) {
|
||||||
|
emailsArray = data.messages;
|
||||||
|
} else if (data.emails && Array.isArray(data.emails)) {
|
||||||
|
emailsArray = data.emails;
|
||||||
|
} else {
|
||||||
|
console.error('Unexpected data structure:', data);
|
||||||
|
throw new Error('Invalid response format: emails data not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const validatedEmails = emailsArray.map(email => ({
|
||||||
|
id: email.id || Date.now().toString(),
|
||||||
|
accountId: email.accountId || 1,
|
||||||
|
from: email.from || '',
|
||||||
|
fromName: email.fromName || email.from?.split('@')[0] || 'Unknown',
|
||||||
|
to: email.to || '',
|
||||||
subject: email.subject || '(No subject)',
|
subject: email.subject || '(No subject)',
|
||||||
sender: {
|
body: email.body || '',
|
||||||
name: extractSenderName(email.from || ''),
|
|
||||||
email: extractEmailAddress(email.from || '')
|
|
||||||
},
|
|
||||||
date: email.date || new Date().toISOString(),
|
date: email.date || new Date().toISOString(),
|
||||||
isUnread: !email.read
|
read: !!email.read,
|
||||||
|
starred: !!email.starred,
|
||||||
|
category: email.category || 'inbox'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
console.log('Transformed emails:', transformedEmails);
|
console.log('Processed emails:', validatedEmails);
|
||||||
|
setEmails(validatedEmails);
|
||||||
setEmails(transformedEmails);
|
|
||||||
setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');
|
setMailUrl(data.mailUrl || 'https://espace.slm-lab.net/apps/mail/');
|
||||||
setError(null);
|
setError(null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Fetch error:', err);
|
console.error('Fetch error:', err);
|
||||||
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
setError(err instanceof Error ? err.message : 'Error fetching emails');
|
||||||
|
setEmails([]);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user