From 9f628aa9f4858a66b8e7b6f4b24055b1a397f67c Mon Sep 17 00:00:00 2001 From: le king fu Date: Wed, 15 Jul 2026 21:30:17 -0400 Subject: [PATCH] refactor(categories): unify both category trees' collapse state onto useCollapsibleGroups Replace the two hand-rolled Set-of-ids collapse state machines in the category trees with the shared useCollapsibleGroups hook (a strict superset after #288), keeping each tree's distinct recursive render and CategoryTree's drag-and-drop untouched. - CategoryTree (Categories page): storageKey null + defaultExpanded true, so every parent opens with no seeding; drops the local Set + collectExpandable. - CategoryTaxonomyTree + guide page: storageKey null + defaultExpanded false (collapsed by default); exports a shared TAXONOMY_COLLAPSE_ACCESSORS. - Fix the guide's button bug: allExpanded = expanded.size > 0 flipped to "Collapse all" after opening a single node; now uses the hook's correct allExpanded (every group must be open). - Also migrate StepDiscover (4th consumer of CategoryTaxonomyTree, same button bug) onto the hook for a green build and consistency. Both trees pass a flattened node list to the bulk ops; behaviour preserved: Categories opens expanded, the guide/wizard open collapsed. Resolves #290 Generated autonomously by /autopilot run of 2026-07-15 Co-Authored-By: Claude Opus 4.8 (1M context) --- CHANGELOG.fr.md | 4 ++ CHANGELOG.md | 4 ++ .../categories-migration/StepDiscover.tsx | 47 +++++++------- .../categories/CategoryTaxonomyTree.tsx | 36 ++++++++--- src/components/categories/CategoryTree.tsx | 64 ++++++++++--------- src/pages/CategoriesStandardGuidePage.tsx | 51 ++++++++------- 6 files changed, 118 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.fr.md b/CHANGELOG.fr.md index 6b86224..945ac24 100644 --- a/CHANGELOG.fr.md +++ b/CHANGELOG.fr.md @@ -7,6 +7,10 @@ - Rapports : les trois tableaux de rapport hiérarchiques (Comparaison réel-vs-réel, réel-vs-budget, et Tendances par catégorie) permettent désormais de **replier ou déplier chaque niveau de la hiérarchie de catégories**, plus seulement le premier. Chaque catégorie parente — y compris les intermédiaires — porte son propre chevron ; en déplier une révèle ses enfants directs (eux-mêmes repliés), pour descendre un niveau à la fois. Les tableaux s'ouvrent entièrement repliés (seules les catégories de premier niveau visibles), « Tout déplier » ouvre tous les niveaux d'un coup, et vos choix de repli sont désormais enregistrés **par profil** — dans la base de données du profil, donc détruits avec lui — au lieu du navigateur. Les sous-totaux et les lignes de résultat restent rigoureusement inchangés quel que soit ce que vous repliez (#288). - Budget : la grille budget se **replie et se déplie elle aussi à chaque niveau de catégorie**, à l'image des rapports. Chaque catégorie parente porte un chevron, la grille **s'ouvre entièrement repliée** (seules les catégories de premier niveau visibles) pour un aperçu compact, et « Tout déplier » ouvre tous les niveaux en un clic. Replier une catégorie est purement visuel — les totaux de section et les lignes de résultat sont inchangés, et aucune valeur de budget que vous avez saisie n'est jamais perdue. Vos choix de repli sont enregistrés par profil (#289). +### Corrigé + +- Arbre des catégories standard (le guide autonome comme l'étape d'aperçu de l'assistant de migration des catégories) : le bouton « Tout déplier / Tout replier » reflète désormais l'état réel de l'arbre — il reste sur « Tout déplier » tant que tous les groupes ne sont pas ouverts, au lieu de basculer sur « Tout replier » dès qu'une seule catégorie était dépliée (#290). + ## [0.13.0] - 2026-07-12 ### Ajouté diff --git a/CHANGELOG.md b/CHANGELOG.md index c66b933..b2a1b30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ - Reports: the three hierarchical report tables (real-vs-real Compare, real-vs-budget Compare, and Trends by category) now let you **collapse or expand every level of the category hierarchy**, not just the top level. Every parent category — including the intermediate ones — carries its own chevron; expanding one reveals its direct children (themselves collapsed), so you drill down one level at a time. The tables open fully collapsed (only the top-level categories visible), "Expand all" opens every level at once, and your collapse choices are now saved **per profile** — in the profile's own database, so they are destroyed with the profile — instead of in the browser. Subtotals and result lines stay exactly the same whatever you fold away (#288). - Budget: the budget grid now **collapses and expands at every category level** too, matching the reports. Each parent category carries a chevron, the grid **opens fully collapsed** (only the top-level categories visible) for a compact overview, and "Expand all" opens every level in one click. Folding a category is purely visual — section totals and the result lines are unchanged, and no budget figure you have typed is ever lost. Your collapse choices are saved per profile (#289). +### Fixed + +- Standard categories tree (both the standalone guide and the category-migration wizard's overview step): the "Expand all / Collapse all" button now reflects the tree's real state — it stays on "Expand all" until every group is open, instead of flipping to "Collapse all" as soon as a single category was expanded (#290). + ## [0.13.0] - 2026-07-12 ### Added diff --git a/src/components/categories-migration/StepDiscover.tsx b/src/components/categories-migration/StepDiscover.tsx index 7793b33..e90ac0c 100644 --- a/src/components/categories-migration/StepDiscover.tsx +++ b/src/components/categories-migration/StepDiscover.tsx @@ -1,22 +1,27 @@ -import { useState } from "react"; +import { useMemo, useState } from "react"; import { useTranslation } from "react-i18next"; import { ArrowRight, ChevronsDownUp, ChevronsUpDown, Search } from "lucide-react"; import { useCategoryTaxonomy } from "../../hooks/useCategoryTaxonomy"; -import CategoryTaxonomyTree from "../categories/CategoryTaxonomyTree"; +import CategoryTaxonomyTree, { + TAXONOMY_COLLAPSE_ACCESSORS, +} from "../categories/CategoryTaxonomyTree"; import type { TaxonomyNode } from "../../services/categoryTaxonomyService"; +import { useCollapsibleGroups } from "../../hooks/useCollapsibleGroups"; interface StepDiscoverProps { onNext: () => void; } -function collectAllIds(nodes: TaxonomyNode[]): number[] { - const ids: number[] = []; +// Flattens the taxonomy so the hook's bulk ops (expand/collapse all, allExpanded) +// can walk every parent at any depth. +function flattenNodes(nodes: TaxonomyNode[]): TaxonomyNode[] { + const flat: TaxonomyNode[] = []; const walk = (n: TaxonomyNode) => { - ids.push(n.id); + flat.push(n); n.children.forEach(walk); }; nodes.forEach(walk); - return ids; + return flat; } function countNodes(nodes: TaxonomyNode[]): { @@ -52,25 +57,23 @@ export default function StepDiscover({ onNext }: StepDiscoverProps) { const { t } = useTranslation(); const { taxonomy } = useCategoryTaxonomy(); const [search, setSearch] = useState(""); - const [expanded, setExpanded] = useState>(() => new Set()); + + // State machine only (issue #290): in-memory (storageKey null), collapsed by + // default — same as the standalone guide page it shares CategoryTaxonomyTree with. + const groups = useCollapsibleGroups(null, TAXONOMY_COLLAPSE_ACCESSORS, { + defaultExpanded: false, + }); const counts = countNodes(taxonomy.roots); const total = counts.roots + counts.subcategories + counts.leaves; - const toggleNode = (id: number) => { - setExpanded((prev) => { - const next = new Set(prev); - if (next.has(id)) next.delete(id); - else next.add(id); - return next; - }); - }; + const flatNodes = useMemo(() => flattenNodes(taxonomy.roots), [taxonomy.roots]); - const handleExpandAll = () => { - setExpanded(new Set(collectAllIds(taxonomy.roots))); - }; - const handleCollapseAll = () => setExpanded(new Set()); - const allExpanded = expanded.size > 0; + const handleExpandAll = () => groups.expandAll(flatNodes); + const handleCollapseAll = () => groups.collapseAll(flatNodes); + // Correct "all expanded" test (issue #290): every group must be open, fixing the + // old expanded.size > 0 bug that flipped the button after a single expand. + const allExpanded = groups.allExpanded(flatNodes); return (
@@ -146,8 +149,8 @@ export default function StepDiscover({ onNext }: StepDiscoverProps) {
diff --git a/src/components/categories/CategoryTaxonomyTree.tsx b/src/components/categories/CategoryTaxonomyTree.tsx index aba341e..675d348 100644 --- a/src/components/categories/CategoryTaxonomyTree.tsx +++ b/src/components/categories/CategoryTaxonomyTree.tsx @@ -2,33 +2,49 @@ import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { ChevronRight, ChevronDown } from "lucide-react"; import type { TaxonomyNode } from "../../services/categoryTaxonomyService"; +import type { CollapseAccessors } from "../../utils/collapsibleRows"; + +/** + * Collapse-state accessors for the taxonomy tree, shared by every consumer that + * drives it through `useCollapsibleGroups` (the guide page and the migration + * wizard's Discover step) — issue #290. Only keyOf/isParent are consulted: the + * tree renders recursively and gates each node on `isCollapsed(node)`, so it never + * calls `visibleRows`; parentKeyOf/depthOf are unused stubs (TaxonomyNode carries + * no parent_id). + */ +export const TAXONOMY_COLLAPSE_ACCESSORS: CollapseAccessors = { + keyOf: (node) => `p:${node.id}`, + parentKeyOf: () => null, + isParent: (node) => node.children.length > 0, + depthOf: () => 0, +}; interface CategoryTaxonomyTreeProps { nodes: TaxonomyNode[]; - expanded: Set; - onToggle: (id: number) => void; + isCollapsed: (node: TaxonomyNode) => boolean; + onToggle: (node: TaxonomyNode) => void; searchQuery: string; } interface NodeRowProps { node: TaxonomyNode; depth: number; - expanded: Set; - onToggle: (id: number) => void; + isCollapsed: (node: TaxonomyNode) => boolean; + onToggle: (node: TaxonomyNode) => void; visibleIds: Set | null; } function NodeRow({ node, depth, - expanded, + isCollapsed, onToggle, visibleIds, }: NodeRowProps) { const { t } = useTranslation(); const label = t(node.i18n_key, { defaultValue: node.name }); const hasChildren = node.children.length > 0; - const isExpanded = expanded.has(node.id); + const isExpanded = !isCollapsed(node); // Filter children by visibility set (search mode) if provided. const visibleChildren = useMemo(() => { @@ -63,7 +79,7 @@ function NodeRow({ {hasChildren ? (