import { useState, useEffect } from 'react' import { ChevronLeft, ChevronRight, Loader2 } from 'lucide-react' import { useDeadlines } from '../../hooks/useTasks'; const DAY_LABELS = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'] const MONTH_NAMES = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ] function getDaysInMonth(year: number, month: number) { return new Date(year, month + 1, 0).getDate() } function getFirstDayOfMonth(year: number, month: number) { return new Date(year, month, 1).getDay() } const formatDate = (dateInput: Date | string) => new Date(dateInput).toLocaleDateString('id-ID', { day: 'numeric', month: 'long', year: 'numeric' }); export default function HomeCalendar() { const [today, setToday] = useState(new Date()); const { deadlines, loading } = useDeadlines(); useEffect(() => { const interval = setInterval(() => { setToday(new Date()) }, 60 * 1000) return () => clearInterval(interval) }, []) const [currentYear, setCurrentYear] = useState(today.getFullYear()) const [currentMonth, setCurrentMonth] = useState(today.getMonth()) const daysInMonth = getDaysInMonth(currentYear, currentMonth) const firstDay = getFirstDayOfMonth(currentYear, currentMonth) const prevMonth = () => { if (currentMonth === 0) { setCurrentMonth(11) setCurrentYear(y => y - 1) } else { setCurrentMonth(m => m - 1) } } const nextMonth = () => { if (currentMonth === 11) { setCurrentMonth(0) setCurrentYear(y => y + 1) } else { setCurrentMonth(m => m + 1) } } const isToday = (day: number) => day === today.getDate() && currentMonth === today.getMonth() && currentYear === today.getFullYear() const hasDeadline = (day: number) => deadlines.some(d => d.date.getDate() === day && d.date.getMonth() === currentMonth && d.date.getFullYear() === currentYear ) const upcomingDeadlines = deadlines.filter(d => { return ( d.date.getMonth() === currentMonth && d.date.getFullYear() === currentYear ); }).sort((a, b) => a.date.getTime() - b.date.getTime()); // Previous month trailing days const prevMonthDays = getDaysInMonth(currentYear, currentMonth - 1 < 0 ? 11 : currentMonth - 1) const trailingDays = Array.from({ length: firstDay }, (_, i) => prevMonthDays - firstDay + 1 + i) // Current month days const currentDays = Array.from({ length: daysInMonth }, (_, i) => i + 1) // Next month leading days const totalCells = Math.ceil((firstDay + daysInMonth) / 7) * 7 const leadingDays = Array.from({ length: totalCells - firstDay - daysInMonth }, (_, i) => i + 1) // Tanggal Akhir Pekan const isBold = (day: number) => { const d = new Date(currentYear, currentMonth, day).getDay() return d === 0 || d === 6 } if (loading) { return (
Upcoming Deadlines
{upcomingDeadlines.length > 0 ? (No upcoming deadlines this month.
)}