-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce BundleItem component and memoize sidebar groups for p…
…erformance (#5312) ✨ (index.tsx): introduce MemoizedSidebarGroup component to improve performance by memoizing sorted bundles calculation and rendering 📝 (bundleItems/index.tsx): add BundleItem component to render individual bundle items in the sidebar with collapsible functionality
- Loading branch information
1 parent
5aa87ee
commit b3b5290
Showing
3 changed files
with
178 additions
and
69 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
...ntend/src/pages/FlowPage/components/flowSidebarComponent/components/bundleItems/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import ForwardedIconComponent from "@/components/common/genericIconComponent"; | ||
import { | ||
Disclosure, | ||
DisclosureContent, | ||
DisclosureTrigger, | ||
} from "@/components/ui/disclosure"; | ||
import { SidebarMenuButton, SidebarMenuItem } from "@/components/ui/sidebar"; | ||
import { memo } from "react"; | ||
import SidebarItemsList from "../sidebarItemsList"; | ||
|
||
export const BundleItem = memo( | ||
({ | ||
item, | ||
isOpen, | ||
onOpenChange, | ||
dataFilter, | ||
nodeColors, | ||
chatInputAdded, | ||
onDragStart, | ||
sensitiveSort, | ||
handleKeyDownInput, | ||
}: { | ||
item: any; | ||
isOpen: boolean; | ||
onOpenChange: (isOpen: boolean) => void; | ||
dataFilter: any; | ||
nodeColors: any; | ||
chatInputAdded: any; | ||
onDragStart: any; | ||
sensitiveSort: any; | ||
handleKeyDownInput: any; | ||
}) => { | ||
if ( | ||
!dataFilter[item.name] || | ||
Object.keys(dataFilter[item.name]).length === 0 | ||
) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
<Disclosure key={item.name} open={isOpen} onOpenChange={onOpenChange}> | ||
<SidebarMenuItem> | ||
<DisclosureTrigger className="group/collapsible"> | ||
<SidebarMenuButton asChild> | ||
<div | ||
tabIndex={0} | ||
onKeyDown={(e) => handleKeyDownInput(e, item.name)} | ||
className="flex cursor-pointer items-center gap-2" | ||
data-testid={`disclosure-bundles-${item.display_name.toLowerCase()}`} | ||
> | ||
<ForwardedIconComponent | ||
name={item.icon} | ||
className="h-4 w-4 text-muted-foreground group-aria-expanded/collapsible:text-primary" | ||
/> | ||
<span className="flex-1 group-aria-expanded/collapsible:font-semibold"> | ||
{item.display_name} | ||
</span> | ||
<ForwardedIconComponent | ||
name="ChevronRight" | ||
className="-mr-1 h-4 w-4 text-muted-foreground transition-all group-aria-expanded/collapsible:rotate-90" | ||
/> | ||
</div> | ||
</SidebarMenuButton> | ||
</DisclosureTrigger> | ||
<DisclosureContent> | ||
<SidebarItemsList | ||
item={item} | ||
dataFilter={dataFilter} | ||
nodeColors={nodeColors} | ||
chatInputAdded={chatInputAdded} | ||
onDragStart={onDragStart} | ||
sensitiveSort={sensitiveSort} | ||
/> | ||
</DisclosureContent> | ||
</SidebarMenuItem> | ||
</Disclosure> | ||
</> | ||
); | ||
}, | ||
); | ||
|
||
BundleItem.displayName = "BundleItem"; |
81 changes: 81 additions & 0 deletions
81
...nd/src/pages/FlowPage/components/flowSidebarComponent/components/sidebarBundles/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
SidebarGroup, | ||
SidebarGroupContent, | ||
SidebarGroupLabel, | ||
SidebarMenu, | ||
} from "@/components/ui/sidebar"; | ||
import { memo, useMemo } from "react"; | ||
import { BundleItem } from "../bundleItems"; | ||
|
||
export const MemoizedSidebarGroup = memo( | ||
({ | ||
BUNDLES, | ||
search, | ||
sortedCategories, | ||
dataFilter, | ||
nodeColors, | ||
chatInputAdded, | ||
onDragStart, | ||
sensitiveSort, | ||
openCategories, | ||
setOpenCategories, | ||
handleKeyDownInput, | ||
}: { | ||
BUNDLES: any; | ||
search: any; | ||
sortedCategories: any; | ||
dataFilter: any; | ||
nodeColors: any; | ||
chatInputAdded: any; | ||
onDragStart: any; | ||
sensitiveSort: any; | ||
openCategories: any; | ||
setOpenCategories: any; | ||
handleKeyDownInput: any; | ||
}) => { | ||
// Memoize the sorted bundles calculation | ||
const sortedBundles = useMemo(() => { | ||
return BUNDLES.toSorted((a, b) => { | ||
const referenceArray = search !== "" ? sortedCategories : BUNDLES; | ||
return ( | ||
referenceArray.findIndex((value) => value === a.name) - | ||
referenceArray.findIndex((value) => value === b.name) | ||
); | ||
}); | ||
}, [BUNDLES, search, sortedCategories]); | ||
|
||
return ( | ||
<SidebarGroup className="p-3"> | ||
<SidebarGroupLabel>Bundles</SidebarGroupLabel> | ||
<SidebarGroupContent> | ||
<SidebarMenu> | ||
{sortedBundles.map((item) => ( | ||
<BundleItem | ||
key={item.name} | ||
item={item} | ||
isOpen={openCategories.includes(item.name)} | ||
onOpenChange={(isOpen) => { | ||
setOpenCategories((prev) => | ||
isOpen | ||
? [...prev, item.name] | ||
: prev.filter((cat) => cat !== item.name), | ||
); | ||
}} | ||
dataFilter={dataFilter} | ||
nodeColors={nodeColors} | ||
chatInputAdded={chatInputAdded} | ||
onDragStart={onDragStart} | ||
sensitiveSort={sensitiveSort} | ||
handleKeyDownInput={handleKeyDownInput} | ||
/> | ||
))} | ||
</SidebarMenu> | ||
</SidebarGroupContent> | ||
</SidebarGroup> | ||
); | ||
}, | ||
); | ||
|
||
MemoizedSidebarGroup.displayName = "MemoizedSidebarGroup"; | ||
|
||
export default MemoizedSidebarGroup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters