import React from 'react';
import {
ChevronLeft, Reply, ReplyAll, Forward, Star, MoreHorizontal
} from 'lucide-react';
import { ScrollArea } from '@/components/ui/scroll-area';
import { Avatar, AvatarFallback } from '@/components/ui/avatar';
import { Button } from '@/components/ui/button';
import { Email } from '@/hooks/use-courrier';
interface EmailDetailViewProps {
email: Email & {
html?: string;
text?: string;
starred?: boolean; // Add starred property to interface
};
onBack: () => void;
onReply: () => void;
onReplyAll: () => void;
onForward: () => void;
onToggleStar: () => void;
}
export default function EmailDetailView({
email,
onBack,
onReply,
onReplyAll,
onForward,
onToggleStar
}: EmailDetailViewProps) {
// Format date for display
const formatDate = (dateString: string | Date) => {
// Convert to Date object if string
const date = typeof dateString === 'string' ? new Date(dateString) : dateString;
const now = new Date();
if (date.toDateString() === now.toDateString()) {
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
} else {
return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
}
};
// Render email content based on the email body
const renderEmailContent = () => {
try {
// Enhanced debugging to trace exactly what's in the content
console.log('EmailDetailView renderEmailContent - DETAILED DEBUG', {
emailId: email.id,
subject: email.subject,
hasContent: !!email.content,
contentType: typeof email.content,
contentKeys: email.content && typeof email.content === 'object' ? Object.keys(email.content) : [],
contentStringLength: typeof email.content === 'string' ? email.content.length : 'N/A',
contentHtmlLength: email.content && typeof email.content === 'object' && 'html' in email.content && typeof (email.content as any).html === 'string'
? ((email.content as any).html as string).length
: 0,
contentTextLength: email.content && typeof email.content === 'object' && 'text' in email.content && typeof (email.content as any).text === 'string'
? ((email.content as any).text as string).length
: 0,
contentSample: typeof email.content === 'string'
? email.content.substring(0, 100)
: (email.content && typeof email.content === 'object' && 'html' in email.content && typeof (email.content as any).html === 'string'
? ((email.content as any).html as string).substring(0, 100)
: (email.content && typeof email.content === 'object' && 'text' in email.content && typeof (email.content as any).text === 'string'
? ((email.content as any).text as string).substring(0, 100)
: 'N/A')),
hasHtml: !!email.html,
htmlLength: email.html?.length || 0,
htmlSample: email.html?.substring(0, 100) || 'N/A',
hasText: !!email.text,
textLength: email.text?.length || 0,
textSample: email.text?.substring(0, 100) || 'N/A',
contentIsNull: email.content === null,
contentIsUndefined: email.content === undefined,
});
// Determine what content to use and how to handle it
let contentToUse = '';
if (email.content) {
// If content is a string, use it directly
if (typeof email.content === 'string') {
contentToUse = email.content;
console.log('Using email.content as string', contentToUse.substring(0, 50));
}
// If content is an object with html/text properties
else if (typeof email.content === 'object') {
const contentObj = email.content as {html?: string; text?: string};
if (contentObj.html) {
contentToUse = contentObj.html;
console.log('Using email.content.html', contentToUse.substring(0, 50));
} else if (contentObj.text) {
// Convert plain text to HTML
contentToUse = contentObj.text
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/\n/g, '
');
console.log('Using email.content.text (converted)', contentToUse.substring(0, 50));
}
}
}
// Fall back to html or text properties if content is not available
else if (email.html) {
contentToUse = email.html;
console.log('Using fallback email.html', contentToUse.substring(0, 50));
}
else if (email.text) {
// Convert plain text to HTML with line breaks
contentToUse = email.text
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/\n/g, '
');
console.log('Using fallback email.text (converted)', contentToUse.substring(0, 50));
}
// Log if no content was found
if (!contentToUse) {
console.error('No renderable content found in email!', {
id: email.id,
subject: email.subject
});
}
// Return content or fallback message
return contentToUse ?
{email.from?.[0]?.name || ''} <{email.from?.[0]?.address || ''}>
to {email.to?.[0]?.address || ''}
{email.cc && email.cc.length > 0 && (cc {email.cc.map(c => c.address).join(', ')}
)}{attachment.filename}
{(attachment.size / 1024).toFixed(1)} KB