'use client' import { File } from 'lucide-react' import { useState } from 'react' import { flattenTree, TreeView, TreeViewItem } from 'ui' import { CodeBlock } from 'ui-patterns/CodeBlock' import { RegistryNode } from '@/lib/process-registry' interface BlockItemCodeProps { files: RegistryNode[] } interface TreeNode { name: string children: TreeNode[] metadata: { path: string } } const flattenChildren = (files: RegistryNode[]): TreeNode[] => { return files.map( (node): TreeNode => ({ name: node.name, children: node.children ? flattenChildren(node.children) : [], metadata: { path: node.path }, }) ) } const findFirstFile = (nodes: RegistryNode[]): RegistryNode | null => { for (const node of nodes) { if (node.type === 'file') { return node } if (node.children) { const foundFile = findFirstFile(node.children) if (foundFile) { return foundFile } } } return null } export function BlockItemCode({ files }: BlockItemCodeProps) { // Find the first file to select by default const [selectedFile, setSelectedFile] = useState(findFirstFile(files)) const flattenedData = flattenTree({ name: '', children: flattenChildren(files) }) // Handle file selection from the TreeView const handleNodeSelect = (element: any) => { const findFileByPath = (nodes: RegistryNode[], path: string): RegistryNode | null => { for (const node of nodes) { if (node.path === path) { return node } if (node.children) { const found = findFileByPath(node.children, path) if (found) { return found } } } return null } const filePath = element.metadata.path const foundFile = findFileByPath(files, filePath) if (foundFile?.type === 'directory') return setSelectedFile(foundFile || null) } return (
{/* File browser sidebar */}
n.children?.length).map((n) => n.id)} defaultSelectedIds={flattenedData .filter((n) => n.metadata?.path === selectedFile?.path) .map((n) => n.id)} onNodeSelect={({ element }) => handleNodeSelect(element)} nodeRenderer={({ element, isBranch, isExpanded, getNodeProps, level, isSelected }) => ( } name={element.name} className="gap-1.5" /> )} />
{/* Code display area */} {selectedFile?.content ? ( {selectedFile?.content} ) : (

No file selected or file content unavailable

)}
) }