import { useState, useMemo } from 'react'; import { Tree, Input } from 'antd'; import type { TreeDataNode } from 'antd'; import { ApartmentOutlined, HddOutlined, ClusterOutlined, } from '@ant-design/icons'; import { mockData } from '../../mock/schemaData.ts'; import { useSchemaStore } from '../../store/schemaStore.ts'; const { Search } = Input; export function LeftPanel() { const [searchValue, setSearchValue] = useState(''); const [expandedKeys, setExpandedKeys] = useState(['sites', 'all-devices']); const graph = useSchemaStore((s) => s.graph); const setRightPanelData = useSchemaStore((s) => s.setRightPanelData); const treeData = useMemo((): TreeDataNode[] => { const sitesTree: TreeDataNode[] = mockData.sites .filter((s) => !s.parentSiteId) .map((site) => { const children: TreeDataNode[] = []; // Add devices belonging to this site const siteDevices = mockData.devices.filter( (d) => d.siteId === site.id, ); for (const device of siteDevices) { children.push({ key: device.id, title: device.name, icon: , }); } // Add child sites const childSites = mockData.sites.filter( (s) => s.parentSiteId === site.id, ); for (const childSite of childSites) { const childDevices = mockData.devices.filter( (d) => d.siteId === childSite.id, ); children.push({ key: childSite.id, title: childSite.name, icon: , children: childDevices.map((d) => ({ key: d.id, title: d.name, icon: , })), }); } return { key: site.id, title: site.name, icon: , children, }; }); return [ { key: 'sites', title: 'Сайты', icon: , children: sitesTree, }, ]; }, []); const filteredTreeData = useMemo(() => { if (!searchValue) return treeData; const filterTree = (nodes: TreeDataNode[]): TreeDataNode[] => { return nodes .map((node) => { const title = String(node.title ?? ''); const match = title.toLowerCase().includes(searchValue.toLowerCase()); const filteredChildren = node.children ? filterTree(node.children) : []; if (match || filteredChildren.length > 0) { return { ...node, children: filteredChildren }; } return null; }) .filter(Boolean) as TreeDataNode[]; }; return filterTree(treeData); }, [treeData, searchValue]); const handleSelect = (selectedKeys: React.Key[]) => { const key = selectedKeys[0] as string; if (!key || !graph) return; // Find the node on the graph and center on it const cell = graph.getCellById(key); if (cell) { graph.centerCell(cell); graph.select(cell); // Set right panel data const data = cell.getData() as Record | undefined; if (data) { setRightPanelData(data); } } }; return (
setSearchValue(e.target.value)} allowClear />
setExpandedKeys(keys as string[])} onSelect={handleSelect} blockNode style={{ fontSize: 12 }} />
); }