'use client' import React, { useState, useEffect, useMemo, useRef } from 'react' import { CaretLeft, CaretRight, Plus, MagnifyingGlass, CalendarBlank, List, Check } from '@phosphor-icons/react' import { motion, AnimatePresence } from 'framer-motion' import { useKV } from '../hooks/useKV' import Window from './Window' import { recurringHolidays, getVariableHolidays, Holiday } from '../data/holidays' interface CalendarProps { onClose: () => void onMinimize?: () => void onMaximize?: () => void onFocus?: () => void zIndex?: number } interface CalendarEvent extends Holiday { isCustom?: boolean } type ViewMode = 'day' | 'week' | 'month' | 'year' export function Calendar({ onClose, onMinimize, onMaximize, onFocus, zIndex }: CalendarProps) { const [windowSize, setWindowSize] = useState({ width: 1100, height: 750 }) const [currentDate, setCurrentDate] = useState(new Date()) const [view, setView] = useState('month') const [customEvents, setCustomEvents] = useKV('custom-calendar-events', []) const [searchQuery, setSearchQuery] = useState('') const [isSearchOpen, setIsSearchOpen] = useState(false) const scrollContainerRef = useRef(null) const [sidebarOpen, setSidebarOpen] = useState(true) // Handle window resize useEffect(() => { const handleResize = () => { const isMobile = window.innerWidth < 768 setWindowSize({ width: Math.min(1100, window.innerWidth - (isMobile ? 20 : 40)), height: Math.min(750, window.innerHeight - (isMobile ? 20 : 40)) }) // Auto-close sidebar on mobile if (isMobile && sidebarOpen) { setSidebarOpen(false) } } handleResize() // Set initial size window.addEventListener('resize', handleResize) return () => window.removeEventListener('resize', handleResize) }, [sidebarOpen]) // Scroll to 8 AM on mount for day/week views useEffect(() => { if ((view === 'day' || view === 'week') && scrollContainerRef.current) { scrollContainerRef.current.scrollTop = 8 * 60 } }, [view]) // Generate all events const allEvents = useMemo(() => { const year = currentDate.getFullYear() const years = [year - 1, year, year + 1] let events: CalendarEvent[] = [...customEvents] years.forEach(y => { const variable = getVariableHolidays(y) const recurring = recurringHolidays.map(h => ({ ...h, date: `${y}-${h.date}` })) events = [...events, ...variable, ...recurring] }) return events }, [currentDate.getFullYear(), customEvents]) const monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] const hours = Array.from({ length: 24 }, (_, i) => i) const getDaysInMonth = (year: number, month: number) => { const firstDay = new Date(year, month, 1) const lastDay = new Date(year, month + 1, 0) const daysInMonth = lastDay.getDate() const startingDayOfWeek = firstDay.getDay() const prevMonthDays = [] const prevMonthLastDay = new Date(year, month, 0).getDate() for (let i = startingDayOfWeek - 1; i >= 0; i--) { prevMonthDays.push({ day: prevMonthLastDay - i, month: month - 1, year: year, isCurrentMonth: false }) } const currentMonthDays = [] for (let i = 1; i <= daysInMonth; i++) { currentMonthDays.push({ day: i, month: month, year: year, isCurrentMonth: true }) } const nextMonthDays = [] const totalDaysSoFar = prevMonthDays.length + currentMonthDays.length const remainingCells = 42 - totalDaysSoFar for (let i = 1; i <= remainingCells; i++) { nextMonthDays.push({ day: i, month: month + 1, year: year, isCurrentMonth: false }) } return [...prevMonthDays, ...currentMonthDays, ...nextMonthDays] } const calendarDays = useMemo(() => { return getDaysInMonth(currentDate.getFullYear(), currentDate.getMonth()) }, [currentDate]) const getEventsForDate = (dateStr: string) => { return allEvents.filter(e => e.date === dateStr) } const isToday = (day: number, month: number, year: number) => { const today = new Date() return day === today.getDate() && month === today.getMonth() && year === today.getFullYear() } const handlePrev = () => { const newDate = new Date(currentDate) if (view === 'month') newDate.setMonth(newDate.getMonth() - 1) else if (view === 'year') newDate.setFullYear(newDate.getFullYear() - 1) else if (view === 'week') newDate.setDate(newDate.getDate() - 7) else if (view === 'day') newDate.setDate(newDate.getDate() - 1) setCurrentDate(newDate) } const handleNext = () => { const newDate = new Date(currentDate) if (view === 'month') newDate.setMonth(newDate.getMonth() + 1) else if (view === 'year') newDate.setFullYear(newDate.getFullYear() + 1) else if (view === 'week') newDate.setDate(newDate.getDate() + 7) else if (view === 'day') newDate.setDate(newDate.getDate() + 1) setCurrentDate(newDate) } const handleToday = () => setCurrentDate(new Date()) const getWeekDays = () => { const startOfWeek = new Date(currentDate) const day = startOfWeek.getDay() const diff = startOfWeek.getDate() - day startOfWeek.setDate(diff) const days = [] for (let i = 0; i < 7; i++) { const d = new Date(startOfWeek) d.setDate(startOfWeek.getDate() + i) days.push(d) } return days } const renderHeaderTitle = () => { if (view === 'year') return {currentDate.getFullYear()} if (view === 'day') { return (
{currentDate.getDate()} {monthNames[currentDate.getMonth()]} {currentDate.getFullYear()}
) } return {monthNames[currentDate.getMonth()]} {currentDate.getFullYear()} } return (
{/* Sidebar */} {sidebarOpen && (
{weekDays.map(d => (
{d.charAt(0)}
))} {calendarDays.map((d, i) => { if (!d.isCurrentMonth) return
{d.day}
const isCurrentDay = isToday(d.day, d.month, d.year) const isSelected = d.day === currentDate.getDate() && d.month === currentDate.getMonth() return (
setCurrentDate(new Date(d.year, d.month, d.day))} className={` text-xs w-6 h-6 flex items-center justify-center rounded-full mx-auto cursor-pointer transition-colors ${isCurrentDay ? 'bg-red-500 text-white font-bold' : isSelected ? 'bg-white/20 text-white' : 'text-gray-300 hover:bg-white/10'} `} > {d.day}
) })}
CALENDARS
{['Home', 'Work', 'Holidays', 'Birthdays'].map((cal, i) => (
{cal}
))}
)} {/* Main Content */}
{/* Toolbar */}
{renderHeaderTitle()}
{view === 'month' && `${monthNames[currentDate.getMonth()].substr(0, 3)} ${currentDate.getFullYear()}`} {view === 'year' && currentDate.getFullYear()} {view === 'day' && `${currentDate.getDate()} ${monthNames[currentDate.getMonth()].substr(0, 3)}`} {view === 'week' && 'Week View'}
{(['day', 'week', 'month', 'year'] as ViewMode[]).map((v) => ( ))}
{/* Mobile view selector */}
{/* View Content */}
{view === 'month' && (
{weekDays.map(day => (
{day} {day.charAt(0)}
))}
{calendarDays.map((dateObj, index) => { const dateStr = `${dateObj.year}-${String(dateObj.month + 1).padStart(2, '0')}-${String(dateObj.day).padStart(2, '0')}` const dayEvents = getEventsForDate(dateStr) const isCurrentDay = isToday(dateObj.day, dateObj.month, dateObj.year) return (
{ setCurrentDate(new Date(dateObj.year, dateObj.month, dateObj.day)) setView('day') }} >
{dateObj.day}
{dayEvents.slice(0, 3).map((event, i) => (
{event.title}
))} {dayEvents.length > 3 && (
+{dayEvents.length - 3} more
)}
) })}
)} {view === 'year' && (
{monthNames.map((month, monthIndex) => { const days = getDaysInMonth(currentDate.getFullYear(), monthIndex) return (

{month}

{weekDays.map(d => (
{d.charAt(0)}
))} {days.map((d, i) => { if (!d.isCurrentMonth) return
const isCurrentDay = isToday(d.day, d.month, d.year) return (
{ setCurrentDate(new Date(d.year, d.month, d.day)) setView('month') }} className={` text-xs w-6 h-6 flex items-center justify-center rounded-full mx-auto cursor-pointer hover:bg-white/10 ${isCurrentDay ? 'bg-red-500 text-white font-bold' : 'text-gray-300'} `} > {d.day}
) })}
) })}
)} {(view === 'week' || view === 'day') && (
{view === 'week' ? getWeekDays().map((d, i) => { const isCurrentDay = isToday(d.getDate(), d.getMonth(), d.getFullYear()) return (
{weekDays[d.getDay()]}
{d.getDate()}
) }) : (
{weekDays[currentDate.getDay()]}
{currentDate.getDate()}
)}
{hours.map((hour) => (
{hour === 0 ? '12 AM' : hour < 12 ? `${hour} AM` : hour === 12 ? '12 PM' : `${hour - 12} PM`}
{/* Time slots */}
))} {/* Current Time Indicator */}
)}
) }