From df1115022a6b71349ac512066410e92f7ca96602 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 18 Aug 2025 01:00:25 +0100 Subject: [PATCH] Refactor DashboardSidebar to support recursive item mapping - Introduced a recursive function to map sidebar items and their children, enhancing the structure and maintainability of the component. - Added onClick functionality for items with paths, improving navigation capabilities. - Improved code readability by restructuring the item mapping logic. --- .../Dashboard/common/DashboardSidebar.jsx | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/components/Dashboard/common/DashboardSidebar.jsx b/src/components/Dashboard/common/DashboardSidebar.jsx index 235ad34..abf82fe 100644 --- a/src/components/Dashboard/common/DashboardSidebar.jsx +++ b/src/components/Dashboard/common/DashboardSidebar.jsx @@ -38,18 +38,35 @@ const DashboardSidebar = ({ if (onCollapse) onCollapse(newCollapsed) } - // Add onClick to each item - const _items = items.map((item) => { - if (item?.type == 'divider') { - return item - } - return { - key: item.key, - icon: item.icon, - label: item.label, - onClick: () => navigate(item.path) - } - }) + // Recursive function to map items and their children + const mapItemsRecursively = (items) => { + return items.map((item) => { + if (item?.type === 'divider') { + return item + } + + const mappedItem = { + key: item.key, + icon: item.icon, + label: item.label + } + + // Add onClick for items with paths (leaf nodes) + if (item.path) { + mappedItem.onClick = () => navigate(item.path) + } + + // Recursively map children if they exist + if (item?.children && Array.isArray(item.children)) { + mappedItem.children = mapItemsRecursively(item.children) + } + + return mappedItem + }) + } + + // Map items recursively + const _items = mapItemsRecursively(items) if (isMobile) { return (