"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, returnToListAfterAction, showPreview, mailLayout, disableThreading, emailsPerPage, mailAttachmentAction, attachmentPosition, archiveMode, hoverActions, hoverActionsMode, hoverActionsCorner, hideInlineImageAttachments, attachmentImagePreviewsEnabled, messageSpacing, 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('messageSpacing', value as typeof messageSpacing)} options={[ { value: 'auto', label: t('message_spacing.auto') }, { value: 'always', label: t('message_spacing.always') }, { value: 'edge', label: t('message_spacing.edge') }, ]} /> )} {!isSettingHidden('deleteAction') && (
updateSetting('archiveMode', value as ArchiveMode)} options={[ { value: 'single', label: t('archive_mode.single') }, { value: 'year', label: t('archive_mode.year') }, { value: 'month', label: t('archive_mode.month') }, ]} /> {archiveMode !== 'single' && (
{reorganizeResult && (

{reorganizeResult}

)}
)}
updateSetting('permanentlyDeleteJunk', checked)} /> updateSetting('returnToListAfterAction', checked)} /> {!isSettingHidden('showPreview') && ( updateSetting('showPreview', checked)} /> )} updateSetting('disableThreading', checked)} /> updateSetting('hideInlineImageAttachments', checked)} /> updateSetting('attachmentImagePreviewsEnabled', checked)} /> {isFeatureEnabled('hoverActionsConfigEnabled') && (

{t('hover_actions.description')}

{ALL_HOVER_ACTIONS.map((action) => { const isEnabled = hoverActions.includes(action.id); return ( ); })}
{(['inline', 'floating'] as const).map((mode) => ( ))}
{hoverActionsMode === 'floating' && (
{(['top-left', 'top-right', 'bottom-left', 'bottom-right'] as const).map((corner) => ( ))}
)}
)} updateSetting('attachmentPosition', value as 'beside-sender' | 'below-header')} options={[ { value: 'beside-sender', label: t('attachment_position.beside-sender') }, { value: 'below-header', label: t('attachment_position.below-header') }, ]} /> {!isSettingHidden('emailsPerPage') && (