import { useState } from 'react' import { Link } from 'react-router-dom' import { Pencil, Globe, Lock, ChevronUp, ChevronDown, GitBranch, FileText } from 'lucide-react' import type { TreeListItem } from '@/types' import { TagBadges } from '@/components/common/TagBadges' import { AddToFolderMenu } from './AddToFolderMenu' import { cn } from '@/lib/utils' import { usePermissions } from '@/hooks/usePermissions' interface TreeTableViewProps { trees: TreeListItem[] onStartSession: (treeId: string) => void onTagClick: (tag: string) => void onFolderCreated: (parentId?: string | null) => void onDeleteTree: (tree: TreeListItem) => void onSortChange?: (sortBy: string) => void onForkTree?: (treeId: string) => void } type SortColumn = 'name' | 'category' | 'version' | 'usage' | 'updated' export function TreeTableView({ trees, onStartSession, onTagClick, onFolderCreated, onSortChange, onForkTree, }: TreeTableViewProps) { const { canEditTree } = usePermissions() const [sortColumn, setSortColumn] = useState(null) const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('asc') const handleSort = (column: SortColumn) => { const newDirection = sortColumn === column && sortDirection === 'asc' ? 'desc' : 'asc' setSortColumn(column) setSortDirection(newDirection) // Map to API sort values const sortMap: Record = { name_asc: 'name', name_desc: 'name_desc', usage_asc: 'usage_count', usage_desc: 'usage_count', updated_asc: 'updated_at', updated_desc: 'updated_at', version_asc: 'version', version_desc: 'version', } const sortKey = `${column}_${newDirection}` const apiSort = sortMap[sortKey] || 'usage_count' onSortChange?.(apiSort) } const getSortIcon = (column: SortColumn) => { if (sortColumn !== column) return null return sortDirection === 'asc' ? ( ) : ( ) } const formatDate = (dateString: string) => { const date = new Date(dateString) return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' }) } return (
{trees.map((tree) => ( ))}
handleSort('name')} >
Name {getSortIcon('name')}
Description handleSort('category')} >
Category {getSortIcon('category')}
Tags handleSort('version')} >
Ver. {getSortIcon('version')}
handleSort('usage')} >
Uses {getSortIcon('usage')}
handleSort('updated')} >
Updated {getSortIcon('updated')}
Actions
{tree.name} {tree.status === 'draft' && ( Draft )} {tree.is_public ? ( ) : ( )}
{tree.description || 'No description'} {tree.category_info && ( {tree.category_info.name} )} {tree.tags && tree.tags.length > 0 && ( )} v{tree.version} {tree.usage_count} {formatDate(tree.updated_at)}
{onForkTree && ( )} {canEditTree({ author_id: tree.author_id, account_id: tree.account_id }) && ( )}
) }