|
| 1 | +import { Button, Divider } from '@heroui/react'; |
| 2 | +import { ResourcesContentsView } from '@luna/screens/home/admin/ResourcesContentsView'; |
| 3 | +import { ResourcesLayout } from '@luna/screens/home/admin/ResourcesLayout'; |
| 4 | +import { |
| 5 | + IconChevronDown, |
| 6 | + IconChevronRight, |
| 7 | + IconFile, |
| 8 | + IconFolder, |
| 9 | +} from '@tabler/icons-react'; |
| 10 | +import { DirectoryTree } from 'nighthouse/browser'; |
| 11 | +import { useCallback, useMemo, useState } from 'react'; |
| 12 | + |
| 13 | +export interface ResourcesTreeViewProps { |
| 14 | + parentPath?: string[]; |
| 15 | + tree: DirectoryTree | undefined | null; |
| 16 | + layout: ResourcesLayout; |
| 17 | +} |
| 18 | + |
| 19 | +export function ResourcesTreeView({ |
| 20 | + parentPath: path = [], |
| 21 | + tree, |
| 22 | + layout, |
| 23 | +}: ResourcesTreeViewProps) { |
| 24 | + // TODO: Use a set here and let the user expand multiple nodes (but only in |
| 25 | + // list view, in column view this doesn't make sense and shouldn't be allowed) |
| 26 | + const [expanded, setExpanded] = useState<string>(); |
| 27 | + |
| 28 | + const sortedEntries = useMemo( |
| 29 | + () => |
| 30 | + tree |
| 31 | + ? Object.entries(tree).sort(([name1, _1], [name2, _2]) => |
| 32 | + name1.localeCompare(name2) |
| 33 | + ) |
| 34 | + : undefined, |
| 35 | + [tree] |
| 36 | + ); |
| 37 | + |
| 38 | + switch (layout) { |
| 39 | + case 'column': |
| 40 | + return ( |
| 41 | + <div className="flex flex-row gap-2"> |
| 42 | + <div className="flex flex-col gap-2"> |
| 43 | + {sortedEntries |
| 44 | + ? sortedEntries.map(([name, subTree]) => ( |
| 45 | + <ResourcesTreeButton |
| 46 | + key={JSON.stringify([...path, name])} |
| 47 | + name={name} |
| 48 | + layout={layout} |
| 49 | + subTree={subTree} |
| 50 | + expanded={expanded} |
| 51 | + setExpanded={setExpanded} |
| 52 | + /> |
| 53 | + )) |
| 54 | + : undefined} |
| 55 | + </div> |
| 56 | + {expanded !== undefined ? ( |
| 57 | + <> |
| 58 | + <div> |
| 59 | + <Divider orientation="vertical" /> |
| 60 | + </div> |
| 61 | + {tree?.[expanded] !== null ? ( |
| 62 | + <> |
| 63 | + <ResourcesTreeView |
| 64 | + key={JSON.stringify([...path, expanded])} |
| 65 | + parentPath={[...path, expanded]} |
| 66 | + tree={tree?.[expanded]!} |
| 67 | + layout={layout} |
| 68 | + /> |
| 69 | + </> |
| 70 | + ) : ( |
| 71 | + <ResourcesContentsView path={[...path, expanded]} /> |
| 72 | + )} |
| 73 | + </> |
| 74 | + ) : undefined} |
| 75 | + </div> |
| 76 | + ); |
| 77 | + case 'list': |
| 78 | + return ( |
| 79 | + <div className="flex flex-col"> |
| 80 | + {sortedEntries |
| 81 | + ? sortedEntries.map(([name, subTree]) => ( |
| 82 | + <> |
| 83 | + <ResourcesTreeButton |
| 84 | + key={JSON.stringify([...path, name])} |
| 85 | + name={name} |
| 86 | + subTree={subTree} |
| 87 | + layout={layout} |
| 88 | + expanded={expanded} |
| 89 | + setExpanded={setExpanded} |
| 90 | + /> |
| 91 | + {expanded === name ? ( |
| 92 | + <div className="ml-4"> |
| 93 | + {subTree === null ? ( |
| 94 | + <ResourcesContentsView path={[...path, expanded]} /> |
| 95 | + ) : ( |
| 96 | + <ResourcesTreeView |
| 97 | + key={JSON.stringify([...path, expanded])} |
| 98 | + parentPath={[...path, expanded]} |
| 99 | + tree={subTree} |
| 100 | + layout={layout} |
| 101 | + /> |
| 102 | + )} |
| 103 | + </div> |
| 104 | + ) : undefined} |
| 105 | + </> |
| 106 | + )) |
| 107 | + : undefined} |
| 108 | + </div> |
| 109 | + ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +function ResourcesTreeButton({ |
| 114 | + name, |
| 115 | + subTree, |
| 116 | + layout, |
| 117 | + expanded, |
| 118 | + setExpanded, |
| 119 | +}: { |
| 120 | + name: string; |
| 121 | + subTree: DirectoryTree | null; |
| 122 | + layout: ResourcesLayout; |
| 123 | + expanded: string | undefined; |
| 124 | + setExpanded: (name?: string) => void; |
| 125 | +}) { |
| 126 | + const isExpanded = useMemo(() => expanded === name, [expanded, name]); |
| 127 | + |
| 128 | + const color = useMemo( |
| 129 | + () => (isExpanded && layout === 'column' ? 'primary' : 'default'), |
| 130 | + [isExpanded, layout] |
| 131 | + ); |
| 132 | + |
| 133 | + const onPress = useCallback(() => { |
| 134 | + setExpanded(isExpanded ? undefined : name); |
| 135 | + }, [name, isExpanded, setExpanded]); |
| 136 | + |
| 137 | + return ( |
| 138 | + <Button onPress={onPress} color={color} variant="faded"> |
| 139 | + <div className="flex flex-row justify-start gap-2 grow"> |
| 140 | + {layout === 'list' ? ( |
| 141 | + isExpanded ? ( |
| 142 | + <IconChevronDown /> |
| 143 | + ) : ( |
| 144 | + <IconChevronRight /> |
| 145 | + ) |
| 146 | + ) : undefined} |
| 147 | + {subTree === null ? <IconFile /> : <IconFolder />} |
| 148 | + {name} |
| 149 | + </div> |
| 150 | + </Button> |
| 151 | + ); |
| 152 | +} |
0 commit comments