import React, { useState, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { ChartBarIcon, LightBulbIcon, SparklesIcon, ClockIcon, TrophyIcon, ChatBubbleBottomCenterTextIcon, } from '@heroicons/react/24/outline'; interface TodaySettingsDropdownProps { isOpen: boolean; onClose: () => void; settings: { showMetrics: boolean; showProductivity: boolean; showIntelligence: boolean; showDueToday: boolean; showCompleted: boolean; showProgressBar: boolean; showDailyQuote: boolean; }; onSettingsChange: (settings: any) => void; } const TodaySettingsDropdown: React.FC = ({ isOpen, onClose, settings, onSettingsChange, }) => { const { t } = useTranslation(); const [localSettings, setLocalSettings] = useState(settings); const [isSaving, setIsSaving] = useState(false); const dropdownRef = useRef(null); useEffect(() => { setLocalSettings(settings); }, [settings]); // Close dropdown when clicking outside useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { onClose(); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isOpen, onClose]); const handleToggle = (key: keyof typeof localSettings) => { const newSettings = { ...localSettings, [key]: !localSettings[key] }; setLocalSettings(newSettings); // Auto-save on change saveSettings(newSettings); }; const saveSettings = async (settingsToSave: typeof localSettings) => { setIsSaving(true); try { const response = await fetch('/api/profile/today-settings', { method: 'PUT', credentials: 'include', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(settingsToSave), }); if (response.ok) { onSettingsChange(settingsToSave); } else { console.error('Failed to save settings'); } } catch (error) { console.error('Error saving settings:', error); } finally { setIsSaving(false); } }; if (!isOpen) return null; const settingsOptions: Array<{key: keyof typeof localSettings, label: string, icon: React.ElementType, disabled?: boolean}> = [ { key: 'showDailyQuote', label: t('settings.showDailyQuote', 'Show Daily Quote'), icon: ChatBubbleBottomCenterTextIcon }, { key: 'showMetrics', label: t('settings.showMetrics', 'Show Metrics'), icon: ChartBarIcon }, { key: 'showProductivity', label: t('settings.showProductivity', 'Show Productivity Insights'), icon: LightBulbIcon }, { key: 'showIntelligence', label: t('settings.showIntelligence', 'Show Intelligence Suggestions'), icon: SparklesIcon }, { key: 'showDueToday', label: t('settings.showDueToday', 'Show Due Today Tasks'), icon: ClockIcon }, { key: 'showCompleted', label: t('settings.showCompleted', 'Show Completed Tasks'), icon: TrophyIcon }, ]; return (

{t('settings.todayPageSettings', 'Today Page Settings')}

{settingsOptions.map((option) => { const IconComponent = option.icon; const isDisabled = option.disabled || isSaving; return (
); })}
{isSaving && (
{t('common.saving', 'Saving...')}
)}
); }; export default TodaySettingsDropdown;