mail page ui correction maj compose 20 bis 8

This commit is contained in:
alma 2025-04-16 14:13:36 +02:00
parent b898907b4c
commit ba392749ce
2 changed files with 89 additions and 81 deletions

View File

@ -20,6 +20,7 @@ interface Email {
starred: boolean;
body: string;
to?: string;
folder: string;
}
interface ImapBox {
@ -92,21 +93,14 @@ function getStoredCredentials(): StoredCredentials | null {
export async function GET() {
try {
console.log('GET /api/mail called');
const credentials = getStoredCredentials();
if (!credentials) {
console.log('No credentials found in cookies');
return NextResponse.json(
{ error: 'No stored credentials found' },
{ status: 401 }
);
}
console.log('Using credentials:', {
...credentials,
password: '***'
});
const imap = new Imap({
user: credentials.email,
password: credentials.password,
@ -119,94 +113,111 @@ export async function GET() {
});
return new Promise((resolve) => {
const emails: Email[] = [];
const allEmails: Email[] = [];
imap.once('ready', () => {
imap.openBox('INBOX', false, (err, box) => {
// Get all mailboxes
imap.getBoxes((err, boxes) => {
if (err) {
console.error('Error opening inbox:', err);
console.error('Error getting mailboxes:', err);
imap.end();
resolve(NextResponse.json({ emails: [], error: 'Failed to open inbox' }));
resolve(NextResponse.json({ emails: [], error: 'Failed to get mailboxes' }));
return;
}
console.log('Inbox opened, total messages:', box.messages.total);
// Define folders to check
const foldersToCheck = ['INBOX', '[Gmail]/Sent Mail', '[Gmail]/Trash', '[Gmail]/Starred'];
let foldersProcessed = 0;
if (box.messages.total === 0) {
imap.end();
resolve(NextResponse.json({
emails: [],
mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
}));
return;
}
foldersToCheck.forEach(folderName => {
imap.openBox(folderName, false, (err, box) => {
if (err) {
console.error(`Error opening ${folderName}:`, err);
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
return;
}
const start = Math.max(1, box.messages.total - 19); // Get last 20 emails
const f = imap.seq.fetch(`${start}:${box.messages.total}`, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
if (box.messages.total === 0) {
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
return;
}
f.on('message', (msg) => {
const email: any = {
id: '',
from: '',
subject: '',
date: new Date(),
read: true,
starred: false,
body: '',
to: ''
};
msg.on('body', (stream, info) => {
let buffer = '';
stream.on('data', (chunk) => {
buffer += chunk.toString('utf8');
const start = Math.max(1, box.messages.total - 19);
const f = imap.seq.fetch(`${start}:${box.messages.total}`, {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT DATE)', 'TEXT'],
struct: true
});
stream.on('end', () => {
if (info.which === 'HEADER.FIELDS (FROM TO SUBJECT DATE)') {
const headers = Imap.parseHeader(buffer);
email.from = headers.from?.[0] || '';
email.to = headers.to?.[0] || '';
email.subject = headers.subject?.[0] || '(No subject)';
email.date = headers.date?.[0] || new Date().toISOString();
} else {
email.body = buffer;
f.on('message', (msg) => {
const email: any = {
id: '',
from: '',
subject: '',
date: new Date(),
read: true,
starred: false,
body: '',
to: '',
folder: folderName // Add folder information
};
msg.on('body', (stream, info) => {
let buffer = '';
stream.on('data', (chunk) => {
buffer += chunk.toString('utf8');
});
stream.on('end', () => {
if (info.which === 'HEADER.FIELDS (FROM TO SUBJECT DATE)') {
const headers = Imap.parseHeader(buffer);
email.from = headers.from?.[0] || '';
email.to = headers.to?.[0] || '';
email.subject = headers.subject?.[0] || '(No subject)';
email.date = headers.date?.[0] || new Date().toISOString();
} else {
email.body = buffer;
}
});
});
msg.once('attributes', (attrs) => {
email.id = attrs.uid;
email.read = attrs.flags?.includes('\\Seen') || false;
email.starred = attrs.flags?.includes('\\Flagged') || false;
});
msg.once('end', () => {
allEmails.push(email);
});
});
f.once('error', (err) => {
console.error(`Fetch error in ${folderName}:`, err);
});
f.once('end', () => {
foldersProcessed++;
if (foldersProcessed === foldersToCheck.length) {
finishProcessing();
}
});
});
msg.once('attributes', (attrs) => {
email.id = attrs.uid;
email.read = attrs.flags?.includes('\\Seen') || false;
email.starred = attrs.flags?.includes('\\Flagged') || false;
});
msg.once('end', () => {
emails.push(email);
});
});
f.once('error', (err) => {
console.error('Fetch error:', err);
imap.end();
resolve(NextResponse.json({
emails: [],
error: 'Failed to fetch emails'
}));
});
f.once('end', () => {
console.log('Emails before sending:', emails);
function finishProcessing() {
console.log('All folders processed, total emails:', allEmails.length);
const response = {
emails: emails,
emails: allEmails,
mailUrl: process.env.NEXTCLOUD_URL ? `${process.env.NEXTCLOUD_URL}/apps/mail/` : null
};
console.log('Final response:', response);
imap.end();
resolve(NextResponse.json(response));
});
}
});
});

View File

@ -477,19 +477,16 @@ export default function MailPage() {
// Update the filteredEmails logic
const filteredEmails = useMemo(() => {
console.log('Current view:', currentView);
console.log('Total emails:', emails.length);
return emails.filter(email => {
switch (currentView) {
case 'inbox':
return !email.deleted && email.folder === 'inbox';
return email.folder === 'INBOX';
case 'starred':
return !email.deleted && email.starred === true;
return email.starred;
case 'sent':
return !email.deleted && email.from.toLowerCase().includes('a.tmiri@governance-labs.org');
return email.folder === '[Gmail]/Sent Mail';
case 'trash':
return email.deleted === true;
return email.folder === '[Gmail]/Trash';
default:
return true;
}