diff --git a/app/[locale]/page.tsx b/app/[locale]/page.tsx index 28841ac0..11e14c39 100644 --- a/app/[locale]/page.tsx +++ b/app/[locale]/page.tsx @@ -518,12 +518,46 @@ export default function Home() { // Find archive mailbox const archiveMailbox = mailboxes.find(m => m.role === "archive" || m.name.toLowerCase() === "archive"); - if (archiveMailbox) { - try { + if (!archiveMailbox) return; + + const { archiveMode } = useSettingsStore.getState(); + + try { + if (archiveMode === 'single') { await moveToMailbox(client, selectedEmail.id, archiveMailbox.id); - } catch (error) { - console.error("Failed to archive email:", error); + } else { + // Determine year/month from the email's received date + const emailDate = new Date(selectedEmail.receivedAt); + const year = emailDate.getFullYear().toString(); + const month = (emailDate.getMonth() + 1).toString().padStart(2, '0'); + const archiveId = archiveMailbox.originalId || archiveMailbox.id; + + // Find or create year subfolder under archive + let yearMailbox = mailboxes.find( + m => m.name === year && m.parentId === archiveId + ); + if (!yearMailbox) { + yearMailbox = await client.createMailbox(year, archiveId); + await fetchMailboxes(client); + } + + if (archiveMode === 'year') { + await moveToMailbox(client, selectedEmail.id, yearMailbox.id); + } else { + // archiveMode === 'month' — find or create month subfolder under year + const yearId = yearMailbox.originalId || yearMailbox.id; + let monthMailbox = mailboxes.find( + m => m.name === month && m.parentId === yearId + ); + if (!monthMailbox) { + monthMailbox = await client.createMailbox(month, yearId); + await fetchMailboxes(client); + } + await moveToMailbox(client, selectedEmail.id, monthMailbox.id); + } } + } catch (error) { + console.error("Failed to archive email:", error); } }; diff --git a/components/settings/email-settings.tsx b/components/settings/email-settings.tsx index 00383769..ee6189bf 100644 --- a/components/settings/email-settings.tsx +++ b/components/settings/email-settings.tsx @@ -3,13 +3,18 @@ import { useState } from 'react'; import { useTranslations } from 'next-intl'; import { useSettingsStore } from '@/stores/settings-store'; +import type { ArchiveMode } from '@/stores/settings-store'; +import { useAuthStore } from '@/stores/auth-store'; +import { useEmailStore } from '@/stores/email-store'; import { SettingsSection, SettingItem, Select, ToggleSwitch } from './settings-section'; import { TrustedSendersModal } from '@/components/trusted-senders-modal'; -import { ChevronRight, AlertTriangle } from 'lucide-react'; +import { ChevronRight, AlertTriangle, FolderSync, Loader2 } from 'lucide-react'; export function EmailSettings() { const t = useTranslations('settings.email_behavior'); const [showTrustedModal, setShowTrustedModal] = useState(false); + const [isReorganizing, setIsReorganizing] = useState(false); + const [reorganizeResult, setReorganizeResult] = useState(null); const { markAsReadDelay, @@ -20,6 +25,7 @@ export function EmailSettings() { externalContentPolicy, mailAttachmentAction, emailAlwaysLightMode, + archiveMode, trustedSenders, updateSetting, } = useSettingsStore(); @@ -32,6 +38,69 @@ export function EmailSettings() { return t('trusted_senders.count_other', { count }); }; + const handleReorganizeArchive = async () => { + const { client } = useAuthStore.getState(); + const { mailboxes, fetchMailboxes } = useEmailStore.getState(); + if (!client) return; + + const archiveMailbox = mailboxes.find(m => m.role === 'archive' || m.name.toLowerCase() === 'archive'); + if (!archiveMailbox) return; + + setIsReorganizing(true); + setReorganizeResult(null); + + try { + const archiveId = archiveMailbox.originalId || archiveMailbox.id; + + // Fetch all emails in the root archive mailbox + const emails = await client.getEmailsInMailbox(archiveId); + let movedCount = 0; + + for (const email of emails) { + const emailDate = new Date(email.receivedAt); + const year = emailDate.getFullYear().toString(); + const month = (emailDate.getMonth() + 1).toString().padStart(2, '0'); + + // Re-read mailboxes from store each iteration in case new ones were created + let currentMailboxes = useEmailStore.getState().mailboxes; + + // Find or create year subfolder + let yearMailbox = currentMailboxes.find( + m => m.name === year && m.parentId === archiveId + ); + if (!yearMailbox) { + yearMailbox = await client.createMailbox(year, archiveId); + await fetchMailboxes(client); + currentMailboxes = useEmailStore.getState().mailboxes; + } + + if (archiveMode === 'year') { + await client.moveEmail(email.id, yearMailbox.id); + movedCount++; + } else { + // month mode + const yearId = yearMailbox.originalId || yearMailbox.id; + let monthMailbox = currentMailboxes.find( + m => m.name === month && m.parentId === yearId + ); + if (!monthMailbox) { + monthMailbox = await client.createMailbox(month, yearId); + await fetchMailboxes(client); + } + await client.moveEmail(email.id, monthMailbox.id); + movedCount++; + } + } + + setReorganizeResult(t('archive_mode.reorganize_success', { count: movedCount })); + } catch (error) { + console.error('Failed to reorganize archive:', error); + setReorganizeResult(t('archive_mode.reorganize_error')); + } finally { + setIsReorganizing(false); + } + }; + return ( {/* Mark as Read */} @@ -68,6 +137,40 @@ export function EmailSettings() { + {/* Archive Mode */} + +
+