import React, { useState, useMemo } from "react"; import { useNavigate } from "react-router-dom"; import { Button, Typography, Skeleton } from "antd"; import { ArrowRightOutlined } from "@ant-design/icons"; import { TaxonomySlug, TermDto } from "@nice/common"; import { api } from "@nice/client"; import { CoursesSectionTag } from "./CoursesSectionTag"; import CourseList from "../../../../components/models/course/list/CourseList"; 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, }, }, take: 10, // 只取前10个 }); const categories = useMemo(() => { const allCategories = isLoading ? [] : data?.map((course) => course.name); return [...Array.from(new Set(allCategories))]; }, [data]); return { categories, isLoading }; } const { Title, Text } = Typography; interface CoursesSectionProps { title: string; description: string; initialVisibleCoursesCount?: number; } const CoursesSection: React.FC = ({ title, description, initialVisibleCoursesCount = 8, }) => { const navigate = useNavigate(); const [selectedCategory, setSelectedCategory] = useState("全部"); const gateGory: GetTaxonomyProps = useGetTaxonomy({ type: TaxonomySlug.CATEGORY, }); return (
{title} {description}
{gateGory.isLoading ? ( ) : ( <> {["全部", ...gateGory.categories].map( (category, idx) => ( ) )} )}
{
}
); }; export default CoursesSection;