courrier multi account restore compose
This commit is contained in:
parent
427392f0d0
commit
5152f9ded0
@ -198,11 +198,8 @@ export default function CourrierPage() {
|
|||||||
|
|
||||||
// Count unread emails for each account and folder
|
// Count unread emails for each account and folder
|
||||||
(emails || []).forEach(email => {
|
(emails || []).forEach(email => {
|
||||||
// Access the 'read' property safely, handling both old and new email formats
|
// Check if email is unread based on flags
|
||||||
const emailWithFlags = email as unknown as EmailWithFlags;
|
const isUnread = email.flags && !email.flags.seen;
|
||||||
const isUnread = (!emailWithFlags.read && emailWithFlags.read !== undefined) ||
|
|
||||||
(emailWithFlags.flags && !emailWithFlags.flags.seen) ||
|
|
||||||
false;
|
|
||||||
|
|
||||||
// Count unread emails for the specific account and folder
|
// Count unread emails for the specific account and folder
|
||||||
if (isUnread && email.accountId && email.folder) {
|
if (isUnread && email.accountId && email.folder) {
|
||||||
|
|||||||
@ -3,11 +3,7 @@ import { useSession } from 'next-auth/react';
|
|||||||
import { useToast } from './use-toast';
|
import { useToast } from './use-toast';
|
||||||
import { formatEmailForReplyOrForward } from '@/lib/utils/email-formatter';
|
import { formatEmailForReplyOrForward } from '@/lib/utils/email-formatter';
|
||||||
import { getCachedEmailsWithTimeout, refreshEmailsInBackground } from '@/lib/services/prefetch-service';
|
import { getCachedEmailsWithTimeout, refreshEmailsInBackground } from '@/lib/services/prefetch-service';
|
||||||
|
import { EmailAddress, EmailAttachment } from '@/lib/types';
|
||||||
export interface EmailAddress {
|
|
||||||
name: string;
|
|
||||||
address: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Email {
|
export interface Email {
|
||||||
id: string;
|
id: string;
|
||||||
@ -16,16 +12,26 @@ export interface Email {
|
|||||||
cc?: EmailAddress[];
|
cc?: EmailAddress[];
|
||||||
bcc?: EmailAddress[];
|
bcc?: EmailAddress[];
|
||||||
subject: string;
|
subject: string;
|
||||||
content: string;
|
content: {
|
||||||
|
text: string;
|
||||||
|
html: string;
|
||||||
|
};
|
||||||
preview?: string;
|
preview?: string;
|
||||||
date: string;
|
date: Date;
|
||||||
read: boolean;
|
flags: {
|
||||||
starred: boolean;
|
seen: boolean;
|
||||||
attachments?: { filename: string; contentType: string; size: number; content?: string }[];
|
answered: boolean;
|
||||||
folder: string;
|
flagged: boolean;
|
||||||
|
draft: boolean;
|
||||||
|
deleted: boolean;
|
||||||
|
};
|
||||||
|
size: number;
|
||||||
hasAttachments: boolean;
|
hasAttachments: boolean;
|
||||||
contentFetched?: boolean;
|
folder: string;
|
||||||
|
contentFetched: boolean;
|
||||||
accountId?: string;
|
accountId?: string;
|
||||||
|
messageId?: string;
|
||||||
|
attachments?: EmailAttachment[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EmailListResult {
|
export interface EmailListResult {
|
||||||
@ -311,7 +317,7 @@ export const useCourrier = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mark the email as read if it's not already
|
// Mark the email as read if it's not already
|
||||||
if (!email.read) {
|
if (!email.flags.seen) {
|
||||||
markEmailAsRead(emailId, true);
|
markEmailAsRead(emailId, true);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -346,12 +352,12 @@ export const useCourrier = () => {
|
|||||||
|
|
||||||
// Update the email in the list
|
// Update the email in the list
|
||||||
setEmails(emails.map(email =>
|
setEmails(emails.map(email =>
|
||||||
email.id === emailId ? { ...email, read: isRead } : email
|
email.id === emailId ? { ...email, flags: { ...email.flags, seen: isRead } } : email
|
||||||
));
|
));
|
||||||
|
|
||||||
// If the selected email is the one being marked, update it too
|
// If the selected email is the one being marked, update it too
|
||||||
if (selectedEmail && selectedEmail.id === emailId) {
|
if (selectedEmail && selectedEmail.id === emailId) {
|
||||||
setSelectedEmail({ ...selectedEmail, read: isRead });
|
setSelectedEmail({ ...selectedEmail, flags: { ...selectedEmail.flags, seen: isRead } });
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -366,7 +372,7 @@ export const useCourrier = () => {
|
|||||||
const email = emails.find(e => e.id === emailId);
|
const email = emails.find(e => e.id === emailId);
|
||||||
if (!email) return;
|
if (!email) return;
|
||||||
|
|
||||||
const newStarredStatus = !email.starred;
|
const newStarredStatus = !email.flags.flagged;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/courrier/${emailId}/star`, {
|
const response = await fetch(`/api/courrier/${emailId}/star`, {
|
||||||
@ -386,12 +392,12 @@ export const useCourrier = () => {
|
|||||||
|
|
||||||
// Update the email in the list
|
// Update the email in the list
|
||||||
setEmails(emails.map(email =>
|
setEmails(emails.map(email =>
|
||||||
email.id === emailId ? { ...email, starred: newStarredStatus } : email
|
email.id === emailId ? { ...email, flags: { ...email.flags, flagged: newStarredStatus } } : email
|
||||||
));
|
));
|
||||||
|
|
||||||
// If the selected email is the one being starred, update it too
|
// If the selected email is the one being starred, update it too
|
||||||
if (selectedEmail && selectedEmail.id === emailId) {
|
if (selectedEmail && selectedEmail.id === emailId) {
|
||||||
setSelectedEmail({ ...selectedEmail, starred: newStarredStatus });
|
setSelectedEmail({ ...selectedEmail, flags: { ...selectedEmail.flags, flagged: newStarredStatus } });
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error toggling star status:', error);
|
console.error('Error toggling star status:', error);
|
||||||
@ -546,10 +552,17 @@ export const useCourrier = () => {
|
|||||||
}, [loadEmails]);
|
}, [loadEmails]);
|
||||||
|
|
||||||
// Format an email for reply or forward
|
// Format an email for reply or forward
|
||||||
const formatEmailForAction = useCallback((email: Email, type: 'reply' | 'reply-all' | 'forward') => {
|
const formatEmailForAction = useCallback((email: Email) => {
|
||||||
if (!email) return null;
|
return {
|
||||||
|
id: email.id,
|
||||||
return formatEmailForReplyOrForward(email, type);
|
subject: email.subject,
|
||||||
|
from: email.from[0]?.address || '',
|
||||||
|
to: email.to.map(addr => addr.address).join(', '),
|
||||||
|
cc: email.cc?.map(addr => addr.address).join(', '),
|
||||||
|
bcc: email.bcc?.map(addr => addr.address).join(', '),
|
||||||
|
content: email.content.text || email.content.html || '',
|
||||||
|
attachments: email.attachments
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Return all the functionality and state values
|
// Return all the functionality and state values
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user