sokko-g/website/src/features/toolboxes/modules/LinksModule.jsx
Shinuwa 22b397ebfa Document Sokko G's architecture and folders, and move JavaScript file headers to JSDoc @file blocks
Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md
(setup and step-by-step recipes) and a README in each main folder, with an
index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to
JSDoc @file blocks, define a three-level header template in website/AGENTS.md
and check the header in tests/file-headers.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:42:13 +01:00

298 lines
13 KiB
JavaScript

/**
* @file Fournit l'outil liens avec ajout manuel et échange texte.
*/
import { toggleListValue } from "../../../utils/collectionUtils.js";
import { useState } from "react";
import { CategoryAutocompleteInput } from "../../../components/CategoryAutocompleteInput.jsx";
import { Icon } from "../../../components/Icon.jsx";
import { useDraftForm } from "../../../hooks/useDraftForm.js";
import { applyGroupedReorderOperation, useGroupedReorder } from "../../../hooks/useGroupedReorder.js";
import { ModuleTextExchangeConfiguration } from "./ModuleConfiguration.jsx";
import { ModuleEmptyMessage } from "./ModuleEmptyMessage.jsx";
function getLinkCategory(link) {
return String(link?.category || "").trim();
}
function setLinkCategory(link, category) {
const cleanCategory = String(category || "").trim();
const nextLink = { ...link };
if (cleanCategory) nextLink.category = cleanCategory;
else delete nextLink.category;
return nextLink;
}
function getLinkEntries(data) {
const ungrouped = data.links.filter((link) => !getLinkCategory(link)).map((link) => ({ type: "item", item: link }));
const categories = data.categoryOrder.map((category) => ({
type: "group",
group: {
category,
links: data.links.filter((link) => getLinkCategory(link) === category)
}
}));
return [...ungrouped, ...categories];
}
export function LinksModule({ toolboxId, moduleId, context, editing, configurationTarget }) {
const data = context.normalizeLinksData(context.getModuleData(toolboxId, moduleId, { links: [] }));
const textContent = context.moduleText?.links || {};
const linkDraft = useDraftForm({ title: "", url: "", category: "" });
const categoryOptions = data.categoryOrder;
const [copiedId, setCopiedId] = useState("");
const {
itemReorder,
groupReorder,
getItemProps,
getGroupProps,
getGroupBoundaryProps,
isItemDragging,
isItemDropTarget,
isGroupDragging,
isGroupDropTarget,
isBoundaryDropTarget,
getDropPlacement
} = useGroupedReorder({
namespace: `links-${moduleId}`,
items: data.links,
getItemId: (link) => link.id,
getItemGroup: getLinkCategory,
reorderFeatures: {
item: {
groupChange: true,
boundaryDrop: true,
ungroupedDrop: true
},
group: {
reorder: true,
boundaryDrop: true,
ungroupedDrop: true,
itemDrop: true
}
},
onItemMove: applyLinkReorder,
onGroupMove: applyLinkReorder,
hierarchy: { enabled: false, stickyParents: false }
});
function save(nextData) {
context.setModuleData(toolboxId, moduleId, nextData, "links");
}
function applyLinkReorder(operation) {
if (operation.type === "group" && data.categoryOrder.includes(operation.sourceId)) {
const categoryOrder = data.categoryOrder.filter((category) => category !== operation.sourceId);
const targetIndex = categoryOrder.indexOf(operation.targetId);
if (targetIndex >= 0) {
categoryOrder.splice(operation.placement === "after" ? targetIndex + 1 : targetIndex, 0, operation.sourceId);
save({ ...data, categoryOrder });
return;
}
}
const nextData = applyGroupedReorderOperation(data, {
operation,
itemsKey: "links",
groupOrderKey: "categoryOrder",
collapsedGroupsKey: "collapsedCategories",
getItemGroup: getLinkCategory,
setItemGroup: setLinkCategory
});
save({
...nextData,
categoryOrder: [
...data.categoryOrder,
...nextData.categoryOrder.filter((category) => !data.categoryOrder.includes(category))
]
});
}
function addLink(event) {
linkDraft.handleSubmit(event, (draft, { reset }) => {
const cleanUrl = context.normalizeUrl(draft.url);
const category = draft.category.trim();
if (!cleanUrl) {
if (category && !data.categoryOrder.includes(category)) {
save({ ...data, categoryOrder: [...data.categoryOrder, category] });
reset();
}
return;
}
const categoryOrder = category && !data.categoryOrder.includes(category) ? [...data.categoryOrder, category] : data.categoryOrder;
save({
...data,
categoryOrder,
links: [
...data.links,
{
id: context.uid("link"),
title: draft.title.trim(),
url: cleanUrl,
...(category ? { category } : {})
}
]
});
reset();
});
}
function deleteCategory(category) {
save({
...data,
categoryOrder: data.categoryOrder.filter((item) => item !== category),
collapsedCategories: data.collapsedCategories.filter((item) => item !== category),
links: data.links.map((link) => getLinkCategory(link) === category ? setLinkCategory(link, "") : link)
});
}
function toggleCategoryCollapsed(category) {
const collapsedCategories = toggleListValue(data.collapsedCategories, category);
save({ ...data, collapsedCategories });
}
async function copyUrl(link) {
const copied = await context.copyText(link.url);
if (!copied) return;
setCopiedId(link.id);
context.notify?.(textContent.copiedTitle || "Copié");
window.setTimeout(() => setCopiedId(""), 1400);
}
return (
<>
{editing && (
<div className="module-add-panel">
<form className="inline-form links-add-form" onSubmit={addLink}>
<input {...linkDraft.getFieldProps("title", { className: "nds-input", "data-size": "sm", type: "text", placeholder: textContent.titlePlaceholder || "Nom du lien" })} />
<input {...linkDraft.getFieldProps("url", { className: "nds-input", "data-size": "sm", type: "url", placeholder: textContent.urlPlaceholder || "https://..." })} />
<CategoryAutocompleteInput {...linkDraft.getFieldProps("category", { className: "nds-input", "data-size": "sm", placeholder: textContent.categoryPlaceholder || "Catégorie" })} options={categoryOptions} />
<button className="nds-button" data-size="sm" data-variant="primary">{textContent.addButton || "Ajouter"}</button>
</form>
</div>
)}
<ModuleTextExchangeConfiguration target={configurationTarget} title={context.moduleConfigurationText?.dataTitle} type="links" data={data} context={context} textContent={textContent} onImport={(nextData) => context.setModuleData(toolboxId, moduleId, nextData, "links")} />
{!data.links.length && <ModuleEmptyMessage>{textContent.emptyLinks || "Aucun lien."}</ModuleEmptyMessage>}
<div className="links-category-list">
{getLinkEntries(data).map((entry) => {
if (entry.type === "group") {
const category = entry.group.category;
const isCollapsed = data.collapsedCategories.includes(category);
const showBoundaryDropZones = Boolean(itemReorder.draggingId || groupReorder.draggingId);
const categoryClassName = [
"links-category-section checklist-section is-grouped",
isCollapsed ? "is-collapsed" : "",
isGroupDragging(category) ? "is-dragging" : "",
isGroupDropTarget(category) ? "is-drop-target" : "",
getDropPlacement("group", category) === "after" ? "drop-after" : ""
].filter(Boolean).join(" ");
return (
<div className="links-category-block" key={`category:${category}`}>
{showBoundaryDropZones && (
<div
className={`links-category-boundary-drop-zone ${isBoundaryDropTarget({ groupId: category, placement: "before" }) ? "is-drop-target" : ""}`}
{...getGroupBoundaryProps({ groupId: category, placement: "before" })}
/>
)}
<section className={categoryClassName} {...getGroupProps({ groupId: category })}>
<div className="checklist-section-header links-category-header">
<div className="links-category-title">
<button
className="task-planner-category-drag-handle nds-icon-button"
data-size="xs"
data-variant="secondary"
type="button"
onPointerDown={(event) => groupReorder.startDrag(event, { groupId: category })}
aria-label={`${textContent.reorderCategoryTitle || "Déplacer la catégorie"} ${category}`}
title={textContent.reorderCategoryTitle || "Déplacer la catégorie"}
>
<Icon name="drag" />
</button>
<h3>{category}</h3>
</div>
<div className="checklist-section-actions links-category-actions">
<span>{entry.group.links.length}</span>
<button
className="checklist-section-collapse-button nds-icon-button"
data-size="xs"
type="button"
onClick={() => toggleCategoryCollapsed(category)}
aria-expanded={!isCollapsed}
aria-label={isCollapsed ? textContent.showCategoryTitle || "Afficher la catégorie" : textContent.hideCategoryTitle || "Réduire la catégorie"}
title={isCollapsed ? textContent.showCategoryTitle || "Afficher la catégorie" : textContent.hideCategoryTitle || "Réduire la catégorie"}
>
<Icon name={isCollapsed ? "chevron-down" : "chevron-up"} />
</button>
<button
className="nds-icon-button"
data-size="xs"
data-variant="danger"
type="button"
onClick={() => deleteCategory(category)}
aria-label={`${textContent.deleteCategoryTitle || "Supprimer la catégorie"} ${category}`}
title={textContent.deleteCategoryTitle || "Supprimer la catégorie"}
>
<Icon name="trash" />
</button>
</div>
</div>
{!isCollapsed && (
<div className="links-list">
{entry.group.links.map(renderLink)}
</div>
)}
</section>
{showBoundaryDropZones && (
<div
className={`links-category-boundary-drop-zone ${isBoundaryDropTarget({ groupId: category, placement: "after" }) ? "is-drop-target" : ""}`}
{...getGroupBoundaryProps({ groupId: category, placement: "after" })}
/>
)}
</div>
);
}
return renderLink(entry.item);
})}
</div>
</>
);
function renderLink(link) {
const className = [
"link-item",
isItemDragging(link.id) ? "is-dragging" : "",
isItemDropTarget(link.id) ? "is-drop-target" : "",
getDropPlacement("item", link.id) === "after" ? "drop-after" : ""
].filter(Boolean).join(" ");
return (
<article className={className} key={link.id} {...getItemProps({ itemId: link.id, groupId: getLinkCategory(link) })}>
<button
className="link-drag-handle nds-icon-button"
data-size="sm"
data-variant="secondary"
type="button"
onPointerDown={(event) => itemReorder.startDrag(event, link.id)}
aria-label={`${textContent.reorderTitle || "Déplacer"} ${link.title || link.url}`}
title={textContent.reorderTitle || "Déplacer"}
>
<Icon name="drag" />
</button>
<a href={link.url} target="_blank" rel="noreferrer" title={link.url}>
<strong>{link.title || context.hostnameFromUrl(link.url)}</strong>
<span>{link.url}</span>
</a>
<div>
<button type="button" className="nds-icon-button" data-size="sm" onClick={() => copyUrl(link)} aria-label={`${textContent.copyTitle || "Copier"} ${link.title || link.url}`} title={copiedId === link.id ? textContent.copiedTitle || "Copié" : textContent.copyTitle || "Copier"}>
<Icon name="copy" />
</button>
<button type="button" className="nds-icon-button" data-size="sm" data-variant="danger" onClick={() => save({ ...data, links: data.links.filter((item) => item.id !== link.id) })} aria-label={`${textContent.deleteTitle || "Supprimer"} ${link.title || link.url}`} title={textContent.deleteTitle || "Supprimer"}>
<Icon name="trash" />
</button>
</div>
</article>
);
}
}