diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index 9fa26389..92847375 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -95,14 +95,13 @@ export default function ComposeEmail({ return; } - // Create initial content immediately - const initialContent = ` -
+ // Set initial loading state + composeBodyRef.current.innerHTML = ` +

-
Loading original message...
+
Loading original message...
`; - composeBodyRef.current.innerHTML = initialContent; // Parse the original email using the API const response = await fetch('/api/parse-email', { @@ -118,6 +117,8 @@ export default function ComposeEmail({ throw new Error(data.error || 'Failed to parse email'); } + const emailContent = data.html || data.text || ''; + // Format the reply/forward content const quotedContent = forwardFrom ? `
@@ -127,35 +128,41 @@ export default function ComposeEmail({ Subject: ${emailToProcess.subject}
To: ${emailToProcess.to}
${emailToProcess.cc ? `Cc: ${emailToProcess.cc}
` : ''} -
- ${data.html || data.text || 'No content available'} +
+
+ ${emailContent}
` : `
On ${new Date(emailToProcess.date).toLocaleString()}, ${emailToProcess.from} wrote:
-
- ${data.html || data.text || 'No content available'} +
+ ${emailContent}
`; - // Set the content in the compose area + // Set the content in the compose area with proper structure + const formattedContent = ` +
+

+ ${quotedContent} +
+ `; + if (composeBodyRef.current) { - const formattedContent = ` -
-
- ${quotedContent} -
- `; composeBodyRef.current.innerHTML = formattedContent; - // Place cursor at the beginning + // Place cursor at the beginning before the quoted content const selection = window.getSelection(); const range = document.createRange(); - range.setStart(composeBodyRef.current.firstChild || composeBodyRef.current, 0); - range.collapse(true); - selection?.removeAllRanges(); - selection?.addRange(range); + const firstDiv = composeBodyRef.current.querySelector('div[style*="min-height: 20px;"]'); + if (firstDiv) { + range.setStart(firstDiv, 0); + range.collapse(true); + selection?.removeAllRanges(); + selection?.addRange(range); + (firstDiv as HTMLElement).focus(); + } // Update compose state setComposeBody(formattedContent); @@ -165,7 +172,7 @@ export default function ComposeEmail({ console.error('Error initializing compose content:', error); if (composeBodyRef.current) { const errorContent = ` -
+

Error loading original message.
@@ -184,10 +191,7 @@ export default function ComposeEmail({ const handleInput = (e: React.FormEvent) => { if (!composeBodyRef.current) return; - const composeArea = composeBodyRef.current.querySelector('.compose-area'); - if (!composeArea) return; - - const content = composeArea.innerHTML; + const content = composeBodyRef.current.innerHTML; if (!content.trim()) { setLocalContent(''); setComposeBody('');