'use client'; import React, { useState } from 'react'; import { FaChevronRight, FaChevronDown } from 'react-icons/fa'; // Import interfaces from the page component interface WikiPage { id: string; title: string; content: string; filePaths: string[]; importance: 'high' | 'medium' | 'low'; relatedPages: string[]; parentId?: string; isSection?: boolean; children?: string[]; } interface WikiSection { id: string; title: string; pages: string[]; subsections?: string[]; } interface WikiStructure { id: string; title: string; description: string; pages: WikiPage[]; sections: WikiSection[]; rootSections: string[]; } interface WikiTreeViewProps { wikiStructure: WikiStructure; currentPageId: string | undefined; onPageSelect: (pageId: string) => void; messages?: { pages?: string; [key: string]: string | undefined; }; } const WikiTreeView: React.FC = ({ wikiStructure, currentPageId, onPageSelect, }) => { const [expandedSections, setExpandedSections] = useState>( new Set(wikiStructure.rootSections) ); const toggleSection = (sectionId: string, event: React.MouseEvent) => { event.stopPropagation(); setExpandedSections(prev => { const newSet = new Set(prev); if (newSet.has(sectionId)) { newSet.delete(sectionId); } else { newSet.add(sectionId); } return newSet; }); }; const renderSection = (sectionId: string, level = 0) => { const section = wikiStructure.sections.find(s => s.id === sectionId); if (!section) return null; const isExpanded = expandedSections.has(sectionId); return (
{isExpanded && (
0 ? 'pl-2 border-l border-[var(--border-color)]/30' : ''}`}> {/* Render pages in this section */} {section.pages.map(pageId => { const page = wikiStructure.pages.find(p => p.id === pageId); if (!page) return null; return ( ); })} {/* Render subsections recursively */} {section.subsections?.map(subsectionId => renderSection(subsectionId, level + 1) )}
)}
); }; // If there are no sections defined yet, or if sections/rootSections are empty arrays, fall back to the flat list view if (!wikiStructure.sections || wikiStructure.sections.length === 0 || !wikiStructure.rootSections || wikiStructure.rootSections.length === 0) { console.log("WikiTreeView: Falling back to flat list view due to missing or empty sections/rootSections"); return ( ); } // Log information about the sections for debugging console.log("WikiTreeView: Rendering tree view with sections:", wikiStructure.sections); console.log("WikiTreeView: Root sections:", wikiStructure.rootSections); return (
{wikiStructure.rootSections.map(sectionId => { const section = wikiStructure.sections.find(s => s.id === sectionId); if (!section) { console.warn(`WikiTreeView: Could not find section with id ${sectionId}`); return null; } return renderSection(sectionId); })}
); }; export default WikiTreeView;