BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect, useCallback } from 'react'; |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 2 | import { useParams, useNavigate } from 'react-router-dom'; |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 3 | import { Button, Card, Form, Input, List, message, Avatar, Spin, Empty } from 'antd'; |
| 4 | import { ArrowLeftOutlined, UserOutlined } from '@ant-design/icons'; |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 5 | import { Layout } from 'antd'; |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 6 | import { listComments, addComment } from './service'; |
| 7 | import { responseSysTorrentComment, SysTorrentComment } from './data'; |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 8 | |
| 9 | const { Content } = Layout; |
| 10 | const { TextArea } = Input; |
| 11 | |
| 12 | interface CommentItem { |
| 13 | id: number; |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame^] | 14 | name: string; |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 15 | content: string; |
| 16 | createTime: string; |
| 17 | createBy: string; |
| 18 | torrentId: number; |
| 19 | } |
| 20 | |
| 21 | const TorrentComments: React.FC = () => { |
| 22 | const { torrentId } = useParams<{ torrentId: string }>(); |
| 23 | const navigate = useNavigate(); |
| 24 | const [form] = Form.useForm(); |
| 25 | const [comments, setComments] = useState<CommentItem[]>([]); |
| 26 | const [submitting, setSubmitting] = useState(false); |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 27 | const [loading, setLoading] = useState(false); |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 28 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 29 | // 格式化时间 |
| 30 | const formatTime = (time: string | Date | null | undefined): string => { |
| 31 | if (!time) return '未知时间'; |
| 32 | |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 33 | try { |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 34 | const date = new Date(time); |
| 35 | const now = new Date(); |
| 36 | const diff = now.getTime() - date.getTime(); |
| 37 | |
| 38 | // 小于1分钟 |
| 39 | if (diff < 60 * 1000) { |
| 40 | return '刚刚'; |
| 41 | } |
| 42 | // 小于1小时 |
| 43 | if (diff < 60 * 60 * 1000) { |
| 44 | return `${Math.floor(diff / (60 * 1000))}分钟前`; |
| 45 | } |
| 46 | // 小于24小时 |
| 47 | if (diff < 24 * 60 * 60 * 1000) { |
| 48 | return `${Math.floor(diff / (60 * 60 * 1000))}小时前`; |
| 49 | } |
| 50 | // 小于7天 |
| 51 | if (diff < 7 * 24 * 60 * 60 * 1000) { |
| 52 | return `${Math.floor(diff / (24 * 60 * 60 * 1000))}天前`; |
| 53 | } |
| 54 | |
| 55 | // 否则显示具体日期 |
| 56 | return date.toLocaleDateString('zh-CN', { |
| 57 | year: 'numeric', |
| 58 | month: '2-digit', |
| 59 | day: '2-digit', |
| 60 | hour: '2-digit', |
| 61 | minute: '2-digit' |
| 62 | }); |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 63 | } catch (error) { |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 64 | return '未知时间'; |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 68 | // 获取用户显示名称 |
| 69 | const getUserDisplayName = (userId: number | null | undefined): string => { |
| 70 | if (!userId) return '匿名用户'; |
| 71 | return `用户${userId}`; |
| 72 | }; |
| 73 | |
| 74 | // 获取用户头像字符 |
| 75 | const getAvatarChar = (userName: string): string => { |
| 76 | if (!userName || userName === '匿名用户') return '?'; |
| 77 | // 如果是"用户123"格式,取数字的最后一位 |
| 78 | const match = userName.match(/\d+$/); |
| 79 | if (match) { |
| 80 | return match[0].slice(-1); |
| 81 | } |
| 82 | return userName[0].toUpperCase(); |
| 83 | }; |
| 84 | |
| 85 | // 获取评论列表 |
| 86 | const fetchComments = useCallback(async () => { |
| 87 | if (!torrentId) return; |
| 88 | |
| 89 | setLoading(true); |
| 90 | try { |
| 91 | |
| 92 | const res = await listComments(Number(torrentId)); |
| 93 | console.log('获取评论列表:', res); |
| 94 | if (res) { |
| 95 | const formattedComments: SysTorrentComment[] = res.data.map((comment: SysTorrentComment) => ({ |
| 96 | id: comment.commentId ?? 0, |
BirdNETM | 632c061 | 2025-05-27 17:41:40 +0800 | [diff] [blame^] | 97 | name: comment.userName ?? '匿名用户', |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 98 | content: comment.content ?? '无内容', |
| 99 | createTime: comment.createTime || '', |
| 100 | createBy: getUserDisplayName(comment.userId), |
| 101 | torrentId: comment.torrentId ?? 0, |
| 102 | })); |
| 103 | |
| 104 | // 按时间倒序排列,最新的在前面 |
| 105 | formattedComments.sort((a, b) => { |
| 106 | const timeA = a.createTime ? new Date(a.createTime).getTime() : 0; |
| 107 | const timeB = b.createTime ? new Date(b.createTime).getTime() : 0; |
| 108 | return timeB - timeA; |
| 109 | }); |
| 110 | |
| 111 | setComments(formattedComments); |
| 112 | } else { |
| 113 | message.error(res?.msg || '获取评论列表失败'); |
| 114 | setComments([]); |
| 115 | } |
| 116 | } catch (error) { |
| 117 | console.error('获取评论失败:', error); |
| 118 | message.error('获取评论失败,请稍后重试'); |
| 119 | setComments([]); |
| 120 | } finally { |
| 121 | setLoading(false); |
| 122 | } |
| 123 | }, [torrentId]); |
| 124 | |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 125 | useEffect(() => { |
| 126 | if (torrentId) { |
| 127 | fetchComments(); |
| 128 | } |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 129 | }, [torrentId, fetchComments]); |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 130 | |
| 131 | // 提交评论 |
| 132 | const handleSubmit = async () => { |
| 133 | try { |
| 134 | const values = await form.validateFields(); |
| 135 | setSubmitting(true); |
| 136 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 137 | // 调用添加评论的API |
| 138 | const response = await addComment({ |
| 139 | torrentId: Number(torrentId), |
| 140 | content: values.content.trim(), |
| 141 | }); |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 142 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 143 | if (response?.code === 200) { |
| 144 | message.success('评论成功'); |
| 145 | form.resetFields(); |
| 146 | // 刷新评论列表 |
| 147 | await fetchComments(); |
| 148 | } else { |
| 149 | message.error(response?.msg || '评论失败,请稍后重试'); |
| 150 | } |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 151 | } catch (error) { |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 152 | console.error('评论失败:', error); |
| 153 | message.error('评论失败,请稍后重试'); |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 154 | } finally { |
| 155 | setSubmitting(false); |
| 156 | } |
| 157 | }; |
| 158 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 159 | // 处理按下 Ctrl+Enter 提交 |
| 160 | const handleKeyDown = (e: React.KeyboardEvent) => { |
| 161 | if (e.ctrlKey && e.key === 'Enter' && !submitting) { |
| 162 | handleSubmit(); |
| 163 | } |
| 164 | }; |
| 165 | |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 166 | return ( |
| 167 | <Content style={{ |
| 168 | height: '100vh', |
| 169 | display: 'flex', |
| 170 | flexDirection: 'column', |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 171 | overflow: 'hidden', |
| 172 | backgroundColor: '#f5f5f5' |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 173 | }}> |
| 174 | {/* 顶部标题栏 */} |
| 175 | <div style={{ |
| 176 | padding: '16px', |
| 177 | borderBottom: '1px solid #f0f0f0', |
| 178 | display: 'flex', |
| 179 | alignItems: 'center', |
| 180 | backgroundColor: '#fff', |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 181 | zIndex: 10, |
| 182 | boxShadow: '0 2px 8px rgba(0, 0, 0, 0.06)' |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 183 | }}> |
| 184 | <Button |
| 185 | type="link" |
| 186 | icon={<ArrowLeftOutlined />} |
| 187 | onClick={() => navigate(-1)} |
| 188 | style={{ marginRight: '10px', padding: 0 }} |
| 189 | /> |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 190 | <span style={{ fontSize: '16px', fontWeight: 'bold' }}> |
| 191 | 种子评论 {comments.length > 0 && `(${comments.length})`} |
| 192 | </span> |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 193 | </div> |
| 194 | |
| 195 | {/* 评论列表区域 - 可滚动 */} |
| 196 | <div style={{ |
| 197 | flex: 1, |
| 198 | overflowY: 'auto', |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 199 | padding: '16px', |
| 200 | paddingBottom: '8px' |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 201 | }}> |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 202 | <Spin spinning={loading}> |
| 203 | {comments.length === 0 && !loading ? ( |
| 204 | <Empty |
| 205 | description="暂无评论" |
| 206 | style={{ marginTop: '60px' }} |
| 207 | /> |
| 208 | ) : ( |
| 209 | <List |
| 210 | className="comment-list" |
| 211 | itemLayout="horizontal" |
| 212 | dataSource={comments} |
| 213 | renderItem={(item) => ( |
| 214 | <Card |
| 215 | style={{ |
| 216 | marginBottom: '12px', |
| 217 | borderRadius: '8px', |
| 218 | boxShadow: '0 1px 2px rgba(0, 0, 0, 0.03)' |
| 219 | }} |
| 220 | bodyStyle={{ padding: '12px 16px' }} |
| 221 | > |
| 222 | <List.Item style={{ border: 'none', padding: 0 }}> |
| 223 | <List.Item.Meta |
| 224 | avatar={ |
| 225 | <Avatar |
| 226 | style={{ |
| 227 | backgroundColor: '#1890ff', |
| 228 | verticalAlign: 'middle' |
| 229 | }} |
| 230 | size="default" |
| 231 | icon={<UserOutlined />} |
| 232 | > |
| 233 | {getAvatarChar(item.createBy)} |
| 234 | </Avatar> |
| 235 | } |
| 236 | title={ |
| 237 | <div style={{ |
| 238 | display: 'flex', |
| 239 | justifyContent: 'space-between', |
| 240 | alignItems: 'center' |
| 241 | }}> |
| 242 | <span style={{ fontWeight: 500 }}>{item.createBy}</span> |
| 243 | <span style={{ |
| 244 | color: '#8c8c8c', |
| 245 | fontSize: '12px', |
| 246 | fontWeight: 'normal' |
| 247 | }}> |
| 248 | {formatTime(item.createTime)} |
| 249 | </span> |
| 250 | </div> |
| 251 | } |
| 252 | description={ |
| 253 | <div style={{ |
| 254 | marginTop: '8px', |
| 255 | color: '#262626', |
| 256 | fontSize: '14px', |
| 257 | lineHeight: '22px', |
| 258 | wordBreak: 'break-word' |
| 259 | }}> |
| 260 | {item.content} |
| 261 | </div> |
| 262 | } |
| 263 | /> |
| 264 | </List.Item> |
| 265 | </Card> |
| 266 | )} |
| 267 | /> |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 268 | )} |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 269 | </Spin> |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 270 | </div> |
| 271 | |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 272 | {/* 评论输入框 - 固定在底部 */} |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 273 | <div style={{ |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 274 | padding: '16px', |
| 275 | backgroundColor: '#fff', |
| 276 | borderTop: '1px solid #f0f0f0', |
| 277 | boxShadow: '0 -2px 8px rgba(0, 0, 0, 0.06)' |
| 278 | }}> |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 279 | <Form form={form} onFinish={handleSubmit}> |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 280 | <Form.Item |
| 281 | name="content" |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 282 | rules={[ |
| 283 | { required: true, message: '请输入评论内容' }, |
| 284 | { whitespace: true, message: '评论内容不能为空' }, |
| 285 | { max: 500, message: '评论内容不能超过500个字符' } |
| 286 | ]} |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 287 | style={{ marginBottom: '12px' }} |
| 288 | > |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 289 | <TextArea |
| 290 | rows={3} |
| 291 | placeholder="请输入您的评论(Ctrl+Enter 快速提交)" |
| 292 | maxLength={500} |
| 293 | showCount |
| 294 | onKeyDown={handleKeyDown} |
| 295 | disabled={submitting} |
| 296 | /> |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 297 | </Form.Item> |
| 298 | <Form.Item style={{ marginBottom: 0, textAlign: 'right' }}> |
| 299 | <Button |
| 300 | htmlType="submit" |
| 301 | loading={submitting} |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 302 | type="primary" |
BirdNETM | ed9f2f9 | 2025-05-26 20:12:30 +0800 | [diff] [blame] | 303 | disabled={loading} |
BirdNETM | b0f7153 | 2025-05-26 17:37:33 +0800 | [diff] [blame] | 304 | > |
| 305 | 提交评论 |
| 306 | </Button> |
| 307 | </Form.Item> |
| 308 | </Form> |
| 309 | </div> |
| 310 | </Content> |
| 311 | ); |
| 312 | }; |
| 313 | |
| 314 | export default TorrentComments; |