mail page fix
This commit is contained in:
parent
abbaa16dbe
commit
ad0edb8fc2
@ -4,7 +4,7 @@ import { getServerSession } from 'next-auth';
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
export async function GET() {
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
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
|
||||
const client = new ImapFlow({
|
||||
host: credentials.host,
|
||||
@ -46,30 +56,43 @@ export async function GET() {
|
||||
|
||||
try {
|
||||
await client.connect();
|
||||
const mailbox = await client.mailboxOpen('INBOX');
|
||||
|
||||
// Fetch only essential message data
|
||||
const messages = await client.fetch('1:20', {
|
||||
// Get list of all mailboxes first
|
||||
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,
|
||||
flags: true
|
||||
flags: true,
|
||||
bodyStructure: true
|
||||
});
|
||||
|
||||
const result = [];
|
||||
for await (const message of messages) {
|
||||
result.push({
|
||||
id: message.uid.toString(),
|
||||
from: message.envelope.from[0].address,
|
||||
id: message.uid,
|
||||
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)',
|
||||
date: message.envelope.date.toISOString(),
|
||||
date: message.envelope.date?.toISOString() || new Date().toISOString(),
|
||||
read: message.flags.has('\\Seen'),
|
||||
starred: message.flags.has('\\Flagged'),
|
||||
folder: mailbox.path
|
||||
folder: mailbox.path,
|
||||
hasAttachments: message.bodyStructure?.type === 'multipart',
|
||||
flags: Array.from(message.flags)
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
emails: result,
|
||||
folders: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Spam']
|
||||
folders: availableFolders,
|
||||
total: mailbox.exists,
|
||||
hasMore: end < mailbox.exists
|
||||
});
|
||||
} finally {
|
||||
try {
|
||||
|
||||
@ -959,7 +959,7 @@ export default function CourrierPage() {
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
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" />
|
||||
</Button>
|
||||
@ -1337,7 +1337,7 @@ export default function CourrierPage() {
|
||||
};
|
||||
|
||||
// Add handleReply function
|
||||
const handleReply = async (type: 'reply' | 'replyAll' | 'forward') => {
|
||||
const handleReply = async (type: 'reply' | 'reply-all' | 'forward') => {
|
||||
if (!selectedEmail) return;
|
||||
|
||||
const getReplyTo = () => {
|
||||
@ -1346,7 +1346,7 @@ export default function CourrierPage() {
|
||||
};
|
||||
|
||||
const getReplyCc = () => {
|
||||
if (type !== 'replyAll') return '';
|
||||
if (type !== 'reply-all') return '';
|
||||
return selectedEmail.cc || '';
|
||||
};
|
||||
|
||||
@ -1375,7 +1375,7 @@ export default function CourrierPage() {
|
||||
|
||||
// Show the compose form and CC field for Reply All
|
||||
setShowCompose(true);
|
||||
setShowCc(type === 'replyAll');
|
||||
setShowCc(type === 'reply-all');
|
||||
setShowBcc(false);
|
||||
setAttachments([]);
|
||||
};
|
||||
@ -1453,16 +1453,9 @@ export default function CourrierPage() {
|
||||
).length;
|
||||
setUnreadCount(unreadInboxEmails);
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
if (err.name === 'AbortError') {
|
||||
setError('Request timed out. Please try again.');
|
||||
} else {
|
||||
setError(err.message);
|
||||
}
|
||||
} else {
|
||||
setError('Failed to fetch emails');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching emails:', error);
|
||||
setError(error instanceof Error ? error.message : 'Failed to fetch emails');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user