From 87a08791660c352eb99ae4eb4be29dca6846730f Mon Sep 17 00:00:00 2001 From: alma Date: Thu, 24 Apr 2025 20:40:10 +0200 Subject: [PATCH] compose mime --- app/courrier/page.tsx | 34 +++++++++++++++++++++++++++------- components/ComposeEmail.tsx | 12 +++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/courrier/page.tsx b/app/courrier/page.tsx index 2492b5a2..12e71f67 100644 --- a/app/courrier/page.tsx +++ b/app/courrier/page.tsx @@ -115,7 +115,7 @@ function EmailContent({ email }: { email: Email }) { try { if (!email.body) { if (mounted) { - setContent(null); + setContent(
No content available
); setIsLoading(false); } return; @@ -124,7 +124,7 @@ function EmailContent({ email }: { email: Email }) { const formattedEmail = email.body.trim(); if (!formattedEmail) { if (mounted) { - setContent(null); + setContent(
No content available
); setIsLoading(false); } return; @@ -137,7 +137,7 @@ function EmailContent({ email }: { email: Email }) { setContent(
); } else if (parsedEmail.text) { @@ -147,7 +147,7 @@ function EmailContent({ email }: { email: Email }) {
); } else { - setContent(null); + setContent(
No content available
); } setError(null); setIsLoading(false); @@ -181,7 +181,7 @@ function EmailContent({ email }: { email: Email }) { return
{error}
; } - return content; + return content ||
No content available
; } function renderEmailContent(email: Email) { @@ -330,15 +330,29 @@ function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward' = 'r function EmailPreview({ email }: { email: Email }) { const [preview, setPreview] = useState(''); const [error, setError] = useState(null); + const [isLoading, setIsLoading] = useState(false); useEffect(() => { let mounted = true; async function loadPreview() { + if (!email?.body) { + if (mounted) setPreview('No content available'); + return; + } + + setIsLoading(true); try { const decoded = await decodeEmail(email.body); if (mounted) { - setPreview(decoded.text || cleanHtml(decoded.html || '')); + if (decoded.text) { + setPreview(decoded.text.substring(0, 150) + '...'); + } else if (decoded.html) { + const cleanText = decoded.html.replace(/<[^>]*>/g, ' ').trim(); + setPreview(cleanText.substring(0, 150) + '...'); + } else { + setPreview('No preview available'); + } setError(null); } } catch (err) { @@ -347,6 +361,8 @@ function EmailPreview({ email }: { email: Email }) { setError('Error generating preview'); setPreview(''); } + } finally { + if (mounted) setIsLoading(false); } } @@ -355,7 +371,11 @@ function EmailPreview({ email }: { email: Email }) { return () => { mounted = false; }; - }, [email.body]); + }, [email?.body]); + + if (isLoading) { + return Loading preview...; + } if (error) { return {error}; diff --git a/components/ComposeEmail.tsx b/components/ComposeEmail.tsx index f87b48f4..9fa26389 100644 --- a/components/ComposeEmail.tsx +++ b/components/ComposeEmail.tsx @@ -113,12 +113,11 @@ export default function ComposeEmail({ body: JSON.stringify({ email: emailToProcess.body }), }); + const data = await response.json(); if (!response.ok) { - throw new Error('Failed to parse email'); + throw new Error(data.error || 'Failed to parse email'); } - const parsedEmail = await response.json(); - // Format the reply/forward content const quotedContent = forwardFrom ? `
@@ -129,14 +128,14 @@ export default function ComposeEmail({ To: ${emailToProcess.to}
${emailToProcess.cc ? `Cc: ${emailToProcess.cc}
` : ''}
- ${parsedEmail.html || parsedEmail.text || 'No content available'} + ${data.html || data.text || 'No content available'}
` : `
On ${new Date(emailToProcess.date).toLocaleString()}, ${emailToProcess.from} wrote:
- ${parsedEmail.html || parsedEmail.text || 'No content available'} + ${data.html || data.text || 'No content available'}
`; @@ -160,6 +159,7 @@ export default function ComposeEmail({ // Update compose state setComposeBody(formattedContent); + setLocalContent(formattedContent); } } catch (error) { console.error('Error initializing compose content:', error); @@ -171,6 +171,8 @@ export default function ComposeEmail({ `; composeBodyRef.current.innerHTML = errorContent; + setComposeBody(errorContent); + setLocalContent(errorContent); } } };