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 { courseDetailSelect, CourseDto, TaxonomySlug, TermDto, } from "@nice/common"; import { api } from "@nice/client"; import CourseCard from "../../courses/components/CourseCard"; import { CoursesSectionTag } from "./CoursesSectionTag"; 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, select: courseDetailSelect, }); 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; useEffect(()=>{ console.log(data) }) return (
{title} {description}
{gateGory.isLoading ? ( ) : ( <> {['全部',...gateGory.categories].map((category,idx) => ( ))} )}
{isDataLoading ? ( ) : ( displayedCourses?.map((course,index) => ( )) )}
{
}
); }; export default CoursesSection;