// Rôle : affiche les pages toolbox, le panneau latéral et leurs contrôles. import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { GuidedTooltipTour } from "../../components/GuidedTooltipTour.jsx"; import { Icon } from "../../components/Icon.jsx"; import { ImportButton } from "../../components/ImportButton.jsx"; import { ToolboxManagementModal } from "../../components/ToolboxModals.jsx"; import { useGuideChecklist } from "../games/useGuideChecklist.js"; import { StorageQuota } from "../../components/StorageQuota.jsx"; import { useGroupedReorder } from "../../hooks/useGroupedReorder.js"; import { useInlineEdit } from "../../hooks/useInlineEdit.js"; import { compressImage } from "../../utils/imageCompression.js"; import { getGameCardCover, ToolboxCard, ToolboxGameIcon, ToolboxIconPicker } from "./ToolboxCard.jsx"; import { AddToolControls, ToolboxModules } from "./modules/index.jsx"; import { getMemosDataAfterPlannerRemoval } from "./modules/memosPlannerUtils.js"; import { ToolboxSpaces } from "./ToolboxSpaces.jsx"; import { addModuleToToolboxSpace, decodeSpaceColumn, encodeSpaceColumn, getActiveToolboxSpace, getModuleSpace, moveToolboxModule, removeModuleFromToolboxSpaces } from "./toolboxSpaceUtils.js"; import { clampQty, hostnameFromUrl, normalizeCalculatorData, normalizeCalendarData, normalizeChecklistData, normalizeCombosData, normalizeCountersData, normalizeEquipmentPlannerData, normalizeGoalTreeData, normalizeImageAnnotationData, normalizeLinksData, normalizeMemosData, normalizeNotepadData, normalizeRandomizerData, normalizeRngSimulatorData, normalizeTableData, normalizeTaskPlannerData, normalizeTimerData, summarizeEquipmentPlannerData, normalizeUrl, uid } from "./storage/toolboxStorage.js"; const DRAWER_INITIAL_WIDTH = 700; function getDrawerMaxWidth() { return Math.max(DRAWER_INITIAL_WIDTH, Math.floor(window.innerWidth * 0.94)); } async function copyText(value) { try { await navigator.clipboard.writeText(value); return true; } catch { return false; } } export function ToolboxesPage({ siteContent, toolboxes, getToolboxGame, actions, storageUsage, toastPosition, setToastPosition }) { const content = siteContent.toolboxes; const [managementOpen, setManagementOpen] = useState(false); const { itemReorder, getItemProps, isItemDragging, isItemDropTarget, getDropPlacement } = useGroupedReorder({ namespace: "toolbox-cards", items: toolboxes, getItemId: (toolbox) => toolbox.id, orientation: "horizontal", getPlacement: (event, target) => { const rect = target.getBoundingClientRect(); return event.clientY > rect.top + rect.height / 2 || event.clientX > rect.left + rect.width / 2 ? "after" : "before"; }, onItemMove: (operation) => { const nextIds = toolboxes.map((toolbox) => toolbox.id).filter((id) => id !== operation.sourceId); const targetIndex = nextIds.indexOf(operation.targetId); nextIds.splice(operation.placement === "after" ? targetIndex + 1 : targetIndex, 0, operation.sourceId); actions.updateToolboxOrder(nextIds); } }); return (

{content.eyebrow}

{content.title}

{content.summary}

{content.libraryLink}

{content.collectionEyebrow}

{content.collectionTitle}

{toolboxes.length}
{toolboxes.length ? toolboxes.map((toolbox) => ( )) : (

{content.emptyTitle}

{content.emptyText}

)}
{managementOpen && setManagementOpen(false)} />}
); } export function ToolboxPage(props) { const { toolboxId, toolboxes, getToolboxGame } = props; const toolbox = toolboxes.find((item) => item.id === toolboxId); if (!toolbox) return

Toolbox introuvable

Retour
; return ; } function shouldShowTutorial(toolbox, pendingTutorialToolboxId) { return toolbox?.tutorial === true || pendingTutorialToolboxId === toolbox?.id; } function ToolboxView({ siteContent, toolbox, toolboxGame, embedded, actions, storageUsage, storageReady = true, getModuleData, updateToolbox, updateModuleData, addImageFiles, pendingTutorialToolboxId = "", setPendingTutorialToolboxId, guideChecklist }) { const toolboxId = toolbox.id; const moduleColumns = embedded || Number(toolbox.moduleColumns) === 1 ? 1 : 2; const activeSpace = getActiveToolboxSpace(toolbox); const spacesText = siteContent.toolboxes.spaces; const toolboxGameCover = getGameCardCover(toolboxGame); const moduleText = siteContent.toolboxes.modules; const [showTutorial, setShowTutorial] = useState(() => shouldShowTutorial(toolbox, pendingTutorialToolboxId)); const [pendingModuleId, setPendingModuleId] = useState(""); const dragOriginSpaceRef = useRef(""); const dragSucceededRef = useRef(false); const hoverActivatedRef = useRef(false); const previousDraggingIdRef = useRef(""); const tutorialRequested = shouldShowTutorial(toolbox, pendingTutorialToolboxId); const [previousTutorial, setPreviousTutorial] = useState({ toolboxId, requested: tutorialRequested }); // Consommer la demande de lancement ne doit pas interrompre la visite en cours. if (previousTutorial.toolboxId !== toolboxId || previousTutorial.requested !== tutorialRequested) { setPreviousTutorial({ toolboxId, requested: tutorialRequested }); if (previousTutorial.toolboxId !== toolboxId || tutorialRequested) setShowTutorial(tutorialRequested); } const moduleContext = { guideChecklist, getModuleData, ensureModuleData: actions.ensureModuleData, isModuleDataReady: actions.isModuleDataReady, storageStatus: siteContent.toolboxes.storageStatus, storageReady, toolboxModules: toolbox.modules, moduleText, moduleConfigurationText: siteContent.toolboxes.moduleConfiguration, normalizeChecklistData, normalizeCombosData, normalizeLinksData, normalizeMemosData, normalizeCountersData, normalizeEquipmentPlannerData, normalizeGoalTreeData, normalizeCalculatorData, normalizeCalendarData, normalizeImageAnnotationData, normalizeNotepadData, normalizeRandomizerData, normalizeRngSimulatorData, normalizeTableData, normalizeTimerData, normalizeTaskPlannerData, summarizeEquipmentPlannerData, normalizeUrl, hostnameFromUrl, copyText, notify: actions.notify, compressImageFile: compressImage, clampQty, uid, setModuleData: updateModuleData, addImageFiles, createImageAnnotationModule, setImage: actions.setImage, refresh: () => {} }; const moduleLocations = useMemo(() => { const locations = new Map(); toolbox.spaces.forEach((space) => { space.moduleOrder.one.forEach((moduleId) => { const column = space.moduleOrder.two[1].includes(moduleId) ? "right" : "left"; locations.set(moduleId, { spaceId: space.id, column }); }); }); return locations; }, [toolbox.spaces]); const rawModuleReorder = useGroupedReorder({ namespace: `toolbox-modules-${toolbox.id}`, items: toolbox.modules, getItemId: (module) => module.id, getItemGroup: (module) => { const location = moduleLocations.get(module.id); return encodeSpaceColumn(location?.spaceId || toolbox.defaultSpaceId, moduleColumns === 1 ? "one" : location?.column || "left"); }, getEffectiveGroup: (module) => { const location = moduleLocations.get(module.id); return encodeSpaceColumn(location?.spaceId || toolbox.defaultSpaceId, moduleColumns === 1 ? "one" : location?.column || "left"); }, getParentId: () => toolbox.id, reorderFeatures: { item: { groupChange: true, boundaryDrop: true } }, canMoveItem: (operation) => operation.targetType !== "group" || decodeSpaceColumn(operation.sourceGroup).spaceId !== operation.targetGroup, onItemMove: (operation) => { let targetSpaceId = ""; let targetColumn = ""; let targetModuleId = ""; if (operation.targetType === "group") { targetSpaceId = operation.targetGroup; } else if (operation.targetType === "item") { targetModuleId = operation.targetId; targetSpaceId = getModuleSpace(toolbox, targetModuleId)?.id || ""; } else if (operation.targetType === "boundary") { const target = decodeSpaceColumn(operation.targetGroup); targetSpaceId = target.spaceId; targetColumn = target.column; } if (!targetSpaceId) return; const sourceSpace = getModuleSpace(toolbox, operation.sourceId); dragSucceededRef.current = true; updateToolbox(moveToolboxModule(toolbox, operation.sourceId, { targetSpaceId, targetModuleId, targetColumn, placement: operation.placement, moduleColumns })); if (sourceSpace?.id !== targetSpaceId) { const destination = toolbox.spaces.find((space) => space.id === targetSpaceId); actions.notify(spacesText.moveSuccess.replace("{space}", destination?.name || "")); } }, hierarchy: { enabled: true, stickyParents: true } }); const moduleReorder = { ...rawModuleReorder, itemReorder: { ...rawModuleReorder.itemReorder, startDrag: (event, moduleId) => { dragOriginSpaceRef.current = getModuleSpace(toolbox, moduleId)?.id || activeSpace.id; dragSucceededRef.current = false; hoverActivatedRef.current = false; rawModuleReorder.itemReorder.startDrag(event, moduleId); } } }; const activateHoveredSpace = useCallback((spaceId) => { if (spaceId === toolbox.activeSpaceId) return; hoverActivatedRef.current = true; updateToolbox({ ...toolbox, activeSpaceId: spaceId }); }, [toolbox, updateToolbox]); useEffect(() => { const draggingId = rawModuleReorder.itemReorder.draggingId; if (previousDraggingIdRef.current && !draggingId) { if (!dragSucceededRef.current && hoverActivatedRef.current && toolbox.spaces.some((space) => space.id === dragOriginSpaceRef.current)) { updateToolbox({ ...toolbox, activeSpaceId: dragOriginSpaceRef.current }); } dragOriginSpaceRef.current = ""; dragSucceededRef.current = false; hoverActivatedRef.current = false; } previousDraggingIdRef.current = draggingId; }, [rawModuleReorder.itemReorder.draggingId, toolbox, updateToolbox]); function addModule(type, initialData = null) { if (!type) return; const module = { id: uid("mod"), type }; updateToolbox(addModuleToToolboxSpace(toolbox, module, activeSpace.id)); setPendingModuleId(module.id); if (initialData) updateModuleData(toolbox.id, module.id, initialData, type); } function createImageAnnotationModule(dataUrl) { if (!dataUrl) return; const moduleId = uid("mod"); updateToolbox(addModuleToToolboxSpace(toolbox, { id: moduleId, type: "imageAnnotation" }, activeSpace.id)); setPendingModuleId(moduleId); updateModuleData(toolbox.id, moduleId, { image: dataUrl, markers: [] }, "imageAnnotation"); } return (

Toolbox

{!embedded && !toolboxGame && updateToolbox({ ...toolbox, icon, customCover })} tutorialTarget="icon" />} {!embedded && toolboxGameCover && } {embedded ?

{toolbox.name}

: ( name !== toolbox.name && updateToolbox({ ...toolbox, name })} /> )}
{toolboxGame && ( actions.setDrawerGameId("") : undefined} > {embedded ? "Vers la toolbox complète" : "Vers la page de jeu"} )}
{!embedded && showTutorial && ( { if (pendingTutorialToolboxId === toolbox.id) setPendingTutorialToolboxId?.(""); setShowTutorial(false); }} onStarted={() => { if (pendingTutorialToolboxId === toolbox.id) setPendingTutorialToolboxId?.(""); if (toolbox.tutorial === true) updateToolbox({ ...toolbox, tutorial: false }); }} /> )} {!embedded && (
)} setPendingModuleId("")} onRename={(moduleId, title) => updateToolbox({ ...toolbox, modules: toolbox.modules.map((module) => module.id === moduleId ? { ...module, title } : module) })} onUpdateModule={(moduleId, updater) => updateToolbox({ ...toolbox, modules: toolbox.modules.map((module) => module.id === moduleId ? updater(module) : module) })} onMoveToSpace={(moduleId, targetSpaceId) => { const destination = toolbox.spaces.find((space) => space.id === targetSpaceId); updateToolbox(moveToolboxModule(toolbox, moduleId, { targetSpaceId, moduleColumns })); actions.notify(spacesText.moveSuccess.replace("{space}", destination?.name || "")); }} onDelete={(moduleId) => actions.setConfirmModal({ title: "Retirer l'outil", message: "Retirer cet outil de la toolbox ?", confirmLabel: "Retirer", danger: true, onResolve: (confirmed) => { if (!confirmed) return; const removedModule = toolbox.modules.find((module) => module.id === moduleId); const remainingModules = toolbox.modules.filter((module) => module.id !== moduleId); if (removedModule?.type === "taskPlanner") { toolbox.modules.filter((module) => module.type === "memos").forEach((memosModule) => { const memosData = normalizeMemosData(getModuleData(toolbox.id, memosModule.id, { items: [] })); const nextMemosData = getMemosDataAfterPlannerRemoval(memosData, moduleId, remainingModules); if (nextMemosData !== memosData) updateModuleData(toolbox.id, memosModule.id, nextMemosData, "memos"); }); } actions.removeModuleData(toolbox.id, moduleId); updateToolbox(removeModuleFromToolboxSpaces(toolbox, moduleId)); } })} /> {!embedded && }
); } export function ToolboxDrawer({ gameId, game, siteContent, toolboxes, links, actions, storageUsage, storageReady = true, getModuleData, updateToolbox, updateModuleData, addImageFiles, visible = true, compact = false }) { const checklist = useGuideChecklist(); const guideChecklist = checklist.available && checklist.guideId.split("/")[0] === gameId ? checklist : undefined; const selectedId = links[gameId] || ""; const toolbox = toolboxes.find((item) => item.id === selectedId); const [width, setWidth] = useState(DRAWER_INITIAL_WIDTH); const panelRef = useRef(null); useEffect(() => { const clampToViewport = () => { if (window.innerWidth < DRAWER_INITIAL_WIDTH) return; setWidth((currentWidth) => Math.min(currentWidth, getDrawerMaxWidth())); }; window.addEventListener("resize", clampToViewport); return () => window.removeEventListener("resize", clampToViewport); }, []); function startResize(event) { const panel = panelRef.current; if (!panel) return; event.preventDefault(); const startX = event.clientX; const startWidth = panel.getBoundingClientRect().width; const maxWidth = getDrawerMaxWidth(); document.body.classList.add("is-resizing-drawer"); const onMove = (moveEvent) => { const nextWidth = Math.min(Math.max(startWidth + startX - moveEvent.clientX, DRAWER_INITIAL_WIDTH), maxWidth); setWidth(nextWidth); }; const stop = () => { document.body.classList.remove("is-resizing-drawer"); window.removeEventListener("pointermove", onMove); window.removeEventListener("pointerup", stop); window.removeEventListener("pointercancel", stop); }; window.addEventListener("pointermove", onMove); window.addEventListener("pointerup", stop); window.addEventListener("pointercancel", stop); } return ( ); } function ToolboxDrawerEmptyState({ gameId, toolboxes, links, actions, content }) { const linkedToolboxIds = useMemo(() => new Set(Object.values(links).filter(Boolean)), [links]); const availableToolboxes = useMemo( () => toolboxes.filter((toolbox) => !linkedToolboxIds.has(toolbox.id)), [linkedToolboxIds, toolboxes] ); const [selectedId, setSelectedId] = useState(""); const validSelectedId = availableToolboxes.some((toolbox) => toolbox.id === selectedId) ? selectedId : ""; const hasAvailableToolboxes = availableToolboxes.length > 0; return (

{content.emptyText}

{ event.preventDefault(); if (validSelectedId) actions.linkToolboxToGame(gameId, validSelectedId); }}>
); } function EditableTitle({ value, fallback, onSave, className = "module-title", tutorialTarget = "" }) { const inlineEdit = useInlineEdit({ value, onCommit: (title) => onSave(title || fallback), blurOnEscape: true }); return (

{value}

); }