22301014 | 3d96630 | 2025-06-07 22:54:40 +0800 | [diff] [blame^] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { useParams, useNavigate } from 'react-router'; |
| 3 | import { Button, Tabs, message, Spin, Tag, Typography, Space, Divider } from 'antd'; |
| 4 | import { ArrowLeftOutlined, LikeOutlined, BugOutlined, CommentOutlined } from '@ant-design/icons'; |
| 5 | import WorkAPI from '../../api/workApi'; |
| 6 | import BugReportSection from '../../components/BugReportSection'; |
| 7 | import DiscussionSection from '../../components/DiscussionSection'; |
| 8 | import type { Work } from '../../api/otherType'; |
| 9 | |
| 10 | const { Title, Text, Paragraph } = Typography; |
| 11 | const { TabPane } = Tabs; |
| 12 | |
| 13 | const WorkPage: React.FC = () => { |
| 14 | const { id } = useParams<{ id: string }>(); |
| 15 | const navigate = useNavigate(); |
| 16 | const [work, setWork] = useState<Work | null>(null); |
| 17 | const [loading, setLoading] = useState(true); |
| 18 | const [activeTab, setActiveTab] = useState('details'); |
| 19 | |
| 20 | // 加载作品数据 |
| 21 | useEffect(() => { |
| 22 | const loadWork = async () => { |
| 23 | try { |
| 24 | const workData = await WorkAPI.getWorkById(Number(id)); |
| 25 | setWork(workData); |
| 26 | } catch (error) { |
| 27 | message.error('加载作品失败'); |
| 28 | navigate('/'); |
| 29 | } finally { |
| 30 | setLoading(false); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | loadWork(); |
| 35 | }, [id, navigate]); |
| 36 | |
| 37 | // 点赞处理 |
| 38 | const handleLike = async () => { |
| 39 | try { |
| 40 | await WorkAPI.likeWork(Number(id)); |
| 41 | setWork(prev => prev ? { ...prev, likes: prev.likes + 1 } : null); |
| 42 | message.success('点赞成功'); |
| 43 | } catch (error) { |
| 44 | message.error('点赞失败'); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | if (loading) { |
| 49 | return <Spin size="large" className="center-spinner" />; |
| 50 | } |
| 51 | |
| 52 | if (!work) { |
| 53 | return <div>作品不存在</div>; |
| 54 | } |
| 55 | |
| 56 | return ( |
| 57 | <div className="work-page-container"> |
| 58 | <Button |
| 59 | type="text" |
| 60 | icon={<ArrowLeftOutlined />} |
| 61 | onClick={() => navigate(-1)} |
| 62 | className="back-button" |
| 63 | > |
| 64 | 返回 |
| 65 | </Button> |
| 66 | |
| 67 | <div className="work-header"> |
| 68 | <Title level={2}>{work.title}</Title> |
| 69 | <Text type="secondary">作者: {work.author}</Text> |
| 70 | |
| 71 | <div className="work-meta"> |
| 72 | <Space size="middle"> |
| 73 | <Tag color="blue">{work.categoryName}</Tag> |
| 74 | <Text>浏览: {work.views}</Text> |
| 75 | <Text>点赞: {work.likes}</Text> |
| 76 | <Text>上传时间: {new Date(work.createTime).toLocaleDateString()}</Text> |
| 77 | </Space> |
| 78 | </div> |
| 79 | |
| 80 | <Button |
| 81 | type="primary" |
| 82 | icon={<LikeOutlined />} |
| 83 | onClick={handleLike} |
| 84 | className="like-button" |
| 85 | > |
| 86 | 点赞 |
| 87 | </Button> |
| 88 | </div> |
| 89 | |
| 90 | <Divider /> |
| 91 | |
| 92 | <Tabs activeKey={activeTab} onChange={setActiveTab}> |
| 93 | <TabPane tab="作品详情" key="details"> |
| 94 | <div className="work-content"> |
| 95 | <Title level={4}>作品描述</Title> |
| 96 | <Paragraph>{work.description}</Paragraph> |
| 97 | |
| 98 | <Title level={4}>作品内容</Title> |
| 99 | <div className="content-container"> |
| 100 | {work.content} |
| 101 | </div> |
| 102 | </div> |
| 103 | </TabPane> |
| 104 | |
| 105 | <TabPane |
| 106 | tab={ |
| 107 | <span> |
| 108 | <BugOutlined /> Bug反馈 |
| 109 | </span> |
| 110 | } |
| 111 | key="bugs" |
| 112 | > |
| 113 | <BugReportSection workId={work.id} /> |
| 114 | </TabPane> |
| 115 | |
| 116 | <TabPane |
| 117 | tab={ |
| 118 | <span> |
| 119 | <CommentOutlined /> 交流区 |
| 120 | </span> |
| 121 | } |
| 122 | key="discussions" |
| 123 | > |
| 124 | <DiscussionSection workId={work.id} /> |
| 125 | </TabPane> |
| 126 | </Tabs> |
| 127 | </div> |
| 128 | ); |
| 129 | }; |
| 130 | |
| 131 | export default WorkPage; |