Split remaining toolbox modules by responsibility
All checks were successful
Deploy Sokko G / deploy (push) Successful in 16s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 16s
This commit is contained in:
parent
43a9e791c5
commit
a0d7e80a1a
25 changed files with 2199 additions and 1992 deletions
282
website/src/features/toolboxes/modules/useCalendarController.js
Normal file
282
website/src/features/toolboxes/modules/useCalendarController.js
Normal file
|
|
@ -0,0 +1,282 @@
|
|||
// Rôle : orchestre l'état, les handlers et les effets du module calendrier.
|
||||
import { useCallback, useMemo, useRef, useState } from "react";
|
||||
import {
|
||||
WEEK_SLOT_MINUTES,
|
||||
addDays,
|
||||
getCalendarOccurrences,
|
||||
getLocalDayIndex,
|
||||
getMonthEnd,
|
||||
getMonthGridDays,
|
||||
getMonthStart,
|
||||
getWeekDays,
|
||||
getWeekStart,
|
||||
minutesToTimeValue,
|
||||
parseLocalDateKey
|
||||
} from "./calendarUtils.js";
|
||||
import { WEEK_SLOT_COUNT, getWeekGridLayout } from "./calendarLayout.js";
|
||||
import {
|
||||
createDefaultDraft,
|
||||
getDraftSelection,
|
||||
normalizeDraftAlertOffsets
|
||||
} from "./calendarDraft.js";
|
||||
import {
|
||||
buildCalendarEventFromDraft,
|
||||
buildPastedCalendarEvent,
|
||||
getCalendarDraftFromEvent
|
||||
} from "./calendarEventActions.js";
|
||||
import {
|
||||
useCalendarClock,
|
||||
useCalendarEditingReset,
|
||||
usePruneExpiredCalendarEvents
|
||||
} from "./useCalendarLifecycle.js";
|
||||
import { useCalendarDraftControls } from "./useCalendarDraftControls.js";
|
||||
|
||||
export function useCalendarController({ data, context, toolboxId, moduleId, editing = false, setEditing, textContent }) {
|
||||
const [now, setNow] = useState(() => new Date());
|
||||
const [displayedMonth, setDisplayedMonth] = useState(() => getMonthStart(new Date()));
|
||||
const weekStart = useMemo(() => getWeekStart(new Date(), data.weekStartsOn), [data.weekStartsOn]);
|
||||
const monthStart = useMemo(() => getMonthStart(displayedMonth), [displayedMonth]);
|
||||
const monthEnd = useMemo(() => getMonthEnd(monthStart), [monthStart]);
|
||||
const monthGridDays = useMemo(() => getMonthGridDays(monthStart), [monthStart]);
|
||||
const [selection, setSelection] = useState(null);
|
||||
const [monthSelection, setMonthSelection] = useState(null);
|
||||
const [draft, setDraft] = useState(() => createDefaultDraft());
|
||||
const [activeEventId, setActiveEventId] = useState("");
|
||||
const [activeOccurrenceDate, setActiveOccurrenceDate] = useState("");
|
||||
const [copiedEvent, setCopiedEvent] = useState(null);
|
||||
const [hideEmptyHours, setHideEmptyHours] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const previousEditingRef = useRef(editing);
|
||||
const days = useMemo(() => getWeekDays(weekStart), [weekStart]);
|
||||
const weekEnd = useMemo(() => addDays(weekStart, 7), [weekStart]);
|
||||
const rangeStart = data.viewMode === "month" ? monthStart : weekStart;
|
||||
const rangeEnd = data.viewMode === "month" ? monthEnd : weekEnd;
|
||||
const occurrences = useMemo(() => getCalendarOccurrences(data.events, rangeStart, rangeEnd), [data.events, rangeEnd, rangeStart]);
|
||||
const draftSelection = data.viewMode === "week" ? getDraftSelection(draft) : null;
|
||||
const nowMinutes = now.getHours() * 60 + now.getMinutes();
|
||||
const nowDayIndex = getLocalDayIndex(now);
|
||||
const formOpen = editing || Boolean(activeEventId);
|
||||
const weekGridLayout = useMemo(() => {
|
||||
return getWeekGridLayout(occurrences, data.viewMode === "week" && hideEmptyHours && !formOpen);
|
||||
}, [data.viewMode, formOpen, hideEmptyHours, occurrences]);
|
||||
const { updateAlertMode, updateDraft, toggleAlertOffset } = useCalendarDraftControls({ setDraft, setError });
|
||||
|
||||
const save = useCallback((nextData) => {
|
||||
context.setModuleData(toolboxId, moduleId, nextData, "calendar");
|
||||
}, [context, moduleId, toolboxId]);
|
||||
|
||||
function initializeWeek() {
|
||||
save({ ...data, initialized: true, viewMode: "week", weekStartsOn: 1, events: [] });
|
||||
}
|
||||
|
||||
function initializeMonth() {
|
||||
save({ ...data, initialized: true, viewMode: "month", weekStartsOn: 1, events: [] });
|
||||
}
|
||||
|
||||
const resetDraft = useCallback(() => {
|
||||
setDraft(createDefaultDraft());
|
||||
setSelection(null);
|
||||
setMonthSelection(null);
|
||||
setActiveEventId("");
|
||||
setActiveOccurrenceDate("");
|
||||
setError("");
|
||||
}, []);
|
||||
|
||||
function closeForm() {
|
||||
resetDraft();
|
||||
setEditing?.(false);
|
||||
}
|
||||
|
||||
function applySelection(dayIndex, startSlot, endSlot = startSlot) {
|
||||
const orderedStart = Math.min(startSlot, endSlot);
|
||||
const orderedEnd = Math.max(startSlot, endSlot);
|
||||
updateDraft({
|
||||
id: "",
|
||||
dayIndex,
|
||||
startTime: minutesToTimeValue(orderedStart * WEEK_SLOT_MINUTES),
|
||||
endTime: minutesToTimeValue(Math.min(23 * 60 + 59, (Math.min(WEEK_SLOT_COUNT, orderedEnd + 1)) * WEEK_SLOT_MINUTES))
|
||||
});
|
||||
}
|
||||
|
||||
function applyMonthSelection(startDate, endDate = startDate) {
|
||||
const orderedStart = startDate <= endDate ? startDate : endDate;
|
||||
const orderedEnd = startDate <= endDate ? endDate : startDate;
|
||||
const start = parseLocalDateKey(orderedStart);
|
||||
setActiveEventId("");
|
||||
setActiveOccurrenceDate("");
|
||||
updateDraft({
|
||||
id: "",
|
||||
startDate: orderedStart,
|
||||
endDate: orderedEnd,
|
||||
dayIndex: start ? getLocalDayIndex(start) : 0
|
||||
});
|
||||
}
|
||||
|
||||
function pasteEventAtTarget(target) {
|
||||
const nextEvent = buildPastedCalendarEvent({ copiedEvent, target, data, draft, context });
|
||||
if (!nextEvent) return;
|
||||
save({ ...data, initialized: true, events: [...data.events, nextEvent] });
|
||||
context.notify?.(textContent.pastedTitle || "Événement collé.");
|
||||
}
|
||||
|
||||
function pasteCopiedEventAtDraftSelection() {
|
||||
if (!copiedEvent) return;
|
||||
if (data.viewMode === "month") {
|
||||
if (!parseLocalDateKey(draft.startDate)) return;
|
||||
pasteEventAtTarget({
|
||||
startDate: draft.startDate,
|
||||
monthlyRepeatBy: draft.monthlyRepeatBy || "monthDay",
|
||||
slotIndex: -1
|
||||
});
|
||||
return;
|
||||
}
|
||||
const targetSelection = getDraftSelection(draft);
|
||||
if (targetSelection) pasteEventAtTarget({ dayIndex: targetSelection.dayIndex, slotIndex: targetSelection.startSlot });
|
||||
}
|
||||
|
||||
function handleSlotPointerDown(dayIndex, slotIndex) {
|
||||
setActiveEventId("");
|
||||
setActiveOccurrenceDate("");
|
||||
setSelection({ dayIndex, startSlot: slotIndex, endSlot: slotIndex });
|
||||
applySelection(dayIndex, slotIndex);
|
||||
}
|
||||
|
||||
function handleSlotPointerEnter(event, dayIndex, slotIndex) {
|
||||
if (event.buttons !== 1) return;
|
||||
setSelection((current) => {
|
||||
if (!current || current.dayIndex !== dayIndex) return current;
|
||||
applySelection(dayIndex, current.startSlot, slotIndex);
|
||||
return { ...current, endSlot: slotIndex };
|
||||
});
|
||||
}
|
||||
|
||||
function handleMonthPointerDown(dateKey) {
|
||||
setActiveEventId("");
|
||||
setActiveOccurrenceDate("");
|
||||
setMonthSelection({ startDate: dateKey, endDate: dateKey });
|
||||
applyMonthSelection(dateKey);
|
||||
}
|
||||
|
||||
function handleMonthPointerEnter(event, dateKey) {
|
||||
if (event.buttons !== 1) return;
|
||||
setMonthSelection((current) => {
|
||||
if (!current) return current;
|
||||
applyMonthSelection(current.startDate, dateKey);
|
||||
return { ...current, endDate: dateKey };
|
||||
});
|
||||
}
|
||||
|
||||
function editEvent(event, occurrence = null) {
|
||||
const { draft: nextDraft, occurrenceDate } = getCalendarDraftFromEvent({ event, occurrence, now });
|
||||
if (occurrenceDate) setDisplayedMonth(getMonthStart(parseLocalDateKey(occurrenceDate)));
|
||||
setDraft(nextDraft);
|
||||
setActiveEventId(event.id);
|
||||
setActiveOccurrenceDate(occurrenceDate);
|
||||
setError("");
|
||||
}
|
||||
|
||||
function deleteEvent(eventId) {
|
||||
save({ ...data, initialized: true, events: data.events.filter((event) => event.id !== eventId) });
|
||||
if (draft.id === eventId || activeEventId === eventId) closeForm();
|
||||
}
|
||||
|
||||
function deleteOccurrence(eventId) {
|
||||
const event = data.events.find((item) => item.id === eventId);
|
||||
if (!event || !activeOccurrenceDate || event.repeat === "none") return;
|
||||
const events = data.events.map((item) => item.id === eventId
|
||||
? { ...item, excludedOccurrences: [...new Set([...(item.excludedOccurrences || []), activeOccurrenceDate])].sort() }
|
||||
: item);
|
||||
save({ ...data, initialized: true, events });
|
||||
closeForm();
|
||||
}
|
||||
|
||||
function copyEvent(event) {
|
||||
setCopiedEvent(event);
|
||||
context.notify?.(textContent.copiedTitle || "Événement copié.");
|
||||
}
|
||||
|
||||
function submitEvent(event) {
|
||||
event.preventDefault();
|
||||
const result = buildCalendarEventFromDraft({ draft, data, context });
|
||||
if (result.error === "required") {
|
||||
setError(textContent.requiredError || "Titre, date et heure de début requis.");
|
||||
return;
|
||||
}
|
||||
if (result.error === "range") {
|
||||
setError(textContent.invalidRangeError || "La fin doit être après le début.");
|
||||
return;
|
||||
}
|
||||
const events = draft.id
|
||||
? data.events.map((item) => item.id === draft.id ? result.event : item)
|
||||
: [...data.events, result.event];
|
||||
save({ ...data, initialized: true, events });
|
||||
closeForm();
|
||||
}
|
||||
|
||||
function handleKeyDown(event) {
|
||||
if (!event.ctrlKey && !event.metaKey) return;
|
||||
const target = event.target;
|
||||
if (target?.closest?.("input, textarea, select, [contenteditable='true']")) return;
|
||||
const key = event.key.toLowerCase();
|
||||
if (key === "c") {
|
||||
const activeEvent = data.events.find((item) => item.id === activeEventId);
|
||||
if (!activeEvent) return;
|
||||
event.preventDefault();
|
||||
copyEvent(activeEvent);
|
||||
}
|
||||
if (key === "v") {
|
||||
if (!copiedEvent) return;
|
||||
event.preventDefault();
|
||||
pasteCopiedEventAtDraftSelection();
|
||||
}
|
||||
}
|
||||
|
||||
useCalendarClock(setNow);
|
||||
useCalendarEditingReset({ editing, previousEditingRef, resetDraft, setActiveEventId, setDraft, setError, setSelection });
|
||||
usePruneExpiredCalendarEvents({ activeEventId, data, rangeEnd, rangeStart, resetDraft, save });
|
||||
|
||||
return {
|
||||
initializeWeek,
|
||||
initializeMonth,
|
||||
contentProps: {
|
||||
data,
|
||||
context,
|
||||
draft: { ...draft, alertOffsetMinutes: draft.alertMode === "off" ? [] : normalizeDraftAlertOffsets(draft.alertOffsetMinutes) },
|
||||
error,
|
||||
activeOccurrenceDate,
|
||||
monthStart,
|
||||
monthGridDays,
|
||||
occurrences,
|
||||
textContent,
|
||||
activeEventId,
|
||||
formOpen,
|
||||
draftSelection,
|
||||
selection,
|
||||
monthSelection,
|
||||
days,
|
||||
weekGridLayout,
|
||||
now,
|
||||
nowDayIndex,
|
||||
nowMinutes,
|
||||
hideEmptyHours,
|
||||
onKeyDown: handleKeyDown,
|
||||
onSubmit: submitEvent,
|
||||
onDraftChange: updateDraft,
|
||||
onAlertModeChange: updateAlertMode,
|
||||
onAlertOffsetToggle: toggleAlertOffset,
|
||||
onDeleteEvent: deleteEvent,
|
||||
onDeleteOccurrence: deleteOccurrence,
|
||||
onClose: closeForm,
|
||||
onDisplayedMonthChange: setDisplayedMonth,
|
||||
onImport: (nextData) => save(nextData),
|
||||
onHideEmptyHoursToggle: () => setHideEmptyHours((current) => !current),
|
||||
onMonthPointerDown: handleMonthPointerDown,
|
||||
onMonthPointerEnter: handleMonthPointerEnter,
|
||||
onMonthPointerUp: () => setMonthSelection(null),
|
||||
onSlotPointerDown: handleSlotPointerDown,
|
||||
onSlotPointerEnter: handleSlotPointerEnter,
|
||||
onSlotPointerUp: () => setSelection(null),
|
||||
onEditEvent: editEvent
|
||||
}
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue