import React from "react"; import { CalendarDaysIcon, CalendarIcon, PlayIcon, ArrowPathIcon } from "@heroicons/react/24/outline"; import { TagIcon, FolderIcon } from "@heroicons/react/24/solid"; import { useTranslation } from "react-i18next"; import TaskPriorityIcon from "./TaskPriorityIcon"; import TaskTags from "./TaskTags"; import { Project } from "../../entities/Project"; import { Task, StatusType } from "../../entities/Task"; interface TaskHeaderProps { task: Task; project?: Project; onTaskClick: (e: React.MouseEvent) => void; onToggleCompletion?: () => void; hideProjectName?: boolean; onToggleToday?: (taskId: number) => Promise; onTaskUpdate?: (task: Task) => Promise; isOverdue?: boolean; } const TaskHeader: React.FC = ({ task, project, onTaskClick, onToggleCompletion, hideProjectName = false, onToggleToday, onTaskUpdate, isOverdue = false, }) => { const { t } = useTranslation(); 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'); return new Date(dueDate).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric', }); }; 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 handleTodayToggle = async (e: React.MouseEvent) => { e.stopPropagation(); // Prevent opening task modal if (onToggleToday && task.id) { try { await onToggleToday(task.id); } catch (error) { console.error('Failed to toggle today status:', error); } } }; const handlePlayToggle = async (e: React.MouseEvent) => { e.stopPropagation(); // Prevent opening task modal if (task.id && (task.status === 'not_started' || task.status === 'in_progress' || task.status === 0 || task.status === 1) && onTaskUpdate) { try { const isCurrentlyInProgress = task.status === 'in_progress' || task.status === 1; const updatedTask = { ...task, status: (isCurrentlyInProgress ? 'not_started' : 'in_progress') as StatusType, // Automatically add to today plan when setting to in_progress today: isCurrentlyInProgress ? task.today : true }; await onTaskUpdate(updatedTask); } catch (error) { console.error('Failed to toggle in progress status:', error); } } }; return (
{/* Full view (md and larger) */}
{task.name} {isOverdue && ( overdue )}
{/* Project, tags, due date, and recurrence in same row, with spacing when they exist */}
{project && !hideProjectName && (
{project.name}
)} {project && !hideProjectName && task.tags && task.tags.length > 0 && ( )} {task.tags && task.tags.length > 0 && (
{task.tags.map(tag => tag.name).join(', ')}
)} {((project && !hideProjectName) || (task.tags && task.tags.length > 0)) && task.due_date && ( )} {task.due_date && (
{formatDueDate(task.due_date)}
)} {((project && !hideProjectName) || (task.tags && task.tags.length > 0) || task.due_date) && task.recurrence_type && task.recurrence_type !== 'none' && ( )} {task.recurrence_type && task.recurrence_type !== 'none' && (
{formatRecurrence(task.recurrence_type)}
)}
{/* Today Plan Controls */} {onToggleToday && ( )} {/* Play/In Progress Controls */} {(task.status === 'not_started' || task.status === 'in_progress' || task.status === 0 || task.status === 1) && ( )}
{/* Mobile view (below md breakpoint) */}
{/* Task Name with Priority Icon and Project Name */}
{/* Priority Icon */} {/* Task Title and Project Name */}
{/* Task Title */}
{task.name} {isOverdue && ( overdue )}
{/* Project, tags, due date, and recurrence - each on separate lines */}
{project && !hideProjectName && (
{project.name}
)} {task.tags && task.tags.length > 0 && (
{task.tags.map(tag => tag.name).join(', ')}
)} {task.due_date && (
{formatDueDate(task.due_date)}
)} {task.recurrence_type && task.recurrence_type !== 'none' && (
{formatRecurrence(task.recurrence_type)}
)}
{/* Mobile badges row */}
{/* Play/In Progress Controls - Mobile */} {(task.status === 'not_started' || task.status === 'in_progress' || task.status === 0 || task.status === 1) && ( )} {/* Today Plan Controls - Mobile */} {onToggleToday && ( )}
); }; export default TaskHeader;