From ccba9991df4c365d584f644cb162f5e37935de78 Mon Sep 17 00:00:00 2001 From: Eric NGUYEN Date: Fri, 16 Sep 2022 16:52:10 +0200 Subject: [PATCH] Fix sort of categories + improve perf --- src/Components/Sidebar/Sidebar.tsx | 33 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/Components/Sidebar/Sidebar.tsx b/src/Components/Sidebar/Sidebar.tsx index ec2bab1..85fa2d1 100644 --- a/src/Components/Sidebar/Sidebar.tsx +++ b/src/Components/Sidebar/Sidebar.tsx @@ -15,19 +15,31 @@ function HandleDragStart(event: React.DragEvent): void { event.dataTransfer.setData('type', (event.target as HTMLButtonElement).id); } -interface CategoryWrapper { +interface SidebarCategory { category: ICategory children: JSX.Element[] } export function Sidebar(props: ISidebarProps): JSX.Element { const rootElements: Array = []; - const categories = new Map(props.categories.map(category => { - return [category.Type, { + const categories = new Map(props.categories.map(category => [ + category.Type, + { category, children: [] - }]; - })); + } + ])); + + // build the categories (sorted with categories first) + categories.forEach((categoryWrapper, categoryName) => { + rootElements.push( + + { categoryWrapper.children } + ); + }); // build the components props.componentOptions.forEach(componentOption => { @@ -60,17 +72,6 @@ export function Sidebar(props: ISidebarProps): JSX.Element { category.children.push(componentButton); }); - // build the categories - categories.forEach((categoryWrapper, categoryName) => { - rootElements.unshift( - - { categoryWrapper.children } - ); - }); - const isOpenClasses = props.isOpen ? 'left-16' : '-left-64'; return (