import { useState } from "react"; import conferencesData from "@/data/conferences.yml"; import { Conference } from "@/types/conference"; import { Calendar as CalendarIcon, Tag, CircleDot } from "lucide-react"; import { Calendar } from "@/components/ui/calendar"; import { parseISO, format, isValid, startOfMonth, endOfMonth, isSameMonth } from "date-fns"; const CalendarPage = () => { const [selectedDate, setSelectedDate] = useState(new Date()); // Helper function to safely parse dates const safeParseISO = (dateString: string | undefined | number): Date | null => { if (!dateString || dateString === 'TBD') return null; // Convert to string if it's a number const dateStr = typeof dateString === 'number' ? dateString.toString() : dateString; try { // Try to parse the date, handling different formats const normalizedDate = dateStr.replace(/(\d{4})-(\d{1})-(\d{1,2})/, '$1-0$2-$3') .replace(/(\d{4})-(\d{2})-(\d{1})/, '$1-$2-0$3'); const parsedDate = parseISO(normalizedDate); return isValid(parsedDate) ? parsedDate : null; } catch (error) { console.error("Error parsing date:", dateString); return null; } }; // Get all events (conferences and deadlines) for a given month const getMonthEvents = (date: Date) => { return conferencesData.filter((conf: Conference) => { const deadlineDate = safeParseISO(conf.deadline); const startDate = safeParseISO(conf.start); return (deadlineDate && isSameMonth(deadlineDate, date)) || (startDate && isSameMonth(startDate, date)); }); }; // Get all unique dates (deadlines and conference dates) const getDatesWithEvents = () => { const dates = { conferences: new Set(), deadlines: new Set() }; conferencesData.forEach((conf: Conference) => { const deadlineDate = safeParseISO(conf.deadline); const startDate = safeParseISO(conf.start); const endDate = safeParseISO(conf.end); if (deadlineDate) { dates.deadlines.add(format(deadlineDate, 'yyyy-MM-dd')); } // If conference has both start and end dates, add all dates in between if (startDate && endDate) { let currentDate = startDate; while (currentDate <= endDate) { dates.conferences.add(format(currentDate, 'yyyy-MM-dd')); currentDate = new Date(currentDate.setDate(currentDate.getDate() + 1)); } } else if (startDate) { // If only start date is available, add just that date dates.conferences.add(format(startDate, 'yyyy-MM-dd')); } }); return { conferences: Array.from(dates.conferences).map(date => parseISO(date)), deadlines: Array.from(dates.deadlines).map(date => parseISO(date)) }; }; // Get conferences for selected date const getConferencesForDate = (date: Date) => { const formattedDate = format(date, 'yyyy-MM-dd'); return conferencesData.filter((conf: Conference) => { const deadlineDate = safeParseISO(conf.deadline); const startDate = safeParseISO(conf.start); const endDate = safeParseISO(conf.end); const deadlineDateStr = deadlineDate ? format(deadlineDate, 'yyyy-MM-dd') : null; const isDeadlineMatch = deadlineDateStr === formattedDate; // Check if the date falls within the conference duration let isConferenceDate = false; if (startDate && endDate) { isConferenceDate = date >= startDate && date <= endDate; } else if (startDate) { isConferenceDate = format(startDate, 'yyyy-MM-dd') === formattedDate; } return isDeadlineMatch || isConferenceDate; }); }; const monthEvents = selectedDate ? getMonthEvents(selectedDate) : []; const datesWithEvents = getDatesWithEvents(); return (

Calendar Overview

{/* Color Legend */}
Conference Dates
Submission Deadlines
{/* Month Events */} {selectedDate && (

Events in {format(selectedDate, 'MMMM yyyy')}

{monthEvents.length === 0 ? (

No events this month.

) : (
{monthEvents.map((conf: Conference) => (

{conf.title}

{conf.deadline && safeParseISO(conf.deadline) && isSameMonth(safeParseISO(conf.deadline)!, selectedDate) && (

Submission Deadline: {format(safeParseISO(conf.deadline)!, 'MMMM d, yyyy')}

)} {conf.start && (

Conference Date: {format(safeParseISO(conf.start)!, 'MMMM d')} {conf.end ? ` - ${format(safeParseISO(conf.end)!, 'MMMM d, yyyy')}` : `, ${format(safeParseISO(conf.start)!, 'yyyy')}`}

)}
{conf.tags.map((tag) => ( {tag} ))}
))}
)}
)}
); }; export default CalendarPage;