// RĂ´le : rend le canvas de l'arbre d'objectifs, son orientation et ses connecteurs. import { useLayoutEffect, useMemo, useRef, useState } from "react"; import { DiagramConnectors } from "../../../components/DiagramConnectors.jsx"; import { Icon } from "../../../components/Icon.jsx"; import { GoalTreeBranch } from "./GoalTreeBranch.jsx"; import { getVisibleGoalTreeEdges } from "./goalTreeDisplay.js"; export function GoalTreeView({ evaluatedNodes, selectedNode, editing, reorder, textContent, treeOrientation, verticalView, viewScale, storageReady, onSelect, onAdd, onAddContentItem, onSetStepLeafQuantity, onSetStepChildrenCompletion, onUpdateNodeItem, onEditNode, onToggleBranch, onDeleteNode, onToggleOrientation }) { const [canvasSize, setCanvasSize] = useState({ width: 0, height: 0 }); const treeViewRef = useRef(null); const treeEdges = useMemo(() => getVisibleGoalTreeEdges(evaluatedNodes), [evaluatedNodes]); const connectorTopologyKey = useMemo(() => [ treeOrientation, treeEdges.map((edge) => `${edge.from}>${edge.to}`).join("|"), evaluatedNodes.map((node) => `${node.id}:${node.kind}:${node.collapsed ? 1 : 0}:${node.progress.ratio}`).join("|") ].join("::"), [evaluatedNodes, treeEdges, treeOrientation]); const [connectorRefreshTick, setConnectorRefreshTick] = useState(0); const connectorRefreshKey = `${storageReady ? "ready" : "loading"}::${connectorTopologyKey}::${connectorRefreshTick}`; useLayoutEffect(() => { if (!storageReady || !evaluatedNodes.length) return undefined; let firstFrameId = 0; let secondFrameId = 0; const timeoutIds = []; const refreshConnectors = () => setConnectorRefreshTick((value) => value + 1); firstFrameId = requestAnimationFrame(() => { refreshConnectors(); secondFrameId = requestAnimationFrame(refreshConnectors); }); timeoutIds.push(setTimeout(refreshConnectors, 80)); timeoutIds.push(setTimeout(refreshConnectors, 240)); window.addEventListener("load", refreshConnectors); return () => { cancelAnimationFrame(firstFrameId); cancelAnimationFrame(secondFrameId); timeoutIds.forEach(clearTimeout); window.removeEventListener("load", refreshConnectors); }; }, [storageReady, connectorTopologyKey, evaluatedNodes.length, editing]); useLayoutEffect(() => { const canvas = treeViewRef.current; if (!canvas) return undefined; let frameId = 0; const measureCanvas = () => { const rect = canvas.getBoundingClientRect(); const nextSize = { width: Math.ceil(Math.max(canvas.scrollWidth, canvas.offsetWidth, rect.width / viewScale)), height: Math.ceil(Math.max(canvas.scrollHeight, canvas.offsetHeight, rect.height / viewScale)) }; setCanvasSize((current) => current.width === nextSize.width && current.height === nextSize.height ? current : nextSize); }; const scheduleMeasure = () => { cancelAnimationFrame(frameId); frameId = requestAnimationFrame(measureCanvas); }; const resizeObserver = new ResizeObserver(scheduleMeasure); resizeObserver.observe(canvas); scheduleMeasure(); return () => { cancelAnimationFrame(frameId); resizeObserver.disconnect(); }; }, [connectorTopologyKey, editing, viewScale]); return (