import React, { useEffect, useState, useRef } from 'react'; import { usePlanning, PlanningModes } from '@/context/PlanningContext'; import { format, startOfWeek, addDays, isSameDay } from 'date-fns'; import { fr } from 'date-fns/locale'; import { getWeekEvents } from '@/utils/events'; import { isToday } from 'date-fns'; const WeekView = ({ onDateClick, onEventClick, events }) => { const { currentDate, planningMode, parentView } = usePlanning(); const [currentTime, setCurrentTime] = useState(new Date()); const scrollContainerRef = useRef(null); // Ajouter cette référence // Déplacer ces déclarations avant leur utilisation const timeSlots = Array.from({ length: 24 }, (_, i) => i); const weekStart = startOfWeek(currentDate, { weekStartsOn: 1 }); const weekDays = Array.from({ length: 7 }, (_, i) => addDays(weekStart, i)); // Maintenant on peut utiliser weekDays const isCurrentWeek = weekDays.some((day) => isSameDay(day, new Date())); // Mettre à jour la position de la ligne toutes les minutes useEffect(() => { const interval = setInterval(() => { setCurrentTime(new Date()); }, 60000); return () => clearInterval(interval); }, []); // Modifier l'useEffect pour l'auto-scroll useEffect(() => { if (scrollContainerRef.current && isCurrentWeek) { const currentHour = new Date().getHours(); const scrollPosition = currentHour * 80; // Ajout d'un délai pour laisser le temps au DOM de se mettre à jour setTimeout(() => { scrollContainerRef.current.scrollTop = scrollPosition - 200; }, 0); } }, [currentDate, isCurrentWeek]); // Ajout de currentDate dans les dépendances // Calculer la position de la ligne de temps const getCurrentTimePosition = () => { const hours = currentTime.getHours(); const minutes = currentTime.getMinutes(); const rowHeight = 5; // Hauteur des lignes en rem (h-20 = 5rem) return `${(hours + minutes / 60) * rowHeight}rem`; }; // Utiliser les événements déjà filtrés passés en props const weekEventsMap = weekDays.reduce((acc, day) => { acc[format(day, 'yyyy-MM-dd')] = getWeekEvents(day, events); return acc; }, {}); const isWeekend = (date) => { const day = date.getDay(); return day === 0 || day === 6; }; const findOverlappingEvents = (event, dayEvents) => { const eventStart = new Date(event.start); const eventEnd = new Date(event.end); return dayEvents.filter((otherEvent) => { if (otherEvent.id === event.id) return false; const otherStart = new Date(otherEvent.start); const otherEnd = new Date(otherEvent.end); return !(otherEnd <= eventStart || otherStart >= eventEnd); }); }; const calculateEventStyle = (event, dayEvents) => { 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; // Trouver les événements qui se chevauchent const overlappingEvents = findOverlappingEvents(event, dayEvents); const eventIndex = overlappingEvents.findIndex((e) => e.id > event.id) + 1; const totalOverlapping = overlappingEvents.length + 1; // Calculer la largeur et la position horizontale const width = `calc((100% / ${totalOverlapping}) - 4px)`; const left = `calc((100% / ${totalOverlapping}) * ${eventIndex})`; return { height: `${duration}rem`, position: 'absolute', width, left, backgroundColor: `${event.color}15`, borderLeft: `3px solid ${event.color}`, borderRadius: '0.25rem', zIndex: 1, transform: `translateY(${startMinutes}rem)`, }; }; const renderEventInCell = (event, dayEvents) => { const eventStyle = calculateEventStyle(event, dayEvents); return (
{ e.stopPropagation(); onEventClick(event); } } >
{event.title}
{format(new Date(event.start), 'HH:mm')} -{' '} {format(new Date(event.end), 'HH:mm')}
{event.location && (
{event.location}
)}
); }; return (
{/* En-tête des jours */}
{weekDays.map((day) => (
{format(day, 'EEEE', { locale: fr })}
{format(day, 'd', { locale: fr })}
))}
{/* Grille horaire */}
{/* Ligne de temps actuelle */} {isCurrentWeek && (
)}
{timeSlots.map((hour) => (
{`${hour.toString().padStart(2, '0')}:00`}
{weekDays.map((day) => { const dayKey = format(day, 'yyyy-MM-dd'); const dayEvents = weekEventsMap[dayKey] || []; return (
{ const date = new Date(day); date.setHours(hour); onDateClick(date); } } >
{dayEvents .filter((event) => { const eventStart = new Date(event.start); return eventStart.getHours() === hour; }) .map((event) => renderEventInCell(event, dayEvents))}
); })}
))}
); }; export default WeekView;