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.
This commit is contained in:
Tom Butcher 2025-08-18 01:00:25 +01:00
parent b74a4bb174
commit df1115022a

View File

@ -38,18 +38,35 @@ const DashboardSidebar = ({
if (onCollapse) onCollapse(newCollapsed) if (onCollapse) onCollapse(newCollapsed)
} }
// Add onClick to each item // Recursive function to map items and their children
const _items = items.map((item) => { const mapItemsRecursively = (items) => {
if (item?.type == 'divider') { return items.map((item) => {
return item if (item?.type === 'divider') {
} return item
return { }
key: item.key,
icon: item.icon, const mappedItem = {
label: item.label, key: item.key,
onClick: () => navigate(item.path) 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) { if (isMobile) {
return ( return (