'use client' import { File } from 'lucide-react' import dynamic from 'next/dynamic' import { useState } from 'react' import { cn, flattenTree, TreeView, TreeViewItem } from 'ui' import type { CodeBlockLang } from 'ui-patterns/CodeBlock' import { RegistryNode } from '@/lib/process-registry' const CodeBlock = dynamic( () => import('ui-patterns/CodeBlock').then((module) => module.CodeBlock), { ssr: false, loading: () => (
Loading code preview...
), } ) interface BlockItemCodeProps { files: RegistryNode[] embedded?: boolean } 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 LANGUAGES: Record = { bash: 'bash', html: 'html', js: 'js', json: 'json', jsx: 'jsx', sh: 'bash', sql: 'sql', toml: 'toml', yaml: 'yaml', yml: 'yaml', } const languageFor = (fileName: string | undefined): CodeBlockLang => { const normalized = fileName?.toLowerCase() ?? '' if (normalized.startsWith('.env')) return 'bash' if (normalized === 'deno.lock') return 'json' return LANGUAGES[normalized.split('.').pop() ?? ''] ?? 'ts' } 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, embedded = false }: 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 = (filePath: string) => { 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 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 }) => { if (typeof element.metadata?.path === 'string') { handleNodeSelect(element.metadata.path) } }} 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

)}
) }