Files
PyUIBuilder/src/sidebar/treeviewContainer.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-03-08 20:11:22 +05:30
import { useEffect, useMemo, useState } from "react"
import { CloseCircleFilled, SearchOutlined } from "@ant-design/icons"
import { SidebarWidgetCard, TreeViewCard } from "../components/cards"
import { Tree } from "antd"
2025-03-09 11:07:08 +05:30
import { useWidgetContext } from "../canvas/context/widgetContext"
2025-03-08 20:11:22 +05:30
2025-03-09 11:07:08 +05:30
function transformWidgets(widgets, widgetRefs, isTopLevel=true) {
// console.log("Wdiegts refs: ", widgetRefs)
return widgets.map(widget => ({
2025-03-11 07:23:17 +05:30
title: widgetRefs.current[widget.id].current.getDisplayName(), // Assuming widgetType is a class
2025-03-09 11:07:08 +05:30
key: widget.id,
isTopLevel: isTopLevel,
widgetRef: widgetRefs.current[widget.id],
children: widget.children.length > 0 ? transformWidgets(widget.children, widgetRefs, false) : []
2025-03-09 17:48:23 +05:30
}))
2025-03-09 11:07:08 +05:30
}
2025-03-08 20:11:22 +05:30
/**
*
* @param {function} onWidgetsUpdate - this is a callback that will be called once the sidebar is populated with widgets
* @returns
*/
2025-03-09 11:07:08 +05:30
function TreeviewContainer() {
2025-03-08 20:11:22 +05:30
2025-03-09 11:07:08 +05:30
const {widgets, widgetRefs} = useWidgetContext()
2025-03-08 20:11:22 +05:30
2025-03-09 11:07:08 +05:30
const transformedContent = useMemo(() => {
return (transformWidgets(widgets, widgetRefs))
}, [widgets, widgetRefs])
2025-03-08 20:11:22 +05:30
2025-03-09 11:07:08 +05:30
const topLevelKeys = transformedContent.filter(cont => cont.isTopLevel).map(cont => cont.key)
2025-03-08 20:11:22 +05:30
2025-03-09 11:07:08 +05:30
2025-03-08 20:11:22 +05:30
return (
<div className="tw-w-full tw-p-2 tw-gap-4 tw-flex tw-flex-col tw-overflow-x-hidden">
{/* <SearchComponent onSearch={onSearch} searchValue={searchValue}
onClear={() => setSearchValue("")} /> */}
<div className="tw-flex tw-flex-col tw-gap-2 tw-w-full tw-h-full tw-p-1">
2025-03-10 10:37:57 +05:30
<Tree showLine
treeData={transformedContent}
2025-03-08 20:11:22 +05:30
titleRender={(nodeData) =>
2025-03-09 11:07:08 +05:30
<TreeViewCard widgetId={nodeData.id} title={nodeData.title}
widgetRef={nodeData.widgetRef}
isTopLevel={nodeData.isTopLevel}/>
2025-03-08 20:11:22 +05:30
}
2025-03-09 11:07:08 +05:30
defaultExpandedKeys={topLevelKeys}
2025-03-08 20:11:22 +05:30
>
</Tree>
2025-03-09 17:48:23 +05:30
{
Object.keys(transformedContent || {}).length === 0 &&
<div className="tw-text-sm tw-place-content-center">
Start adding widgets to view it in tree view
</div>
}
2025-03-08 20:11:22 +05:30
</div>
</div>
)
}
export default TreeviewContainer