The 669 // comments sitting right above a function, method or hook become /** */ blocks, unchanged in wording; eslint directives stay right above the line they target. website/AGENTS.md now asks for JSDoc on function comments and // inside function bodies. ExpeditionPlanner.jsx grew past 300 lines and gets its full header, as the header test requires. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
5 KiB
JavaScript
107 lines
5 KiB
JavaScript
/**
|
|
* @file Surveille, sur toutes les pages, les alertes des outils Timer et Calendrier des toolboxes visibles, et les
|
|
* annonce par une notification.
|
|
*
|
|
* Utilisé par : main.jsx.
|
|
* Attention : seules les toolboxes visibles (page ouverte ou tiroir) sont surveillées (`getVisibleToolboxIds`).
|
|
*/
|
|
|
|
import { useEffect, useRef } from "react";
|
|
import { getCalendarAlertOffsets, getCurrentCalendarOccurrence, getNextCalendarAlertTime } from "../features/toolboxes/modules/calendarUtils.js";
|
|
import { getCountdownTargetMs } from "../features/toolboxes/modules/timerUtils.js";
|
|
import { moduleStorageKey } from "../features/toolboxes/storage/toolboxStorage.js";
|
|
|
|
/** `drawerToolboxId` : la toolbox affichée par le tiroir (liée au jeu, ou choisie dans Sokko TCG), vide tant qu'il est masqué. */
|
|
export function getVisibleToolboxIds(route, drawerToolboxId) {
|
|
const visibleIds = new Set();
|
|
const toolboxMatch = route.match(/^\/toolbox\/([^/]+)/);
|
|
if (toolboxMatch?.[1]) visibleIds.add(toolboxMatch[1]);
|
|
if (drawerToolboxId) visibleIds.add(drawerToolboxId);
|
|
return visibleIds;
|
|
}
|
|
|
|
function isAlertAllowed(alertMode, toolboxId, visibleToolboxIds) {
|
|
if (alertMode === "off") return false;
|
|
if (alertMode === "visible") return visibleToolboxIds.has(toolboxId);
|
|
return alertMode === "site";
|
|
}
|
|
|
|
function formatAlertMessage(template, toolbox, label) {
|
|
return template
|
|
.replaceAll("{label}", label)
|
|
.replaceAll("{toolbox}", toolbox.name);
|
|
}
|
|
|
|
export function useToolboxAlerts({ toolboxes, moduleData, route, drawerToolboxId, normalizeTimerData, normalizeCalendarData, textContent, notify }) {
|
|
const previousTargetsRef = useRef(new Map());
|
|
const resumedCalendarEventsRef = useRef(new Set());
|
|
const lastTickRef = useRef(null);
|
|
|
|
useEffect(() => {
|
|
const visibleToolboxIds = getVisibleToolboxIds(route, drawerToolboxId);
|
|
const timerAlertTemplate = textContent?.timer?.alertMessage || "Compte à rebours terminé : {label}";
|
|
const calendarAlertTemplate = textContent?.calendar?.alertMessage || "Événement à venir : {label}";
|
|
const calendarResumeTemplate = textContent?.calendar?.resumeAlertMessage || "Événement en cours : {label}";
|
|
|
|
function tick() {
|
|
const nowMs = Date.now();
|
|
const lastTickMs = lastTickRef.current ?? nowMs;
|
|
const nextTargets = new Map();
|
|
|
|
toolboxes.forEach((toolbox) => {
|
|
toolbox.modules.forEach((module) => {
|
|
if (module.type === "timer") {
|
|
const data = normalizeTimerData(moduleData[moduleStorageKey(toolbox.id, module.id)]);
|
|
|
|
data.countdowns.forEach((countdown) => {
|
|
if (!isAlertAllowed(countdown.alertMode, toolbox.id, visibleToolboxIds)) return;
|
|
|
|
const key = `${toolbox.id}:${module.id}:${countdown.id}`;
|
|
const targetMs = getCountdownTargetMs(countdown, nowMs);
|
|
const previousTargetMs = previousTargetsRef.current.get(key) || targetMs;
|
|
nextTargets.set(key, targetMs);
|
|
|
|
if (!targetMs || previousTargetMs <= lastTickMs || previousTargetMs > nowMs) return;
|
|
notify(formatAlertMessage(timerAlertTemplate, toolbox, countdown.label), { persistent: true });
|
|
});
|
|
}
|
|
|
|
if (module.type === "calendar") {
|
|
const data = normalizeCalendarData(moduleData[moduleStorageKey(toolbox.id, module.id)]);
|
|
|
|
data.events.forEach((event) => {
|
|
if (!isAlertAllowed(event.alertMode, toolbox.id, visibleToolboxIds)) return;
|
|
|
|
const key = `${toolbox.id}:${module.id}:${event.id}`;
|
|
getCalendarAlertOffsets(event).forEach((offset) => {
|
|
const alertKey = `${key}:alert:${offset}`;
|
|
const alertMs = getNextCalendarAlertTime({ ...event, alertOffsetMinutes: offset }, nowMs);
|
|
const previousAlertMs = previousTargetsRef.current.get(alertKey) || alertMs;
|
|
if (alertMs) nextTargets.set(alertKey, alertMs);
|
|
|
|
if (alertMs && previousAlertMs > lastTickMs && previousAlertMs <= nowMs) {
|
|
notify(formatAlertMessage(calendarAlertTemplate, toolbox, event.title), { persistent: true });
|
|
}
|
|
});
|
|
|
|
const currentOccurrence = getCurrentCalendarOccurrence(event, nowMs);
|
|
if (!currentOccurrence) return;
|
|
if (!getCalendarAlertOffsets(event).includes(0)) return;
|
|
const resumeKey = `${key}:${currentOccurrence.startMs}`;
|
|
if (resumedCalendarEventsRef.current.has(resumeKey)) return;
|
|
resumedCalendarEventsRef.current.add(resumeKey);
|
|
notify(formatAlertMessage(calendarResumeTemplate, toolbox, event.title), { persistent: true });
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
previousTargetsRef.current = nextTargets;
|
|
lastTickRef.current = nowMs;
|
|
}
|
|
|
|
tick();
|
|
const intervalId = window.setInterval(tick, 500);
|
|
return () => window.clearInterval(intervalId);
|
|
}, [drawerToolboxId, moduleData, normalizeCalendarData, normalizeTimerData, notify, route, textContent, toolboxes]);
|
|
}
|