courrier clean
This commit is contained in:
parent
e3db0a2ae1
commit
7820663c78
@ -2309,8 +2309,7 @@ export default function CourrierPage() {
|
|||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
{/* Compose Email Modal - Commented out due to type mismatch */}
|
{/* Compose Email Modal */}
|
||||||
{/* The component expected different props than what we're providing */}
|
|
||||||
<ComposeEmail
|
<ComposeEmail
|
||||||
showCompose={showCompose}
|
showCompose={showCompose}
|
||||||
setShowCompose={setShowCompose}
|
setShowCompose={setShowCompose}
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useRef, useState } from 'react';
|
import { useState, useRef, useEffect } from 'react';
|
||||||
|
import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal, Loader2 } from 'lucide-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 { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle, CardFooter } from '@/components/ui/card';
|
||||||
import { X, Paperclip, ChevronDown, ChevronUp, SendHorizontal } from 'lucide-react';
|
import DOMPurify from 'isomorphic-dompurify';
|
||||||
|
import { formatEmailForReplyOrForward } from '@/lib/services/email-service';
|
||||||
import { Email } from '@/app/courrier/page';
|
import { Email } from '@/app/courrier/page';
|
||||||
|
|
||||||
interface ComposeEmailProps {
|
interface ComposeEmailProps {
|
||||||
@ -33,12 +35,6 @@ interface ComposeEmailProps {
|
|||||||
};
|
};
|
||||||
onSend: (email: any) => Promise<void>;
|
onSend: (email: any) => Promise<void>;
|
||||||
onCancel: () => void;
|
onCancel: () => void;
|
||||||
onBodyChange?: (body: string) => void;
|
|
||||||
initialTo?: string;
|
|
||||||
initialSubject?: string;
|
|
||||||
initialBody?: string;
|
|
||||||
initialCc?: string;
|
|
||||||
initialBcc?: string;
|
|
||||||
replyTo?: Email | null;
|
replyTo?: Email | null;
|
||||||
forwardFrom?: Email | null;
|
forwardFrom?: Email | null;
|
||||||
}
|
}
|
||||||
@ -64,135 +60,172 @@ export default function ComposeEmail({
|
|||||||
setAttachments,
|
setAttachments,
|
||||||
handleSend,
|
handleSend,
|
||||||
originalEmail,
|
originalEmail,
|
||||||
onSend,
|
|
||||||
onCancel,
|
|
||||||
onBodyChange,
|
|
||||||
initialTo,
|
|
||||||
initialSubject,
|
|
||||||
initialBody,
|
|
||||||
initialCc,
|
|
||||||
initialBcc,
|
|
||||||
replyTo,
|
replyTo,
|
||||||
forwardFrom
|
forwardFrom,
|
||||||
|
onSend,
|
||||||
|
onCancel
|
||||||
}: ComposeEmailProps) {
|
}: ComposeEmailProps) {
|
||||||
const [sending, setSending] = useState(false);
|
const [sending, setSending] = useState(false);
|
||||||
const editorRef = useRef<HTMLDivElement>(null);
|
const editorRef = useRef<HTMLDivElement>(null);
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
if (!showCompose) return null;
|
useEffect(() => {
|
||||||
|
if (editorRef.current) {
|
||||||
const handleInput = (e: React.FormEvent<HTMLDivElement>) => {
|
editorRef.current.focus();
|
||||||
const content = e.currentTarget.innerHTML;
|
|
||||||
setComposeBody(content);
|
|
||||||
if (onBodyChange) {
|
|
||||||
onBodyChange(content);
|
|
||||||
}
|
}
|
||||||
|
}, [showCompose]);
|
||||||
|
|
||||||
|
// Initialize content when replying or forwarding
|
||||||
|
useEffect(() => {
|
||||||
|
// Handle reply or forward initialization
|
||||||
|
if (replyTo) {
|
||||||
|
// For reply/reply-all
|
||||||
|
const formattedEmail = formatEmailForReplyOrForward(replyTo as any, 'reply');
|
||||||
|
setComposeTo(formattedEmail.to);
|
||||||
|
setComposeSubject(formattedEmail.subject);
|
||||||
|
setComposeBody(formattedEmail.body);
|
||||||
|
|
||||||
|
// Show CC if needed for reply-all
|
||||||
|
if (formattedEmail.cc) {
|
||||||
|
setComposeCc(formattedEmail.cc);
|
||||||
|
setShowCc(true);
|
||||||
|
}
|
||||||
|
} else if (forwardFrom) {
|
||||||
|
// For forward
|
||||||
|
initializeForwardedEmail(forwardFrom);
|
||||||
|
}
|
||||||
|
}, [replyTo, forwardFrom]);
|
||||||
|
|
||||||
|
// Initialize forwarded email content
|
||||||
|
const initializeForwardedEmail = async (email: any) => {
|
||||||
|
if (!email) return;
|
||||||
|
|
||||||
|
// Format subject with Fwd: prefix if needed
|
||||||
|
const subjectBase = email.subject || '(No subject)';
|
||||||
|
const subjectRegex = /^(Fwd|FW|Forward):\s*/i;
|
||||||
|
const subject = subjectRegex.test(subjectBase)
|
||||||
|
? subjectBase
|
||||||
|
: `Fwd: ${subjectBase}`;
|
||||||
|
|
||||||
|
setComposeSubject(subject);
|
||||||
|
|
||||||
|
// Create header for forwarded email
|
||||||
|
const headerHtml = `
|
||||||
|
<div style="border-top: 1px solid #e1e1e1; margin-top: 20px; padding-top: 15px; font-family: Arial, sans-serif;">
|
||||||
|
<div style="margin-bottom: 15px;">
|
||||||
|
<div style="font-weight: normal; margin-bottom: 10px;">---------- Forwarded message ---------</div>
|
||||||
|
<div><b>From:</b> ${email.fromName || email.from}</div>
|
||||||
|
<div><b>Date:</b> ${new Date(email.date).toLocaleString()}</div>
|
||||||
|
<div><b>Subject:</b> ${email.subject || '(No subject)'}</div>
|
||||||
|
<div><b>To:</b> ${email.to}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
|
||||||
|
// Prepare content
|
||||||
|
let contentHtml = '<div style="color: #666; font-style: italic; padding: 15px; border: 1px dashed #ccc; margin: 10px 0; text-align: center; background-color: #f9f9f9;">No content available</div>';
|
||||||
|
|
||||||
|
if (email.content) {
|
||||||
|
// Sanitize the content
|
||||||
|
contentHtml = DOMPurify.sanitize(email.content, {
|
||||||
|
ADD_TAGS: ['style'],
|
||||||
|
FORBID_TAGS: ['script', 'iframe']
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set body with header and content
|
||||||
|
setComposeBody(`
|
||||||
|
${headerHtml}
|
||||||
|
<div style="margin-top: 10px;">
|
||||||
|
${contentHtml}
|
||||||
|
</div>
|
||||||
|
`);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSendEmail = async () => {
|
if (!showCompose) return null;
|
||||||
if (!composeTo.trim()) {
|
|
||||||
alert('Please enter a recipient');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSending(true);
|
const handleFileSelection = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
try {
|
if (e.target.files && e.target.files.length > 0) {
|
||||||
// Prepare email data for sending
|
const newAttachments = Array.from(e.target.files).map(file => ({
|
||||||
const emailData = {
|
name: file.name,
|
||||||
to: composeTo,
|
type: file.type,
|
||||||
cc: composeCc,
|
size: file.size,
|
||||||
bcc: composeBcc,
|
file
|
||||||
subject: composeSubject,
|
}));
|
||||||
body: composeBody,
|
setAttachments([...attachments, ...newAttachments]);
|
||||||
attachments: attachments
|
|
||||||
};
|
|
||||||
|
|
||||||
// Call the provided onSend function
|
|
||||||
await onSend(emailData);
|
|
||||||
|
|
||||||
// Reset the form
|
|
||||||
onCancel();
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error sending email:', error);
|
|
||||||
alert('Failed to send email. Please try again.');
|
|
||||||
} finally {
|
|
||||||
setSending(false);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
|
<div className="fixed inset-0 bg-black/60 flex items-center justify-center z-50 p-4">
|
||||||
<Card className="w-full max-w-3xl bg-white shadow-lg">
|
<Card className="w-full max-w-3xl">
|
||||||
<CardHeader className="flex flex-row items-center justify-between p-4 border-b">
|
<CardHeader className="flex flex-row items-center border-b p-4">
|
||||||
<CardTitle className="text-lg font-medium">
|
<CardTitle className="text-lg">
|
||||||
{replyTo ? 'Reply' : forwardFrom ? 'Forward' : 'New Message'}
|
{replyTo ? 'Reply' : forwardFrom ? 'Forward' : 'New Message'}
|
||||||
</CardTitle>
|
</CardTitle>
|
||||||
<Button variant="ghost" size="icon" onClick={onCancel}>
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="ml-auto"
|
||||||
|
onClick={onCancel}
|
||||||
|
>
|
||||||
<X className="h-4 w-4" />
|
<X className="h-4 w-4" />
|
||||||
</Button>
|
</Button>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="p-4 space-y-4">
|
<CardContent className="p-4 space-y-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<div className="flex items-center">
|
<div className="flex items-center border-b py-2">
|
||||||
<span className="w-20 text-sm text-gray-500">To:</span>
|
<span className="w-16 text-sm text-gray-500">To:</span>
|
||||||
<Input
|
<Input
|
||||||
value={composeTo}
|
value={composeTo}
|
||||||
onChange={(e) => setComposeTo(e.target.value)}
|
onChange={(e) => setComposeTo(e.target.value)}
|
||||||
placeholder="recipient@example.com"
|
|
||||||
className="border-0 focus-visible:ring-0"
|
className="border-0 focus-visible:ring-0"
|
||||||
|
placeholder="recipient@example.com"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{showCc && (
|
{showCc && (
|
||||||
<div className="flex items-center">
|
<div className="flex items-center border-b py-2">
|
||||||
<span className="w-20 text-sm text-gray-500">Cc:</span>
|
<span className="w-16 text-sm text-gray-500">Cc:</span>
|
||||||
<Input
|
<Input
|
||||||
value={composeCc}
|
value={composeCc}
|
||||||
onChange={(e) => setComposeCc(e.target.value)}
|
onChange={(e) => setComposeCc(e.target.value)}
|
||||||
placeholder="cc@example.com"
|
|
||||||
className="border-0 focus-visible:ring-0"
|
className="border-0 focus-visible:ring-0"
|
||||||
|
placeholder="cc@example.com"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{showBcc && (
|
{showBcc && (
|
||||||
<div className="flex items-center">
|
<div className="flex items-center border-b py-2">
|
||||||
<span className="w-20 text-sm text-gray-500">Bcc:</span>
|
<span className="w-16 text-sm text-gray-500">Bcc:</span>
|
||||||
<Input
|
<Input
|
||||||
value={composeBcc}
|
value={composeBcc}
|
||||||
onChange={(e) => setComposeBcc(e.target.value)}
|
onChange={(e) => setComposeBcc(e.target.value)}
|
||||||
placeholder="bcc@example.com"
|
|
||||||
className="border-0 focus-visible:ring-0"
|
className="border-0 focus-visible:ring-0"
|
||||||
|
placeholder="bcc@example.com"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
<div className="flex items-center">
|
<div className="flex items-center border-b py-2">
|
||||||
<span className="w-20 text-sm text-gray-500">Subject:</span>
|
<span className="w-16 text-sm text-gray-500">Subject:</span>
|
||||||
<Input
|
<Input
|
||||||
value={composeSubject}
|
value={composeSubject}
|
||||||
onChange={(e) => setComposeSubject(e.target.value)}
|
onChange={(e) => setComposeSubject(e.target.value)}
|
||||||
placeholder="Subject"
|
|
||||||
className="border-0 focus-visible:ring-0"
|
className="border-0 focus-visible:ring-0"
|
||||||
|
placeholder="Subject"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center text-xs text-blue-600 space-x-4 ml-20">
|
<div className="flex items-center text-xs text-blue-600 space-x-2 py-1">
|
||||||
{!showCc && (
|
{!showCc && (
|
||||||
<button
|
<button onClick={() => setShowCc(true)} className="hover:underline">
|
||||||
type="button"
|
|
||||||
onClick={() => setShowCc(true)}
|
|
||||||
className="hover:underline"
|
|
||||||
>
|
|
||||||
Add Cc
|
Add Cc
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
{!showBcc && (
|
{!showBcc && (
|
||||||
<button
|
<button onClick={() => setShowBcc(true)} className="hover:underline">
|
||||||
type="button"
|
|
||||||
onClick={() => setShowBcc(true)}
|
|
||||||
className="hover:underline"
|
|
||||||
>
|
|
||||||
Add Bcc
|
Add Bcc
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
@ -200,23 +233,22 @@ export default function ComposeEmail({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
className="border rounded p-3 min-h-[200px] max-h-[400px] overflow-auto"
|
ref={editorRef}
|
||||||
|
className="min-h-[300px] p-2 focus:outline-none overflow-auto"
|
||||||
contentEditable
|
contentEditable
|
||||||
dangerouslySetInnerHTML={{ __html: composeBody }}
|
dangerouslySetInnerHTML={{ __html: composeBody }}
|
||||||
onInput={handleInput}
|
onInput={(e) => setComposeBody(e.currentTarget.innerHTML)}
|
||||||
ref={editorRef}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{attachments.length > 0 && (
|
{attachments.length > 0 && (
|
||||||
<div className="border-t pt-2">
|
<div className="border-t pt-3">
|
||||||
<h4 className="text-sm text-gray-500 mb-2">Attachments</h4>
|
<h4 className="text-sm font-medium mb-2">Attachments</h4>
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
{attachments.map((attachment, index) => (
|
{attachments.map((attachment, index) => (
|
||||||
<div key={index} className="flex items-center bg-gray-100 rounded px-2 py-1">
|
<div key={index} className="flex items-center bg-gray-100 rounded px-2 py-1">
|
||||||
<span className="text-sm">{attachment.name}</span>
|
<span className="text-sm truncate max-w-[150px]">{attachment.name}</span>
|
||||||
<button
|
<button
|
||||||
type="button"
|
className="ml-1 text-gray-500 hover:text-gray-700"
|
||||||
className="ml-2 text-gray-400 hover:text-gray-600"
|
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
const newAttachments = [...attachments];
|
const newAttachments = [...attachments];
|
||||||
newAttachments.splice(index, 1);
|
newAttachments.splice(index, 1);
|
||||||
@ -231,27 +263,17 @@ export default function ComposeEmail({
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
<CardFooter className="flex justify-between p-4 border-t">
|
<CardFooter className="border-t p-4 flex justify-between">
|
||||||
<div className="flex space-x-2">
|
<div className="flex items-center space-x-2">
|
||||||
<input
|
<input
|
||||||
type="file"
|
type="file"
|
||||||
className="hidden"
|
|
||||||
ref={fileInputRef}
|
ref={fileInputRef}
|
||||||
onChange={(e) => {
|
className="hidden"
|
||||||
if (e.target.files?.length) {
|
onChange={handleFileSelection}
|
||||||
const newAttachments = Array.from(e.target.files).map(file => ({
|
|
||||||
name: file.name,
|
|
||||||
type: file.type,
|
|
||||||
content: URL.createObjectURL(file),
|
|
||||||
file
|
|
||||||
}));
|
|
||||||
setAttachments([...attachments, ...newAttachments]);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
multiple
|
multiple
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
variant="ghost"
|
variant="outline"
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={() => fileInputRef.current?.click()}
|
onClick={() => fileInputRef.current?.click()}
|
||||||
>
|
>
|
||||||
@ -260,12 +282,24 @@ export default function ComposeEmail({
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<Button
|
<Button
|
||||||
onClick={handleSendEmail}
|
onClick={async () => {
|
||||||
|
setSending(true);
|
||||||
|
try {
|
||||||
|
await handleSend();
|
||||||
|
onCancel();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error sending email:', error);
|
||||||
|
} finally {
|
||||||
|
setSending(false);
|
||||||
|
}
|
||||||
|
}}
|
||||||
disabled={sending || !composeTo.trim()}
|
disabled={sending || !composeTo.trim()}
|
||||||
className="bg-blue-600 hover:bg-blue-700 text-white"
|
|
||||||
>
|
>
|
||||||
{sending ? (
|
{sending ? (
|
||||||
<>Sending...</>
|
<>
|
||||||
|
<Loader2 className="h-4 w-4 mr-1 animate-spin" />
|
||||||
|
Sending...
|
||||||
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<SendHorizontal className="h-4 w-4 mr-1" />
|
<SendHorizontal className="h-4 w-4 mr-1" />
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user