"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 { AlertTriangle, FolderSync, Loader2 } from 'lucide-react'; import { usePolicyStore } from '@/stores/policy-store'; export function ReadingSettings() { const t = useTranslations('settings.email_behavior'); const [isReorganizing, setIsReorganizing] = useState(false); const [reorganizeResult, setReorganizeResult] = useState(null); const { isSettingLocked, isSettingHidden, isFeatureEnabled } = usePolicyStore(); const { markAsReadDelay, deleteAction, permanentlyDeleteJunk, showPreview, mailLayout, disableThreading, plainTextMode, emailsPerPage, mailAttachmentAction, attachmentPosition, archiveMode, hoverActions, hoverActionsMode, hoverActionsCorner, hideInlineImageAttachments, attachmentImagePreviewsEnabled, updateSetting, } = useSettingsStore(); const isFocusedLayout = mailLayout === 'focus'; 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; 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'); let currentMailboxes = useEmailStore.getState().mailboxes; 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 { 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 ( {!isSettingHidden('markAsReadDelay') && ( 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')}
)}
)}
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: '10', label: t('emails_per_page.10') }, { value: '25', label: t('emails_per_page.25') }, { value: '50', label: t('emails_per_page.50') }, { value: '100', label: t('emails_per_page.100') }, ]} /> )} ); }