import { XMarkIcon, BookOpenIcon } from "@heroicons/react/24/outline"; import { ChevronDownIcon, ClockIcon, PlayCircleIcon, } from "@heroicons/react/24/outline"; import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/24/outline"; import React, { useState, useRef, useContext } from "react"; import { SectionDto, TaxonomySlug } from "@nice/common"; import { SyllabusHeader } from "./SyllabusHeader"; import { SectionItem } from "./SectionItem"; import { CollapsedButton } from "./CollapsedButton"; import { CourseDetailContext } from "../CourseDetailContext"; import { api } from "@nice/client"; interface CourseSyllabusProps { sections: SectionDto[]; onLectureClick?: (lectureId: string) => void; isOpen: boolean; onToggle: () => void; } export const CourseSyllabus: React.FC = ({ sections, onLectureClick, isOpen, onToggle, }) => { const { isHeaderVisible } = useContext(CourseDetailContext); const [expandedSections, setExpandedSections] = useState( sections.map((section) => section.id) // 默认展开所有章节 ); const sectionRefs = useRef<{ [key: string]: HTMLDivElement | null }>({}); const toggleSection = (sectionId: string) => { setExpandedSections((prev) => prev.includes(sectionId) ? prev.filter((id) => id !== sectionId) : [...prev, sectionId] ); // 直接滚动,无需延迟 sectionRefs.current[sectionId]?.scrollIntoView({ behavior: "smooth", block: "start", }); }; return ( <> {/* 收起按钮直接显示 */} {!isOpen && (
)}
{isOpen && (
{sections.map((section, index) => ( (sectionRefs.current[section.id] = el) } index={index + 1} section={section} isExpanded={expandedSections.includes( section.id )} onToggle={toggleSection} onLectureClick={onLectureClick} /> ))}
)}
); };