import { useState } from 'react' import { ChevronLeft, ChevronRight, Calendar as CalendarIcon, CalendarDays } from 'lucide-react' import { PRIORITY_CONFIG } from '../utils/api' import { useLanguage } from '../i18n/LanguageContext' const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] function getMonthData(year, month) { const firstDay = new Date(year, month, 1).getDay() const daysInMonth = new Date(year, month + 1, 0).getDate() const prevDays = new Date(year, month, 0).getDate() const cells = [] // Previous month trailing days for (let i = firstDay - 1; i >= 0; i--) { cells.push({ day: prevDays - i, current: false, date: new Date(year, month - 1, prevDays - i) }) } // Current month for (let d = 1; d <= daysInMonth; d++) { cells.push({ day: d, current: true, date: new Date(year, month, d) }) } // Next month leading days const remaining = 42 - cells.length for (let d = 1; d <= remaining; d++) { cells.push({ day: d, current: false, date: new Date(year, month + 1, d) }) } return cells } function getWeekData(startDate) { const cells = [] const start = new Date(startDate) start.setDate(start.getDate() - start.getDay()) for (let i = 0; i < 7; i++) { const d = new Date(start) d.setDate(start.getDate() + i) cells.push({ day: d.getDate(), current: true, date: d }) } return cells } function dateKey(d) { return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` } export default function TaskCalendarView({ tasks, onTaskClick }) { const { t } = useLanguage() const today = new Date() const [year, setYear] = useState(today.getFullYear()) const [month, setMonth] = useState(today.getMonth()) const [calView, setCalView] = useState('month') const [weekStart, setWeekStart] = useState(() => { const d = new Date(); d.setDate(d.getDate() - d.getDay()); return d }) const cells = calView === 'month' ? getMonthData(year, month) : getWeekData(weekStart) const todayKey = dateKey(today) // Group tasks by due_date const tasksByDate = {} const unscheduled = [] for (const task of tasks) { const dd = task.due_date || task.dueDate if (dd) { const key = dd.slice(0, 10) // yyyy-mm-dd if (!tasksByDate[key]) tasksByDate[key] = [] tasksByDate[key].push(task) } else { unscheduled.push(task) } } const prevMonth = () => { if (month === 0) { setMonth(11); setYear(y => y - 1) } else setMonth(m => m - 1) } const nextMonth = () => { if (month === 11) { setMonth(0); setYear(y => y + 1) } else setMonth(m => m + 1) } const prevWeek = () => setWeekStart(d => { const n = new Date(d); n.setDate(n.getDate() - 7); return n }) const nextWeek = () => setWeekStart(d => { const n = new Date(d); n.setDate(n.getDate() + 7); return n }) const goToday = () => { setYear(today.getFullYear()); setMonth(today.getMonth()) const d = new Date(); d.setDate(d.getDate() - d.getDay()); setWeekStart(d) } const monthLabel = new Date(year, month).toLocaleString('default', { month: 'long', year: 'numeric' }) const weekLabel = (() => { const start = new Date(weekStart) start.setDate(start.getDate() - start.getDay()) const end = new Date(start); end.setDate(start.getDate() + 6) const fmt = (d) => d.toLocaleString('default', { month: 'short', day: 'numeric' }) return `${fmt(start)} – ${fmt(end)}, ${end.getFullYear()}` })() const getPillColor = (task) => { const p = task.priority || 'medium' if (p === 'urgent') return 'bg-red-500 text-white' if (p === 'high') return 'bg-orange-400 text-white' if (p === 'medium') return 'bg-amber-400 text-amber-900' return 'bg-gray-300 text-text-secondary' } return (
{/* Calendar grid */}
{/* Nav */}

{calView === 'month' ? monthLabel : weekLabel}

{/* Day headers */}
{DAYS.map(d => (
{d}
))}
{/* Cells */}
{cells.map((cell, i) => { const key = dateKey(cell.date) const isToday = key === todayKey const dayTasks = tasksByDate[key] || [] return (
{cell.day}
{dayTasks.slice(0, calView === 'week' ? 10 : 3).map(task => ( ))} {dayTasks.length > (calView === 'week' ? 10 : 3) && (
+{dayTasks.length - (calView === 'week' ? 10 : 3)} more
)}
) })}
{/* Unscheduled sidebar */} {unscheduled.length > 0 && (

{t('tasks.unscheduled')}

{unscheduled.map(task => { const priority = PRIORITY_CONFIG[task.priority] || PRIORITY_CONFIG.medium return ( ) })}
)}
) }