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

271 lines
9.7 KiB
TypeScript
Raw Normal View History

2025-02-21 17:02:51 +08:00
import React, { useState, useMemo, useEffect } from 'react';
2025-02-24 19:10:38 +08:00
import { useNavigate, useParams, useSearchParams } from 'react-router-dom';
2025-02-25 10:02:40 +08:00
import { Button, Card, Typography, Tag, Progress, Spin, Empty } from 'antd';
2025-02-06 16:32:31 +08:00
import {
PlayCircleOutlined,
UserOutlined,
ClockCircleOutlined,
TeamOutlined,
StarOutlined,
ArrowRightOutlined,
2025-02-24 19:10:38 +08:00
EyeOutlined,
2025-02-06 16:32:31 +08:00
} from '@ant-design/icons';
2025-02-25 10:01:17 +08:00
import { CourseDto, TaxonomySlug, TermDto } from '@nice/common';
2025-02-21 17:02:51 +08:00
import { api } from '@nice/client';
2025-02-24 19:10:38 +08:00
// const {courseId} = useParams();
2025-02-24 08:52:50 +08:00
interface GetTaxonomyProps {
categories: string[];
isLoading: boolean;
}
2025-02-24 19:10:38 +08:00
function useGetTaxonomy({ type }): GetTaxonomyProps {
const { data, isLoading }: { data: TermDto[], isLoading: boolean } = api.term.findMany.useQuery({
where: {
taxonomy: {
//TaxonomySlug.CATEGORY
slug: type
}
},
include: {
children: true
},
take: 10, // 只取前10个
orderBy: {
createdAt: 'desc', // 按创建时间降序排列
},
2025-02-24 08:52:50 +08:00
})
const categories = useMemo(() => {
2025-02-24 19:10:38 +08:00
const allCategories = isLoading ? [] : data?.map((course) => course.name);
return [...Array.from(new Set(allCategories))];
2025-02-24 08:52:50 +08:00
}, [data]);
2025-02-24 19:10:38 +08:00
return { categories, isLoading }
2025-02-24 08:52:50 +08:00
}
2025-02-25 10:58:35 +08:00
// 不同分类跳转
2025-02-25 10:01:17 +08:00
function useFetchCoursesByCategory(category: string) {
const isAll = category === '全部';
2025-02-25 10:58:35 +08:00
const { data, isLoading }: { data: CourseDto[], isLoading: boolean } = api.post.findMany.useQuery({
where: isAll ? {} : {
2025-02-25 10:01:17 +08:00
terms: {
some: {
2025-02-25 10:58:35 +08:00
name: category
2025-02-25 10:01:17 +08:00
},
},
},
take: 8,
2025-02-25 10:58:35 +08:00
include: {
terms: true
2025-02-25 10:01:17 +08:00
}
});
2025-02-25 10:58:35 +08:00
return { data, isLoading };
2025-02-25 10:01:17 +08:00
}
2025-02-06 16:32:31 +08:00
2025-02-25 10:58:35 +08:00
2025-02-06 16:32:31 +08:00
const { Title, Text } = Typography;
interface Course {
id: number;
title: string;
instructor: string;
students: number;
rating: number;
level: string;
duration: string;
category: string;
progress: number;
thumbnail: string;
}
interface CoursesSectionProps {
title: string;
description: string;
courses: Course[];
2025-02-25 10:58:35 +08:00
isLoading: boolean
2025-02-06 16:32:31 +08:00
initialVisibleCoursesCount?: number;
}
const CoursesSection: React.FC<CoursesSectionProps> = ({
title,
description,
courses,
2025-02-24 21:47:34 +08:00
isLoading,
2025-02-06 16:32:31 +08:00
initialVisibleCoursesCount = 8,
}) => {
const navigate = useNavigate();
const [selectedCategory, setSelectedCategory] = useState<string>('全部');
const [visibleCourses, setVisibleCourses] = useState(initialVisibleCoursesCount);
2025-02-24 19:10:38 +08:00
const gateGory: GetTaxonomyProps = useGetTaxonomy({
2025-02-21 17:02:51 +08:00
type: TaxonomySlug.CATEGORY,
})
2025-02-25 10:58:35 +08:00
const { data, isLoading: isDataLoading } = useFetchCoursesByCategory(selectedCategory);
useEffect(() => {
2025-02-25 10:01:17 +08:00
console.log('data:', data)
})
const handleClick = (course: CourseDto) => {
2025-02-25 10:08:43 +08:00
navigate(`/course/${course.id}/detail`);
2025-02-24 19:10:38 +08:00
}
2025-02-25 10:58:35 +08:00
useEffect(() => {
2025-02-25 10:02:40 +08:00
console.log('data:', data)
})
2025-02-06 16:32:31 +08:00
const filteredCourses = useMemo(() => {
return selectedCategory === '全部'
2025-02-25 10:01:17 +08:00
? data
: data?.filter(c => c.terms.some(t => t.name === selectedCategory));
}, [selectedCategory, data]);
2025-02-06 16:32:31 +08:00
2025-02-25 10:01:17 +08:00
const displayedCourses = isDataLoading ? [] : filteredCourses?.slice(0, visibleCourses);
2025-02-06 16:32:31 +08:00
return (
2025-02-24 19:10:38 +08:00
<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">
2025-02-06 16:32:31 +08:00
<div>
<Title
level={2}
2025-02-24 19:10:38 +08:00
className="font-bold text-5xl mb-6 bg-gradient-to-r from-blue-600 to-purple-600 bg-clip-text text-transparent"
2025-02-06 16:32:31 +08:00
>
{title}
</Title>
2025-02-24 19:10:38 +08:00
<Text type="secondary" className="text-xl font-light text-gray-600">
2025-02-06 16:32:31 +08:00
{description}
</Text>
</div>
</div>
2025-02-24 19:10:38 +08:00
<div className="mb-12 flex flex-wrap gap-4">
{gateGory.isLoading ? <Spin className='m-3' /> :
2025-02-21 17:02:51 +08:00
(
<>
2025-02-24 19:10:38 +08:00
<Tag
color={selectedCategory === "全部" ? 'blue' : 'default'}
onClick={() => setSelectedCategory("全部")}
className={`px-6 py-2 text-base cursor-pointer rounded-full transition-all duration-300 ${selectedCategory === "全部"
? 'bg-blue-600 text-white shadow-lg'
: 'bg-white text-gray-600 hover:bg-gray-100'
}`}
></Tag>
2025-02-25 10:58:35 +08:00
{gateGory.categories.map((category) => (
<Tag
key={category}
color={selectedCategory === category ? 'blue' : 'default'}
onClick={() => {
setSelectedCategory(category)
console.log(category)
}}
className={`px-6 py-2 text-base cursor-pointer rounded-full transition-all duration-300 ${selectedCategory === category
? 'bg-blue-600 text-white shadow-lg'
: 'bg-white text-gray-600 hover:bg-gray-100'
}`}
>
{category}
</Tag>
))
2025-02-24 19:10:38 +08:00
}
2025-02-25 10:58:35 +08:00
2025-02-21 17:02:51 +08:00
</>
)
2025-02-24 19:10:38 +08:00
}
2025-02-06 16:32:31 +08:00
</div>
2025-02-24 19:10:38 +08:00
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-8">
2025-02-25 10:58:35 +08:00
{displayedCourses.length=== 0 ? (
<div className="col-span-full">
<Empty description="暂无课程" image={Empty.PRESENTED_IMAGE_DEFAULT} />
</div>
) : displayedCourses?.map((course) => (
2025-02-06 16:32:31 +08:00
<Card
2025-02-24 19:10:38 +08:00
onClick={() => handleClick(course)}
2025-02-06 16:32:31 +08:00
key={course.id}
hoverable
2025-02-24 19:10:38 +08:00
className="group overflow-hidden rounded-2xl border border-gray-200 bg-white
shadow-xl hover:shadow-2xl transition-all duration-300 transform hover:-translate-y-2"
2025-02-06 16:32:31 +08:00
cover={
2025-02-24 19:10:38 +08:00
<div className="relative h-56 bg-gradient-to-br from-gray-900 to-gray-800 overflow-hidden">
2025-02-06 16:32:31 +08:00
<div
2025-02-24 19:10:38 +08:00
className="absolute inset-0 bg-cover bg-center transform transition-all duration-700 ease-out group-hover:scale-110"
2025-02-25 10:01:17 +08:00
style={{ backgroundImage: `url(${course?.meta?.thumbnail})` }}
2025-02-06 16:32:31 +08:00
/>
2025-02-24 19:10:38 +08:00
<div className="absolute inset-0 bg-gradient-to-t from-black/60 to-transparent opacity-80 group-hover:opacity-60 transition-opacity duration-300" />
<PlayCircleOutlined className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-6xl text-white/90 opacity-0 group-hover:opacity-100 transition-all duration-300" />
2025-02-06 16:32:31 +08:00
</div>
}
>
2025-02-24 19:10:38 +08:00
<div className="px-4">
2025-02-06 16:32:31 +08:00
<div className="flex gap-2 mb-4">
<Tag
color="blue"
2025-02-24 19:10:38 +08:00
className="px-3 py-1 rounded-full bg-blue-100 text-blue-600 border-0"
2025-02-06 16:32:31 +08:00
>
2025-02-25 10:01:17 +08:00
{course.terms[0].name}
2025-02-06 16:32:31 +08:00
</Tag>
<Tag
color={
2025-02-25 10:01:17 +08:00
course.terms[1].name === '入门'
2025-02-06 16:32:31 +08:00
? 'green'
2025-02-25 10:01:17 +08:00
: course.terms[1].name === '中级'
2025-02-06 16:32:31 +08:00
? 'blue'
: 'purple'
}
2025-02-24 19:10:38 +08:00
className="px-3 py-1 rounded-full border-0"
2025-02-06 16:32:31 +08:00
>
2025-02-25 10:01:17 +08:00
{course.terms[1].name}
2025-02-06 16:32:31 +08:00
</Tag>
</div>
<Title
level={4}
2025-02-24 19:10:38 +08:00
className="mb-4 line-clamp-2 font-bold leading-snug text-gray-800 hover:text-blue-600 transition-colors duration-300 group-hover:scale-[1.02] transform origin-left"
2025-02-06 16:32:31 +08:00
>
2025-02-24 19:10:38 +08:00
<button > {course.title}</button>
2025-02-06 16:32:31 +08:00
</Title>
2025-02-24 19:10:38 +08:00
<div className="flex items-center mb-4 p-2 rounded-lg transition-all duration-300 hover:bg-blue-50 group">
<TeamOutlined className="text-blue-500 text-lg transform group-hover:scale-110 transition-transform duration-300" />
<div className="ml-2 flex items-center flex-grow">
<Text className="font-medium text-blue-500 hover:text-blue-600 transition-colors duration-300">
</Text>
</div>
<span className="flex items-center bg-blue-100 px-2 py-1 rounded-full text-blue-600 hover:bg-blue-200 transition-colors duration-300">
<EyeOutlined className="ml-1.5 text-sm" />
2025-02-25 10:01:17 +08:00
<span className="text-xs font-medium">{course?.meta?.views}</span>
2025-02-06 16:32:31 +08:00
</span>
</div>
<div className="pt-4 border-t border-gray-100 text-center">
<Button
type="primary"
size="large"
className="w-full shadow-[0_8px_20px_-6px_rgba(59,130,246,0.5)] hover:shadow-[0_12px_24px_-6px_rgba(59,130,246,0.6)]
transform hover:translate-y-[-2px] transition-all duration-500 ease-out"
>
</Button>
</div>
2025-02-24 19:10:38 +08:00
2025-02-06 16:32:31 +08:00
</div>
</Card>
))}
</div>
2025-02-24 21:47:34 +08:00
{filteredCourses?.length >= visibleCourses && (
2025-02-24 19:10:38 +08:00
<div className='flex items-center gap-4 justify-between mt-12'>
<div className='h-[1px] flex-grow bg-gradient-to-r from-transparent via-gray-300 to-transparent'></div>
<div className="flex justify-end">
<Button
type="link"
2025-02-06 16:32:31 +08:00
onClick={() => navigate('/courses')}
2025-02-24 19:10:38 +08:00
className="flex items-center gap-2 text-gray-600 hover:text-blue-600 font-medium transition-colors duration-300"
2025-02-06 16:32:31 +08:00
>
2025-02-24 19:10:38 +08:00
<ArrowRightOutlined />
</Button>
2025-02-06 16:32:31 +08:00
</div>
</div>
2025-02-25 10:58:35 +08:00
)}
2025-02-06 16:32:31 +08:00
</div>
</section>
);
};
export default CoursesSection;