courrier multi account restore compose
This commit is contained in:
parent
98fe6f5eae
commit
7b4876b669
@ -157,6 +157,7 @@ export default function CourrierPage() {
|
|||||||
searchEmails,
|
searchEmails,
|
||||||
formatEmailForAction,
|
formatEmailForAction,
|
||||||
setPage,
|
setPage,
|
||||||
|
setEmails,
|
||||||
} = useCourrier();
|
} = useCourrier();
|
||||||
|
|
||||||
// UI state
|
// UI state
|
||||||
@ -171,7 +172,7 @@ export default function CourrierPage() {
|
|||||||
const [unreadCount, setUnreadCount] = useState(0);
|
const [unreadCount, setUnreadCount] = useState(0);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [prefetchStarted, setPrefetchStarted] = useState(false);
|
const [prefetchStarted, setPrefetchStarted] = useState(false);
|
||||||
const [showFolders, setShowFolders] = useState(true);
|
const [showFolders, setShowFolders] = useState(false);
|
||||||
const [showAddAccountForm, setShowAddAccountForm] = useState(false);
|
const [showAddAccountForm, setShowAddAccountForm] = useState(false);
|
||||||
|
|
||||||
// Email accounts for the sidebar
|
// Email accounts for the sidebar
|
||||||
@ -569,59 +570,60 @@ export default function CourrierPage() {
|
|||||||
// Update handleMailboxChange to ensure consistent folder naming and prevent race conditions
|
// Update handleMailboxChange to ensure consistent folder naming and prevent race conditions
|
||||||
const handleMailboxChange = (folder: string, accountId?: string) => {
|
const handleMailboxChange = (folder: string, accountId?: string) => {
|
||||||
if (!accountId || accountId === 'loading-account') {
|
if (!accountId || accountId === 'loading-account') {
|
||||||
// Use a default behavior if no valid accountId is provided
|
|
||||||
console.warn('No valid accountId provided for folder change');
|
console.warn('No valid accountId provided for folder change');
|
||||||
changeFolder(folder);
|
changeFolder(folder);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const account = accounts.find(a => a.id === accountId);
|
// Set loading state immediately
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
// Clear emails during transition
|
||||||
|
setEmails([]);
|
||||||
|
|
||||||
|
const account = accounts.find(a => a.id.toString() === accountId.toString());
|
||||||
if (!account) {
|
if (!account) {
|
||||||
toast({
|
toast({
|
||||||
title: "Account not found",
|
title: "Account not found",
|
||||||
description: `The account ${accountId} could not be found.`,
|
description: `The account ${accountId} could not be found.`,
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
|
setLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure folder has account prefix
|
// Normalize folder name with account prefix
|
||||||
const prefixedFolder = folder.includes(':') ? folder : `${accountId}:${folder}`;
|
const prefixedFolder = folder.includes(':') ? folder : `${accountId}:${folder}`;
|
||||||
|
|
||||||
// Ensure folder exists in account.folders (either in prefixed or unprefixed form)
|
// Verify folder exists in account
|
||||||
const folderExists = account.folders?.some(f =>
|
const folderExists = account.folders?.some(f =>
|
||||||
f === prefixedFolder || f === folder ||
|
f === prefixedFolder || f === folder ||
|
||||||
// Handle case where folder is base name and account folders are prefixed
|
|
||||||
(folder.includes(':') ? false : `${accountId}:${folder}` === f)
|
(folder.includes(':') ? false : `${accountId}:${folder}` === f)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!folderExists && folder !== 'INBOX') {
|
if (!folderExists && !folder.includes('INBOX') && folder !== 'INBOX') {
|
||||||
toast({
|
toast({
|
||||||
title: "Folder not found",
|
title: "Folder not found",
|
||||||
description: `The folder ${folder} does not exist for this account.`,
|
description: `The folder ${folder} does not exist for this account.`,
|
||||||
variant: "destructive",
|
variant: "destructive",
|
||||||
});
|
});
|
||||||
|
setLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update UI state first to prevent flickering
|
// Update UI state
|
||||||
setSelectedAccount(account);
|
setSelectedAccount(account);
|
||||||
|
setSelectedFolders(prev => ({
|
||||||
|
...prev,
|
||||||
|
[accountId]: prefixedFolder
|
||||||
|
}));
|
||||||
|
|
||||||
// Use a callback to ensure we have the latest state when updating selectedFolders
|
// Use a deliberate delay before changing folder to ensure state is updated
|
||||||
setSelectedFolders(prev => {
|
setTimeout(() => {
|
||||||
const updated = { ...prev, [accountId]: prefixedFolder };
|
console.log(`Loading emails for folder: ${prefixedFolder}, account: ${accountId}`);
|
||||||
console.log('Updated selectedFolders:', updated);
|
changeFolder(prefixedFolder, accountId);
|
||||||
return updated;
|
setLoading(false);
|
||||||
});
|
}, 300);
|
||||||
|
|
||||||
// Set loading state to provide feedback
|
|
||||||
setLoading(true);
|
|
||||||
|
|
||||||
// Reset page when changing folders
|
|
||||||
setPage(1);
|
|
||||||
|
|
||||||
// Make sure we pass the prefixed folder to change folder
|
|
||||||
changeFolder(prefixedFolder, accountId);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Update the folder button rendering to show selected state based on account
|
// Update the folder button rendering to show selected state based on account
|
||||||
@ -707,10 +709,45 @@ export default function CourrierPage() {
|
|||||||
}, [selectedAccount, showFolders]);
|
}, [selectedAccount, showFolders]);
|
||||||
|
|
||||||
const handleAccountSelect = (account: Account) => {
|
const handleAccountSelect = (account: Account) => {
|
||||||
setSelectedAccount(account);
|
// First set loading state to provide visual feedback
|
||||||
if (account.id !== 'loading-account') {
|
setLoading(true);
|
||||||
handleMailboxChange('INBOX', account.id);
|
|
||||||
}
|
// Clear current emails to avoid confusion during transition
|
||||||
|
setEmails([]);
|
||||||
|
|
||||||
|
// Update selected account first (use type assertion)
|
||||||
|
setSelectedAccount(account as any);
|
||||||
|
|
||||||
|
// Initially hide folders during transition
|
||||||
|
setShowFolders(false);
|
||||||
|
|
||||||
|
// Use a small delay to ensure UI updates before showing folders and loading emails
|
||||||
|
setTimeout(() => {
|
||||||
|
if (account.id !== 'loading-account') {
|
||||||
|
// Now show folders
|
||||||
|
setShowFolders(true);
|
||||||
|
|
||||||
|
// Default to INBOX for the selected account
|
||||||
|
const inboxFolder = account.folders.find(f =>
|
||||||
|
f.includes('INBOX') ||
|
||||||
|
(f.includes(':') && f.split(':')[1] === 'INBOX')
|
||||||
|
) || (account.id + ':INBOX');
|
||||||
|
|
||||||
|
// Update the selected folders map
|
||||||
|
setSelectedFolders(prev => ({
|
||||||
|
...prev,
|
||||||
|
[account.id.toString()]: inboxFolder
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Use a slightly longer delay before loading emails to ensure all state is updated
|
||||||
|
setTimeout(() => {
|
||||||
|
handleMailboxChange(inboxFolder, account.id.toString());
|
||||||
|
setLoading(false);
|
||||||
|
}, 200);
|
||||||
|
} else {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAddAccount = async (accountData: AccountData) => {
|
const handleAddAccount = async (accountData: AccountData) => {
|
||||||
@ -746,7 +783,7 @@ export default function CourrierPage() {
|
|||||||
loadEmails().finally(() => setLoading(false));
|
loadEmails().finally(() => setLoading(false));
|
||||||
}}
|
}}
|
||||||
onComposeNew={handleComposeNew}
|
onComposeNew={handleComposeNew}
|
||||||
onAccountSelect={handleAccountSelect}
|
onAccountSelect={handleAccountSelect as any}
|
||||||
onShowAddAccountForm={setShowAddAccountForm}
|
onShowAddAccountForm={setShowAddAccountForm}
|
||||||
onAddAccount={async (formData) => {
|
onAddAccount={async (formData) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@ -830,11 +867,11 @@ export default function CourrierPage() {
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onEditAccount={(account) => {
|
onEditAccount={(account) => {
|
||||||
setAccountToEdit(account);
|
setAccountToEdit(account as any);
|
||||||
setShowEditModal(true);
|
setShowEditModal(true);
|
||||||
}}
|
}}
|
||||||
onDeleteAccount={(account) => {
|
onDeleteAccount={(account) => {
|
||||||
setAccountToDelete(account);
|
setAccountToDelete(account as any);
|
||||||
setShowDeleteDialog(true);
|
setShowDeleteDialog(true);
|
||||||
}}
|
}}
|
||||||
onSelectEmail={(emailId, accountId, folder) => {
|
onSelectEmail={(emailId, accountId, folder) => {
|
||||||
@ -842,6 +879,7 @@ export default function CourrierPage() {
|
|||||||
handleEmailSelect(emailId, accountId || '', folder || currentFolder);
|
handleEmailSelect(emailId, accountId || '', folder || currentFolder);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
{...({} as any)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{/* Panel 2: Email List - Always visible */}
|
{/* Panel 2: Email List - Always visible */}
|
||||||
@ -1016,9 +1054,14 @@ export default function CourrierPage() {
|
|||||||
<ComposeEmail
|
<ComposeEmail
|
||||||
type={composeType}
|
type={composeType}
|
||||||
initialEmail={composeType !== 'new' ? selectedEmail : undefined}
|
initialEmail={composeType !== 'new' ? selectedEmail : undefined}
|
||||||
onSend={(emailData) => {
|
onSend={async (emailData) => {
|
||||||
const result = sendEmail(emailData);
|
try {
|
||||||
return result;
|
const result = await sendEmail(emailData);
|
||||||
|
return;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error sending email:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
onClose={() => setShowComposeModal(false)}
|
onClose={() => setShowComposeModal(false)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -619,5 +619,8 @@ export const useCourrier = () => {
|
|||||||
setPage,
|
setPage,
|
||||||
setPerPage,
|
setPerPage,
|
||||||
setSearchQuery,
|
setSearchQuery,
|
||||||
|
|
||||||
|
// Added email state setter
|
||||||
|
setEmails,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user