import React, { useState, useMemo, ReactNode } from "react"; import { Typography, Skeleton } from "antd"; import { TaxonomySlug, TermDto } from "@nice/common"; import { api } from "@nice/client"; import { CoursesSectionTag } from "./CoursesSectionTag"; import LookForMore from "./LookForMore"; import PostList from "@web/src/components/models/course/list/PostList"; 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, }, parentId: null, }, take: 11, // 只取前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; postType:string; render?:(post)=>ReactNode; to:string } const CoursesSection: React.FC = ({ title, description, initialVisibleCoursesCount = 8, postType, render, to }) => { const [selectedCategory, setSelectedCategory] = useState("全部"); const gateGory: GetTaxonomyProps = useGetTaxonomy({ type: TaxonomySlug.CATEGORY, }); return (
{title} {description}
{gateGory.isLoading ? ( ) : ( <> {["全部", ...gateGory.categories].map( (category, idx) => ( ) )} )}
render(post)} params={{ page: 1, pageSize: initialVisibleCoursesCount, where: { terms: !(selectedCategory === "全部") ? { some: { name: selectedCategory, }, } : {}, type: postType }, }} showPagination={false} cols={4}>
); }; export default CoursesSection;