import { useState } from 'react' const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] function CalendarView() { const today = new Date() const [viewMode, setViewMode] = useState('month') const [cursorDate, setCursorDate] = useState(new Date(today.getFullYear(), today.getMonth(), 1)) const monthLabel = cursorDate.toLocaleString('en-US', { month: 'long', year: 'numeric' }) const year = cursorDate.getFullYear() const month = cursorDate.getMonth() const firstDay = new Date(year, month, 1) const lastDay = new Date(year, month + 1, 0) const leadingDays = firstDay.getDay() const totalDays = lastDay.getDate() const previousMonthTotal = new Date(year, month, 0).getDate() const monthGrid = Array.from({ length: 42 }, (_, index) => { const dayNumber = index - leadingDays + 1 const isPreviousMonth = dayNumber < 1 const isCurrentMonth = dayNumber >= 1 && dayNumber <= totalDays const isNextMonth = dayNumber > totalDays const displayDay = isPreviousMonth ? previousMonthTotal + dayNumber : isNextMonth ? dayNumber - totalDays : dayNumber const date = isCurrentMonth ? new Date(year, month, dayNumber) : null const isToday = date && date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear() return { displayDay, isCurrentMonth, isToday, } }) const startOfWeek = new Date(today) startOfWeek.setDate(today.getDate() - today.getDay()) const weekGrid = Array.from({ length: 7 }, (_, index) => { const date = new Date(startOfWeek) date.setDate(startOfWeek.getDate() + index) return { label: weekDays[index], day: date.getDate(), month: date.toLocaleString('en-US', { month: 'short' }), isToday: date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear(), } }) const yearGrid = Array.from({ length: 12 }, (_, monthIndex) => { const date = new Date(year, monthIndex, 1) return { month: monthIndex, label: date.toLocaleString('en-US', { month: 'long' }), } }) const handlePrevious = () => { if (viewMode === 'year') { setCursorDate(new Date(cursorDate.getFullYear() - 1, cursorDate.getMonth(), 1)) return } if (viewMode === 'week') { const date = new Date(cursorDate) date.setDate(cursorDate.getDate() - 7) setCursorDate(date) return } setCursorDate(new Date(cursorDate.getFullYear(), cursorDate.getMonth() - 1, 1)) } const handleNext = () => { if (viewMode === 'year') { setCursorDate(new Date(cursorDate.getFullYear() + 1, cursorDate.getMonth(), 1)) return } if (viewMode === 'week') { const date = new Date(cursorDate) date.setDate(cursorDate.getDate() + 7) setCursorDate(date) return } setCursorDate(new Date(cursorDate.getFullYear(), cursorDate.getMonth() + 1, 1)) } return (

Calendar

{viewMode === 'year' ? cursorDate.getFullYear() : monthLabel}

Switch between month, year, and week views.

{viewMode === 'month' ? (
{weekDays.map((label) => (
{label}
))} {monthGrid.map((day, index) => (
{day.displayDay}
))}
) : null} {viewMode === 'week' ? (
{weekGrid.map((day) => (
{day.label} {day.day}

{day.month}

))}
) : null} {viewMode === 'year' ? (
{yearGrid.map((month) => (
{month.label} {cursorDate.getFullYear()}
))}
) : null}
) } export default CalendarView