import React, { useState, useMemo, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { Button, Card, Typography, Tag, Spin, Empty } from "antd"; import { PlayCircleOutlined, TeamOutlined, ArrowRightOutlined, } from "@ant-design/icons"; import { CourseDto, TaxonomySlug, TermDto } from "@nice/common"; import { api } from "@nice/client"; import CourseCard from "../../courses/components/CourseCard"; interface GetTaxonomyProps { categories: string[]; isLoading: boolean; } function useGetTaxonomy({ type }): GetTaxonomyProps { const { data, isLoading }: { data: TermDto[]; isLoading: boolean } = api.term.findMany.useQuery({ where: { taxonomy: { slug: type, }, }, include: { children: true, }, take: 10, // 只取前10个 orderBy: {}, }); const categories = useMemo(() => { const allCategories = isLoading ? [] : data?.map((course) => course.name); return [...Array.from(new Set(allCategories))]; }, [data]); return { categories, isLoading }; } // 不同分类跳转 function useFetchCoursesByCategory(category: string) { const isAll = category === "全部"; const { data, isLoading }: { data: CourseDto[]; isLoading: boolean } = api.post.findMany.useQuery({ where: isAll ? {} : { terms: { some: { name: category, }, }, }, take: 8, include: { terms: true, depts: true, }, }); return { data, isLoading }; } const { Title, Text } = Typography; interface CoursesSectionProps { title: string; description: string; initialVisibleCoursesCount?: number; } const CoursesSection: React.FC = ({ title, description, }) => { const navigate = useNavigate(); const [selectedCategory, setSelectedCategory] = useState("全部"); const gateGory: GetTaxonomyProps = useGetTaxonomy({ type: TaxonomySlug.CATEGORY, }); const { data, isLoading: isDataLoading } = useFetchCoursesByCategory(selectedCategory); const filteredCourses = useMemo(() => { return selectedCategory === "全部" ? data : data?.filter((c) => c.terms.some((t) => t.name === selectedCategory) ); }, [selectedCategory, data]); const displayedCourses = isDataLoading ? [] : filteredCourses; return (
{title} {description}
{gateGory.isLoading ? ( ) : ( <> setSelectedCategory("全部")} className={`px-6 py-2 text-base cursor-pointer rounded-full transition-all duration-300 ${ selectedCategory === "全部" ? "bg-blue-600 text-white shadow-lg" : "bg-white text-gray-600 hover:bg-gray-100" }`}> 全部 {gateGory.categories.map((category) => ( { setSelectedCategory(category); }} className={`px-6 py-2 text-base cursor-pointer rounded-full transition-all duration-300 ${ selectedCategory === category ? "bg-blue-600 text-white shadow-lg" : "bg-white text-gray-600 hover:bg-gray-100" }`}> {category} ))} )}
{displayedCourses.length === 0 ? (
) : ( displayedCourses?.map((course) => ( )) )}
{
}
); }; export default CoursesSection;