diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index 18764a55..188b52d1 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -113,179 +113,20 @@ export default function ComposeEmail({ // Check if we have content if (!emailToProcess?.content) { console.error('[DEBUG] No email content found to process'); - - // Try to use body property if content is not available (for backward compatibility) - if (emailToProcess && 'body' in emailToProcess && emailToProcess.body) { - console.log('[DEBUG] Using body property as fallback for content'); - emailToProcess.content = emailToProcess.body; - } else if (emailToProcess) { - console.log('[DEBUG] Attempting to fetch email content directly'); - try { - // Fetch the email content if not available - const response = await fetch(`/api/courrier/${emailToProcess.id}?folder=${encodeURIComponent(emailToProcess.folder || 'INBOX')}&fetchFull=true`); - - if (!response.ok) { - throw new Error(`Failed to fetch email content: ${response.status}`); - } - - const fullContent = await response.json(); - console.log('[DEBUG] API response for full email content:', { - hasContent: !!fullContent?.content, - hasBody: !!fullContent?.body, - hasHtml: !!fullContent?.html, - hasText: !!fullContent?.text, - contentLength: fullContent?.content?.length || 0 - }); - - // Update the email content with the fetched full content - if (fullContent && fullContent.content) { - console.log('[DEBUG] Successfully fetched content for reply/forward'); - emailToProcess.content = fullContent.content; - } else if (fullContent && fullContent.body) { - console.log('[DEBUG] Successfully fetched body for reply/forward'); - emailToProcess.content = fullContent.body; - } else if (fullContent && fullContent.html) { - console.log('[DEBUG] Successfully fetched HTML for reply/forward'); - emailToProcess.content = fullContent.html; - } else if (fullContent && fullContent.text) { - console.log('[DEBUG] Successfully fetched TEXT for reply/forward'); - emailToProcess.content = fullContent.text; - } else { - console.error('[DEBUG] No usable content found in API response'); - - // Try using fullContent directly if it's a string - if (typeof fullContent === 'string' && fullContent.length > 0) { - console.log('[DEBUG] Using fullContent string directly'); - emailToProcess.content = fullContent; - } else { - throw new Error('No content in fetched email'); - } - } - - // Ensure we actually have content - if (!emailToProcess.content || emailToProcess.content.trim().length === 0) { - console.error('[DEBUG] Content still empty after fetch, using fallback'); - // Use any available preview or raw data - emailToProcess.content = emailToProcess.preview || - (fullContent.raw ? fullContent.raw : 'Email content unavailable'); - } - } catch (fetchError) { - console.error('[DEBUG] Error fetching email content:', fetchError); - composeBodyRef.current.innerHTML = ` -
${emailContent}`;
- }
- }
-
- parseSuccess = true;
- } catch (error) {
- console.error('[DEBUG] API parse error:', error);
- // Try to use the content directly if API fails
- emailContent = emailToProcess.content;
-
- // If content looks like HTML, use it directly, otherwise wrap in pre tags
- if (!emailContent.startsWith('<') || !emailContent.endsWith('>')) {
- emailContent = `${emailContent}`;
- }
- }
-
- if (!emailContent || !emailContent.trim()) {
- console.warn('[DEBUG] No content available after parsing, trying direct content');
- // Final fallback: Try to use direct content or preview
- emailContent = emailToProcess.content || emailToProcess.preview ||
- (emailToProcess.body ?
- `${emailToProcess.body}` :
- '${cleanedContent}`;
- }
-
- // For HTML content, ensure it's properly contained
- return cleanedContent;
- };
-
- // Process email content
+ const type = replyTo ? 'reply' : 'forward';
+
+ // Use simple, reliable formatting for the quoted content
const formatEmailAddresses = (addresses: any) => {
if (!addresses) return 'Unknown';
if (typeof addresses === 'string') return addresses;
@@ -294,25 +135,53 @@ export default function ComposeEmail({
}
return String(addresses);
};
-
- const quotedContent = forwardFrom ? `
+
+ // Extract plain text content for reliable display
+ let emailContent = '';
+ try {
+ // Parse the original email to get clean content
+ const response = await fetch('/api/parse-email', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ email: emailToProcess.content }),
+ });
+
+ if (response.ok) {
+ const data = await response.json();
+ emailContent = data.text || '';
+ // If no text content, try to extract from HTML
+ if (!emailContent && data.html) {
+ // Create a temporary div to extract text from HTML
+ const tempDiv = document.createElement('div');
+ tempDiv.innerHTML = data.html;
+ emailContent = tempDiv.textContent || tempDiv.innerText || '';
+ }
+ }
+ } catch (error) {
+ console.error('[DEBUG] Error parsing email:', error);
+ // Fallback to simple content extraction
+ emailContent = emailToProcess.content.replace(/<[^>]*>/g, '');
+ }
+
+ // Format the content based on reply type
+ const quotedContent = type === 'forward' ? `
` : `
`;
@@ -364,7 +233,7 @@ export default function ComposeEmail({
e.stopPropagation();
e.preventDefault(); // Prevent the parent container from scrolling
}
- }, { passive: false });
+ }, { passive: false }); // Important for preventDefault to work
// Mark this element as having a scroll handler attached
(container as HTMLElement).setAttribute('data-scroll-handler-attached', 'true');