'use client'; import { useState } from 'react'; import { ChevronDown, ChevronRight, Folder, FolderOpen } from 'lucide-react'; import { cn } from '@/lib/utils'; import type { FileRow } from '@/components/files/file-grid'; interface FolderNode { name: string; fullPath: string; children: Record; } function buildFolderTree(files: FileRow[]): FolderNode { const root: FolderNode = { name: '', fullPath: '', children: {} }; for (const file of files) { const parts = file.storagePath ? file.storagePath.split('/').slice(0, -1) : []; if (parts.length <= 1) continue; // skip files directly in root/port folder let node = root; let accumulated = ''; for (const part of parts.slice(1)) { // skip portSlug prefix accumulated = accumulated ? `${accumulated}/${part}` : part; if (!node.children[part]) { node.children[part] = { name: part, fullPath: accumulated, children: {} }; } node = node.children[part]!; } } return root; } interface FolderNodeComponentProps { node: FolderNode; currentFolder: string; onFolderSelect: (path: string) => void; depth?: number; } function FolderNodeComponent({ node, currentFolder, onFolderSelect, depth = 0, }: FolderNodeComponentProps) { const [expanded, setExpanded] = useState(true); const hasChildren = Object.keys(node.children).length > 0; const isSelected = currentFolder === node.fullPath; return (
{hasChildren && expanded && (
{Object.values(node.children).map((child) => ( ))}
)}
); } interface FolderTreeProps { files: (FileRow & { storagePath: string })[]; currentFolder: string; onFolderSelect: (path: string) => void; } export function FolderTree({ files, currentFolder, onFolderSelect }: FolderTreeProps) { const tree = buildFolderTree(files); return (
{Object.values(tree.children).map((child) => ( ))}
); }