// Rôle : rend les vues calendrier semaine et mois avec occurrences, sélection et ligne horaire courante.
import { Tooltip } from "../../../components/Tooltip.jsx";
import {
WEEK_SLOT_MINUTES,
addDays,
formatLocalDateKey,
minutesToTimeValue,
pad2
} from "./calendarUtils.js";
import {
HOUR_HALF_SLOT_COUNT,
WEEK_COMPACT_EVENT_HEIGHT,
WEEKDAY_LABELS,
getMonthOccurrenceSegments,
getWeekMinutePosition,
layoutConcurrentOccurrences
} from "./calendarLayout.js";
function formatDayLabel(day) {
return day.toLocaleDateString(undefined, { weekday: "long" });
}
function formatHourLabel(hourIndex) {
const minutes = hourIndex * 60;
return `${pad2(Math.floor(minutes / 60))}:${pad2(minutes % 60)}`;
}
function getSelectionRange(selection) {
if (!selection) return null;
const startSlot = Math.min(selection.startSlot, selection.endSlot);
const endSlot = Math.max(selection.startSlot, selection.endSlot);
return { dayIndex: selection.dayIndex, startSlot, endSlot };
}
function isSlotSelected(selection, dayIndex, slotIndex) {
const range = getSelectionRange(selection);
return Boolean(range && range.dayIndex === dayIndex && slotIndex >= range.startSlot && slotIndex <= range.endSlot);
}
function getMonthSelectionRange(selection) {
if (!selection) return null;
const startDate = selection.startDate <= selection.endDate ? selection.startDate : selection.endDate;
const endDate = selection.startDate <= selection.endDate ? selection.endDate : selection.startDate;
return { startDate, endDate };
}
function isMonthDateSelected(selection, dateKey) {
const range = getMonthSelectionRange(selection);
return Boolean(range && dateKey >= range.startDate && dateKey <= range.endDate);
}
function formatMonthTitle(dateLike) {
return new Date(dateLike).toLocaleDateString(undefined, { month: "long", year: "numeric" });
}
export function CalendarWeekGrid({
days,
occurrences,
textContent,
activeEventId,
formOpen,
draftSelection,
selection,
weekGridLayout,
nowDayIndex,
nowMinutes,
onSlotPointerDown,
onSlotPointerEnter,
onSlotPointerUp,
onEditEvent
}) {
return (
{days.map((day) => {formatDayLabel(day)}
)}
{weekGridLayout.rows.map((row) => (
row.type === "hidden"
? ...
: {formatHourLabel(row.hourIndex)}
))}
{days.map((day, dayIndex) => {
const dayOccurrences = layoutConcurrentOccurrences(occurrences.filter((occurrence) => occurrence.event.dayIndex === dayIndex));
const isToday = dayIndex === nowDayIndex;
return (
{weekGridLayout.rows.map((row) => (
row.type === "hidden" ? (
{dayIndex === 3 && {`${formatHourLabel(row.startHour)} - ${formatHourLabel(row.endHour + 1)}`}}
) : (
{Array.from({ length: HOUR_HALF_SLOT_COUNT }, (_, halfIndex) => {
const slotIndex = row.hourIndex * HOUR_HALF_SLOT_COUNT + halfIndex;
const slotLabel = minutesToTimeValue(slotIndex * WEEK_SLOT_MINUTES);
return (
)
))}
{dayOccurrences.map((occurrence) => (
onEditEvent(occurrence.event)}
/>
))}
{isToday && (
{minutesToTimeValue(nowMinutes)}
)}
);
})}
);
}
function CalendarOccurrence({ occurrence, layout, active, onEdit }) {
const durationMinutes = Math.max(WEEK_SLOT_MINUTES, (occurrence.endMs - occurrence.startMs) / 60000 || WEEK_SLOT_MINUTES);
const startMinutes = occurrence.event.startMinutes;
const timeLabel = `${minutesToTimeValue(occurrence.event.startMinutes)}${occurrence.event.endMinutes ? ` - ${minutesToTimeValue(occurrence.event.endMinutes)}` : ""}`;
const compact = durationMinutes < 120;
const top = getWeekMinutePosition(layout, startMinutes);
const bottom = getWeekMinutePosition(layout, Math.min(24 * 60, startMinutes + durationMinutes));
const height = compact ? WEEK_COMPACT_EVENT_HEIGHT : Math.max(WEEK_COMPACT_EVENT_HEIGHT, bottom - top);
return (
{occurrence.event.title}{timeLabel}>}
onClick={onEdit}
>
{occurrence.event.title}
{!compact && {timeLabel}}
);
}
export function CalendarMonthView({
monthStart,
monthGridDays,
occurrences,
textContent,
activeEventId,
selectedRange,
dragSelection,
todayDate,
nowLabel,
onPreviousMonth,
onCurrentMonth,
onNextMonth,
onPointerDown,
onPointerEnter,
onPointerUp,
onEditEvent
}) {
return (
<>
{formatMonthTitle(monthStart)}
>
);
}
function CalendarMonthGrid({ days, occurrences, textContent, activeEventId, selectedRange, dragSelection, todayDate, nowLabel, onPointerDown, onPointerEnter, onPointerUp, onEditEvent }) {
const monthBars = getMonthOccurrenceSegments(days, occurrences);
const weekRowCount = Math.ceil(days.length / WEEKDAY_LABELS.length);
return (
{WEEKDAY_LABELS.map((day) => {day}
)}
{days.map((day) => {
const dateKey = formatLocalDateKey(day.date);
const isToday = todayDate === dateKey;
return (
day.inMonth && onPointerDown(dateKey)}
onPointerEnter={(event) => day.inMonth && onPointerEnter(event, dateKey)}
onPointerUp={() => day.inMonth && onPointerUp()}
onKeyDown={(event) => {
if (!day.inMonth || (event.key !== "Enter" && event.key !== " ")) return;
event.preventDefault();
onPointerDown(dateKey);
onPointerUp();
}}
aria-label={day.inMonth ? `${textContent.dayLabel || "Jour"} ${day.dayNumber}` : undefined}
>
{day.inMonth && (
<>
{day.dayNumber}
{isToday && {nowLabel}}
>
)}
);
})}
{monthBars.map((segment, index) => (
{
event.stopPropagation();
onEditEvent(segment.occurrence.event, segment.occurrence);
}}
/>
))}
);
}
function CalendarMonthBar({ segment, active, onEdit }) {
const gridRow = Math.floor(segment.startIndex / WEEKDAY_LABELS.length) + 2;
const startColumn = segment.startIndex % WEEKDAY_LABELS.length + 1;
const endColumn = segment.endIndex % WEEKDAY_LABELS.length + 2;
const laneCount = Math.max(1, Math.min(3, segment.laneCount || 1));
const barHeight = laneCount === 1 ? 28 : laneCount === 2 ? 23 : 18;
const barGap = laneCount === 3 ? 3 : 4;
const startLabel = new Date(segment.occurrence.startMs).toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" });
const endDate = addDays(new Date(segment.occurrence.endMs), -1);
const endLabel = endDate.toLocaleDateString(undefined, { day: "2-digit", month: "short", year: "numeric" });
const dateLabel = startLabel === endLabel ? startLabel : `${startLabel} - ${endLabel}`;
return (
{segment.occurrence.event.title}{dateLabel}>}
onPointerDown={(event) => event.stopPropagation()}
onClick={onEdit}
>
{segment.occurrence.event.title}
);
}