mail page rest
This commit is contained in:
parent
1311ffb815
commit
1e3a8fb50f
@ -1557,56 +1557,75 @@ export default function CourrierPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Add handleReply function
|
// Add handleReply function
|
||||||
const handleReply = (type: 'reply' | 'replyAll' | 'forward') => {
|
const handleReply = async (type: 'reply' | 'replyAll' | 'forward') => {
|
||||||
if (!selectedEmail) return;
|
// First, ensure we have the full email content
|
||||||
|
if (!selectedEmail) {
|
||||||
|
setError('No email selected');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const getReplySubject = () => {
|
if (!selectedEmail.body) {
|
||||||
const subject = selectedEmail.subject;
|
|
||||||
if (type === 'forward') {
|
|
||||||
return subject.startsWith('Fwd:') ? subject : `Fwd: ${subject}`;
|
|
||||||
}
|
|
||||||
return subject.startsWith('Re:') ? subject : `Re: ${subject}`;
|
|
||||||
};
|
|
||||||
|
|
||||||
const getReplyTo = () => {
|
|
||||||
switch (type) {
|
|
||||||
case 'reply':
|
|
||||||
return selectedEmail.from;
|
|
||||||
case 'replyAll':
|
|
||||||
// For Reply All, only put the original sender in To
|
|
||||||
return selectedEmail.from;
|
|
||||||
case 'forward':
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getReplyCc = () => {
|
|
||||||
if (type === 'replyAll') {
|
|
||||||
// For Reply All, put all other recipients in CC, excluding the sender and current user
|
|
||||||
const allRecipients = new Set([
|
|
||||||
...(selectedEmail.to?.split(',') || []),
|
|
||||||
...(selectedEmail.cc?.split(',') || [])
|
|
||||||
]);
|
|
||||||
// Remove the sender and current user from CC
|
|
||||||
allRecipients.delete(selectedEmail.from);
|
|
||||||
allRecipients.delete(accounts[1]?.email);
|
|
||||||
return Array.from(allRecipients)
|
|
||||||
.map(email => email.trim())
|
|
||||||
.filter(email => email) // Remove empty strings
|
|
||||||
.join(', ');
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
};
|
|
||||||
|
|
||||||
const getReplyBody = () => {
|
|
||||||
try {
|
try {
|
||||||
|
// Fetch the full email content
|
||||||
|
const response = await fetch(`/api/mail/${selectedEmail.id}`);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch email content');
|
||||||
|
}
|
||||||
|
const emailData = await response.json();
|
||||||
|
|
||||||
|
// Update the selected email with the full content
|
||||||
|
setSelectedEmail(prev => {
|
||||||
|
if (!prev) return null;
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
body: emailData.body,
|
||||||
|
to: emailData.to,
|
||||||
|
cc: emailData.cc,
|
||||||
|
bcc: emailData.bcc
|
||||||
|
};
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error fetching email content:', error);
|
||||||
|
// Show error to user
|
||||||
|
setError('Failed to load email content. Please try again.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper functions for reply composition
|
||||||
|
const getReplySubject = (): string => {
|
||||||
|
if (!selectedEmail) return '';
|
||||||
|
const prefix = type === 'forward' ? 'Fwd:' : 'Re:';
|
||||||
|
return `${prefix} ${selectedEmail.subject}`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getReplyTo = (): string => {
|
||||||
|
if (!selectedEmail) return '';
|
||||||
|
if (type === 'forward') return '';
|
||||||
|
return selectedEmail.from;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getReplyCc = (): string => {
|
||||||
|
if (!selectedEmail) return '';
|
||||||
|
if (type === 'forward' || type === 'reply') return '';
|
||||||
|
return selectedEmail.cc || '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const getReplyBody = (): string => {
|
||||||
|
if (!selectedEmail?.body) return '';
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Now we can safely parse the full email content
|
||||||
const parsed = parseFullEmail(selectedEmail.body);
|
const parsed = parseFullEmail(selectedEmail.body);
|
||||||
let originalContent = '';
|
let originalContent = '';
|
||||||
|
|
||||||
// Get the content from either HTML or text part
|
// Get the content from either HTML or text part
|
||||||
if (parsed.html) {
|
if (parsed.html) {
|
||||||
|
// Use the existing MIME decoding for HTML content
|
||||||
|
originalContent = decodeMIME(parsed.html, 'quoted-printable', 'utf-8');
|
||||||
|
|
||||||
// Convert HTML to plain text for the reply
|
// Convert HTML to plain text for the reply
|
||||||
originalContent = parsed.html
|
originalContent = originalContent
|
||||||
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
|
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '')
|
||||||
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
|
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')
|
||||||
.replace(/<br\s*\/?>/gi, '\n')
|
.replace(/<br\s*\/?>/gi, '\n')
|
||||||
@ -1631,12 +1650,15 @@ export default function CourrierPage() {
|
|||||||
.replace(/\n{3,}/g, '\n\n')
|
.replace(/\n{3,}/g, '\n\n')
|
||||||
.trim();
|
.trim();
|
||||||
} else if (parsed.text) {
|
} else if (parsed.text) {
|
||||||
originalContent = parsed.text.trim();
|
// Use the existing MIME decoding for plain text content
|
||||||
|
originalContent = decodeMIME(parsed.text, 'quoted-printable', 'utf-8').trim();
|
||||||
} else {
|
} else {
|
||||||
// Fallback to raw body if parsing fails
|
// Fallback to raw body if parsing fails, but still try to decode it
|
||||||
originalContent = selectedEmail.body
|
originalContent = decodeMIME(
|
||||||
.replace(/<[^>]+>/g, '')
|
selectedEmail.body.replace(/<[^>]+>/g, ''),
|
||||||
.trim();
|
'quoted-printable',
|
||||||
|
'utf-8'
|
||||||
|
).trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up the content
|
// Clean up the content
|
||||||
@ -1690,27 +1712,30 @@ export default function CourrierPage() {
|
|||||||
replyHeader = `\n\nOn ${formattedDate}, ${selectedEmail.from} wrote:\n`;
|
replyHeader = `\n\nOn ${formattedDate}, ${selectedEmail.from} wrote:\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Indent the original content
|
return replyHeader + originalContent;
|
||||||
const indentedContent = originalContent
|
|
||||||
.split('\n')
|
|
||||||
.map(line => line ? `> ${line}` : '>') // Keep empty lines as '>' for better readability
|
|
||||||
.join('\n');
|
|
||||||
|
|
||||||
return `${replyHeader}${indentedContent}`;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error formatting reply:', error);
|
console.error('Error preparing reply body:', error);
|
||||||
return `\n\nOn ${new Date(selectedEmail.date).toLocaleString()}, ${selectedEmail.from} wrote:\n> ${selectedEmail.body}`;
|
return '';
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Open compose modal with reply details
|
// Prepare the reply email
|
||||||
setShowCompose(true);
|
const replyEmail = {
|
||||||
setComposeTo(getReplyTo());
|
to: getReplyTo(),
|
||||||
setComposeSubject(getReplySubject());
|
cc: getReplyCc(),
|
||||||
setComposeBody(getReplyBody());
|
subject: getReplySubject(),
|
||||||
setComposeCc(getReplyCc());
|
body: getReplyBody()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Update the compose form with the reply content
|
||||||
|
setComposeTo(replyEmail.to);
|
||||||
|
setComposeCc(replyEmail.cc);
|
||||||
|
setComposeSubject(replyEmail.subject);
|
||||||
|
setComposeBody(replyEmail.body);
|
||||||
setComposeBcc('');
|
setComposeBcc('');
|
||||||
// Show CC field automatically for Reply All
|
|
||||||
|
// Show the compose form and CC field for Reply All
|
||||||
|
setShowCompose(true);
|
||||||
setShowCc(type === 'replyAll');
|
setShowCc(type === 'replyAll');
|
||||||
setShowBcc(false);
|
setShowBcc(false);
|
||||||
setAttachments([]);
|
setAttachments([]);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user