import React, { useState, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { ChartBarIcon, LightBulbIcon, SparklesIcon, ClockIcon, TrophyIcon, ChatBubbleBottomCenterTextIcon, ListBulletIcon, } from '@heroicons/react/24/outline'; interface TodaySettingsDropdownProps { isOpen: boolean; onClose: () => void; settings: { showMetrics: boolean; showProductivity: boolean; showNextTaskSuggestion: boolean; showSuggestions: boolean; showDueToday: boolean; showCompleted: boolean; showProgressBar: boolean; showDailyQuote: boolean; }; profileSettings?: { productivity_assistant_enabled?: boolean; next_task_suggestion_enabled?: boolean; }; onSettingsChange: (settings: any) => void; } const TodaySettingsDropdown: React.FC = ({ isOpen, onClose, settings, profileSettings, 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, }, // Only show productivity option if enabled in profile ...(profileSettings?.productivity_assistant_enabled === true ? [ { key: 'showProductivity' as keyof typeof localSettings, label: t( 'settings.showProductivity', 'Show Productivity Insights' ), icon: LightBulbIcon, }, ] : []), // Only show next task suggestion option if enabled in profile ...(profileSettings?.next_task_suggestion_enabled === true ? [ { key: 'showNextTaskSuggestion' as keyof typeof localSettings, label: t( 'settings.showNextTaskSuggestion', 'Next Task Suggestion' ), icon: SparklesIcon, }, ] : []), { key: 'showSuggestions', label: t('settings.showSuggestions', 'Show Suggested'), icon: ListBulletIcon, }, { 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;