From 9cde8ac9d0746000706d2fee64f4e533c434b23c Mon Sep 17 00:00:00 2001 From: alma Date: Wed, 30 Apr 2025 22:14:45 +0200 Subject: [PATCH] courrier preview --- hooks/use-email-fetch.ts | 49 +++++++---------------------------- lib/services/email-service.ts | 4 ++- lib/types.ts | 2 ++ 3 files changed, 14 insertions(+), 41 deletions(-) diff --git a/hooks/use-email-fetch.ts b/hooks/use-email-fetch.ts index 99aee13d..d078b94c 100644 --- a/hooks/use-email-fetch.ts +++ b/hooks/use-email-fetch.ts @@ -77,47 +77,16 @@ export function useEmailFetch({ onEmailLoaded, onError }: UseEmailFetchProps = { const data = await response.json(); console.log('useEmailFetch: Raw API response:', JSON.stringify(data, null, 2)); - // Transform the data if needed - const transformedEmail = { - ...data, - // Create a proper content object that includes all required fields - content: data.content || { - text: data.text || '', - html: data.html || '', - isHtml: !!data.html, // Set isHtml based on whether html exists - direction: 'ltr' // Default to left-to-right - } - }; + // Use the data directly as it already has the standardized format from the server + const transformedEmail = data; - // If data contains content as a string or object without isHtml property, - // normalize it to conform to our EmailContent interface - if (typeof transformedEmail.content === 'string') { - // If content is a string, normalize it to the proper structure - const contentStr = transformedEmail.content; - transformedEmail.content = { - text: contentStr, - html: contentStr.startsWith('<') ? contentStr : undefined, - isHtml: contentStr.startsWith('<'), - direction: 'ltr' - }; - console.log('useEmailFetch: Normalized string content to object'); - } else if (transformedEmail.content && typeof transformedEmail.content === 'object') { - // Ensure the content object has all required fields - if (!('isHtml' in transformedEmail.content)) { - transformedEmail.content.isHtml = !!transformedEmail.content.html; - console.log('useEmailFetch: Added missing isHtml field to content'); - } - if (!('direction' in transformedEmail.content)) { - transformedEmail.content.direction = 'ltr'; - console.log('useEmailFetch: Added missing direction field to content'); - } - if (!('text' in transformedEmail.content) && !transformedEmail.content.text) { - transformedEmail.content.text = ''; - console.log('useEmailFetch: Added missing text field to content'); - } - } - - console.log('useEmailFetch: Transformed email:', JSON.stringify(transformedEmail, null, 2)); + console.log('useEmailFetch: Email from API ready to use:', JSON.stringify({ + id: transformedEmail.id, + subject: transformedEmail.subject, + contentType: typeof transformedEmail.content, + hasIsHtml: transformedEmail.content && 'isHtml' in transformedEmail.content, + hasDirection: transformedEmail.content && 'direction' in transformedEmail.content + }, null, 2)); setState({ email: transformedEmail, loading: false, error: null }); onEmailLoaded?.(transformedEmail); diff --git a/lib/services/email-service.ts b/lib/services/email-service.ts index 1f12baa8..94fd30ab 100644 --- a/lib/services/email-service.ts +++ b/lib/services/email-service.ts @@ -757,7 +757,9 @@ export async function getEmailContent( })), content: { text: parsedEmail.text || '', - html: rawHtml || '' + html: rawHtml || '', + isHtml: !!rawHtml, + direction: 'ltr' // Default to left-to-right }, folder: normalizedFolder, contentFetched: true, diff --git a/lib/types.ts b/lib/types.ts index c146e03a..ca6fed2d 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -41,6 +41,8 @@ export interface EmailMessage { content: { text: string; html: string; + isHtml: boolean; // Whether the email is HTML or plain text + direction: string; // Text direction (ltr or rtl) }; preview?: string; date: Date;