mail page imap connection mime 5 bis rest 16 login page 26

This commit is contained in:
alma 2025-04-16 10:20:04 +02:00
parent bdf2743bc7
commit 2253478e76

View File

@ -41,11 +41,6 @@ export function Email() {
const response = await fetch('/api/mail');
const data = await response.json();
// Add debug logs
console.log('Raw API response:', data);
console.log('Type of data:', typeof data);
console.log('Type of data.emails:', data.emails ? typeof data.emails : 'undefined');
if (!response.ok) {
if (response.status === 401) {
signIn();
@ -54,26 +49,17 @@ export function Email() {
throw new Error(data.error || 'Failed to fetch emails');
}
// Ensure data.emails is an array
const emailsArray = Array.isArray(data.emails) ? data.emails : [];
console.log('Emails array:', emailsArray);
// Transform the email data to match the expected format
const transformedEmails = emailsArray.map((email: any) => {
console.log('Processing email:', email);
return {
id: email.id || String(Math.random()), // Fallback ID if none exists
subject: email.subject || '(No subject)',
sender: {
name: email.from ? (email.from.split('<')[0].trim().replace(/"/g, '') || email.from) : 'Unknown',
email: email.from ? ((email.from.match(/<(.+)>/) || [])[1] || email.from) : 'unknown@example.com'
},
date: email.date || new Date().toISOString(),
isUnread: !email.read
};
});
console.log('Transformed emails:', transformedEmails);
// Transform the raw emails into the expected format
const transformedEmails = (data || []).map((email: any) => ({
id: String(email.id),
subject: email.subject || '(No subject)',
sender: {
name: extractSenderName(email.from),
email: extractEmailAddress(email.from)
},
date: email.date,
isUnread: !email.read
}));
setEmails(transformedEmails);
setMailUrl(data.mailUrl);
@ -87,6 +73,25 @@ export function Email() {
}
};
// Helper functions to parse email addresses
const extractSenderName = (from: string): string => {
if (!from) return 'Unknown';
const match = from.match(/^([^<]+)?/);
if (match && match[1]) {
return match[1].trim().replace(/"/g, '');
}
return from;
};
const extractEmailAddress = (from: string): string => {
if (!from) return '';
const match = from.match(/<([^>]+)>/);
if (match && match[1]) {
return match[1];
}
return from;
};
// Initial fetch
useEffect(() => {
if (status === 'authenticated') {