import { useState, useCallback } from "react"; import { CaretDown, CaretRight, FolderSimple, GithubLogo, Globe, Link, PuzzlePiece, } from "../ui/icons"; import type { SkillRecord } from "@shared/types"; import { SkillFileTree, buildTree } from "./SkillFileTree"; const sourceIconMap: Record = { github: GithubLogo, local_path: FolderSimple, url: Link, skills_sh: Globe, managed: PuzzlePiece, }; export function SkillList({ skills, selectedSkillId, skillFilter, onSelectSkill, }: { skills: SkillRecord[]; selectedSkillId: string | null; skillFilter: string; onSelectSkill: (id: string) => void; }) { const [expandedSkillId, setExpandedSkillId] = useState(null); const [expandedDirs, setExpandedDirs] = useState>>({}); const [selectedPaths, setSelectedPaths] = useState>({}); const filteredSkills = skills.filter((skill) => { const haystack = `${skill.name} ${skill.key} ${skill.slug}`.toLowerCase(); return haystack.includes(skillFilter.toLowerCase()); }); const handleToggleSkill = useCallback((skillId: string) => { setExpandedSkillId((prev) => (prev === skillId ? null : skillId)); }, []); const handleToggleDir = useCallback((skillId: string, path: string) => { setExpandedDirs((prev) => { const current = prev[skillId] ?? new Set(); const next = new Set(current); if (next.has(path)) { next.delete(path); } else { next.add(path); } return { ...prev, [skillId]: next }; }); }, []); const handleSelectPath = useCallback((skillId: string, path: string) => { setSelectedPaths((prev) => ({ ...prev, [skillId]: path })); }, []); if (filteredSkills.length === 0) { return (
No skills match this filter.
); } return (
{filteredSkills.map((skill) => { const isExpanded = expandedSkillId === skill.id; const isSelected = skill.id === selectedSkillId; const tree = buildTree(skill.fileInventory); const SourceIcon = sourceIconMap[skill.sourceType] ?? PuzzlePiece; const dirState = expandedDirs[skill.id] ?? new Set(); const filePath = selectedPaths[skill.id] ?? "SKILL.md"; return (
{tree.length > 0 && ( handleToggleDir(skill.id, path)} onSelectPath={(path) => handleSelectPath(skill.id, path)} /> )}
); })}
); }