"use client"; import { useState, useRef } from 'react'; import { useTranslations } from 'next-intl'; import { useSettingsStore } from '@/stores/settings-store'; import { useConfig } from '@/hooks/use-config'; import { SettingsSection, SettingItem, ToggleSwitch } from './settings-section'; import { Button } from '@/components/ui/button'; import { usePolicyStore } from '@/stores/policy-store'; import { ALL_DEBUG_CATEGORIES } from '@/stores/settings-store'; export function AdvancedSettings() { const t = useTranslations('settings.advanced'); const tCommon = useTranslations('common'); const { debugMode, debugCategories, senderFavicons, settingsSyncDisabled, updateSetting, resetToDefaults, exportSettings, importSettings } = useSettingsStore(); const { settingsSyncEnabled } = useConfig(); const [showResetConfirm, setShowResetConfirm] = useState(false); const fileInputRef = useRef(null); const { isSettingLocked, isSettingHidden, isFeatureEnabled } = usePolicyStore(); const handleExport = () => { const settingsJson = exportSettings(); const blob = new Blob([settingsJson], { type: 'application/json' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `webmail-settings-${new Date().toISOString().split('T')[0]}.json`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; const handleImport = () => { fileInputRef.current?.click(); }; const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; const reader = new FileReader(); reader.onload = (event) => { const json = event.target?.result as string; const success = importSettings(json); if (success) { alert(t('../../settings.import_success')); } else { alert(t('../../settings.import_error')); } }; reader.readAsText(file); }; const handleReset = () => { if (showResetConfirm) { resetToDefaults(); setShowResetConfirm(false); alert(t('../../settings.save_success')); } else { setShowResetConfirm(true); setTimeout(() => setShowResetConfirm(false), 5000); } }; return ( {/* Debug Mode */} {!isSettingHidden('debugMode') && isFeatureEnabled('debugModeEnabled') && ( updateSetting('debugMode', checked)} /> )} {/* Debug Categories */} {debugMode && !isSettingHidden('debugMode') && isFeatureEnabled('debugModeEnabled') && (

{t('debug_categories.description')}

{ALL_DEBUG_CATEGORIES.map((cat) => ( { updateSetting('debugCategories', { ...debugCategories, [cat.id]: checked, }); }} /> ))}
)} {/* Settings Sync */} {settingsSyncEnabled && ( updateSetting('settingsSyncDisabled', !checked)} /> )} {/* Sender Favicons (Experimental) */} updateSetting('senderFavicons', checked)} /> {/* Export Settings */} {isFeatureEnabled('settingsExportEnabled') && ( )} {/* Import Settings */} {isFeatureEnabled('settingsExportEnabled') && ( <> )} {/* Reset Settings */}
); }