mail page fix design

This commit is contained in:
alma 2025-04-21 19:46:01 +02:00
parent 0549e3f020
commit 69d4a69713
2 changed files with 72 additions and 94 deletions

View File

@ -328,93 +328,61 @@ const initialSidebarItems = [
} }
]; ];
function getReplyBody(email: Email, type: 'reply' | 'reply-all' | 'forward'): string { function getReplyBody(email: any, type: 'reply' | 'reply-all' | 'forward' = 'reply'): string {
if (!email.body) return '';
try {
// Split email into headers and body
const [headersPart, ...bodyParts] = email.body.split('\r\n\r\n');
if (!headersPart || bodyParts.length === 0) {
throw new Error('Invalid email format: missing headers or body');
}
const body = bodyParts.join('\r\n\r\n');
// Parse headers using Infomaniak MIME decoder
const headerInfo = parseEmailHeaders(headersPart);
const boundary = extractBoundary(headersPart);
let content = ''; let content = '';
// If it's a multipart email if (email.body) {
if (boundary) { // Handle multipart emails
const parts = body.split(`--${boundary}`); if (email.body.includes('Content-Type: multipart/alternative')) {
const parts = email.body.split('--');
// Find HTML part first, fallback to text part for (const part of parts) {
const htmlPart = parts.find(part => part.toLowerCase().includes('content-type: text/html')); if (part.includes('Content-Type: text/html')) {
const textPart = parts.find(part => part.toLowerCase().includes('content-type: text/plain')); content = part.split('\n\n')[1] || '';
break;
const selectedPart = htmlPart || textPart; }
if (selectedPart) {
const [partHeaders, ...partBodyParts] = selectedPart.split('\r\n\r\n');
const partBody = partBodyParts.join('\r\n\r\n');
const partHeaderInfo = parseEmailHeaders(partHeaders);
content = partHeaderInfo.encoding === 'quoted-printable'
? decodeQuotedPrintable(partBody, partHeaderInfo.charset)
: partBody;
} }
} else { } else {
content = headerInfo.encoding === 'quoted-printable' content = email.body;
? decodeQuotedPrintable(body, headerInfo.charset)
: body;
} }
// Convert plain text to HTML if needed // Clean and structure the content
if (!headerInfo.contentType.includes('text/html')) { content = content
.replace(/<br\s*\/?>/gi, '\n')
.replace(/<p>/gi, '\n')
.replace(/<\/p>/gi, '\n')
.replace(/<div>/gi, '\n')
.replace(/<\/div>/gi, '\n')
.trim();
// Convert plain text to HTML while preserving formatting
content = content content = content
.split('\n') .split('\n')
.map(line => { .map(line => `<p>${line}</p>`)
if (!line.trim()) return '<br>';
if (line.startsWith('>')) {
return `<p class="text-gray-600" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">${line}</p>`;
}
return `<p dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">${line}</p>`;
})
.join(''); .join('');
}
// Clean HTML content // Add proper quoting structure
content = cleanHtml(content); const quotedContent = `
<blockquote style="border-left: 2px solid #ccc; padding-left: 10px; margin: 10px 0 0 0">
${content}
</blockquote>
`;
const date = new Date(email.date).toLocaleString(); // Add metadata based on type
const metadata = `
if (type === 'forward') { <div style="color: #666; font-size: 0.9em; margin-bottom: 10px;">
return ` ${type === 'forward' ? 'Forwarded message' : 'Original message'}<br/>
<div class="prose max-w-none" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;"> From: ${email.from}<br/>
<div class="border-l-4 border-gray-300 pl-4 my-4"> Date: ${new Date(email.date).toLocaleString()}<br/>
<p class="text-sm text-gray-600 mb-2" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;"><strong>From:</strong> ${email.from}</p> Subject: ${email.subject}
<p class="text-sm text-gray-600 mb-2" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;"><strong>Date:</strong> ${date}</p>
<p class="text-sm text-gray-600 mb-2" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;"><strong>Subject:</strong> ${email.subject}</p>
<p class="text-sm text-gray-600 mb-2" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;"><strong>To:</strong> ${Array.isArray(email.to) ? email.to.join(', ') : email.to}</p>
<div class="mt-4 prose-sm" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">${content}</div>
</div>
</div>
`;
} else {
return `
<div class="prose max-w-none" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">
<div class="border-l-4 border-gray-300 pl-4 my-4">
<p class="text-sm text-gray-600 mb-2" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">On ${date}, ${email.from} wrote:</p>
<div class="mt-4 prose-sm" dir="ltr" style="unicode-bidi: bidi-override; direction: ltr;">${content}</div>
</div>
</div> </div>
`; `;
return type === 'forward'
? `<div>${metadata}${quotedContent}</div>`
: `<div><br/><br/>${metadata}${quotedContent}</div>`;
} }
} catch (error) {
console.error('Error processing email body:', error);
return ''; return '';
}
} }
export default function CourrierPage() { export default function CourrierPage() {

View File

@ -1,10 +1,17 @@
'use client'; 'use client';
import { useRef, useEffect } from 'react'; import { useRef, useEffect, useState } from 'react';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input'; import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
import { Paperclip, X } from 'lucide-react'; import { Paperclip, X } from 'lucide-react';
import { Textarea } from '@/components/ui/textarea';
// Direction detection utility
function detectDirection(text: string): 'rtl' | 'ltr' {
const rtlChars = /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]/;
return rtlChars.test(text) ? 'rtl' : 'ltr';
}
interface ComposeEmailProps { interface ComposeEmailProps {
showCompose: boolean; showCompose: boolean;
@ -49,17 +56,22 @@ export default function ComposeEmail({
setAttachments, setAttachments,
handleSend handleSend
}: ComposeEmailProps) { }: ComposeEmailProps) {
const composeBodyRef = useRef<HTMLDivElement>(null); const editorRef = useRef<HTMLDivElement>(null);
const [direction, setDirection] = useState<'ltr' | 'rtl'>('ltr');
useEffect(() => { useEffect(() => {
if (composeBodyRef.current) { if (editorRef.current) {
composeBodyRef.current.innerHTML = composeBody; editorRef.current.innerHTML = composeBody;
const plainText = editorRef.current.textContent || '';
setDirection(detectDirection(plainText));
} }
}, [composeBody]); }, [composeBody]);
const handleInput = (e: React.FormEvent<HTMLDivElement>) => { const handleInput = () => {
if (composeBodyRef.current) { if (editorRef.current) {
setComposeBody(composeBodyRef.current.innerHTML); const plainText = editorRef.current.textContent || '';
setDirection(detectDirection(plainText));
setComposeBody(editorRef.current.innerHTML);
} }
}; };
@ -213,7 +225,7 @@ export default function ComposeEmail({
{/* Message Body */} {/* Message Body */}
<div className="flex-1 min-h-[200px] overflow-auto"> <div className="flex-1 min-h-[200px] overflow-auto">
<div <div
ref={composeBodyRef} ref={editorRef}
contentEditable contentEditable
className="prose max-w-none min-h-[200px] p-4 border border-gray-300 rounded-lg bg-white" className="prose max-w-none min-h-[200px] p-4 border border-gray-300 rounded-lg bg-white"
style={{ style={{
@ -224,11 +236,9 @@ export default function ComposeEmail({
fontFamily: 'inherit', fontFamily: 'inherit',
fontSize: 'inherit', fontSize: 'inherit',
lineHeight: 'inherit', lineHeight: 'inherit',
textAlign: 'left', textAlign: 'left'
direction: 'ltr',
unicodeBidi: 'bidi-override'
}} }}
dir="ltr" dir={direction}
onInput={handleInput} onInput={handleInput}
/> />
</div> </div>