compose mime
This commit is contained in:
parent
a629e23a70
commit
fa85613f31
@ -103,20 +103,30 @@ function splitEmailHeadersAndBody(emailBody: string): { headers: string; body: s
|
|||||||
function EmailContent({ email }: { email: Email }) {
|
function EmailContent({ email }: { email: Email }) {
|
||||||
const [content, setContent] = useState<React.ReactNode>(null);
|
const [content, setContent] = useState<React.ReactNode>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let mounted = true;
|
let mounted = true;
|
||||||
|
|
||||||
async function loadContent() {
|
async function loadContent() {
|
||||||
|
if (!email) return;
|
||||||
|
|
||||||
|
setIsLoading(true);
|
||||||
try {
|
try {
|
||||||
if (!email.body) {
|
if (!email.body) {
|
||||||
if (mounted) setContent(null);
|
if (mounted) {
|
||||||
|
setContent(null);
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formattedEmail = email.body.trim();
|
const formattedEmail = email.body.trim();
|
||||||
if (!formattedEmail) {
|
if (!formattedEmail) {
|
||||||
if (mounted) setContent(null);
|
if (mounted) {
|
||||||
|
setContent(null);
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,12 +150,14 @@ function EmailContent({ email }: { email: Email }) {
|
|||||||
setContent(null);
|
setContent(null);
|
||||||
}
|
}
|
||||||
setError(null);
|
setError(null);
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error rendering email content:', err);
|
console.error('Error rendering email content:', err);
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setError('Error rendering email content. Please try again.');
|
setError('Error rendering email content. Please try again.');
|
||||||
setContent(null);
|
setContent(null);
|
||||||
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +167,15 @@ function EmailContent({ email }: { email: Email }) {
|
|||||||
return () => {
|
return () => {
|
||||||
mounted = false;
|
mounted = false;
|
||||||
};
|
};
|
||||||
}, [email.body]);
|
}, [email?.body]);
|
||||||
|
|
||||||
|
if (isLoading) {
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-center py-8">
|
||||||
|
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-blue-500"></div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return <div className="text-red-500">{error}</div>;
|
return <div className="text-red-500">{error}</div>;
|
||||||
@ -589,53 +609,60 @@ export default function CourrierPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the selected email first to show preview immediately
|
|
||||||
setSelectedEmail(email);
|
|
||||||
|
|
||||||
// Fetch the full email content
|
|
||||||
const response = await fetch(`/api/mail/${emailId}`);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to fetch full email content');
|
|
||||||
}
|
|
||||||
|
|
||||||
const fullEmail = await response.json();
|
|
||||||
|
|
||||||
// Update the email in the list and selected email with full content
|
|
||||||
setEmails(prevEmails => prevEmails.map(email =>
|
|
||||||
email.id === emailId
|
|
||||||
? { ...email, body: fullEmail.body }
|
|
||||||
: email
|
|
||||||
));
|
|
||||||
|
|
||||||
setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body } : prev);
|
|
||||||
|
|
||||||
// Try to mark as read in the background
|
|
||||||
try {
|
try {
|
||||||
const markReadResponse = await fetch(`/api/mail/mark-read`, {
|
// Set the selected email first to show preview immediately
|
||||||
method: 'POST',
|
setSelectedEmail(email);
|
||||||
headers: {
|
setContentLoading(true);
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
// Fetch the full email content
|
||||||
body: JSON.stringify({
|
const response = await fetch(`/api/mail/${emailId}`);
|
||||||
emailId,
|
if (!response.ok) {
|
||||||
isRead: true,
|
throw new Error('Failed to fetch full email content');
|
||||||
}),
|
}
|
||||||
});
|
|
||||||
|
const fullEmail = await response.json();
|
||||||
|
|
||||||
|
// Update the email in the list and selected email with full content
|
||||||
|
setEmails(prevEmails => prevEmails.map(email =>
|
||||||
|
email.id === emailId
|
||||||
|
? { ...email, body: fullEmail.body || email.body }
|
||||||
|
: email
|
||||||
|
));
|
||||||
|
|
||||||
|
setSelectedEmail(prev => prev ? { ...prev, body: fullEmail.body || prev.body } : prev);
|
||||||
|
setContentLoading(false);
|
||||||
|
|
||||||
if (markReadResponse.ok) {
|
// Try to mark as read in the background
|
||||||
// Only update the emails list if the API call was successful
|
try {
|
||||||
setEmails((prevEmails: Email[]) =>
|
const markReadResponse = await fetch(`/api/mail/mark-read`, {
|
||||||
prevEmails.map((email: Email): Email =>
|
method: 'POST',
|
||||||
email.id === emailId
|
headers: {
|
||||||
? { ...email, read: true }
|
'Content-Type': 'application/json',
|
||||||
: email
|
},
|
||||||
)
|
body: JSON.stringify({
|
||||||
);
|
emailId,
|
||||||
} else {
|
isRead: true,
|
||||||
console.error('Failed to mark email as read:', await markReadResponse.text());
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (markReadResponse.ok) {
|
||||||
|
// Only update the emails list if the API call was successful
|
||||||
|
setEmails((prevEmails: Email[]) =>
|
||||||
|
prevEmails.map((email: Email): Email =>
|
||||||
|
email.id === emailId
|
||||||
|
? { ...email, read: true }
|
||||||
|
: email
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.error('Failed to mark email as read:', await markReadResponse.text());
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error marking email as read:', error);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error marking email as read:', error);
|
console.error('Error fetching email content:', error);
|
||||||
|
setContentLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user