BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame^] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { useParams, useNavigate } from 'react-router-dom'; |
| 3 | import { Button, Card, Form, Input, List, message, Avatar } from 'antd'; |
| 4 | import { ArrowLeftOutlined } from '@ant-design/icons'; |
| 5 | import { Layout } from 'antd'; |
| 6 | |
| 7 | const { Content } = Layout; |
| 8 | const { TextArea } = Input; |
| 9 | |
| 10 | interface CommentItem { |
| 11 | id: number; |
| 12 | content: string; |
| 13 | createTime: string; |
| 14 | createBy: string; |
| 15 | torrentId: number; |
| 16 | } |
| 17 | |
| 18 | const TorrentComments: React.FC = () => { |
| 19 | const { torrentId } = useParams<{ torrentId: string }>(); |
| 20 | const navigate = useNavigate(); |
| 21 | const [form] = Form.useForm(); |
| 22 | const [comments, setComments] = useState<CommentItem[]>([]); |
| 23 | const [submitting, setSubmitting] = useState(false); |
| 24 | |
| 25 | // 获取评论列表 |
| 26 | const fetchComments = async () => { |
| 27 | try { |
| 28 | // 模拟评论数据 |
| 29 | const mockComments: CommentItem[] = [ |
| 30 | { |
| 31 | id: 1, |
| 32 | content: '这个种子非常好,下载速度很快!', |
| 33 | createTime: '2024-01-15 14:30:00', |
| 34 | createBy: '张三', |
| 35 | torrentId: Number(torrentId) |
| 36 | }, |
| 37 | { |
| 38 | id: 2, |
| 39 | content: '资源质量很高,感谢分享。', |
| 40 | createTime: '2024-01-15 15:20:00', |
| 41 | createBy: '李四', |
| 42 | torrentId: Number(torrentId) |
| 43 | }, |
| 44 | { |
| 45 | id: 3, |
| 46 | content: '这个版本很完整,推荐下载!', |
| 47 | createTime: '2024-01-15 16:45:00', |
| 48 | createBy: '王五', |
| 49 | torrentId: Number(torrentId) |
| 50 | }, |
| 51 | { |
| 52 | id: 1, |
| 53 | content: '这个种子非常好,下载速度很快!', |
| 54 | createTime: '2024-01-15 14:30:00', |
| 55 | createBy: '张三', |
| 56 | torrentId: Number(torrentId) |
| 57 | }, |
| 58 | { |
| 59 | id: 2, |
| 60 | content: '资源质量很高,感谢分享。', |
| 61 | createTime: '2024-01-15 15:20:00', |
| 62 | createBy: '李四', |
| 63 | torrentId: Number(torrentId) |
| 64 | }, |
| 65 | { |
| 66 | id: 3, |
| 67 | content: '这个版本很完整,推荐下载!', |
| 68 | createTime: '2024-01-15 16:45:00', |
| 69 | createBy: '王五', |
| 70 | torrentId: Number(torrentId) |
| 71 | } |
| 72 | ]; |
| 73 | setComments(mockComments); |
| 74 | } catch (error) { |
| 75 | message.error('获取评论失败'); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | useEffect(() => { |
| 80 | if (torrentId) { |
| 81 | fetchComments(); |
| 82 | } |
| 83 | }, [torrentId]); |
| 84 | |
| 85 | // 提交评论 |
| 86 | const handleSubmit = async () => { |
| 87 | try { |
| 88 | const values = await form.validateFields(); |
| 89 | setSubmitting(true); |
| 90 | |
| 91 | // TODO: 替换为实际的API调用 |
| 92 | // await addComment({ |
| 93 | // torrentId: Number(torrentId), |
| 94 | // content: values.content, |
| 95 | // }); |
| 96 | |
| 97 | message.success('评论成功'); |
| 98 | form.resetFields(); |
| 99 | fetchComments(); // 刷新评论列表 |
| 100 | } catch (error) { |
| 101 | message.error('评论失败'); |
| 102 | } finally { |
| 103 | setSubmitting(false); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | return ( |
| 108 | <Content style={{ |
| 109 | height: '100vh', |
| 110 | display: 'flex', |
| 111 | flexDirection: 'column', |
| 112 | overflow: 'hidden' // 防止内容溢出 |
| 113 | }}> |
| 114 | {/* 顶部标题栏 */} |
| 115 | <div style={{ |
| 116 | padding: '16px', |
| 117 | borderBottom: '1px solid #f0f0f0', |
| 118 | display: 'flex', |
| 119 | alignItems: 'center', |
| 120 | backgroundColor: '#fff', |
| 121 | zIndex: 10 |
| 122 | }}> |
| 123 | <Button |
| 124 | type="link" |
| 125 | icon={<ArrowLeftOutlined />} |
| 126 | onClick={() => navigate(-1)} |
| 127 | style={{ marginRight: '10px', padding: 0 }} |
| 128 | /> |
| 129 | <span style={{ fontSize: '16px', fontWeight: 'bold' }}>种子评论</span> |
| 130 | </div> |
| 131 | |
| 132 | {/* 评论列表区域 - 可滚动 */} |
| 133 | <div style={{ |
| 134 | flex: 1, |
| 135 | overflowY: 'auto', |
| 136 | padding: '0 16px', |
| 137 | paddingBottom: '16px' |
| 138 | }}> |
| 139 | <List |
| 140 | className="comment-list" |
| 141 | itemLayout="horizontal" |
| 142 | dataSource={comments} |
| 143 | renderItem={(item) => ( |
| 144 | <List.Item> |
| 145 | <List.Item.Meta |
| 146 | avatar={<Avatar>{item.createBy[0]}</Avatar>} |
| 147 | title={item.createBy} |
| 148 | description={ |
| 149 | <div> |
| 150 | <div>{item.content}</div> |
| 151 | <div style={{ color: '#8c8c8c', fontSize: '12px', marginTop: '8px' }}> |
| 152 | {item.createTime} |
| 153 | </div> |
| 154 | </div> |
| 155 | } |
| 156 | /> |
| 157 | </List.Item> |
| 158 | )} |
| 159 | /> |
| 160 | </div> |
| 161 | |
| 162 | {/* 评论输入框 - 固定在父组件底部 */} |
| 163 | <div style={{ |
| 164 | position: 'relative', |
| 165 | padding: '16px', |
| 166 | backgroundColor: '#fff', |
| 167 | borderTop: '1px solid #f0f0f0', |
| 168 | boxShadow: '0 -2px 8px rgba(0, 0, 0, 0.06)' |
| 169 | }}> |
| 170 | <Form form={form}> |
| 171 | <Form.Item |
| 172 | name="content" |
| 173 | rules={[{ required: true, message: '请输入评论内容' }]} |
| 174 | style={{ marginBottom: '12px' }} |
| 175 | > |
| 176 | <TextArea rows={3} placeholder="请输入您的评论" /> |
| 177 | </Form.Item> |
| 178 | <Form.Item style={{ marginBottom: 0, textAlign: 'right' }}> |
| 179 | <Button |
| 180 | htmlType="submit" |
| 181 | loading={submitting} |
| 182 | onClick={handleSubmit} |
| 183 | type="primary" |
| 184 | > |
| 185 | 提交评论 |
| 186 | </Button> |
| 187 | </Form.Item> |
| 188 | </Form> |
| 189 | </div> |
| 190 | </Content> |
| 191 | ); |
| 192 | }; |
| 193 | |
| 194 | export default TorrentComments; |