Add library tools to local toolboxes
This commit is contained in:
parent
daa5eba8bd
commit
ec7a4a4c1d
15 changed files with 650 additions and 9 deletions
|
|
@ -1,8 +1,10 @@
|
|||
// Rôle : affiche la librairie des outils avec des exemples locaux non persistés.
|
||||
import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { Icon } from "../components/Icon.jsx";
|
||||
import { LibraryCopyModal } from "../components/LibraryCopyModal.jsx";
|
||||
import { LIBRARY_CATEGORY_DEFINITIONS, getToolboxModuleDefinition } from "../features/toolboxes/toolboxModuleRegistry.js";
|
||||
import { MODULE_COMPONENTS } from "../features/toolboxes/modules/toolboxModuleComponents.jsx";
|
||||
import { navigate } from "../router/hashRouter.js";
|
||||
import {
|
||||
clampQty,
|
||||
hostnameFromUrl,
|
||||
|
|
@ -116,11 +118,12 @@ function getLibraryCategories(modules, content) {
|
|||
})).filter((category) => category.modules.length);
|
||||
}
|
||||
|
||||
export function LibraryPage({ siteContent, actions }) {
|
||||
export function LibraryPage({ siteContent, actions, toolboxes = [], storageReady = true }) {
|
||||
const content = siteContent.library;
|
||||
const [libraryPayload, setLibraryPayload] = useState(null);
|
||||
const [libraryError, setLibraryError] = useState("");
|
||||
const [moduleData, setModuleData] = useState({});
|
||||
const [copyRequest, setCopyRequest] = useState(null);
|
||||
const [navigation, setNavigation] = useState(() => ({ anchorId: getCurrentLibraryAnchor(), behavior: "auto" }));
|
||||
const libraryCategories = useMemo(() => getLibraryCategories(libraryPayload?.toolbox?.modules || [], content), [content, libraryPayload]);
|
||||
const activeCategory = findCategoryForAnchor(libraryCategories, navigation.anchorId) || libraryCategories[0] || null;
|
||||
|
|
@ -216,6 +219,29 @@ export function LibraryPage({ siteContent, actions }) {
|
|||
}
|
||||
}), [actions, libraryPayload, moduleData, selectAnchor, siteContent.toolboxes.modules]);
|
||||
|
||||
const openCopyModal = useCallback((module) => {
|
||||
const sourceData = cloneData(moduleData[`${LIBRARY_TOOLBOX_ID}:${module.id}`] || libraryPayload?.modules?.[module.id] || {});
|
||||
const dependencyDefinition = getToolboxModuleDefinition(module.type).copyDependency;
|
||||
const dependencyId = dependencyDefinition && sourceData[dependencyDefinition.dataField] && sourceData[dependencyDefinition.dataField] !== "all"
|
||||
? sourceData[dependencyDefinition.dataField]
|
||||
: "";
|
||||
const dependency = dependencyId
|
||||
? libraryPayload?.toolbox?.modules?.find((item) => item.id === dependencyId && item.type === dependencyDefinition.type)
|
||||
: null;
|
||||
setCopyRequest({
|
||||
toolTitle: module.title || getToolboxModuleDefinition(module.type).label || "Outil",
|
||||
sourceModule: { id: module.id, type: module.type, title: module.title || "", data: sourceData },
|
||||
sourceData,
|
||||
dependencyModules: dependency ? [{
|
||||
id: dependency.id,
|
||||
type: dependency.type,
|
||||
title: dependency.title || "",
|
||||
data: cloneData(moduleData[`${LIBRARY_TOOLBOX_ID}:${dependency.id}`] || libraryPayload?.modules?.[dependency.id] || {})
|
||||
}] : [],
|
||||
missingDependency: Boolean(dependencyId && !dependency)
|
||||
});
|
||||
}, [libraryPayload, moduleData]);
|
||||
|
||||
return (
|
||||
<div className="library-page">
|
||||
<section className="page-hero nds-page-hero library-hero">
|
||||
|
|
@ -279,6 +305,9 @@ export function LibraryPage({ siteContent, actions }) {
|
|||
advancedCopySuccess={content.copyExampleSuccess}
|
||||
controlLegendHeading={content.controlLegendHeading}
|
||||
context={moduleContext}
|
||||
copyContent={content.copyToToolbox}
|
||||
storageReady={storageReady}
|
||||
onCopy={() => openCopyModal(module)}
|
||||
/>
|
||||
))}
|
||||
</section>
|
||||
|
|
@ -287,6 +316,29 @@ export function LibraryPage({ siteContent, actions }) {
|
|||
</div>
|
||||
</>
|
||||
)}
|
||||
{copyRequest && <LibraryCopyModal
|
||||
content={content.copyToToolbox}
|
||||
toolTitle={copyRequest.toolTitle}
|
||||
toolboxes={toolboxes}
|
||||
hasDependency={copyRequest.dependencyModules.length > 0}
|
||||
onClose={() => setCopyRequest(null)}
|
||||
onConfirm={async (selection) => {
|
||||
if (copyRequest.missingDependency) return false;
|
||||
const result = await actions.addLibraryToolToToolbox({
|
||||
sourceModule: copyRequest.sourceModule,
|
||||
sourceData: copyRequest.sourceData,
|
||||
dependencyModules: selection.copyData ? copyRequest.dependencyModules : [],
|
||||
...selection
|
||||
});
|
||||
if (!result) return false;
|
||||
setCopyRequest(null);
|
||||
actions.notify(content.copyToToolbox.success.replace("{toolbox}", result.toolboxName), {
|
||||
actionLabel: content.copyToToolbox.openLabel,
|
||||
onAction: () => navigate(`/toolbox/${result.toolboxId}`)
|
||||
});
|
||||
return true;
|
||||
}}
|
||||
/>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -335,7 +387,7 @@ function LibraryDocNav({ content, categories, activeCategoryKey, onSelectCategor
|
|||
);
|
||||
}
|
||||
|
||||
function LibraryToolExample({ module, sourceData, description, categoryTitle, docs, featureHeading, advancedHeading, advancedCopyLabel, advancedCopySuccess, controlLegendHeading, context }) {
|
||||
function LibraryToolExample({ module, sourceData, description, categoryTitle, docs, featureHeading, advancedHeading, advancedCopyLabel, advancedCopySuccess, controlLegendHeading, context, copyContent, storageReady, onCopy }) {
|
||||
const definition = getToolboxModuleDefinition(module.type);
|
||||
const { Component } = MODULE_COMPONENTS[module.type] || MODULE_COMPONENTS.notepad;
|
||||
const [scrollable, setScrollable] = useState(false);
|
||||
|
|
@ -354,6 +406,13 @@ function LibraryToolExample({ module, sourceData, description, categoryTitle, do
|
|||
</div>
|
||||
<span className="library-tool-badge nds-badge">{categoryTitle}</span>
|
||||
{description && <p>{description}</p>}
|
||||
<div className="library-tool-copy-action">
|
||||
<p>{copyContent.isolationHelp}</p>
|
||||
<button className="nds-button" data-size="sm" data-variant="primary" type="button" disabled={!storageReady} onClick={onCopy}>
|
||||
<Icon name="add" />
|
||||
{copyContent.triggerLabel}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className={`library-example-layout ${Array.isArray(docs?.controls) && docs.controls.length ? "has-legend" : ""}`}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue