mail page fix

This commit is contained in:
alma 2025-04-21 17:17:03 +02:00
parent abbaa16dbe
commit ad0edb8fc2
2 changed files with 40 additions and 24 deletions

View File

@ -4,7 +4,7 @@ import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import { prisma } from '@/lib/prisma'; import { prisma } from '@/lib/prisma';
export async function GET() { export async function GET(request: Request) {
try { try {
const session = await getServerSession(authOptions); const session = await getServerSession(authOptions);
if (!session?.user?.id) { if (!session?.user?.id) {
@ -28,6 +28,16 @@ export async function GET() {
); );
} }
// Get query parameters
const url = new URL(request.url);
const folder = url.searchParams.get('folder') || 'INBOX';
const page = parseInt(url.searchParams.get('page') || '1');
const limit = parseInt(url.searchParams.get('limit') || '20');
// Calculate start and end sequence numbers
const start = (page - 1) * limit + 1;
const end = start + limit - 1;
// Connect to IMAP server // Connect to IMAP server
const client = new ImapFlow({ const client = new ImapFlow({
host: credentials.host, host: credentials.host,
@ -46,30 +56,43 @@ export async function GET() {
try { try {
await client.connect(); await client.connect();
const mailbox = await client.mailboxOpen('INBOX');
// Fetch only essential message data // Get list of all mailboxes first
const messages = await client.fetch('1:20', { const mailboxes = await client.list();
const availableFolders = mailboxes.map(box => box.path);
// Open the requested mailbox
const mailbox = await client.mailboxOpen(folder);
// Fetch messages from the current folder
const messages = await client.fetch(`${start}:${end}`, {
envelope: true, envelope: true,
flags: true flags: true,
bodyStructure: true
}); });
const result = []; const result = [];
for await (const message of messages) { for await (const message of messages) {
result.push({ result.push({
id: message.uid.toString(), id: message.uid,
from: message.envelope.from[0].address, from: message.envelope.from?.[0]?.address || '',
fromName: message.envelope.from?.[0]?.name || message.envelope.from?.[0]?.address?.split('@')[0] || '',
to: message.envelope.to?.map(addr => addr.address).join(', ') || '',
subject: message.envelope.subject || '(No subject)', subject: message.envelope.subject || '(No subject)',
date: message.envelope.date.toISOString(), date: message.envelope.date?.toISOString() || new Date().toISOString(),
read: message.flags.has('\\Seen'), read: message.flags.has('\\Seen'),
starred: message.flags.has('\\Flagged'), starred: message.flags.has('\\Flagged'),
folder: mailbox.path folder: mailbox.path,
hasAttachments: message.bodyStructure?.type === 'multipart',
flags: Array.from(message.flags)
}); });
} }
return NextResponse.json({ return NextResponse.json({
emails: result, emails: result,
folders: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Spam'] folders: availableFolders,
total: mailbox.exists,
hasMore: end < mailbox.exists
}); });
} finally { } finally {
try { try {

View File

@ -959,7 +959,7 @@ export default function CourrierPage() {
variant="ghost" variant="ghost"
size="icon" size="icon"
className="text-gray-400 hover:text-gray-900 h-9 w-9" className="text-gray-400 hover:text-gray-900 h-9 w-9"
onClick={() => handleReply('replyAll')} onClick={() => handleReply('reply-all')}
> >
<ReplyAll className="h-4 w-4" /> <ReplyAll className="h-4 w-4" />
</Button> </Button>
@ -1337,7 +1337,7 @@ export default function CourrierPage() {
}; };
// Add handleReply function // Add handleReply function
const handleReply = async (type: 'reply' | 'replyAll' | 'forward') => { const handleReply = async (type: 'reply' | 'reply-all' | 'forward') => {
if (!selectedEmail) return; if (!selectedEmail) return;
const getReplyTo = () => { const getReplyTo = () => {
@ -1346,7 +1346,7 @@ export default function CourrierPage() {
}; };
const getReplyCc = () => { const getReplyCc = () => {
if (type !== 'replyAll') return ''; if (type !== 'reply-all') return '';
return selectedEmail.cc || ''; return selectedEmail.cc || '';
}; };
@ -1375,7 +1375,7 @@ export default function CourrierPage() {
// Show the compose form and CC field for Reply All // Show the compose form and CC field for Reply All
setShowCompose(true); setShowCompose(true);
setShowCc(type === 'replyAll'); setShowCc(type === 'reply-all');
setShowBcc(false); setShowBcc(false);
setAttachments([]); setAttachments([]);
}; };
@ -1453,16 +1453,9 @@ export default function CourrierPage() {
).length; ).length;
setUnreadCount(unreadInboxEmails); setUnreadCount(unreadInboxEmails);
} }
} catch (err) { } catch (error) {
if (err instanceof Error) { console.error('Error fetching emails:', error);
if (err.name === 'AbortError') { setError(error instanceof Error ? error.message : 'Failed to fetch emails');
setError('Request timed out. Please try again.');
} else {
setError(err.message);
}
} else {
setError('Failed to fetch emails');
}
} finally { } finally {
setLoading(false); setLoading(false);
} }