| 743 | } |
| 744 | |
| 745 | function calculatePathToNode<T extends { id: string; children?: T[] }>( |
| 746 | nodes: T[], |
| 747 | searchNode: T, |
| 748 | path: string[] = [] |
| 749 | ): string[] | undefined { |
| 750 | const nodeIndex = nodes.findIndex((node) => node.id === searchNode.id); |
| 751 | |
| 752 | if (nodeIndex !== -1) { |
| 753 | return [...path, searchNode.id]; |
| 754 | } |
| 755 | |
| 756 | for (const node of nodes) { |
| 757 | if (!node.children) { |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | const foundPath = calculatePathToNode(node.children || [], searchNode, [ |
| 762 | ...path, |
| 763 | node.id, |
| 764 | ]); |
| 765 | |
| 766 | if (foundPath && foundPath.length > path.length) { |
| 767 | return foundPath; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return; |
| 772 | } |