courrier preview
This commit is contained in:
parent
e88ef38222
commit
3fb8266a9e
@ -515,6 +515,15 @@ export default function CourrierPage() {
|
||||
const handleDeleteConfirm = async () => {
|
||||
await deleteEmails(selectedEmailIds);
|
||||
setShowDeleteConfirm(false);
|
||||
|
||||
// Clear selected emails after deletion
|
||||
// Using setEmails will reset the selection state
|
||||
setLoading(true);
|
||||
setPage(1);
|
||||
loadEmails(1, 20, false).finally(() => {
|
||||
// Selection will be cleared by loading new emails
|
||||
setLoading(false);
|
||||
});
|
||||
};
|
||||
|
||||
const handleGoToLogin = () => {
|
||||
|
||||
@ -505,61 +505,6 @@ export const useEmailState = () => {
|
||||
}
|
||||
}, [state.emails, toast, loadEmails, logEmailOp]);
|
||||
|
||||
// Delete emails
|
||||
const deleteEmails = useCallback(async (emailIds: string[]) => {
|
||||
if (emailIds.length === 0) return;
|
||||
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
try {
|
||||
logEmailOp('DELETE', `Deleting ${emailIds.length} emails`);
|
||||
|
||||
// Find the first email to get account ID and folder
|
||||
const firstEmail = state.emails.find(e => e.id === emailIds[0]);
|
||||
const accountId = firstEmail?.accountId || 'default';
|
||||
const folder = firstEmail?.folder || state.currentFolder;
|
||||
const { normalizedFolder } = normalizeFolderAndAccount(folder, accountId);
|
||||
|
||||
// Make API call to delete emails
|
||||
const response = await fetch('/api/courrier/delete', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
emailIds,
|
||||
folder: normalizedFolder,
|
||||
accountId
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete emails');
|
||||
}
|
||||
|
||||
// Clear selections
|
||||
dispatch({ type: 'CLEAR_SELECTED_EMAILS' });
|
||||
|
||||
// Reload emails to get updated list
|
||||
loadEmails(state.page, state.perPage, true);
|
||||
|
||||
toast({
|
||||
title: "Emails Deleted",
|
||||
description: `${emailIds.length} email(s) moved to trash`
|
||||
});
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
logEmailOp('ERROR', `Failed to delete emails: ${error instanceof Error ? error.message : String(error)}`);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: 'Failed to delete emails'
|
||||
});
|
||||
return false;
|
||||
} finally {
|
||||
dispatch({ type: 'SET_LOADING', payload: false });
|
||||
}
|
||||
}, [state.emails, state.currentFolder, toast, loadEmails, logEmailOp]);
|
||||
|
||||
// Function to check for new emails without disrupting the user
|
||||
const checkForNewEmails = useCallback(async () => {
|
||||
if (!session?.user?.id) return;
|
||||
@ -611,16 +556,81 @@ export const useEmailState = () => {
|
||||
duration: 5000
|
||||
});
|
||||
|
||||
// Force refresh the current view to show the new emails immediately
|
||||
// This ensures panel 2 (email list) gets updated
|
||||
// Full refresh just like the refresh button in sidebar
|
||||
// Reset to page 1 to ensure we get the newest emails
|
||||
dispatch({ type: 'SET_PAGE', payload: 1 });
|
||||
loadEmails(1, state.perPage, false);
|
||||
|
||||
// Also update unread counts - this will be handled in the effect
|
||||
// The fetchUnreadCounts function will be available when this callback is called
|
||||
} else {
|
||||
logEmailOp('CHECK_NEW_EMAILS', 'No new emails found');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking for new emails:', error);
|
||||
}
|
||||
}, [session?.user?.id, state.currentFolder, state.isLoading, state.emails, state.perPage, toast, loadEmails, logEmailOp]);
|
||||
}, [session?.user?.id, state.currentFolder, state.isLoading, state.emails, state.perPage, toast, loadEmails, logEmailOp, dispatch]);
|
||||
|
||||
// Delete emails
|
||||
const deleteEmails = useCallback(async (emailIds: string[]) => {
|
||||
if (emailIds.length === 0) return;
|
||||
|
||||
dispatch({ type: 'SET_LOADING', payload: true });
|
||||
|
||||
try {
|
||||
logEmailOp('DELETE', `Deleting ${emailIds.length} emails`);
|
||||
|
||||
// Find the first email to get account ID and folder
|
||||
const firstEmail = state.emails.find(e => e.id === emailIds[0]);
|
||||
const accountId = firstEmail?.accountId || 'default';
|
||||
const folder = firstEmail?.folder || state.currentFolder;
|
||||
const { normalizedFolder } = normalizeFolderAndAccount(folder, accountId);
|
||||
|
||||
// Make API call to delete emails
|
||||
const response = await fetch('/api/courrier/delete', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
emailIds,
|
||||
folder: normalizedFolder,
|
||||
accountId
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to delete emails');
|
||||
}
|
||||
|
||||
// Clear selections
|
||||
dispatch({ type: 'CLEAR_SELECTED_EMAILS' });
|
||||
|
||||
// Show toast notification
|
||||
toast({
|
||||
title: "Emails Deleted",
|
||||
description: `${emailIds.length} email(s) moved to trash`
|
||||
});
|
||||
|
||||
// Full refresh just like the refresh button in sidebar
|
||||
// Reset to page 1 to ensure we get the updated email list
|
||||
dispatch({ type: 'SET_PAGE', payload: 1 });
|
||||
loadEmails(1, state.perPage, false);
|
||||
|
||||
// Also update unread counts - this will be handled in the effect
|
||||
// The fetchUnreadCounts function will be available when this callback is called
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
logEmailOp('ERROR', `Failed to delete emails: ${error instanceof Error ? error.message : String(error)}`);
|
||||
toast({
|
||||
variant: "destructive",
|
||||
title: "Error",
|
||||
description: 'Failed to delete emails'
|
||||
});
|
||||
return false;
|
||||
} finally {
|
||||
dispatch({ type: 'SET_LOADING', payload: false });
|
||||
}
|
||||
}, [state.emails, state.currentFolder, state.perPage, toast, loadEmails, logEmailOp, dispatch]);
|
||||
|
||||
// Send email
|
||||
const sendEmail = useCallback(async (emailData: EmailData) => {
|
||||
@ -1148,11 +1158,15 @@ export const useEmailState = () => {
|
||||
// Log that we're manually checking
|
||||
logEmailOp('MANUAL_CHECK', 'Manually checking for new emails');
|
||||
|
||||
// Wait a short moment to allow potential concurrent operations to complete
|
||||
setTimeout(() => {
|
||||
checkForNewEmails();
|
||||
}, 500);
|
||||
}, [state.isLoading, checkForNewEmails, logEmailOp]);
|
||||
// Reset to page 1 to ensure we get the newest emails
|
||||
dispatch({ type: 'SET_PAGE', payload: 1 });
|
||||
|
||||
// Perform a complete refresh of emails
|
||||
loadEmails(1, state.perPage, false);
|
||||
|
||||
// Also update unread counts
|
||||
fetchUnreadCounts();
|
||||
}, [state.isLoading, state.perPage, loadEmails, logEmailOp, dispatch, fetchUnreadCounts]);
|
||||
|
||||
// Return all state values and actions
|
||||
return {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user