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 response = await fetch('/api/mail');
const data = await response.json(); 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.ok) {
if (response.status === 401) { if (response.status === 401) {
signIn(); signIn();
@ -54,26 +49,17 @@ export function Email() {
throw new Error(data.error || 'Failed to fetch emails'); throw new Error(data.error || 'Failed to fetch emails');
} }
// Ensure data.emails is an array // Transform the raw emails into the expected format
const emailsArray = Array.isArray(data.emails) ? data.emails : []; const transformedEmails = (data || []).map((email: any) => ({
console.log('Emails array:', emailsArray); id: String(email.id),
subject: email.subject || '(No subject)',
// Transform the email data to match the expected format sender: {
const transformedEmails = emailsArray.map((email: any) => { name: extractSenderName(email.from),
console.log('Processing email:', email); email: extractEmailAddress(email.from)
return { },
id: email.id || String(Math.random()), // Fallback ID if none exists date: email.date,
subject: email.subject || '(No subject)', isUnread: !email.read
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);
setEmails(transformedEmails); setEmails(transformedEmails);
setMailUrl(data.mailUrl); 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 // Initial fetch
useEffect(() => { useEffect(() => {
if (status === 'authenticated') { if (status === 'authenticated') {