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

View File

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