mail page ui correction maj compose 20
This commit is contained in:
parent
49b084808b
commit
fe7cbf5ff4
@ -39,14 +39,17 @@ interface Email {
|
|||||||
id: number;
|
id: number;
|
||||||
accountId: number;
|
accountId: number;
|
||||||
from: string;
|
from: string;
|
||||||
fromName: string;
|
fromName?: string;
|
||||||
to: string;
|
to: string;
|
||||||
subject: string;
|
subject: string;
|
||||||
body: string;
|
body: string;
|
||||||
date: string;
|
date: string;
|
||||||
read: boolean;
|
read: boolean;
|
||||||
starred: boolean;
|
starred: boolean;
|
||||||
category: string;
|
deleted: boolean;
|
||||||
|
category?: string;
|
||||||
|
cc?: string;
|
||||||
|
bcc?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Attachment {
|
interface Attachment {
|
||||||
@ -463,7 +466,7 @@ export default function MailPage() {
|
|||||||
const [attachments, setAttachments] = useState<Attachment[]>([]);
|
const [attachments, setAttachments] = useState<Attachment[]>([]);
|
||||||
const [folders, setFolders] = useState<string[]>([]);
|
const [folders, setFolders] = useState<string[]>([]);
|
||||||
|
|
||||||
// Filter emails based on current view and selected account
|
// Update the filteredEmails logic
|
||||||
const filteredEmails = useMemo(() => {
|
const filteredEmails = useMemo(() => {
|
||||||
return emails.filter(email => {
|
return emails.filter(email => {
|
||||||
// First filter by account if one is selected
|
// First filter by account if one is selected
|
||||||
@ -474,16 +477,15 @@ export default function MailPage() {
|
|||||||
// Then filter by current view
|
// Then filter by current view
|
||||||
switch (currentView) {
|
switch (currentView) {
|
||||||
case 'inbox':
|
case 'inbox':
|
||||||
return !email.deleted && email.category === 'inbox';
|
return !email.deleted && !email.category?.includes('sent');
|
||||||
case 'starred':
|
case 'starred':
|
||||||
return !email.deleted && email.starred;
|
return !email.deleted && email.starred === true;
|
||||||
case 'sent':
|
case 'sent':
|
||||||
return !email.deleted && email.category === 'sent';
|
return !email.deleted && email.category?.includes('sent');
|
||||||
case 'trash':
|
case 'trash':
|
||||||
return email.deleted;
|
return email.deleted === true;
|
||||||
default:
|
default:
|
||||||
// Handle folder views
|
return false;
|
||||||
return !email.deleted && email.category === currentView;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [emails, currentView, selectedAccount]);
|
}, [emails, currentView, selectedAccount]);
|
||||||
@ -522,55 +524,33 @@ export default function MailPage() {
|
|||||||
checkCredentials();
|
checkCredentials();
|
||||||
}, [router]);
|
}, [router]);
|
||||||
|
|
||||||
// Fetch emails from IMAP API
|
// Update the loadEmails function to properly set email properties
|
||||||
const loadEmails = async (view?: string, accountId?: number, folder?: string) => {
|
const loadEmails = async () => {
|
||||||
try {
|
try {
|
||||||
console.log('Loading emails...', { view, accountId, folder });
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
let url = '/api/mail';
|
const response = await fetch('/api/mail');
|
||||||
const params = new URLSearchParams();
|
|
||||||
|
|
||||||
if (view) params.append('view', view);
|
|
||||||
if (accountId) params.append('accountId', accountId.toString());
|
|
||||||
if (folder) params.append('folder', folder);
|
|
||||||
|
|
||||||
if (params.toString()) {
|
|
||||||
url += `?${params.toString()}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(url);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json();
|
throw new Error('Failed to load emails');
|
||||||
if (errorData.error === 'No stored credentials found') {
|
|
||||||
router.push('/mail/login');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
throw new Error(errorData.error || 'Failed to load emails');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
// Update accounts and folders if provided in the response
|
// Process the emails array with proper properties
|
||||||
if (data.accounts) {
|
const processedEmails = data.emails.map((email: any) => ({
|
||||||
setAccounts([
|
|
||||||
{ id: 0, name: 'All', email: '', color: 'bg-gray-500' },
|
|
||||||
...data.accounts
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.folders) {
|
|
||||||
setFolders(data.folders);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process the emails array from data.emails
|
|
||||||
const processedEmails = data.emails.map((email: Email) => ({
|
|
||||||
...email,
|
...email,
|
||||||
date: new Date(email.date),
|
id: email.id,
|
||||||
read: email.read || false,
|
accountId: email.accountId,
|
||||||
starred: email.starred || false,
|
from: email.from,
|
||||||
|
fromName: email.fromName || email.from.split('@')[0],
|
||||||
|
to: email.to,
|
||||||
|
subject: email.subject,
|
||||||
|
body: decodeMimeContent(email.body),
|
||||||
|
date: new Date(email.date).toISOString(),
|
||||||
|
read: Boolean(email.read),
|
||||||
|
starred: Boolean(email.starred),
|
||||||
|
deleted: Boolean(email.deleted),
|
||||||
category: email.category || 'inbox'
|
category: email.category || 'inbox'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@ -585,8 +565,8 @@ export default function MailPage() {
|
|||||||
|
|
||||||
// Update useEffect to load emails when view changes
|
// Update useEffect to load emails when view changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadEmails(currentView, selectedAccount?.id);
|
loadEmails();
|
||||||
}, [currentView, selectedAccount]);
|
}, [currentView]);
|
||||||
|
|
||||||
// Format date for display
|
// Format date for display
|
||||||
const formatDate = (dateString: string) => {
|
const formatDate = (dateString: string) => {
|
||||||
@ -638,20 +618,30 @@ export default function MailPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Update the toggleStarred function to properly handle starring
|
||||||
const toggleStarred = async (emailId: number, e: React.MouseEvent) => {
|
const toggleStarred = async (emailId: number, e: React.MouseEvent) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fetch('/api/mail/toggle-star', {
|
// Optimistically update the UI
|
||||||
|
setEmails(emails.map(email =>
|
||||||
|
email.id === emailId ? { ...email, starred: !email.starred } : email
|
||||||
|
));
|
||||||
|
|
||||||
|
// Send request to server
|
||||||
|
const response = await fetch('/api/mail/toggle-star', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ emailId })
|
body: JSON.stringify({ emailId })
|
||||||
});
|
});
|
||||||
|
|
||||||
const updatedEmails = emails.map(email =>
|
if (!response.ok) {
|
||||||
email.id === emailId ? { ...email, starred: !email.starred } : email
|
// Revert the change if the server request fails
|
||||||
);
|
setEmails(emails.map(email =>
|
||||||
setEmails(updatedEmails);
|
email.id === emailId ? { ...email, starred: !email.starred } : email
|
||||||
|
));
|
||||||
|
throw new Error('Failed to toggle star');
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error toggling star:', error);
|
console.error('Error toggling star:', error);
|
||||||
}
|
}
|
||||||
@ -891,6 +881,18 @@ export default function MailPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Add debug logging to help track the filtering
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('Current view:', currentView);
|
||||||
|
console.log('Total emails:', emails.length);
|
||||||
|
console.log('Filtered emails:', filteredEmails.length);
|
||||||
|
console.log('Filter criteria:', {
|
||||||
|
starred: filteredEmails.filter(e => e.starred).length,
|
||||||
|
sent: filteredEmails.filter(e => e.category?.includes('sent')).length,
|
||||||
|
deleted: filteredEmails.filter(e => e.deleted).length
|
||||||
|
});
|
||||||
|
}, [currentView, emails, filteredEmails]);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<div className="flex h-[calc(100vh-theme(spacing.12))] items-center justify-center bg-gray-100 mt-12">
|
<div className="flex h-[calc(100vh-theme(spacing.12))] items-center justify-center bg-gray-100 mt-12">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user