'use client'; import Link from 'next/link'; import { useState, useEffect } from 'react'; import { ThemeToggle } from './ThemeToggle'; interface SidebarItem { title: string; slug: string; children?: SidebarItem[]; } interface SidebarProps { sidebarData: SidebarItem[]; currentSlug?: string; } export function Sidebar({ sidebarData, currentSlug }: SidebarProps) { // Use a map to track open state of each section (by slug) const [openSections, setOpenSections] = useState>({}); useEffect(() => { // Automatically open the section that contains the current slug if (currentSlug) { const parentSection = sidebarData.find(item => item.children?.some(child => child.slug === currentSlug) ); if (parentSection) { setOpenSections(prev => ({ ...prev, [parentSection.slug]: true })); } } }, [currentSlug, sidebarData]); const toggleSection = (slug: string) => { setOpenSections(prev => ({ ...prev, [slug]: !prev[slug] })); }; return ( ); }