panel 2 courier api restore
This commit is contained in:
parent
c12a1b2891
commit
f805463949
@ -176,11 +176,16 @@ export async function GET(request: Request) {
|
|||||||
// Function to get mailboxes
|
// Function to get mailboxes
|
||||||
const getMailboxes = async () => {
|
const getMailboxes = async () => {
|
||||||
try {
|
try {
|
||||||
|
console.log("Getting list of mailboxes...");
|
||||||
const mailboxes = [];
|
const mailboxes = [];
|
||||||
const list = await imapClient.list();
|
const list = await imapClient.list();
|
||||||
|
console.log(`Found ${list.length} mailboxes from IMAP server`);
|
||||||
|
|
||||||
for (const mailbox of list) {
|
for (const mailbox of list) {
|
||||||
mailboxes.push(mailbox.path);
|
mailboxes.push(mailbox.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("Available mailboxes:", mailboxes);
|
||||||
return mailboxes;
|
return mailboxes;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error listing mailboxes:", error);
|
console.error("Error listing mailboxes:", error);
|
||||||
|
|||||||
@ -1893,63 +1893,66 @@ export default function CourrierPage() {
|
|||||||
loadEmails();
|
loadEmails();
|
||||||
}, [currentView]);
|
}, [currentView]);
|
||||||
|
|
||||||
|
// Create a function to load folders that will be available throughout the component
|
||||||
|
const loadFolders = async () => {
|
||||||
|
console.log('[DEBUG] Explicitly loading folders from standalone function...');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Make a specific request just to get folders
|
||||||
|
const timestamp = Date.now(); // Cache busting
|
||||||
|
const response = await fetch(`/api/courrier?folder=INBOX&page=1&limit=1&skipCache=true&_t=${timestamp}`, {
|
||||||
|
headers: {
|
||||||
|
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
||||||
|
'Pragma': 'no-cache'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
console.log('[DEBUG] Folder response data:', JSON.stringify(data, null, 2));
|
||||||
|
|
||||||
|
// Check for mailboxes field (from IMAP) and use it if available
|
||||||
|
const folders = data.mailboxes || data.folders || [];
|
||||||
|
|
||||||
|
if (folders && folders.length > 0) {
|
||||||
|
console.log('[DEBUG] Successfully loaded folders:', folders);
|
||||||
|
|
||||||
|
setAvailableFolders(folders);
|
||||||
|
|
||||||
|
// Update the mail account with folders
|
||||||
|
setAccounts(prev => {
|
||||||
|
console.log('[DEBUG] Updating account with folders:', folders);
|
||||||
|
return prev.map(account =>
|
||||||
|
account.id === 1
|
||||||
|
? { ...account, folders: folders }
|
||||||
|
: account
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.warn('[DEBUG] No folders found in response. Response data:', data);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error('[DEBUG] Folder request failed:', response.status);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[DEBUG] Error explicitly loading folders:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Improve the folder loading logic with a delay and better reliability
|
// Improve the folder loading logic with a delay and better reliability
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let isMounted = true; // For cleanup
|
let isMounted = true; // For cleanup
|
||||||
|
|
||||||
const loadFolders = async () => {
|
// Only load if we don't have folders yet and we're not already loading
|
||||||
// Only load if we don't have folders yet and we're not already loading
|
if ((!accounts[1]?.folders || accounts[1]?.folders?.length === 0) && !loading) {
|
||||||
if ((!accounts[1]?.folders || accounts[1]?.folders?.length === 0) && !loading) {
|
console.log('[DEBUG] Triggering folder load in useEffect...');
|
||||||
console.log('Explicitly loading folders with delay...');
|
|
||||||
|
// Set a small delay to ensure other loading operations have completed
|
||||||
// Set a small delay to ensure other loading operations have completed
|
setTimeout(() => {
|
||||||
setTimeout(async () => {
|
if (!isMounted) return;
|
||||||
if (!isMounted) return;
|
loadFolders();
|
||||||
|
}, 500); // 500ms delay
|
||||||
setLoading(true);
|
}
|
||||||
try {
|
|
||||||
// Make a specific request just to get folders
|
|
||||||
const timestamp = Date.now(); // Cache busting
|
|
||||||
const response = await fetch(`/api/courrier?folder=INBOX&page=1&limit=1&skipCache=true&_t=${timestamp}`, {
|
|
||||||
headers: {
|
|
||||||
'Cache-Control': 'no-cache, no-store, must-revalidate',
|
|
||||||
'Pragma': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
if (data.folders && data.folders.length > 0) {
|
|
||||||
console.log('Successfully loaded folders:', data.folders);
|
|
||||||
|
|
||||||
if (isMounted) {
|
|
||||||
setAvailableFolders(data.folders);
|
|
||||||
|
|
||||||
// Update the mail account with folders
|
|
||||||
setAccounts(prev => prev.map(account =>
|
|
||||||
account.id === 1
|
|
||||||
? { ...account, folders: data.folders }
|
|
||||||
: account
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.warn('No folders found in response');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error('Folder request failed:', response.status);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error explicitly loading folders:', error);
|
|
||||||
} finally {
|
|
||||||
if (isMounted) {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 500); // 500ms delay
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
loadFolders();
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
isMounted = false;
|
isMounted = false;
|
||||||
@ -1959,27 +1962,28 @@ export default function CourrierPage() {
|
|||||||
// Add a new function to fetch the IMAP credentials email
|
// Add a new function to fetch the IMAP credentials email
|
||||||
const fetchImapCredentials = async () => {
|
const fetchImapCredentials = async () => {
|
||||||
try {
|
try {
|
||||||
console.log("Fetching IMAP credentials...");
|
console.log("[DEBUG] Fetching IMAP credentials...");
|
||||||
const response = await fetch("/api/courrier/credentials", {
|
const response = await fetch("/api/courrier/credentials", {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
"Cache-Control": "no-cache"
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("IMAP credentials response status:", response.status);
|
console.log("[DEBUG] IMAP credentials response status:", response.status);
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({}));
|
const errorData = await response.json().catch(() => ({}));
|
||||||
console.error("Error fetching IMAP credentials:", response.status, errorData);
|
console.error("[DEBUG] Error fetching IMAP credentials:", response.status, errorData);
|
||||||
throw new Error(`Failed to fetch IMAP credentials: ${response.status}`);
|
throw new Error(`Failed to fetch IMAP credentials: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
console.log("IMAP credentials data:", data);
|
console.log("[DEBUG] IMAP credentials data:", data);
|
||||||
|
|
||||||
if (data && data.credentials && data.credentials.email) {
|
if (data && data.credentials && data.credentials.email) {
|
||||||
console.log("Setting account with IMAP email:", data.credentials.email);
|
console.log("[DEBUG] Setting account with IMAP email:", data.credentials.email);
|
||||||
setAccounts(prev => prev.map(account =>
|
setAccounts(prev => prev.map(account =>
|
||||||
account.id === 1
|
account.id === 1
|
||||||
? {
|
? {
|
||||||
@ -1989,11 +1993,17 @@ export default function CourrierPage() {
|
|||||||
}
|
}
|
||||||
: account
|
: account
|
||||||
));
|
));
|
||||||
|
|
||||||
|
// After setting the account email, explicitly load folders
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log("[DEBUG] Triggering folder load after setting account");
|
||||||
|
loadFolders();
|
||||||
|
}, 1000);
|
||||||
} else {
|
} else {
|
||||||
console.log("No valid IMAP credentials found in response:", data);
|
console.log("[DEBUG] No valid IMAP credentials found in response:", data);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error in fetchImapCredentials:", error);
|
console.error("[DEBUG] Error in fetchImapCredentials:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user