import React, { useState, useEffect, useCallback } from 'react'; import { Link } from 'react-router-dom'; import { CalendarDaysIcon, CalendarIcon, ArrowPathIcon, ListBulletIcon, ChevronDownIcon, CheckIcon, } from '@heroicons/react/24/outline'; import { TagIcon, FolderIcon, FireIcon } from '@heroicons/react/24/solid'; import { useTranslation } from 'react-i18next'; import TaskPriorityIcon from '../Shared/Icons/TaskPriorityIcon'; import { Project } from '../../entities/Project'; import { Task } from '../../entities/Task'; import { fetchSubtasks } from '../../utils/tasksService'; import { isTaskCompleted, isTaskInProgress } from '../../constants/taskStatus'; import TaskStatusControl from './TaskStatusControl'; import { parseDateString } from '../../utils/dateUtils'; interface TaskHeaderProps { task: Task; project?: Project; onTaskClick: (e: React.MouseEvent) => void; onToggleCompletion?: () => void; hideProjectName?: boolean; onToggleToday?: (taskId: number, task?: Task) => Promise; onTaskUpdate?: (task: Task) => Promise; isOverdue?: boolean; showSubtasks?: boolean; hasSubtasks?: boolean; onSubtasksToggle?: (e: React.MouseEvent) => void; // Props for edit and delete functionality onEdit?: (e: React.MouseEvent) => void; onDelete?: (e: React.MouseEvent) => void; isUpcomingView?: boolean; } const TaskHeader: React.FC = ({ task, project, onTaskClick, onToggleCompletion, hideProjectName = false, onToggleToday: _onToggleToday, onTaskUpdate, showSubtasks, hasSubtasks, onSubtasksToggle, // Props for edit and delete functionality onEdit: _onEdit, onDelete: _onDelete, isUpcomingView = false, }) => { const { t } = useTranslation(); void _onToggleToday; void _onEdit; void _onDelete; const SubtasksToggleButton = () => { if (!hasSubtasks || !onSubtasksToggle) return null; return ( ); }; const formatDueDate = (dueDate: string) => { const today = new Date().toISOString().split('T')[0]; const tomorrow = new Date(Date.now() + 24 * 60 * 60 * 1000) .toISOString() .split('T')[0]; const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000) .toISOString() .split('T')[0]; if (dueDate === today) return t('dateIndicators.today', 'TODAY'); if (dueDate === tomorrow) return t('dateIndicators.tomorrow', 'TOMORROW'); if (dueDate === yesterday) return t('dateIndicators.yesterday', 'YESTERDAY'); const date = parseDateString(dueDate); if (!date) return dueDate; return date.toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric', }); }; const formatDeferUntil = (deferUntil: string): string | null => { const date = new Date(deferUntil); if (Number.isNaN(date.getTime())) { return null; } const datePart = date.toLocaleDateString(undefined, { weekday: 'short', month: 'short', day: 'numeric', }); const timePart = date.toLocaleTimeString(undefined, { hour: 'numeric', minute: '2-digit', }); return `${datePart} • ${timePart}`; }; const formatRecurrence = (recurrenceType: string) => { switch (recurrenceType) { case 'daily': return t('recurrence.daily', 'Daily'); case 'weekly': return t('recurrence.weekly', 'Weekly'); case 'monthly': return t('recurrence.monthly', 'Monthly'); case 'monthly_weekday': return t('recurrence.monthlyWeekday', 'Monthly'); case 'monthly_last_day': return t('recurrence.monthlyLastDay', 'Monthly'); default: return t('recurrence.recurring', 'Recurring'); } }; const formattedDeferUntil = task.defer_until ? formatDeferUntil(task.defer_until) : null; // Check if task has metadata (project, tags, due_date, completed_at, recurrence_type, recurring_parent_id, or defer_until) const hasMetadata = (project && !hideProjectName) || (task.tags && task.tags.length > 0) || task.due_date || (isTaskCompleted(task.status) && task.completed_at) || (task.recurrence_type && task.recurrence_type !== 'none') || task.recurring_parent_id || !!formattedDeferUntil; return (
{ e.preventDefault(); e.stopPropagation(); onTaskClick(e); }} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); e.stopPropagation(); onTaskClick(e as any); } }} > {/* Full view (md and larger) */}
{isUpcomingView ? (
{/* Full width title that wraps */}
{task.habit_mode && ( )} {task.original_name || task.name}
{/* Show project and tags info in upcoming view */} {project && !hideProjectName && (
{ // Prevent navigation if we're already on this project's page if ( window.location.pathname === `/project/${project.id}` ) { e.preventDefault(); } e.stopPropagation(); }} > {project.name}
)} {task.tags && task.tags.length > 0 && (
{task.tags.map((tag, index) => ( e.stopPropagation() } > {tag.name} {index < task.tags!.length - 1 && ', '} ))}
)}
) : (
{task.habit_mode && ( )} {task.original_name || task.name}
)} {/* Project, tags, due date, and recurrence in same row, with spacing when they exist */} {!isUpcomingView && (
{project && !hideProjectName && (
{ // Prevent navigation if we're already on this project's page if ( window.location.pathname === `/project/${project.id}` ) { e.preventDefault(); } e.stopPropagation(); }} > {project.name}
)} {task.tags && task.tags.length > 0 && (
{task.tags.map((tag, index) => ( e.stopPropagation() } > {tag.name} {index < task.tags!.length - 1 && ', '} ))}
)} {task.due_date && (
{formatDueDate(task.due_date)}
)} {isTaskCompleted(task.status) && task.completed_at && (
{formatDueDate( task.completed_at.split( 'T' )[0] )}
)} {task.recurrence_type && task.recurrence_type !== 'none' && (
{formatRecurrence( task.recurrence_type )}
)} {task.recurring_parent_id && (
{t( 'recurrence.instance', 'Recurring task instance' )}
)} {formattedDeferUntil && (
{formattedDeferUntil}
)}
)}
{!isUpcomingView && !task.habit_mode && onToggleCompletion && (
)}
{/* Mobile view (below md breakpoint) */}
{/* Priority Icon - Centered vertically with entire card */}
{/* Task content - full width */}
{/* Task Title */}
{task.habit_mode && ( )} {task.original_name || task.name}
{/* Project, tags, due date, and recurrence */}
{project && !hideProjectName && (
{ // Prevent navigation if we're already on this project's page if ( window.location.pathname === `/project/${project.id}` ) { e.preventDefault(); } e.stopPropagation(); }} > {project.name}
)} {task.tags && task.tags.length > 0 && (
{task.tags.map((tag, index) => ( e.stopPropagation() } > {tag.name} {index < task.tags!.length - 1 && ', '} ))}
)} {!isUpcomingView && task.due_date && (
{formatDueDate(task.due_date)}
)} {isTaskCompleted(task.status) && task.completed_at && (
{formatDueDate( task.completed_at.split('T')[0] )}
)} {task.recurrence_type && task.recurrence_type !== 'none' && (
{formatRecurrence( task.recurrence_type )}
)} {task.recurring_parent_id && (
{t( 'recurrence.instance', 'Recurring task instance' )}
)} {formattedDeferUntil && (
{formattedDeferUntil}
)}
{onToggleCompletion && (
)}
); }; // Subtasks Display Component interface SubtasksDisplayProps { showSubtasks: boolean; loadingSubtasks: boolean; subtasks: Task[]; onTaskClick: (e: React.MouseEvent, task: Task) => void; } const SubtasksDisplay: React.FC = ({ showSubtasks, loadingSubtasks, subtasks, onTaskClick, }) => { const { t } = useTranslation(); if (!showSubtasks) return null; return (
{loadingSubtasks ? (
{t('loading.subtasks', 'Loading subtasks...')}
) : subtasks.length > 0 ? ( subtasks.map((subtask) => (
{ e.stopPropagation(); onTaskClick(e, subtask); }} >
{/* Left side - Task info */}
{subtask.original_name || subtask.name}
{/* Right side - Status indicator */}
{isTaskCompleted(subtask.status) ? ( ) : null}
)) ) : (
{t('subtasks.noSubtasks', 'No subtasks found')}
)}
); }; // TaskWithSubtasks Component that combines both interface TaskWithSubtasksProps extends TaskHeaderProps { onSubtaskClick?: (subtask: Task) => void; } const TaskWithSubtasks: React.FC = (props) => { const [showSubtasks, setShowSubtasks] = useState(false); const [subtasks, setSubtasks] = useState([]); const [loadingSubtasks, setLoadingSubtasks] = useState(false); const loadSubtasks = useCallback(async () => { if (!props.task.uid) return; setLoadingSubtasks(true); try { const subtasksData = await fetchSubtasks(props.task.uid); setSubtasks(subtasksData); setShowSubtasks(subtasksData.length > 0); } catch (error) { console.error('Failed to load subtasks:', error); setSubtasks([]); setShowSubtasks(false); } finally { setLoadingSubtasks(false); } }, [props.task.id]); useEffect(() => { const subtasksData = props.task.subtasks || []; const hasSubtasksFromData = subtasksData.length > 0; setSubtasks(subtasksData); setShowSubtasks(hasSubtasksFromData); if (!hasSubtasksFromData) { void loadSubtasks(); } }, [props.task.id, props.task.subtasks, loadSubtasks]); return ( <> 0 || loadingSubtasks} onSubtasksToggle={(e) => { e.stopPropagation(); setShowSubtasks((prev) => !prev); }} /> { e.stopPropagation(); // Call the parent's onSubtaskClick handler if provided if (props.onSubtaskClick) { props.onSubtaskClick(task); } }} /> ); }; export { TaskWithSubtasks }; export default React.memo(TaskHeader);