import React, { useEffect, useState, useRef } from 'react'; import { usePlanning } from '@/context/PlanningContext'; import { format, startOfWeek, addDays, subDays, isSameDay, isToday, } from 'date-fns'; import { fr } from 'date-fns/locale'; import { getWeekEvents } from '@/utils/events'; import { CalendarDays, ChevronLeft, ChevronRight, Plus } from 'lucide-react'; const DayView = ({ onDateClick, onEventClick, events, onOpenDrawer }) => { const { currentDate, setCurrentDate, parentView, schedules } = usePlanning(); const [currentTime, setCurrentTime] = useState(new Date()); const scrollRef = useRef(null); const timeSlots = Array.from({ length: 24 }, (_, i) => i); const weekStart = startOfWeek(currentDate, { weekStartsOn: 1 }); const weekDays = Array.from({ length: 7 }, (_, i) => addDays(weekStart, i)); const isCurrentDay = isSameDay(currentDate, new Date()); const dayEvents = getWeekEvents(currentDate, events) || []; useEffect(() => { const interval = setInterval(() => setCurrentTime(new Date()), 60000); return () => clearInterval(interval); }, []); useEffect(() => { if (scrollRef.current && isCurrentDay) { const currentHour = new Date().getHours(); setTimeout(() => { scrollRef.current.scrollTop = currentHour * 80 - 200; }, 0); } }, [currentDate, isCurrentDay]); const getCurrentTimePosition = () => { const hours = currentTime.getHours(); const minutes = currentTime.getMinutes(); return `${(hours + minutes / 60) * 5}rem`; }; const getScheduleColor = (event) => { const schedule = schedules?.find( (item) => Number(item.id) === Number(event.planning) ); return schedule?.color || event.color || '#6B7280'; }; const getScheduleClassLevelLabel = (event) => { const schedule = schedules?.find( (item) => Number(item.id) === Number(event.planning) ); const scheduleName = schedule?.name || ''; if (!scheduleName) return ''; return scheduleName; }; const calculateEventStyle = (event, allDayEvents) => { const start = new Date(event.start); const end = new Date(event.end); const startMinutes = (start.getMinutes() / 60) * 5; const duration = ((end - start) / (1000 * 60 * 60)) * 5; const scheduleColor = getScheduleColor(event); const overlapping = allDayEvents.filter((other) => { if (other.id === event.id) return false; const oStart = new Date(other.start); const oEnd = new Date(other.end); return !(oEnd <= start || oStart >= end); }); const eventIndex = overlapping.findIndex((e) => e.id > event.id) + 1; const total = overlapping.length + 1; return { height: `${Math.max(duration, 1.5)}rem`, position: 'absolute', width: `calc((100% / ${total}) - 4px)`, left: `calc((100% / ${total}) * ${eventIndex})`, backgroundColor: `${event.color}15`, borderLeft: `3px solid ${event.color}`, borderRadius: '0.25rem', zIndex: 1, transform: `translateY(${startMinutes}rem)`, }; }; return (