52 lines
2.3 KiB
JavaScript
52 lines
2.3 KiB
JavaScript
// Rôle : affiche en bas de page les mémos et alertes de tâches de la toolbox courante ou liée au jeu.
|
|
import { useEffect, useMemo, useState } from "react";
|
|
import { moduleStorageKey, normalizeMemosData } from "./storage/toolboxStorage.js";
|
|
import { MemosTicker } from "./modules/MemosTicker.jsx";
|
|
import { getPlannerTaskTickerItems, resolveMemosPlannerLink } from "./modules/memosPlannerUtils.js";
|
|
|
|
function getRouteToolboxId(route, links) {
|
|
const toolboxMatch = route.match(/^\/toolbox\/([^/]+)/);
|
|
if (toolboxMatch) return decodeURIComponent(toolboxMatch[1]);
|
|
const gameMatch = route.match(/^\/games\/([^/]+)/);
|
|
return gameMatch ? links[gameMatch[1]] || "" : "";
|
|
}
|
|
|
|
export function GlobalMemosTicker({ route, toolboxes, links, moduleData, textContent }) {
|
|
const [nowMs, setNowMs] = useState(() => Date.now());
|
|
|
|
useEffect(() => {
|
|
const intervalId = window.setInterval(() => setNowMs(Date.now()), 60000);
|
|
return () => window.clearInterval(intervalId);
|
|
}, []);
|
|
|
|
const items = useMemo(() => {
|
|
const toolboxId = getRouteToolboxId(route, links);
|
|
const toolbox = toolboxes.find((item) => item.id === toolboxId);
|
|
if (!toolbox) return [];
|
|
return toolbox.modules
|
|
.filter((module) => module.type === "memos")
|
|
.flatMap((module) => {
|
|
const data = normalizeMemosData(moduleData[moduleStorageKey(toolbox.id, module.id)]);
|
|
if (!data.tickerEnabled) return [];
|
|
const plannerLink = resolveMemosPlannerLink(data.plannerLink, toolbox.modules);
|
|
const taskItems = getPlannerTaskTickerItems({
|
|
plannerLink,
|
|
modules: toolbox.modules,
|
|
getPlannerData: (plannerId) => moduleData[moduleStorageKey(toolbox.id, plannerId)],
|
|
nowMs,
|
|
alertTemplate: textContent?.taskAlertTemplate
|
|
});
|
|
return [
|
|
...data.items.map((memo) => ({ ...memo, id: `${module.id}:${memo.id}` })),
|
|
...taskItems.map((item) => ({ ...item, id: `${module.id}:${item.id}` }))
|
|
];
|
|
});
|
|
}, [route, toolboxes, links, moduleData, nowMs, textContent?.taskAlertTemplate]);
|
|
|
|
if (!items.length) return null;
|
|
return (
|
|
<aside className="global-memos-ticker" aria-label={textContent?.tickerLabel || "Mémos"}>
|
|
<MemosTicker items={items} label={textContent?.tickerLabel || "Mémos"} />
|
|
</aside>
|
|
);
|
|
}
|