import React, { useEffect, useState, useCallback } from 'react'; import { format } from 'date-fns'; import { el, enUS, es, ja, uk, de } from 'date-fns/locale'; import { useTranslation } from 'react-i18next'; import i18n from 'i18next'; import { ClipboardDocumentListIcon, ArrowPathIcon, FolderIcon, CheckCircleIcon, ArrowUpIcon, ArrowDownIcon, ChevronDownIcon, ChevronRightIcon, Cog6ToothIcon, CalendarDaysIcon, } from '@heroicons/react/24/outline'; import { fetchTasks, updateTask, deleteTask, toggleTaskToday, } from '../../utils/tasksService'; import { fetchProjects } from '../../utils/projectsService'; import { Task } from '../../entities/Task'; import { useStore } from '../../store/useStore'; import TaskList from './TaskList'; import TodayPlan from './TodayPlan'; import { Metrics } from '../../entities/Metrics'; import ProductivityAssistant from '../Productivity/ProductivityAssistant'; import NextTaskSuggestion from './NextTaskSuggestion'; import WeeklyCompletionChart from './WeeklyCompletionChart'; import TodaySettingsDropdown from './TodaySettingsDropdown'; const getLocale = (language: string) => { switch (language) { case 'el': return el; case 'es': return es; case 'jp': return ja; case 'ua': return uk; case 'de': return de; default: return enUS; } }; const TasksToday: React.FC = () => { const { t } = useTranslation(); // Get tasks from store at the top level to avoid conditional hook usage const storeTasks = useStore((state) => state.tasksStore.tasks); // Temporarily use local state to debug infinite loop const [isLoading, setIsLoading] = useState(false); const [isError, setIsError] = useState(false); const [hasInitialized, setHasInitialized] = useState(false); // Keep local state for UI-specific data const [localProjects, setLocalProjects] = useState([]); const [isInitialLoading, setIsInitialLoading] = useState(true); const [dailyQuote, setDailyQuote] = useState(''); const [productivityAssistantEnabled, setProductivityAssistantEnabled] = useState(true); const [isSettingsEnabled, setIsSettingsEnabled] = useState(false); const [todaySettings, setTodaySettings] = useState({ showMetrics: false, showProductivity: false, showNextTaskSuggestion: false, showSuggestions: false, showDueToday: true, showCompleted: true, showProgressBar: true, // Always enabled showDailyQuote: true, }); const [nextTaskSuggestionEnabled, setNextTaskSuggestionEnabled] = useState(true); const [profileSettings, setProfileSettings] = useState({ productivity_assistant_enabled: false, next_task_suggestion_enabled: false, }); const [showNextTaskSuggestion, setShowNextTaskSuggestion] = useState(true); const [isSuggestedCollapsed, setIsSuggestedCollapsed] = useState(() => { const stored = localStorage.getItem('suggestedTasksCollapsed'); return stored === 'true'; }); const [isCompletedCollapsed, setIsCompletedCollapsed] = useState(() => { const stored = localStorage.getItem('completedTasksCollapsed'); return stored === 'true'; }); const [isSettingsLoaded, setIsSettingsLoaded] = useState(false); // Metrics from the API const [metrics, setMetrics] = useState({ total_open_tasks: 0, tasks_pending_over_month: 0, tasks_in_progress_count: 0, tasks_in_progress: [], tasks_due_today: [], suggested_tasks: [], tasks_completed_today: [], weekly_completions: [], }); // Helper function to get completion trend vs average const getCompletionTrend = () => { const todayCount = metrics.tasks_completed_today.length; // Calculate average: sum of all completed tasks divided by 7 days // The average represents the daily average across the week if (metrics.weekly_completions.length === 0) { return { direction: 'same', difference: 0, percentage: 0, todayCount, averageCount: 0, }; } // Sum all completed tasks from the weekly data const totalCompletedTasks = metrics.weekly_completions.reduce( (sum, completion) => sum + completion.count, 0 ); // Average is total completed tasks divided by 7 const averageCount = totalCompletedTasks / 7; // Calculate percentage change vs average let percentage = 0; if (averageCount > 0) { percentage = Math.round( ((todayCount - averageCount) / averageCount) * 100 ); } else if (todayCount > 0) { // If average was 0 but today has completions, it's a 100%+ increase percentage = 100; } if (todayCount > averageCount) { return { direction: 'up', difference: Math.round((todayCount - averageCount) * 10) / 10, // Round to 1 decimal percentage: Math.abs(percentage), todayCount, averageCount: Math.round(averageCount * 10) / 10, // Round to 1 decimal }; } else if (todayCount < averageCount) { return { direction: 'down', difference: Math.round((averageCount - todayCount) * 10) / 10, // Round to 1 decimal percentage: Math.abs(percentage), todayCount, averageCount: Math.round(averageCount * 10) / 10, // Round to 1 decimal }; } else { return { direction: 'same', difference: 0, percentage: 0, todayCount, averageCount: Math.round(averageCount * 10) / 10, // Round to 1 decimal }; } }; // Track mounting state to prevent state updates after unmount const isMounted = React.useRef(false); // Function to handle next task suggestion dismissal const handleCloseNextTaskSuggestion = () => { setShowNextTaskSuggestion(false); }; // Toggle functions for collapsible sections const toggleSuggestedCollapsed = () => { const newState = !isSuggestedCollapsed; setIsSuggestedCollapsed(newState); localStorage.setItem('suggestedTasksCollapsed', newState.toString()); }; const toggleCompletedCollapsed = () => { const newState = !isCompletedCollapsed; setIsCompletedCollapsed(newState); localStorage.setItem('completedTasksCollapsed', newState.toString()); }; // Load data once on component mount useEffect(() => { isMounted.current = true; // Only fetch data once on mount const loadData = async () => { if (!isMounted.current || hasInitialized || isLoading) { return; } setIsLoading(true); try { const { tasks: fetchedTasks, metrics: fetchedMetrics } = await fetchTasks('?type=today'); if (isMounted.current) { // setTasks(fetchedTasks); // Removed local state if (isMounted.current) { // setTasks(fetchedTasks); // Removed local state setMetrics(fetchedMetrics); useStore.getState().tasksStore.setTasks(fetchedTasks); setIsError(false); } } } catch (error) { console.error('Failed to fetch tasks:', error); if (isMounted.current) { setIsError(true); } } // Load all profile settings in a single API call instead of multiple calls try { const response = await fetch('/api/profile', { credentials: 'include', }); if (response.ok) { const userData = await response.json(); if (isMounted.current) { // Set productivity assistant setting setProductivityAssistantEnabled( userData.productivity_assistant_enabled !== undefined ? userData.productivity_assistant_enabled : true ); // Set next task suggestion setting setNextTaskSuggestionEnabled( userData.next_task_suggestion_enabled !== undefined ? userData.next_task_suggestion_enabled : true ); // Parse today_settings if it's a string, or use the object directly let settings; if (userData.today_settings) { if (typeof userData.today_settings === 'string') { try { settings = JSON.parse( userData.today_settings ); } catch (error) { console.error( 'Error parsing today_settings:', error ); settings = null; } } else { settings = userData.today_settings; } } // Use parsed settings or fall back to defaults settings = settings || { showMetrics: false, showProductivity: false, showNextTaskSuggestion: false, showSuggestions: false, showDueToday: true, showCompleted: true, showProgressBar: true, // Always enabled showDailyQuote: true, }; // Store profile settings const currentProfileSettings = { productivity_assistant_enabled: userData.productivity_assistant_enabled === true, next_task_suggestion_enabled: userData.next_task_suggestion_enabled === true, }; setProfileSettings(currentProfileSettings); // Sync with profile AI & productivity features if ( userData.productivity_assistant_enabled !== undefined ) { settings.showProductivity = userData.productivity_assistant_enabled; } if ( userData.next_task_suggestion_enabled !== undefined ) { settings.showNextTaskSuggestion = userData.next_task_suggestion_enabled; } // Ensure progress bar is always enabled settings.showProgressBar = true; setTodaySettings(settings); setIsSettingsLoaded(true); } } else { setIsSettingsLoaded(true); } } catch (error) { console.error('Failed to load profile settings:', error); // Set defaults on error if (isMounted.current) { setProductivityAssistantEnabled(true); setNextTaskSuggestionEnabled(true); setIsSettingsLoaded(true); } } try { // Load projects first const projectsData = await fetchProjects(); if (isMounted.current) { const safeProjectsData = Array.isArray(projectsData) ? projectsData : []; setLocalProjects(safeProjectsData); useStore .getState() .projectsStore.setProjects(safeProjectsData); } } catch (error) { console.error('Projects loading error:', error); if (isMounted.current) { setLocalProjects([]); // Error handling is now managed by the store } } // Tasks will be loaded via the store's loadTasks method called earlier // Load daily quote from translations try { const response = await fetch( `/locales/${i18n.language}/quotes.json` ); if (response.ok) { const data = await response.json(); if ( isMounted.current && data.quotes && data.quotes.length > 0 ) { // Get a random quote from the translated quotes const randomIndex = Math.floor( Math.random() * data.quotes.length ); setDailyQuote(data.quotes[randomIndex]); } } else { // Fallback to English if language file doesn't exist const fallbackResponse = await fetch( '/locales/en/quotes.json' ); if (fallbackResponse.ok) { const fallbackData = await fallbackResponse.json(); if ( isMounted.current && fallbackData.quotes && fallbackData.quotes.length > 0 ) { const randomIndex = Math.floor( Math.random() * fallbackData.quotes.length ); setDailyQuote(fallbackData.quotes[randomIndex]); } } } } catch (error) { console.error('Failed to load daily quote:', error); // Ultimate fallback if (isMounted.current) { setDailyQuote('Focus on progress, not perfection.'); } } // Set loading to false and mark as initialized if (isMounted.current) { setIsInitialLoading(false); setIsLoading(false); setHasInitialized(true); } }; loadData(); // Cleanup function to prevent state updates after unmount return () => { isMounted.current = false; }; }, []); // Empty dependency array - only run once on mount // Memoize task handlers to prevent recreating functions on each render const handleTaskUpdate = useCallback( async (updatedTask: Task): Promise => { if (!updatedTask.id || !isMounted.current) return; // Optimistically update UI setMetrics((prevMetrics) => { const newMetrics = { ...prevMetrics }; // Helper to remove task from a list const removeTask = (list: Task[]) => list.filter((task) => task.id !== updatedTask.id); // Helper to add or update task in a list const updateOrAddTask = (list: Task[], taskToProcess: Task) => { const existingIndex = list.findIndex( (task) => task.id === taskToProcess.id ); if (existingIndex > -1) { // Task exists, update it by creating a new object and a new array // Preserve subtasks data to prevent loss return list.map((task, index) => index === existingIndex ? { ...task, ...taskToProcess, // Explicitly preserve subtasks data subtasks: taskToProcess.subtasks || taskToProcess.Subtasks || task.subtasks || task.Subtasks || [], Subtasks: taskToProcess.subtasks || taskToProcess.Subtasks || task.subtasks || task.Subtasks || [], } : task ); } else { // Task does not exist, add it by creating a new array return [...list, taskToProcess]; } }; // Remove task from all potential "active" lists first newMetrics.today_plan_tasks = removeTask( newMetrics.today_plan_tasks || [] ); newMetrics.suggested_tasks = removeTask( newMetrics.suggested_tasks || [] ); newMetrics.tasks_due_today = removeTask( newMetrics.tasks_due_today || [] ); newMetrics.tasks_in_progress = removeTask( newMetrics.tasks_in_progress || [] ); newMetrics.tasks_completed_today = removeTask( newMetrics.tasks_completed_today || [] ); // Always remove from completed first // Now, add the task to the appropriate list(s) based on its new status if (updatedTask.status === 'done' || updatedTask.status === 2) { // If completed, add to tasks_completed_today if it was completed today if (updatedTask.completed_at) { const completedDate = new Date( updatedTask.completed_at ); const today = new Date(); if ( format(completedDate, 'yyyy-MM-dd') === format(today, 'yyyy-MM-dd') ) { newMetrics.tasks_completed_today = updateOrAddTask( newMetrics.tasks_completed_today, updatedTask ); } } } else { // If not completed, add to relevant active lists if ( updatedTask.today && updatedTask.status !== 'archived' ) { newMetrics.today_plan_tasks = updateOrAddTask( newMetrics.today_plan_tasks, updatedTask ); } if (updatedTask.status === 'in_progress') { newMetrics.tasks_in_progress = updateOrAddTask( newMetrics.tasks_in_progress, updatedTask ); } // Check if due today (and not already in today_plan_tasks or in_progress) const isDueToday = updatedTask.due_date && format(new Date(updatedTask.due_date), 'yyyy-MM-dd') === format(new Date(), 'yyyy-MM-dd'); if ( isDueToday && updatedTask.status !== 'archived' && !newMetrics.today_plan_tasks.some( (t) => t.id === updatedTask.id ) && !newMetrics.tasks_in_progress.some( (t) => t.id === updatedTask.id ) ) { newMetrics.tasks_due_today = updateOrAddTask( newMetrics.tasks_due_today, updatedTask ); } // Check for suggested tasks (and not already in other active lists) const isSuggested = !updatedTask.today && !updatedTask.project_id && !updatedTask.due_date; if ( isSuggested && updatedTask.status !== 'archived' && updatedTask.status !== 'done' && updatedTask.status !== 2 && !newMetrics.today_plan_tasks.some( (t) => t.id === updatedTask.id ) && !newMetrics.tasks_due_today.some( (t) => t.id === updatedTask.id ) && !newMetrics.tasks_in_progress.some( (t) => t.id === updatedTask.id ) ) { newMetrics.suggested_tasks = updateOrAddTask( newMetrics.suggested_tasks, updatedTask ); } } // Recalculate total_open_tasks based on the updated active lists newMetrics.total_open_tasks = newMetrics.today_plan_tasks.length + newMetrics.suggested_tasks.length + newMetrics.tasks_due_today.length + newMetrics.tasks_in_progress.length; return newMetrics; }); // Update the store with the updated task useStore.getState().tasksStore.updateTaskInStore(updatedTask); try { // Make API call to persist the change await updateTask(updatedTask.id, updatedTask); } catch (error) { console.error('Error updating task:', error); // Revert UI on error if necessary, or re-fetch to sync // For now, just log the error } }, [] // Dependencies are now handled by direct state manipulation ); const handleTaskDelete = useCallback( async (taskId: number): Promise => { if (!isMounted.current) return; try { await deleteTask(taskId); // Reload tasks to reflect the change const { tasks: updatedTasks, metrics: updatedMetrics } = await fetchTasks('?type=today'); if (isMounted.current) { useStore.getState().tasksStore.setTasks(updatedTasks); setMetrics(updatedMetrics); } } catch (error) { console.error('Error deleting task:', error); } }, [] ); const handleToggleToday = useCallback( async (taskId: number): Promise => { if (!isMounted.current) return; try { await toggleTaskToday(taskId); // Reload tasks to reflect the change const { tasks: updatedTasks, metrics: updatedMetrics } = await fetchTasks('?type=today'); if (isMounted.current) { useStore.getState().tasksStore.setTasks(updatedTasks); setMetrics(updatedMetrics); } } catch (error) { console.error('Error toggling task today status:', error); } }, [] ); const handleTaskCompletionToggle = useCallback( async (updatedTask: Task): Promise => { if (!isMounted.current) return; try { // The updatedTask is already the result of the API call from TaskItem // Use the centralized task update handler to update UI optimistically await handleTaskUpdate(updatedTask); } catch (error) { console.error('Error toggling task completion:', error); } }, [handleTaskUpdate] ); // Calculate today's progress for the progress bar const getTodayProgress = () => { const todayTasks = metrics.today_plan_tasks || []; const completedToday = metrics.tasks_completed_today.length; const totalTodayTasks = todayTasks.length + completedToday; return { completed: completedToday, total: totalTodayTasks, percentage: totalTodayTasks === 0 ? 0 : Math.round((completedToday / totalTodayTasks) * 100), }; }; const todayProgress = getTodayProgress(); // Handle settings change const handleSettingsChange = (newSettings: typeof todaySettings) => { setTodaySettings(newSettings); }; // Show loading state until both data and settings are loaded (only for initial load) if (isInitialLoading || !isSettingsLoaded) { return (

{t('common.loading', 'Loading...')}

); } // Show error state if (isError && storeTasks.length === 0) { return (

{t('errors.somethingWentWrong', 'Something went wrong')}

); } return (
{/* Today Header with Icons on the Right */}

{t('tasks.today')},

{format(new Date(), 'PPP', { locale: getLocale(i18n.language), })}
{/* Today Navigation Icons */}
setIsSettingsEnabled(false) } settings={todaySettings} profileSettings={profileSettings} onSettingsChange={handleSettingsChange} />
{/* Today Progress Bar and Daily Quote */}
{/* Progress Bar - always show when setting is enabled */} {todaySettings.showProgressBar && (
)} {/* Daily Quote - show independently of progress bar */} {todaySettings.showDailyQuote && dailyQuote && (

{dailyQuote}

)}
{/* Metrics Section - Always reserve space to prevent layout shift */} {!isSettingsLoaded ? ( // Invisible placeholder that reserves the exact same space ) : todaySettings.showMetrics ? (
{/* Combined Task & Project Metrics */}

{t('dashboard.overview')}

{t('tasks.backlog')}

{metrics.total_open_tasks}

{t('tasks.inProgress')}

{metrics.tasks_in_progress_count}

{t('tasks.dueToday')}

{metrics.tasks_due_today.length}

{t( 'tasks.completedToday', 'Completed Today' )}

{(() => { const trend = getCompletionTrend(); const getTooltipText = () => { if ( trend.direction === 'same' ) { return t( 'dashboard.sameAsAverage', 'Same as average' ); } else if ( trend.direction === 'up' ) { return t( 'dashboard.betterThanAverage', '{{percentage}}% more than average', { percentage: trend.percentage, } ); } else { return t( 'dashboard.worseThanAverage', '{{percentage}}% less than average', { percentage: trend.percentage, } ); } }; return ( <> {(trend.direction === 'up' || trend.direction === 'down') && (
{trend.direction === 'up' && ( )} {trend.direction === 'down' && ( )}
{getTooltipText()}
)}

{ metrics .tasks_completed_today .length }

); })()}

{t('projects.active')}

{Array.isArray(localProjects) ? localProjects.filter( (project) => project.active ).length : 0}

{/* Weekly Completion Chart */}
) : null} {/* Productivity Assistant - Conditionally Rendered */} {!isSettingsLoaded ? ( // Invisible placeholder for productivity assistant ) : todaySettings.showProductivity && productivityAssistantEnabled && profileSettings.productivity_assistant_enabled === true ? ( ) : null} {/* Next Task Suggestion - At top of tasks section */} {!isSettingsLoaded ? ( // Invisible placeholder for next task suggestion ) : todaySettings.showNextTaskSuggestion && nextTaskSuggestionEnabled && showNextTaskSuggestion && profileSettings.next_task_suggestion_enabled === true ? (
) : null} {/* Today Plan */} {/* Suggested Tasks - Separate setting */} {!isSettingsLoaded ? ( // Invisible placeholder for suggestions ) : todaySettings.showSuggestions && metrics.suggested_tasks.length > 0 ? (

{t('tasks.suggested')}

{metrics.suggested_tasks.length} {isSuggestedCollapsed ? ( ) : ( )}
{!isSuggestedCollapsed && ( )}
) : null} {/* Due Today Tasks - Conditionally Rendered */} {isSettingsLoaded && todaySettings.showDueToday && metrics.tasks_due_today.length > 0 && (

{t('tasks.dueToday')}

)} {/* Completed Tasks - Conditionally Rendered */} {isSettingsLoaded && todaySettings.showCompleted && (() => { const completedToday = metrics.tasks_completed_today; // Use the already filtered list from backend return (

{t('tasks.completedToday')}

{completedToday.length} {isCompletedCollapsed ? ( ) : ( )}
{!isCompletedCollapsed && (completedToday.length > 0 ? ( ) : (

{t( 'tasks.noCompletedTasksToday', 'No completed tasks today.' )}

))}
); })()} {metrics.tasks_due_today.length === 0 && metrics.tasks_in_progress.length === 0 && metrics.suggested_tasks.length === 0 && (metrics.today_plan_tasks || []).length > 0 && (

{t('tasks.noTasksAvailable')}

)}
); }; export default TasksToday;