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

105 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-02-25 19:31:01 +08:00
import React, { useState, useMemo } 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";
import CourseList from "@web/src/components/models/course/list/CourseList";
2025-02-25 20:15:48 +08:00
import LookForMore from "./LookForMore";
2025-02-24 08:52:50 +08:00
interface GetTaxonomyProps {
2025-02-26 10:25:25 +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 10:25:25 +08:00
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 };
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 10:25:25 +08:00
title: string;
description: string;
initialVisibleCoursesCount?: number;
2025-02-06 16:32:31 +08:00
}
const CoursesSection: React.FC<CoursesSectionProps> = ({
2025-02-26 10:25:25 +08:00
title,
description,
initialVisibleCoursesCount = 8,
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-20 overflow-hidden bg-gradient-to-b from-gray-50 to-white">
<div className="max-w-screen-2xl mx-auto px-6 relative">
<div className="flex justify-between items-end mb-16">
<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>
2025-02-26 09:01:09 +08:00
2025-02-25 16:23:36 +08:00
<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-25 19:31:01 +08:00
<CourseList
params={{
page: 1,
pageSize: initialVisibleCoursesCount,
where: {
terms: !(selectedCategory === "全部")
? {
some: {
name: selectedCategory,
},
}
: {},
},
}}
showPagination={false}
cols={4}></CourseList>
2025-02-25 20:15:48 +08:00
<LookForMore to={"/courses"}></LookForMore>
2025-02-25 16:23:36 +08:00
</div>
</section>
);
2025-02-06 16:32:31 +08:00
};
export default CoursesSection;