import { useState, useEffect, useRef } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { Clock, MapPin, Users, ChevronLeft, ChevronRight, ChevronDown } from 'lucide-react'; import { schedules } from '../../data/dummy'; function Schedule() { const [currentDate, setCurrentDate] = useState(new Date()); const [selectedDate, setSelectedDate] = useState(null); const [showYearMonthPicker, setShowYearMonthPicker] = useState(false); const [viewMode, setViewMode] = useState('yearMonth'); // 'yearMonth' | 'months' const [slideDirection, setSlideDirection] = useState(0); // -1: prev, 1: next const pickerRef = useRef(null); // 외부 클릭시 팝업 닫기 useEffect(() => { const handleClickOutside = (event) => { if (pickerRef.current && !pickerRef.current.contains(event.target)) { setShowYearMonthPicker(false); setViewMode('yearMonth'); } }; if (showYearMonthPicker) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [showYearMonthPicker]); // 달력 관련 함수 const getDaysInMonth = (year, month) => new Date(year, month + 1, 0).getDate(); const getFirstDayOfMonth = (year, month) => new Date(year, month, 1).getDay(); const year = currentDate.getFullYear(); const month = currentDate.getMonth(); const daysInMonth = getDaysInMonth(year, month); const firstDay = getFirstDayOfMonth(year, month); const days = ['일', '월', '화', '수', '목', '금', '토']; // 스케줄이 있는 날짜 목록 const scheduleDates = schedules.map(s => s.date); const hasSchedule = (day) => { const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; return scheduleDates.includes(dateStr); }; const prevMonth = () => { setSlideDirection(-1); setCurrentDate(new Date(year, month - 1, 1)); }; const nextMonth = () => { setSlideDirection(1); setCurrentDate(new Date(year, month + 1, 1)); }; const selectDate = (day) => { const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; setSelectedDate(selectedDate === dateStr ? null : dateStr); }; // 년도 선택 시 월 선택 모드로 전환 const selectYear = (newYear) => { setCurrentDate(new Date(newYear, month, 1)); setViewMode('months'); }; // 월 선택 시 적용 후 닫기 const selectMonth = (newMonth) => { setCurrentDate(new Date(year, newMonth, 1)); setShowYearMonthPicker(false); setViewMode('yearMonth'); }; // 필터링된 스케줄 const filteredSchedules = selectedDate ? schedules.filter(s => s.date === selectedDate) : schedules; const formatDate = (dateStr) => { const date = new Date(dateStr); const dayNames = ['일', '월', '화', '수', '목', '금', '토']; return { month: date.getMonth() + 1, day: date.getDate(), weekday: dayNames[date.getDay()], }; }; // 년도 범위 (현재 년도 기준 10년 단위) const startYear = Math.floor(year / 10) * 10 - 1; const yearRange = Array.from({ length: 12 }, (_, i) => startYear + i); // 현재 년도/월 확인 함수 const isCurrentYear = (y) => new Date().getFullYear() === y; const isCurrentMonth = (m) => { const today = new Date(); return today.getFullYear() === year && today.getMonth() === m; }; // 월 배열 const monthNames = ['1월', '2월', '3월', '4월', '5월', '6월', '7월', '8월', '9월', '10월', '11월', '12월']; // 년도 범위 이동 const prevYearRange = () => setCurrentDate(new Date(year - 10, month, 1)); const nextYearRange = () => setCurrentDate(new Date(year + 10, month, 1)); return (
{/* 헤더 */}
일정 프로미스나인의 다가오는 일정을 확인하세요
{/* 달력 - 더 큰 사이즈 */}
{/* 달력 헤더 */}
{/* 년/월 선택 팝업 - 달력 카드 중앙 정렬 */} {showYearMonthPicker && ( {/* 헤더 - 년도 범위 이동 */}
{viewMode === 'yearMonth' ? `${yearRange[0]} - ${yearRange[yearRange.length - 1]}` : `${year}년`}
{viewMode === 'yearMonth' && ( {/* 년도 선택 */}
년도
{yearRange.map((y) => ( ))}
{/* 월 선택 */}
{monthNames.map((m, i) => ( ))}
)} {viewMode === 'months' && ( {/* 월 선택 */}
월 선택
{monthNames.map((m, i) => ( ))}
)}
)}
{/* 요일 헤더 + 날짜 그리드 (함께 슬라이드) */} {/* 요일 헤더 */}
{days.map((day, i) => (
{day}
))}
{/* 날짜 그리드 */}
{/* 전달 날짜 */} {Array.from({ length: firstDay }).map((_, i) => { const prevMonthDays = getDaysInMonth(year, month - 1); const day = prevMonthDays - firstDay + i + 1; return (
{day}
); })} {/* 현재 달 날짜 */} {Array.from({ length: daysInMonth }).map((_, i) => { const day = i + 1; const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`; const isSelected = selectedDate === dateStr; const hasEvent = hasSchedule(day); const dayOfWeek = (firstDay + i) % 7; const isToday = new Date().toDateString() === new Date(year, month, day).toDateString(); return ( ); })} {/* 다음달 날짜 (마지막 주만 채우기) */} {(() => { const totalCells = firstDay + daysInMonth; const remainder = totalCells % 7; const nextDays = remainder === 0 ? 0 : 7 - remainder; return Array.from({ length: nextDays }).map((_, i) => (
{i + 1}
)); })()}
{/* 범례 및 전체보기 */}
일정 있음
{/* 스케줄 리스트 */}
{filteredSchedules.length > 0 ? ( filteredSchedules.map((schedule, index) => { const formatted = formatDate(schedule.date); return ( {/* 날짜 영역 */}
{formatted.month}월 {formatted.day} {formatted.weekday}
{/* 스케줄 내용 */}

{schedule.title}

{schedule.time}
{schedule.platform}
{schedule.members.join(', ')}
); }) ) : (
{selectedDate ? '선택한 날짜에 일정이 없습니다.' : '예정된 일정이 없습니다.'}
)}
); } export default Schedule;