|
| 1 | +import { useContext, useMemo, useState } from "react"; |
| 2 | +import { Link } from "react-router-dom"; |
| 3 | +import { styled } from "@linaria/react"; |
| 4 | +import { SchemaClass } from "./api"; |
| 5 | +import { DeclarationsContext, declarationKey } from "./DeclarationsContext"; |
| 6 | + |
| 7 | +interface TreeNode { |
| 8 | + cls: SchemaClass; |
| 9 | + children: TreeNode[]; |
| 10 | +} |
| 11 | + |
| 12 | +interface ModuleGroup { |
| 13 | + module: string; |
| 14 | + roots: TreeNode[]; |
| 15 | +} |
| 16 | + |
| 17 | +function buildTree(classesByKey: Map<string, SchemaClass>): ModuleGroup[] { |
| 18 | + const allNodes = new Map<string, TreeNode>(); |
| 19 | + const hasParentInTree = new Set<string>(); |
| 20 | + |
| 21 | + for (const [key, cls] of classesByKey) { |
| 22 | + allNodes.set(key, { cls, children: [] }); |
| 23 | + } |
| 24 | + |
| 25 | + for (const [key, cls] of classesByKey) { |
| 26 | + for (const parent of cls.parents) { |
| 27 | + const parentKey = declarationKey(parent.module, parent.name); |
| 28 | + const parentNode = allNodes.get(parentKey); |
| 29 | + if (parentNode) { |
| 30 | + hasParentInTree.add(key); |
| 31 | + parentNode.children.push(allNodes.get(key)!); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + for (const node of allNodes.values()) { |
| 37 | + if (node.children.length > 1) { |
| 38 | + node.children.sort((a, b) => a.cls.name.localeCompare(b.cls.name)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const moduleRoots = new Map<string, TreeNode[]>(); |
| 43 | + for (const [key, node] of allNodes) { |
| 44 | + if (!hasParentInTree.has(key)) { |
| 45 | + let roots = moduleRoots.get(node.cls.module); |
| 46 | + if (!roots) { |
| 47 | + roots = []; |
| 48 | + moduleRoots.set(node.cls.module, roots); |
| 49 | + } |
| 50 | + roots.push(node); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + const groups: ModuleGroup[] = []; |
| 55 | + for (const [module, roots] of moduleRoots) { |
| 56 | + roots.sort((a, b) => a.cls.name.localeCompare(b.cls.name)); |
| 57 | + groups.push({ module, roots }); |
| 58 | + } |
| 59 | + groups.sort((a, b) => a.module.localeCompare(b.module)); |
| 60 | + return groups; |
| 61 | +} |
| 62 | + |
| 63 | +function filterTree(nodes: TreeNode[], lower: string): TreeNode[] { |
| 64 | + const result: TreeNode[] = []; |
| 65 | + for (const node of nodes) { |
| 66 | + const nameMatches = node.cls.name.toLowerCase().includes(lower); |
| 67 | + if (nameMatches) { |
| 68 | + result.push(node); |
| 69 | + } else { |
| 70 | + const filteredChildren = filterTree(node.children, lower); |
| 71 | + if (filteredChildren.length > 0) { |
| 72 | + result.push({ cls: node.cls, children: filteredChildren }); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return result; |
| 77 | +} |
| 78 | + |
| 79 | +function filterGroups(groups: ModuleGroup[], query: string): ModuleGroup[] { |
| 80 | + const lower = query.toLowerCase(); |
| 81 | + const result: ModuleGroup[] = []; |
| 82 | + for (const group of groups) { |
| 83 | + const filteredRoots = filterTree(group.roots, lower); |
| 84 | + if (filteredRoots.length > 0) { |
| 85 | + result.push({ module: group.module, roots: filteredRoots }); |
| 86 | + } |
| 87 | + } |
| 88 | + return result; |
| 89 | +} |
| 90 | + |
| 91 | +const ClassLink = styled(Link)` |
| 92 | + text-decoration: none; |
| 93 | + color: var(--text); |
| 94 | + font-size: 14px; |
| 95 | +
|
| 96 | + &:hover { |
| 97 | + color: var(--highlight); |
| 98 | + } |
| 99 | +`; |
| 100 | + |
| 101 | +const TreeList = styled.ul` |
| 102 | + margin: 0; |
| 103 | + padding-left: 20px; |
| 104 | + list-style: none; |
| 105 | +`; |
| 106 | + |
| 107 | +const RootList = styled.ul` |
| 108 | + margin: 0; |
| 109 | + padding: 0; |
| 110 | + list-style: none; |
| 111 | +`; |
| 112 | + |
| 113 | +const TreeHeader = styled.div` |
| 114 | + display: flex; |
| 115 | + align-items: center; |
| 116 | + justify-content: space-between; |
| 117 | + padding: 8px 4px; |
| 118 | + font-size: 14px; |
| 119 | + color: var(--text-dim); |
| 120 | +`; |
| 121 | + |
| 122 | +const TreeFilterInput = styled.input` |
| 123 | + background: var(--group-members); |
| 124 | + border: 1px solid var(--group-border); |
| 125 | + border-radius: 6px; |
| 126 | + padding: 4px 10px; |
| 127 | + font: inherit; |
| 128 | + font-size: 13px; |
| 129 | + color: var(--text); |
| 130 | + width: 200px; |
| 131 | +
|
| 132 | + &:focus { |
| 133 | + outline: none; |
| 134 | + border-color: var(--highlight); |
| 135 | + } |
| 136 | +`; |
| 137 | + |
| 138 | +function TreeNodeView({ node, root }: { node: TreeNode; root: string }) { |
| 139 | + return ( |
| 140 | + <li> |
| 141 | + <ClassLink to={`${root}/${node.cls.module}/${node.cls.name}`}>{node.cls.name}</ClassLink> |
| 142 | + {node.children.length > 0 && ( |
| 143 | + <TreeList> |
| 144 | + {node.children.map((child) => ( |
| 145 | + <TreeNodeView |
| 146 | + key={declarationKey(child.cls.module, child.cls.name)} |
| 147 | + node={child} |
| 148 | + root={root} |
| 149 | + /> |
| 150 | + ))} |
| 151 | + </TreeList> |
| 152 | + )} |
| 153 | + </li> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export function ClassTree() { |
| 158 | + const { classesByKey, root } = useContext(DeclarationsContext); |
| 159 | + const [filter, setFilter] = useState(""); |
| 160 | + |
| 161 | + const groups = useMemo(() => buildTree(classesByKey), [classesByKey]); |
| 162 | + |
| 163 | + const displayGroups = useMemo(() => { |
| 164 | + if (!filter) return groups; |
| 165 | + return filterGroups(groups, filter); |
| 166 | + }, [groups, filter]); |
| 167 | + |
| 168 | + const totalClasses = classesByKey.size; |
| 169 | + |
| 170 | + return ( |
| 171 | + <> |
| 172 | + <TreeHeader> |
| 173 | + <TreeFilterInput |
| 174 | + type="search" |
| 175 | + placeholder={`Filter ${totalClasses} classes...`} |
| 176 | + value={filter} |
| 177 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => setFilter(e.target.value)} |
| 178 | + /> |
| 179 | + </TreeHeader> |
| 180 | + <RootList> |
| 181 | + {displayGroups.map((group) => ( |
| 182 | + <li key={group.module}> |
| 183 | + <strong>{group.module}</strong> |
| 184 | + <TreeList> |
| 185 | + {group.roots.map((node) => ( |
| 186 | + <TreeNodeView |
| 187 | + key={declarationKey(node.cls.module, node.cls.name)} |
| 188 | + node={node} |
| 189 | + root={root} |
| 190 | + /> |
| 191 | + ))} |
| 192 | + </TreeList> |
| 193 | + </li> |
| 194 | + ))} |
| 195 | + </RootList> |
| 196 | + </> |
| 197 | + ); |
| 198 | +} |
0 commit comments