sokko-g/website/src/features/toolboxes/modules/CalendarViews.jsx
Shinuwa ae739b08ad Check in the tests that central files keep their full headers and that every tool has a compactor
tests/file-headers.test.mjs now fails when a source file over 300 lines lacks
"Utilisé par :" and @see, when a listed central file lacks "Utilisé par :", or
when a file adding methods through Object.assign does not say so. The eleven
files below the bar are completed, so no waiting list is needed.
tests/toolbox-compactors.test.mjs makes sure every tool type has a storage
compactor, the gap found while documenting the toolboxes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 11:22:50 +01:00

326 lines
14 KiB
JavaScript

/**
* @file Vues semaine et mois de l'outil Calendrier : occurrences des événements, sélection et ligne de l'heure
* courante.
*
* Utilisé par : CalendarContent.jsx.
* Dépend de : calendarUtils.js (semaines, mois, occurrences).
* @see docs/STORAGE_SCHEMA.md
*/
import { Tooltip } from "../../../components/Tooltip.jsx";
import {
WEEK_SLOT_MINUTES,
addDays,
formatLocalDateKey,
getCalendarColorValue,
getCalendarEventSurfaceVariant,
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 (
<section className="calendar-week-grid" aria-label={textContent.gridLabel || "Calendrier hebdomadaire"}>
<div className="calendar-time-header" />
{days.map((day) => <div key={day.toISOString()} className="calendar-day-header">{formatDayLabel(day)}</div>)}
<div className="calendar-time-column" style={{ gridTemplateRows: weekGridLayout.gridTemplateRows }}>
{weekGridLayout.rows.map((row) => (
row.type === "hidden"
? <span key={`hidden:${row.startHour}`} className="calendar-hidden-hours-label">...</span>
: <span key={row.hourIndex} className="calendar-time-row">{formatHourLabel(row.hourIndex)}</span>
))}
</div>
{days.map((day, dayIndex) => {
const dayOccurrences = layoutConcurrentOccurrences(occurrences.filter((occurrence) => occurrence.event.dayIndex === dayIndex));
const isToday = dayIndex === nowDayIndex;
return (
<div key={day.toISOString()} className={`calendar-day-column ${isToday ? "is-today" : ""}`} style={{ gridTemplateRows: weekGridLayout.gridTemplateRows }}>
{weekGridLayout.rows.map((row) => (
row.type === "hidden" ? (
<div key={`hidden:${row.startHour}`} className="calendar-hidden-hours-cell" aria-hidden="true">
{dayIndex === 3 && <span>{`${formatHourLabel(row.startHour)} - ${formatHourLabel(row.endHour + 1)}`}</span>}
</div>
) : (
<div key={row.hourIndex} className="calendar-hour-cell">
{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 (
<button
key={slotIndex}
type="button"
className={`calendar-slot ${formOpen && isSlotSelected(draftSelection, dayIndex, slotIndex) ? "is-form-selected" : ""} ${isSlotSelected(selection, dayIndex, slotIndex) ? "is-selected" : ""}`}
onPointerDown={() => onSlotPointerDown(dayIndex, slotIndex)}
onPointerEnter={(event) => onSlotPointerEnter(event, dayIndex, slotIndex)}
onPointerUp={onSlotPointerUp}
aria-label={`${formatDayLabel(day)} ${slotLabel}`}
/>
);
})}
</div>
)
))}
<div className="calendar-events-layer">
{dayOccurrences.map((occurrence) => (
<CalendarOccurrence
key={occurrence.id}
occurrence={occurrence}
layout={weekGridLayout}
active={activeEventId === occurrence.event.id}
onEdit={() => onEditEvent(occurrence.event)}
/>
))}
</div>
{isToday && (
<div className="calendar-now-line" style={{ top: `${getWeekMinutePosition(weekGridLayout, nowMinutes)}px` }}>
<span>{minutesToTimeValue(nowMinutes)}</span>
</div>
)}
</div>
);
})}
</section>
);
}
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 top = getWeekMinutePosition(layout, startMinutes);
const bottom = getWeekMinutePosition(layout, Math.min(24 * 60, startMinutes + durationMinutes));
const naturalHeight = bottom - top;
const eventSurfaceSize = naturalHeight < WEEK_COMPACT_EVENT_HEIGHT ? "xs" : naturalHeight < 44 ? "sm" : "md";
const height = eventSurfaceSize === "xs" ? 10 : eventSurfaceSize === "sm" ? WEEK_COMPACT_EVENT_HEIGHT : Math.max(44, naturalHeight);
const color = occurrence.event.color || "violet";
return (
<article
className={`calendar-event nds-event-surface ${active ? "is-active" : ""} ${eventSurfaceSize !== "md" ? "is-compact" : ""}`}
data-color={getCalendarColorValue(color)}
data-size={eventSurfaceSize}
data-variant={getCalendarEventSurfaceVariant(color)}
style={{
top: `${top}px`,
width: "calc((100% / var(--calendar-event-lane-count, 1)) - 2px)",
height: `${height}px`,
"--calendar-event-lane": occurrence.lane || 0,
"--calendar-event-lane-count": occurrence.laneCount || 1,
"--nds-event-color": getCalendarColorValue(color)
}}
>
<Tooltip
as="button"
type="button"
className="calendar-event-button"
tooltipClassName="calendar-event-tooltip"
content={<><strong>{occurrence.event.title}</strong><span>{timeLabel}</span></>}
onClick={onEdit}
>
<strong>{occurrence.event.title}</strong>
<span>{timeLabel}</span>
</Tooltip>
</article>
);
}
export function CalendarMonthView({
monthStart,
monthGridDays,
occurrences,
textContent,
activeEventId,
selectedRange,
dragSelection,
todayDate,
nowLabel,
onPreviousMonth,
onCurrentMonth,
onNextMonth,
onPointerDown,
onPointerEnter,
onPointerUp,
onEditEvent
}) {
return (
<>
<div className="calendar-month-toolbar">
<button className="nds-button" data-size="xs" type="button" onClick={onPreviousMonth} title={textContent.previousMonthTitle || "Mois précédent"} aria-label={textContent.previousMonthTitle || "Mois précédent"}>{textContent.previousShortLabel || "Prec."}</button>
<strong>{formatMonthTitle(monthStart)}</strong>
<button className="nds-button" data-size="xs" type="button" onClick={onCurrentMonth}>{textContent.currentMonthTitle || "Mois courant"}</button>
<button className="nds-button" data-size="xs" type="button" onClick={onNextMonth} title={textContent.nextMonthTitle || "Mois suivant"} aria-label={textContent.nextMonthTitle || "Mois suivant"}>{textContent.nextShortLabel || "Suiv."}</button>
</div>
<CalendarMonthGrid
days={monthGridDays}
occurrences={occurrences}
textContent={textContent}
activeEventId={activeEventId}
selectedRange={selectedRange}
dragSelection={dragSelection}
todayDate={todayDate}
nowLabel={nowLabel}
onPointerDown={onPointerDown}
onPointerEnter={onPointerEnter}
onPointerUp={onPointerUp}
onEditEvent={onEditEvent}
/>
</>
);
}
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 (
<section className="calendar-month-grid" style={{ "--calendar-month-week-count": weekRowCount }} aria-label={textContent.monthGridLabel || "Calendrier mensuel"}>
{WEEKDAY_LABELS.map((day) => <div key={day} className="calendar-month-header">{day}</div>)}
{days.map((day) => {
const dateKey = formatLocalDateKey(day.date);
const isToday = todayDate === dateKey;
return (
<div
key={day.date.toISOString()}
className={`calendar-month-cell ${day.inMonth ? "" : "is-outside"} ${day.inMonth && isMonthDateSelected(selectedRange, dateKey) ? "is-form-selected" : ""} ${day.inMonth && isMonthDateSelected(dragSelection, dateKey) ? "is-selected" : ""} ${day.inMonth && isToday ? "is-today" : ""}`}
role={day.inMonth ? "button" : undefined}
tabIndex={day.inMonth ? 0 : undefined}
onPointerDown={() => 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 && (
<>
<span className="calendar-month-day-number">{day.dayNumber}</span>
{isToday && <span className="calendar-month-now-indicator">{nowLabel}</span>}
</>
)}
</div>
);
})}
<div className="calendar-month-bars-layer" aria-hidden={monthBars.length ? undefined : true}>
{monthBars.map((segment, index) => (
<CalendarMonthBar
key={`${segment.occurrence.id}:${index}`}
segment={segment}
active={activeEventId === segment.occurrence.event.id}
onEdit={(event) => {
event.stopPropagation();
onEditEvent(segment.occurrence.event, segment.occurrence);
}}
/>
))}
</div>
</section>
);
}
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 <= 2 ? 22 : 10;
const barGap = laneCount === 3 ? 2 : 4;
const eventSurfaceSize = barHeight <= 10 ? "xs" : barHeight <= 22 ? "sm" : "md";
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}`;
const color = segment.occurrence.event.color || "violet";
return (
<article
className={`calendar-month-bar nds-event-surface ${active ? "is-active" : ""}`}
data-color={getCalendarColorValue(color)}
data-size={eventSurfaceSize}
data-variant={getCalendarEventSurfaceVariant(color)}
style={{
gridColumn: `${startColumn} / ${endColumn}`,
gridRow,
width: "auto",
"--calendar-event-lane": segment.lane || 0,
"--calendar-month-bar-height": `${barHeight}px`,
"--calendar-month-bar-gap": `${barGap}px`,
"--nds-event-color": getCalendarColorValue(color)
}}
>
<Tooltip
as="button"
type="button"
className="calendar-month-bar-button"
tooltipClassName="calendar-event-tooltip"
content={<><strong>{segment.occurrence.event.title}</strong><span>{dateLabel}</span></>}
onPointerDown={(event) => event.stopPropagation()}
onClick={onEdit}
>
<strong>{segment.occurrence.event.title}</strong>
</Tooltip>
</article>
);
}