diff --git a/components/email/EmailPreview.tsx b/components/email/EmailPreview.tsx index 6e5b729b..2b08292f 100644 --- a/components/email/EmailPreview.tsx +++ b/components/email/EmailPreview.tsx @@ -108,39 +108,23 @@ export default function EmailPreview({ email, loading = false, onReply }: EmailP } try { - console.log('EmailPreview: Raw email content:', { - content: email.content, - html: email.html, - text: email.text, - formattedContent: email.formattedContent - }); + console.log('EmailPreview: Full email object:', JSON.stringify(email, null, 2)); // Get the content in order of preference let content = ''; // If content is an object with html/text if (email.content && typeof email.content === 'object') { - console.log('EmailPreview: Using object content:', email.content); + console.log('EmailPreview: Using object content:', JSON.stringify(email.content, null, 2)); content = email.content.html || email.content.text || ''; } // If content is a string else if (typeof email.content === 'string') { - console.log('EmailPreview: Using direct string content'); + console.log('EmailPreview: Using direct string content:', email.content); content = email.content; } - // Fallback to html/text properties - else { - console.log('EmailPreview: Using html/text properties'); - content = email.html || email.text || ''; - } - // If content is empty, try to get it from the formattedContent property - if (!content && email.formattedContent) { - console.log('EmailPreview: Using formattedContent property'); - content = email.formattedContent; - } - - console.log('EmailPreview: Content before sanitization:', content); + console.log('EmailPreview: Final content before sanitization:', content); // Sanitize the content for display const sanitizedContent = DOMPurify.sanitize(content, { diff --git a/hooks/use-email-fetch.ts b/hooks/use-email-fetch.ts index 5b2f6a4b..f1dfcd7b 100644 --- a/hooks/use-email-fetch.ts +++ b/hooks/use-email-fetch.ts @@ -44,43 +44,45 @@ export function useEmailFetch({ onEmailLoaded, onError }: UseEmailFetchProps = { // Fetch email with proper error handling and cancellation const fetchEmail = useCallback(async (emailId: string, accountId: string, folder: string) => { + if (!emailId || !accountId || !folder) { + console.error('Missing required parameters for fetchEmail'); + return; + } + + setState(prev => ({ ...prev, loading: true, error: null })); + try { - // Cancel any in-flight request - if (abortControllerRef.current) { - abortControllerRef.current.abort(); - } - - // Create new abort controller - abortControllerRef.current = new AbortController(); - - // Validate parameters - validateFetchParams(emailId, accountId, folder); - - setState(prev => ({ ...prev, loading: true, error: null })); - + console.log('useEmailFetch: Fetching email with params:', { emailId, accountId, folder }); const response = await fetch( `/api/courrier/${emailId}?accountId=${encodeURIComponent(accountId)}&folder=${encodeURIComponent(folder)}`, { - signal: abortControllerRef.current.signal + signal: abortControllerRef.current?.signal } ); if (!response.ok) { - const errorData = await response.json(); - throw new Error(errorData.error || 'Failed to fetch email'); + throw new Error(`Failed to fetch email: ${response.statusText}`); } - + const data = await response.json(); + console.log('useEmailFetch: Raw API response:', JSON.stringify(data, null, 2)); - if (!data) { - throw new Error('Email not found'); - } + // Transform the data if needed + const transformedEmail = { + ...data, + content: data.content || { + text: data.text || '', + html: data.html || '' + } + }; - setState({ email: data, loading: false, error: null }); - onEmailLoaded?.(data); + console.log('useEmailFetch: Transformed email:', JSON.stringify(transformedEmail, null, 2)); + + setState({ email: transformedEmail, loading: false, error: null }); + onEmailLoaded?.(transformedEmail); // Mark as read if not already - if (!data.flags?.seen) { + if (!transformedEmail.flags?.seen) { try { await fetch(`/api/courrier/${emailId}/mark-read`, { method: 'POST', @@ -97,6 +99,7 @@ export function useEmailFetch({ onEmailLoaded, onError }: UseEmailFetchProps = { return; } + console.error('useEmailFetch: Error fetching email:', err); const errorMessage = err instanceof Error ? err.message : 'Failed to load email'; setState(prev => ({ ...prev, loading: false, error: errorMessage })); onError?.(errorMessage); diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index 1dc739d1..f31dcace 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -533,12 +533,12 @@ export async function getEmailContent( })), content: { text: parsedEmail.text || '', - html: rawHtml + html: rawHtml || '' }, folder: actualFolder, contentFetched: true, size: size || 0, - accountId: accountId || 'default' // Add accountId to the email object + accountId: accountId || 'default' }; // Cache the email content with account-specific key