/** * @file Rend les sections et items de checklist avec édition inline et quantités. */ import { useEffect, useRef, useState } from "react"; import { CategoryAutocompleteInput } from "../../../components/CategoryAutocompleteInput.jsx"; import { Icon } from "../../../components/Icon.jsx"; import { useInlineEdit } from "../../../hooks/useInlineEdit.js"; import { getSectionHideWhenComplete, isSectionComplete, keepChecklistSection } from "./checklistUtils.js"; export function ChecklistSection({ section, context, data, reorder, onRenameSection, onUpdateSection, onSaveSections, onUpdateItem, onMoveItemToCategory, onDeleteItem, categoryOptions = [] }) { const showTitle = Boolean(section.title); const sectionComplete = showTitle && isSectionComplete(section, context); const hideWhenComplete = getSectionHideWhenComplete(section, data.hideCompletedSections); const isAutoCollapsed = sectionComplete && hideWhenComplete; const isCollapsed = Boolean(section.collapsed) || isAutoCollapsed; const visibleItems = data.hideCompletedItems ? section.items.filter((item) => context.clampQty(item.qtyCurrent, item.qtyTarget) < item.qtyTarget) : section.items; const textContent = context.moduleText?.checklist || {}; const className = [ "checklist-section", showTitle ? "is-grouped" : "", sectionComplete ? "is-complete" : "", isCollapsed ? "is-collapsed" : "", reorder.isGroupDragging(section.id) ? "is-dragging" : "", reorder.isGroupDropTarget(section.id) ? "is-drop-target" : "", reorder.getDropPlacement("group", section.id) === "after" ? "drop-after" : "" ].filter(Boolean).join(" "); const titleEdit = useInlineEdit({ value: section.title, onCommit: (title) => onRenameSection(section.id, title) }); const [editingItemId, setEditingItemId] = useState(""); if (sectionComplete && data.hideCompletedSectionsFully) return null; function saveItems(items) { onUpdateSection(section.id, (entry) => ({ ...entry, items })); } function toggleSectionCollapsed() { const nextCollapsed = !isCollapsed; const nextSections = data.sections.map((entry) => { if (entry.id === section.id) { const nextSection = { ...entry }; if (nextCollapsed) { if (sectionComplete) { nextSection.hideWhenComplete = true; delete nextSection.collapsed; } else { nextSection.collapsed = true; } } else { delete nextSection.collapsed; if (sectionComplete) nextSection.hideWhenComplete = false; } return nextSection; } if (data.hideCompletedSections && !nextCollapsed && entry.title && isSectionComplete(entry, context)) return { ...entry, hideWhenComplete: true }; return entry; }); const nextCompletedSections = nextSections.filter((entry) => entry.title && isSectionComplete(entry, context)); const allHidden = nextCompletedSections.length > 0 && nextCompletedSections.every((entry) => getSectionHideWhenComplete(entry, false)); onSaveSections(nextSections.filter(keepChecklistSection), { hideCompletedSections: allHidden }); } return (