blob: 993fe8ef89aaf00c3ce8e6eea71a6cfd4d3ed48e [file] [log] [blame]
Akane121765b61a72025-05-17 13:52:25 +08001import React, { useState } from 'react';
2import { useParams, useNavigate,useLocation } from 'react-router-dom';
3import './RequestDetail.css';
4
5const RequestDetail = () => {
6 const { id } = useParams();
7 const navigate = useNavigate();
8 const location = useLocation();
9
10 // 模拟数据
11 const [post, setPost] = useState({
12 id: 1,
13 title: '求《药屋少女的呢喃》第二季全集',
14 content: '求1080P带中文字幕版本,最好是内嵌字幕不是外挂的。\n\n希望有热心大佬能分享,可以给积分奖励!',
15 author: '动漫爱好者',
16 authorAvatar: 'https://via.placeholder.com/40',
17 date: '2023-10-15',
18 likeCount: 24,
19 isLiked: false,
20 isFavorited: false
21 });
22
23 const [comments, setComments] = useState([
24 {
25 id: 1,
26 type: 'text',
27 author: '资源达人',
28 authorAvatar: 'https://via.placeholder.com/40',
29 content: '我有第1-5集,需要的话可以私聊',
30 date: '2023-10-15 14:30',
31 likeCount: 5
32 },
33 {
34 id: 2,
35 type: 'torrent',
36 title: '药屋少女的呢喃第二季第8集',
37 size: '1.2GB',
38 author: '种子分享者',
39 authorAvatar: 'https://via.placeholder.com/40',
40 date: '2023-10-16 09:15',
41 likeCount: 8
42 }
43 ]);
44
45 const [newComment, setNewComment] = useState('');
46
47 const handleLikePost = () => {
48 setPost(prev => ({
49 ...prev,
50 likeCount: prev.isLiked ? prev.likeCount - 1 : prev.likeCount + 1,
51 isLiked: !prev.isLiked
52 }));
53 };
54
55 const handleFavoritePost = () => {
56 setPost(prev => ({
57 ...prev,
58 isFavorited: !prev.isFavorited
59 }));
60 };
61
62 const handleCommentSubmit = (e) => {
63 e.preventDefault();
64 if (!newComment.trim()) return;
65
66 const newCommentObj = {
67 id: comments.length + 1,
68 type: 'text',
69 author: '当前用户',
70 authorAvatar: 'https://via.placeholder.com/40',
71 content: newComment,
72 date: new Date().toLocaleString(),
73 likeCount: 0
74 };
75
76 setComments([...comments, newCommentObj]);
77 setNewComment('');
78 };
79
80 const handleDownloadTorrent = (commentId) => {
81 console.log('下载种子', commentId);
82 // 实际下载逻辑
83 };
84
85 const handleBack = () => {
86 const fromTab = location.state?.fromTab; // 从跳转时传递的 state 中获取
87 if (fromTab) {
88 navigate(`/dashboard/${fromTab}`); // 明确返回对应标签页
89 } else {
90 navigate(-1); // 保底策略
91 }
92 }
93
94 return (
95 <div className="request-detail-container">
96 <button className="back-button" onClick={handleBack}>
97 &larr; 返回求种区
98 </button>
99
100 <div className="request-post">
101 <div className="post-header">
102 <img src={post.authorAvatar} alt={post.author} className="post-avatar" />
103 <div className="post-meta">
104 <div className="post-author">{post.author}</div>
105 <div className="post-date">{post.date}</div>
106 </div>
107 </div>
108
109 <h1 className="post-title">{post.title}</h1>
110
111 <div className="post-content">
112 {post.content.split('\n').map((para, i) => (
113 <p key={i}>{para}</p>
114 ))}
115 </div>
116
117 <div className="post-actions">
118 <button
119 className={`like-button ${post.isLiked ? 'liked' : ''}`}
120 onClick={handleLikePost}
121 >
122 👍 点赞 ({post.likeCount})
123 </button>
124 <button
125 className={`favorite-button ${post.isFavorited ? 'favorited' : ''}`}
126 onClick={handleFavoritePost}
127 >
128 {post.isFavorited ? '★ 已收藏' : '☆ 收藏'}
129 </button>
130 </div>
131 </div>
132
133 <div className="comments-section">
134 <h2>回应 ({comments.length})</h2>
135
136 <form onSubmit={handleCommentSubmit} className="comment-form">
137 <textarea
138 value={newComment}
139 onChange={(e) => setNewComment(e.target.value)}
140 placeholder="写下你的回应..."
141 rows="3"
142 required
143 />
144 <div className="form-actions">
145 <button type="submit" className="submit-comment">发表文字回应</button>
146 <button
147 type="button"
148 className="submit-torrent"
149 onClick={() => console.log('打开种子上传对话框')}
150 >
151 上传种子回应
152 </button>
153 </div>
154 </form>
155
156 <div className="comment-list">
157 {comments.map(comment => (
158 <div key={comment.id} className={`comment-item ${comment.type}`}>
159 <img
160 src={comment.authorAvatar}
161 alt={comment.author}
162 className="comment-avatar"
163 />
164
165 <div className="comment-content">
166 <div className="comment-header">
167 <span className="comment-author">{comment.author}</span>
168 <span className="comment-date">{comment.date}</span>
169 </div>
170
171 {comment.type === 'text' ? (
172 <p className="comment-text">{comment.content}</p>
173 ) : (
174 <div className="torrent-comment">
175 <span className="torrent-title">{comment.title}</span>
176 <span className="torrent-size">{comment.size}</span>
177 <button
178 className="download-torrent"
179 onClick={() => handleDownloadTorrent(comment.id)}
180 >
181 立即下载
182 </button>
183 </div>
184 )}
185
186 <button className="comment-like">
187 👍 ({comment.likeCount})
188 </button>
189 </div>
190 </div>
191 ))}
192 </div>
193 </div>
194 </div>
195 );
196};
197
198export default RequestDetail;