fix: update mailbox retrieval logic in archive handling to use fresh state
This commit is contained in:
@@ -820,8 +820,11 @@ export default function Home() {
|
|||||||
const handleArchive = async (emailToArchive: Email | null = selectedEmail) => {
|
const handleArchive = async (emailToArchive: Email | null = selectedEmail) => {
|
||||||
if (!client || !emailToArchive) return;
|
if (!client || !emailToArchive) return;
|
||||||
|
|
||||||
// Find archive mailbox
|
// Read fresh mailboxes from the store — batch archive calls this in a loop,
|
||||||
const archiveMailbox = mailboxes.find(m => m.role === "archive" || m.name.toLowerCase() === "archive");
|
// and each iteration needs to see folders created by prior iterations.
|
||||||
|
const currentMailboxes = useEmailStore.getState().mailboxes;
|
||||||
|
|
||||||
|
const archiveMailbox = currentMailboxes.find(m => m.role === "archive" || m.name.toLowerCase() === "archive");
|
||||||
if (!archiveMailbox) return;
|
if (!archiveMailbox) return;
|
||||||
|
|
||||||
const { archiveMode } = useSettingsStore.getState();
|
const { archiveMode } = useSettingsStore.getState();
|
||||||
@@ -830,14 +833,12 @@ export default function Home() {
|
|||||||
if (archiveMode === 'single') {
|
if (archiveMode === 'single') {
|
||||||
await moveThreadToMailbox(client, emailToArchive.id, archiveMailbox.id);
|
await moveThreadToMailbox(client, emailToArchive.id, archiveMailbox.id);
|
||||||
} else {
|
} else {
|
||||||
// Determine year/month from the email's received date
|
|
||||||
const emailDate = new Date(emailToArchive.receivedAt);
|
const emailDate = new Date(emailToArchive.receivedAt);
|
||||||
const year = emailDate.getFullYear().toString();
|
const year = emailDate.getFullYear().toString();
|
||||||
const month = (emailDate.getMonth() + 1).toString().padStart(2, '0');
|
const month = (emailDate.getMonth() + 1).toString().padStart(2, '0');
|
||||||
const archiveId = archiveMailbox.originalId || archiveMailbox.id;
|
const archiveId = archiveMailbox.originalId || archiveMailbox.id;
|
||||||
|
|
||||||
// Find or create year subfolder under archive
|
let yearMailbox = currentMailboxes.find(
|
||||||
let yearMailbox = mailboxes.find(
|
|
||||||
m => m.name === year && m.parentId === archiveId
|
m => m.name === year && m.parentId === archiveId
|
||||||
);
|
);
|
||||||
if (!yearMailbox) {
|
if (!yearMailbox) {
|
||||||
@@ -848,9 +849,9 @@ export default function Home() {
|
|||||||
if (archiveMode === 'year') {
|
if (archiveMode === 'year') {
|
||||||
await moveThreadToMailbox(client, emailToArchive.id, yearMailbox.id);
|
await moveThreadToMailbox(client, emailToArchive.id, yearMailbox.id);
|
||||||
} else {
|
} else {
|
||||||
// archiveMode === 'month' - find or create month subfolder under year
|
|
||||||
const yearId = yearMailbox.originalId || yearMailbox.id;
|
const yearId = yearMailbox.originalId || yearMailbox.id;
|
||||||
let monthMailbox = mailboxes.find(
|
const afterYear = useEmailStore.getState().mailboxes;
|
||||||
|
let monthMailbox = afterYear.find(
|
||||||
m => m.name === month && m.parentId === yearId
|
m => m.name === month && m.parentId === yearId
|
||||||
);
|
);
|
||||||
if (!monthMailbox) {
|
if (!monthMailbox) {
|
||||||
|
|||||||
Reference in New Issue
Block a user