-
-
-
-
{data.name}
-
{data.jobtitle}
-
-
-
-
+ return (
+
+
+
+
+
{data.name}
+
{data.jobtitle}
- );
+
+
+
+
+ );
}
export default memo(CustomNode);
diff --git a/src/components/tree/flow.component.tsx b/src/components/tree/flow.component.tsx
index a1cf330..6d3241a 100644
--- a/src/components/tree/flow.component.tsx
+++ b/src/components/tree/flow.component.tsx
@@ -1,145 +1,178 @@
import {
- ReactFlow,
- useNodesState,
- useEdgesState,
- Background,
- BackgroundVariant,
- ConnectionLineType,
- Node,
+ ReactFlow,
+ useNodesState,
+ useEdgesState,
+ Background,
+ BackgroundVariant,
+ ConnectionLineType,
+ Node,
} from '@xyflow/react';
import '@xyflow/react/dist/base.css';
import CustomNode from './custom-node.component';
-import { data } from '@/shared/data';
import { getPersonNodeById } from '@/shared/api/node.api';
-import { PersonNode } from '@/shared/person-node.interface';
+import { PersonNode } from '@/shared/interfaces/person-node.interface';
import { getLayoutedElements } from '@/lib/tree-layout';
+import { useEffect } from 'react';
const nodeTypes = {
- custom: CustomNode,
+ custom: CustomNode,
};
-const initNodes: Node
[] = [
- {
- id: data[3].id,
- type: 'custom',
- data: data[3],
- position: { x: 0, y: 50 },
- }
-];
-
const { nodes: layoutedNodes, edges: layoutedEdges } = getLayoutedElements(
- initNodes,
- []
+ [],
+ []
);
const Flow = () => {
- const [nodes, setNodes, onNodesChange] = useNodesState>(layoutedNodes);
- const [edges, setEdges, onEdgesChange] = useEdgesState(layoutedEdges);
-
- const loadNodesRecursively = async (node: PersonNode, currentDepth: number, maxDepth: number) => {
- if (currentDepth >= maxDepth) {
- return { newNodes: [], newEdges: [] };
- }
-
- const parentNodes = await Promise.all(
- node.parents.map((id) => getPersonNodeById(id))
- );
- const childNodes = await Promise.all(
- node.children.map((id) => getPersonNodeById(id))
- );
-
- const newNodes = [
- ...parentNodes
- .filter((parent) => !nodes.some((existingNode) => existingNode.id === parent.id))
- .map((parent) => ({
- id: parent.id,
- type: 'custom',
- data: parent,
- position: { x: 0, y: 0},
- })),
- ...childNodes
- .filter((child) => !nodes.some((existingNode) => existingNode.id === child.id))
- .map((child) => ({
- id: child.id,
- type: 'custom',
- data: child,
- position: { x: 0, y: 0},
- })),
- ];
-
- const newEdges = [
- ...parentNodes.map((parentNode) => ({
- id: `e-${parentNode.id}-${node.id}`,
- source: parentNode.id,
- target: node.id,
- })),
- ...childNodes.map((childNode) => ({
- id: `e-${node.id}-${childNode.id}`,
- source: node.id,
- target: childNode.id,
- }))
- ];
-
- let allNewNodes = newNodes;
- let allNewEdges = newEdges;
-
- for (const parentNode of parentNodes) {
- const { newNodes: nestedNodes, newEdges: nestedEdges } = await loadNodesRecursively(parentNode, currentDepth + 1, maxDepth);
- allNewNodes = [...allNewNodes, ...nestedNodes];
- allNewEdges = [...allNewEdges, ...nestedEdges];
- }
-
- for (const childNode of childNodes) {
- const { newNodes: nestedNodes, newEdges: nestedEdges } = await loadNodesRecursively(childNode, currentDepth + 1, maxDepth);
- allNewNodes = [...allNewNodes, ...nestedNodes];
- allNewEdges = [...allNewEdges, ...nestedEdges];
- }
-
- return { newNodes: allNewNodes, newEdges: allNewEdges };
- };
-
- const onNodeDoubleClick = async (_: unknown, input: Node) => {
- const node = input.data;
-
- const maxDepth = 1;
- const { newNodes, newEdges } = await loadNodesRecursively(node, 0, maxDepth);
-
- const uniqueNodes = [
- ...new Map([...nodes, ...newNodes].map(node => [node.id, node])).values()
- ];
-
- const uniqueEdges = [
- ...new Map([...edges, ...newEdges].map(edge => [edge.id, edge])).values()
- ];
-
- const { nodes: updatedNodes, edges: updatedEdges } = getLayoutedElements(uniqueNodes, uniqueEdges);
-
- setNodes(updatedNodes);
- setEdges(updatedEdges);
- };
-
- return (
-
-
-
+ const [nodes, setNodes, onNodesChange] =
+ useNodesState>(layoutedNodes);
+ const [edges, setEdges, onEdgesChange] = useEdgesState(layoutedEdges);
+
+ useEffect(() => {
+ getPersonNodeById('8').then((x) => {
+ const { nodes, edges } = getLayoutedElements(
+ [
+ {
+ id: x.id,
+ type: 'custom',
+ data: x,
+ position: { x: 0, y: 0 },
+ },
+ ],
+ []
+ );
+
+ setNodes(nodes);
+ setEdges(edges);
+ });
+ }, [setEdges, setNodes]);
+
+ const loadNodesRecursively = async (
+ node: PersonNode,
+ currentDepth: number,
+ maxDepth: number
+ ) => {
+ if (currentDepth >= maxDepth) {
+ return { newNodes: [], newEdges: [] };
+ }
+
+ const parentNodes = await Promise.all(
+ node.parents.map((id) => getPersonNodeById(id))
+ );
+ const childNodes = await Promise.all(
+ node.children.map((id) => getPersonNodeById(id))
+ );
+
+ const newNodes = [
+ ...parentNodes
+ .filter(
+ (parent) =>
+ !nodes.some((existingNode) => existingNode.id === parent.id)
+ )
+ .map((parent) => ({
+ id: parent.id,
+ type: 'custom',
+ data: parent,
+ position: { x: 0, y: 0 },
+ })),
+ ...childNodes
+ .filter(
+ (child) => !nodes.some((existingNode) => existingNode.id === child.id)
+ )
+ .map((child) => ({
+ id: child.id,
+ type: 'custom',
+ data: child,
+ position: { x: 0, y: 0 },
+ })),
+ ];
+
+ const newEdges = [
+ ...parentNodes.map((parentNode) => ({
+ id: `e-${parentNode.id}-${node.id}`,
+ source: parentNode.id,
+ target: node.id,
+ })),
+ ...childNodes.map((childNode) => ({
+ id: `e-${node.id}-${childNode.id}`,
+ source: node.id,
+ target: childNode.id,
+ })),
+ ];
+
+ let allNewNodes = newNodes;
+ let allNewEdges = newEdges;
+
+ for (const parentNode of parentNodes) {
+ const { newNodes: nestedNodes, newEdges: nestedEdges } =
+ await loadNodesRecursively(parentNode, currentDepth + 1, maxDepth);
+ allNewNodes = [...allNewNodes, ...nestedNodes];
+ allNewEdges = [...allNewEdges, ...nestedEdges];
+ }
+
+ for (const childNode of childNodes) {
+ const { newNodes: nestedNodes, newEdges: nestedEdges } =
+ await loadNodesRecursively(childNode, currentDepth + 1, maxDepth);
+ allNewNodes = [...allNewNodes, ...nestedNodes];
+ allNewEdges = [...allNewEdges, ...nestedEdges];
+ }
+
+ return { newNodes: allNewNodes, newEdges: allNewEdges };
+ };
+
+ const onNodeDoubleClick = async (_: unknown, input: Node) => {
+ const node = input.data;
+
+ const maxDepth = 1;
+ const { newNodes, newEdges } = await loadNodesRecursively(
+ node,
+ 0,
+ maxDepth
+ );
+
+ const uniqueNodes = [
+ ...new Map(
+ [...nodes, ...newNodes].map((node) => [node.id, node])
+ ).values(),
+ ];
+
+ const uniqueEdges = [
+ ...new Map(
+ [...edges, ...newEdges].map((edge) => [edge.id, edge])
+ ).values(),
+ ];
+
+ const { nodes: updatedNodes, edges: updatedEdges } = getLayoutedElements(
+ uniqueNodes,
+ uniqueEdges
);
+
+ setNodes(updatedNodes);
+ setEdges(updatedEdges);
+ };
+
+ return (
+
+
+
+ );
};
export default Flow;
diff --git a/src/components/ui/command.tsx b/src/components/ui/command.tsx
index 4216fea..fce5b36 100644
--- a/src/components/ui/command.tsx
+++ b/src/components/ui/command.tsx
@@ -24,7 +24,7 @@ const CommandDialog = ({ children, ...props }: DialogProps) => {
return (