"use client"; import { useState } from 'react'; import { useTranslations } from 'next-intl'; import { useSettingsStore } from '@/stores/settings-store'; import type { ArchiveMode, HoverAction } from '@/stores/settings-store'; import { ALL_HOVER_ACTIONS } from '@/stores/settings-store'; import { useAuthStore } from '@/stores/auth-store'; import { useEmailStore } from '@/stores/email-store'; import { cn } from '@/lib/utils'; import { SettingsSection, SettingItem, Select, ToggleSwitch } from './settings-section'; import { TrustedSendersModal } from '@/components/trusted-senders-modal'; 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, deleteAction, permanentlyDeleteJunk, showPreview, emailsPerPage, externalContentPolicy, mailAttachmentAction, attachmentPosition, emailAlwaysLightMode, archiveMode, hoverActions, trustedSenders, updateSetting, } = useSettingsStore(); // Get count label for trusted senders button const getTrustedSendersCount = () => { const count = trustedSenders.length; if (count === 0) return t('trusted_senders.count_zero'); if (count === 1) return t('trusted_senders.count_one'); 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 */} updateSetting('deleteAction', value as 'trash' | 'permanent')} options={[ { value: 'trash', label: t('delete_action.trash') }, { value: 'permanent', label: t('delete_action.permanent') }, ]} /> {deleteAction === 'permanent' && (
{t('delete_action.warning')}
)}
{/* Archive Mode */}
updateSetting('mailAttachmentAction', value as 'preview' | 'download')} options={[ { value: 'preview', label: t('attachment_click_action.preview') }, { value: 'download', label: t('attachment_click_action.download') }, ]} /> updateSetting('emailsPerPage', parseInt(value))} options={[ { value: '25', label: t('emails_per_page.25') }, { value: '50', label: t('emails_per_page.50') }, { value: '100', label: t('emails_per_page.100') }, ]} /> {/* Always Light Mode for Emails */} updateSetting('emailAlwaysLightMode', checked)} /> {/* External Content */}