import { useEffect, useState } from 'react'; import { useParams, Link } from 'react-router-dom'; import { courseApi } from '../services/api'; import type { Course, Chapter } from '@ai-learning/shared'; export default function CourseDetailPage() { const { id } = useParams<{ id: string }>(); const [course, setCourse] = useState(null); const [chapters, setChapters] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { if (id) { loadCourse(); } }, [id]); const loadCourse = async () => { if (!id) return; try { setLoading(true); const [courseResponse, chaptersResponse] = await Promise.all([ courseApi.getById(id), courseApi.getChapters(id), ]); setCourse(courseResponse.data); setChapters(chaptersResponse.data); } catch (error) { console.error('Failed to load course:', error); } finally { setLoading(false); } }; if (loading) { return (
加载中...
); } if (!course) { return (
课程不存在
); } return (

{course.title}

{course.description}

分类: {course.category} 难度: {course.difficulty} 预计时长: {course.estimatedHours} 小时

课程章节

{chapters.length === 0 ? (

暂无章节

) : (
{chapters.map((chapter) => (

{chapter.order}. {chapter.title}

))}
)}
); }