training_data/apps/web/src/app/main/home/components/CoursesSection.tsx

113 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-02-27 23:06:17 +08:00
import React, { useState, useMemo, ReactNode } from "react";
import { Typography, Skeleton } from "antd";
2025-02-25 19:31:01 +08:00
import { TaxonomySlug, TermDto } from "@nice/common";
2025-02-25 16:23:36 +08:00
import { api } from "@nice/client";
2025-02-25 17:45:00 +08:00
import { CoursesSectionTag } from "./CoursesSectionTag";
2025-02-25 20:15:48 +08:00
import LookForMore from "./LookForMore";
import PostList from "@web/src/components/models/course/list/PostList";
2025-02-24 08:52:50 +08:00
interface GetTaxonomyProps {
2025-02-26 23:18:14 +08:00
categories: string[];
isLoading: boolean;
2025-02-24 08:52:50 +08:00
}
2025-02-24 19:10:38 +08:00
function useGetTaxonomy({ type }): GetTaxonomyProps {
2025-02-26 23:18:14 +08:00
const { data, isLoading }: { data: TermDto[]; isLoading: boolean } =
api.term.findMany.useQuery({
where: {
taxonomy: {
slug: type,
},
2025-02-27 13:32:33 +08:00
parentId: null,
2025-02-26 23:18:14 +08:00
},
take: 11, // 只取前10个
2025-02-26 23:18:14 +08:00
});
const categories = useMemo(() => {
const allCategories = isLoading
? []
: data?.map((course) => course.name);
return [...Array.from(new Set(allCategories))];
}, [data]);
return { categories, isLoading };
2025-02-24 08:52:50 +08:00
}
2025-02-06 16:32:31 +08:00
const { Title, Text } = Typography;
interface CoursesSectionProps {
2025-02-26 23:18:14 +08:00
title: string;
description: string;
initialVisibleCoursesCount?: number;
2025-02-27 23:06:17 +08:00
postType:string;
2025-02-27 23:11:37 +08:00
render?:(post)=>ReactNode;
to:string
2025-02-06 16:32:31 +08:00
}
const CoursesSection: React.FC<CoursesSectionProps> = ({
2025-02-26 23:18:14 +08:00
title,
description,
initialVisibleCoursesCount = 8,
2025-02-27 23:06:17 +08:00
postType,
2025-02-27 23:11:37 +08:00
render,
to
2025-02-06 16:32:31 +08:00
}) => {
2025-02-25 16:23:36 +08:00
const [selectedCategory, setSelectedCategory] = useState<string>("全部");
const gateGory: GetTaxonomyProps = useGetTaxonomy({
type: TaxonomySlug.CATEGORY,
});
return (
<section className="relative py-16 overflow-hidden ">
2025-02-26 19:42:48 +08:00
<div className="max-w-screen-2xl mx-auto px-4 relative">
<div className="flex justify-between items-end mb-12 ">
2025-02-25 16:23:36 +08:00
<div>
<Title
level={2}
className="font-bold text-5xl mb-6 bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent">
{title}
</Title>
<Text
type="secondary"
className="text-xl font-light text-gray-600">
{description}
</Text>
</div>
</div>
<div className="mb-12 flex flex-wrap gap-4">
{gateGory.isLoading ? (
2025-02-25 19:31:01 +08:00
<Skeleton paragraph={{ rows: 2 }}></Skeleton>
2025-02-25 16:23:36 +08:00
) : (
<>
2025-02-25 19:31:01 +08:00
{["全部", ...gateGory.categories].map(
(category, idx) => (
<CoursesSectionTag
key={idx}
category={category}
selectedCategory={selectedCategory}
setSelectedCategory={
setSelectedCategory
}
/>
)
)}
2025-02-25 16:23:36 +08:00
</>
)}
</div>
2025-02-26 23:18:14 +08:00
<PostList
2025-02-27 23:06:17 +08:00
renderItem={(post) => render(post)}
2025-02-25 19:31:01 +08:00
params={{
page: 1,
pageSize: initialVisibleCoursesCount,
where: {
terms: !(selectedCategory === "全部")
? {
2025-02-27 13:32:33 +08:00
some: {
name: selectedCategory,
},
}
2025-02-25 19:31:01 +08:00
: {},
2025-02-27 23:06:17 +08:00
type: postType
2025-02-25 19:31:01 +08:00
},
}}
showPagination={false}
2025-02-26 23:18:14 +08:00
cols={4}></PostList>
2025-02-27 23:11:37 +08:00
<LookForMore to={to}></LookForMore>
2025-02-25 16:23:36 +08:00
</div>
</section>
);
2025-02-06 16:32:31 +08:00
};
export default CoursesSection;