Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 1 | import React, { useState } from 'react';
|
| 2 | import { useParams, useNavigate,useLocation } from 'react-router-dom';
|
| 3 | import './RequestDetail.css';
|
| 4 |
|
| 5 | const 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) => {
|
22301080 | a93bebb | 2025-05-27 19:48:11 +0800 | [diff] [blame^] | 81 |
|
| 82 |
|
| 83 |
|
Akane1217 | 65b61a7 | 2025-05-17 13:52:25 +0800 | [diff] [blame] | 84 | console.log('下载种子', commentId);
|
| 85 | // 实际下载逻辑
|
| 86 | };
|
| 87 |
|
| 88 | const handleBack = () => {
|
| 89 | const fromTab = location.state?.fromTab; // 从跳转时传递的 state 中获取
|
| 90 | if (fromTab) {
|
| 91 | navigate(`/dashboard/${fromTab}`); // 明确返回对应标签页
|
| 92 | } else {
|
| 93 | navigate(-1); // 保底策略
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | return (
|
| 98 | <div className="request-detail-container">
|
| 99 | <button className="back-button" onClick={handleBack}>
|
| 100 | ← 返回求种区
|
| 101 | </button>
|
| 102 |
|
| 103 | <div className="request-post">
|
| 104 | <div className="post-header">
|
| 105 | <img src={post.authorAvatar} alt={post.author} className="post-avatar" />
|
| 106 | <div className="post-meta">
|
| 107 | <div className="post-author">{post.author}</div>
|
| 108 | <div className="post-date">{post.date}</div>
|
| 109 | </div>
|
| 110 | </div>
|
| 111 |
|
| 112 | <h1 className="post-title">{post.title}</h1>
|
| 113 |
|
| 114 | <div className="post-content">
|
| 115 | {post.content.split('\n').map((para, i) => (
|
| 116 | <p key={i}>{para}</p>
|
| 117 | ))}
|
| 118 | </div>
|
| 119 |
|
| 120 | <div className="post-actions">
|
| 121 | <button
|
| 122 | className={`like-button ${post.isLiked ? 'liked' : ''}`}
|
| 123 | onClick={handleLikePost}
|
| 124 | >
|
| 125 | 👍 点赞 ({post.likeCount})
|
| 126 | </button>
|
| 127 | <button
|
| 128 | className={`favorite-button ${post.isFavorited ? 'favorited' : ''}`}
|
| 129 | onClick={handleFavoritePost}
|
| 130 | >
|
| 131 | {post.isFavorited ? '★ 已收藏' : '☆ 收藏'}
|
| 132 | </button>
|
| 133 | </div>
|
| 134 | </div>
|
| 135 |
|
| 136 | <div className="comments-section">
|
| 137 | <h2>回应 ({comments.length})</h2>
|
| 138 |
|
| 139 | <form onSubmit={handleCommentSubmit} className="comment-form">
|
| 140 | <textarea
|
| 141 | value={newComment}
|
| 142 | onChange={(e) => setNewComment(e.target.value)}
|
| 143 | placeholder="写下你的回应..."
|
| 144 | rows="3"
|
| 145 | required
|
| 146 | />
|
| 147 | <div className="form-actions">
|
| 148 | <button type="submit" className="submit-comment">发表文字回应</button>
|
| 149 | <button
|
| 150 | type="button"
|
| 151 | className="submit-torrent"
|
| 152 | onClick={() => console.log('打开种子上传对话框')}
|
| 153 | >
|
| 154 | 上传种子回应
|
| 155 | </button>
|
| 156 | </div>
|
| 157 | </form>
|
| 158 |
|
| 159 | <div className="comment-list">
|
| 160 | {comments.map(comment => (
|
| 161 | <div key={comment.id} className={`comment-item ${comment.type}`}>
|
| 162 | <img
|
| 163 | src={comment.authorAvatar}
|
| 164 | alt={comment.author}
|
| 165 | className="comment-avatar"
|
| 166 | />
|
| 167 |
|
| 168 | <div className="comment-content">
|
| 169 | <div className="comment-header">
|
| 170 | <span className="comment-author">{comment.author}</span>
|
| 171 | <span className="comment-date">{comment.date}</span>
|
| 172 | </div>
|
| 173 |
|
| 174 | {comment.type === 'text' ? (
|
| 175 | <p className="comment-text">{comment.content}</p>
|
| 176 | ) : (
|
| 177 | <div className="torrent-comment">
|
| 178 | <span className="torrent-title">{comment.title}</span>
|
| 179 | <span className="torrent-size">{comment.size}</span>
|
| 180 | <button
|
| 181 | className="download-torrent"
|
| 182 | onClick={() => handleDownloadTorrent(comment.id)}
|
| 183 | >
|
| 184 | 立即下载
|
| 185 | </button>
|
| 186 | </div>
|
| 187 | )}
|
| 188 |
|
| 189 | <button className="comment-like">
|
| 190 | 👍 ({comment.likeCount})
|
| 191 | </button>
|
| 192 | </div>
|
| 193 | </div>
|
| 194 | ))}
|
| 195 | </div>
|
| 196 | </div>
|
| 197 | </div>
|
| 198 | );
|
| 199 | };
|
| 200 |
|
| 201 | export default RequestDetail; |