courrier multi account restore compose

This commit is contained in:
alma 2025-04-28 13:23:33 +02:00
parent ab6e34c4fa
commit 0f9d84c6a9
4 changed files with 44 additions and 55 deletions

View File

@ -40,13 +40,7 @@ export async function GET(request: Request) {
// Try to get from Redis cache first, but only if it's not a search query
if (!searchQuery) {
const cacheKey = accountId ? `${session.user.id}:${accountId}:${folder}` : `${session.user.id}:${folder}`;
const cachedEmails = await getCachedEmailList(
session.user.id,
accountId || 'default',
folder,
page,
perPage
);
const cachedEmails = await getCachedEmailList(session.user.id, folder, page, perPage);
if (cachedEmails) {
console.log(`Using Redis cached emails for ${cacheKey}:${page}:${perPage}`);
return NextResponse.json(cachedEmails);
@ -91,12 +85,12 @@ export async function POST(request: Request) {
// Invalidate Redis cache for the folder
if (folderName) {
await invalidateFolderCache(session.user.id, 'default', folderName);
await invalidateFolderCache(session.user.id, folderName);
} else {
// If no folder specified, invalidate all folders (using a wildcard pattern)
const folders = ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
for (const folder of folders) {
await invalidateFolderCache(session.user.id, 'default', folder);
await invalidateFolderCache(session.user.id, folder);
}
}

View File

@ -312,7 +312,7 @@ export default function CourrierPage() {
folders: accountFolders
};
console.log(`[DEBUG] Updated loading account to real account: ${account.email} with ID ${account.id}`);
} else if (index > 0) {
} else {
// Add additional accounts as new entries
updatedAccounts.push({
id: account.id || `account-${index}`,
@ -321,31 +321,28 @@ export default function CourrierPage() {
color: account.color || 'bg-blue-500',
folders: accountFolders
});
console.log(`[DEBUG] Added additional account: ${account.email} with ID ${account.id}`);
}
});
} else {
// Fallback if accounts array is empty for some reason
updatedAccounts.push({ id: 'all-accounts', name: 'All', email: '', color: 'bg-gray-500' });
// Add all accounts from the API response
data.allAccounts.forEach((account: any) => {
const accountFolders = (account.folders && Array.isArray(account.folders))
? account.folders
: (data.mailboxes && Array.isArray(data.mailboxes))
? data.mailboxes
: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
updatedAccounts.push({
id: account.id,
name: account.display_name || account.email,
email: account.email,
color: account.color || 'bg-blue-500',
folders: accountFolders
});
const firstAccount = data.allAccounts[0];
const accountFolders = (firstAccount.folders && Array.isArray(firstAccount.folders))
? firstAccount.folders
: (data.mailboxes && Array.isArray(data.mailboxes))
? data.mailboxes
: ['INBOX', 'Sent', 'Drafts', 'Trash', 'Junk'];
updatedAccounts.push({
id: firstAccount.id,
name: firstAccount.display_name || firstAccount.email,
email: firstAccount.email,
color: firstAccount.color || 'bg-blue-500',
folders: accountFolders
});
}
} else {
} else if (data.email) {
// Fallback to single account if allAccounts is not available
console.log(`[DEBUG] Fallback to single account: ${data.email}`);

View File

@ -80,12 +80,6 @@ export const useCourrier = () => {
const loadEmails = useCallback(async (isLoadMore = false, accountId?: string) => {
if (!session?.user?.id) return;
// Skip loading if accountId is 'loading-account'
if (accountId === 'loading-account') {
console.log('Skipping email load for loading account');
return;
}
setIsLoading(true);
setError(null);
@ -104,20 +98,13 @@ export const useCourrier = () => {
queryParams.set('search', searchQuery);
}
// Add accountId if provided and not 'loading-account'
if (accountId && accountId !== 'all-accounts' && accountId !== 'loading-account') {
// Add accountId if provided
if (accountId) {
queryParams.set('accountId', accountId);
}
// First try Redis cache with low timeout
const cachedEmails = await getCachedEmailsWithTimeout(
session.user.id,
currentFolder,
currentRequestPage,
perPage,
100,
accountId && accountId !== 'all-accounts' && accountId !== 'loading-account' ? accountId : undefined
);
const cachedEmails = await getCachedEmailsWithTimeout(session.user.id, currentFolder, currentRequestPage, perPage, 100);
if (cachedEmails) {
// Ensure cached data has emails array property
if (Array.isArray(cachedEmails.emails)) {

View File

@ -59,27 +59,38 @@ export async function getCachedEmailsWithTimeout(
folder: string,
page: number,
perPage: number,
timeoutMs: number = 100,
accountId?: string
timeoutMs: number = 100
): Promise<any | null> {
// Skip cache if accountId is 'loading-account'
if (accountId === 'loading-account') {
console.log(`Skipping cache for loading account`);
return null;
}
return new Promise((resolve) => {
const timeoutId = setTimeout(() => {
console.log(`Cache access timeout for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`);
console.log(`Cache access timeout for ${userId}:${folder}:${page}:${perPage}`);
resolve(null);
}, timeoutMs);
getCachedEmailList(userId, accountId || 'default', folder, page, perPage)
getCachedEmailList(userId, folder, page, perPage)
.then(result => {
clearTimeout(timeoutId);
if (result) {
console.log(`Using cached data for ${userId}:${folder}:${page}:${perPage}${accountId ? ` for account ${accountId}` : ''}`);
resolve(result);
console.log(`Using cached data for ${userId}:${folder}:${page}:${perPage}`);
// Validate and normalize the data structure
if (typeof result === 'object') {
// Make sure we have an emails array
if (!result.emails && Array.isArray(result)) {
// If result is an array, convert to proper structure
resolve({ emails: result });
} else if (!result.emails) {
// If no emails property, add empty array
resolve({ ...result, emails: [] });
} else {
// Normal case, return as is
resolve(result);
}
} else {
// Invalid data, return null
console.warn('Invalid cached data format:', result);
resolve(null);
}
} else {
resolve(null);
}