Refactor large toolbox modules
All checks were successful
Deploy Sokko G / deploy (push) Successful in 16s

This commit is contained in:
Shinuwa 2026-08-25 08:20:56 +02:00
parent c2e37ad3f2
commit 6dd64f87c3
19 changed files with 2042 additions and 1659 deletions

View file

@ -0,0 +1,147 @@
// 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 "./GoalTreeParts.jsx";
import { getVisibleGoalTreeEdges } from "./goalTreeDisplay.js";
export function GoalTreeView({
evaluatedNodes,
selectedNode,
editing,
reorder,
textContent,
treeOrientation,
verticalView,
viewScale,
storageReady,
textExchangeOpen,
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, textExchangeOpen]);
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, textExchangeOpen, viewScale]);
return (
<section className={`goal-tree-view tool-split-results ${verticalView ? "is-vertical" : "is-horizontal"} ${editing ? "is-editing" : ""}`} aria-label={textContent.treeTitle || "Arbre d'objectifs"}>
<button
className={`module-scroll-button goal-tree-orientation-switch ${verticalView ? "active" : ""}`}
type="button"
onClick={onToggleOrientation}
aria-label={verticalView ? textContent.horizontalViewTitle || "Afficher horizontalement" : textContent.verticalViewTitle || "Afficher verticalement"}
aria-pressed={verticalView}
title={verticalView ? textContent.horizontalViewTitle || "Afficher horizontalement" : textContent.verticalViewTitle || "Afficher verticalement"}
>
<Icon name="rotate" />
<i aria-hidden="true" />
</button>
<div className="goal-tree-scroll-area themed-scrollbar">
<div className="goal-tree-view-controls">
{editing && (
<button className="goal-tree-add-root-button" type="button" onClick={() => onAdd("")}>
<Icon name="add" />
<span>{textContent.addRootSchemaButton || "Ajouter un objectif a la racine"}</span>
</button>
)}
</div>
{evaluatedNodes.length ? (
<div
className="goal-tree-canvas-frame"
style={{
"--goal-tree-view-scale": viewScale,
"--goal-tree-canvas-width": `${Math.max(1, canvasSize.width) * viewScale}px`,
"--goal-tree-canvas-height": `${Math.max(1, canvasSize.height) * viewScale}px`
}}
>
<div className={`goal-tree-canvas ${verticalView ? "is-vertical" : "is-horizontal"}`} ref={treeViewRef}>
<GoalTreeBranch
nodes={evaluatedNodes}
parentId=""
selectedNodeId={selectedNode?.id || ""}
editing={editing}
reorder={reorder}
textContent={textContent}
onSelect={onSelect}
onAdd={onAdd}
onAddContentItem={onAddContentItem}
onSetStepLeafQuantity={onSetStepLeafQuantity}
onSetStepChildrenCompletion={onSetStepChildrenCompletion}
onUpdateNodeItem={onUpdateNodeItem}
onEditNode={onEditNode}
onToggleBranch={onToggleBranch}
onDeleteNode={onDeleteNode}
/>
<DiagramConnectors containerRef={treeViewRef} edges={treeEdges} refreshKey={connectorRefreshKey} className="goal-tree-connectors" stroke="#947436" scale={viewScale} orientation={treeOrientation} />
</div>
</div>
) : (
<div className="goal-tree-empty">
<p>{textContent.emptyTree || "Aucun objectif."}</p>
</div>
)}
</div>
</section>
);
}