刘嘉昕 | 9cdeca2 | 2025-06-03 16:54:30 +0800 | [diff] [blame] | 1 | // // src/pages/Home.jsx |
| 2 | // import React from 'react'; |
| 3 | // import TorrentList from '../components/torrentlist'; |
| 4 | // import { Link } from 'react-router-dom'; |
| 5 | |
| 6 | // const Home = () => { |
| 7 | // return ( |
| 8 | // <div className="min-h-screen bg-gray-100 p-4"> |
| 9 | // <div className="flex justify-between items-center mb-4"> |
| 10 | // <h1 className="text-2xl font-bold">种子列表</h1> |
| 11 | // <Link to="/upload" className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"> |
| 12 | // 上传种子 |
| 13 | // </Link> |
| 14 | // </div> |
| 15 | // <TorrentList /> |
| 16 | // </div> |
| 17 | // ); |
| 18 | // }; |
| 19 | |
| 20 | // export default Home; |
| 21 | import React, { useState, useEffect } from 'react'; |
| 22 | import { Link } from 'react-router-dom'; |
| 23 | import TorrentList from '../components/torrentlist'; |
| 24 | import Post from '../components/Post'; |
| 25 | import FriendManager from '../components/FriendManager'; |
| 26 | import ChatBox from '../components/ChatBox'; |
| 27 | import RequestBoard from '../components/RequestBoard'; |
| 28 | import { getActivityPreviews, getFullActivities } from '../api/activity'; |
| 29 | |
| 30 | const Home = () => { |
| 31 | const currentUser = { |
| 32 | id: 1, |
| 33 | username: '测试用户', |
| 34 | }; |
| 35 | |
| 36 | const [selectedRelation, setSelectedRelation] = useState(null); |
| 37 | const [activityPreviews, setActivityPreviews] = useState([]); |
| 38 | const [fullActivities, setFullActivities] = useState([]); |
| 39 | const [selectedActivityId, setSelectedActivityId] = useState(null); |
| 40 | |
| 41 | useEffect(() => { |
| 42 | getActivityPreviews().then(res => setActivityPreviews(res.data)); |
| 43 | getFullActivities().then(res => setFullActivities(res.data)); |
| 44 | }, []); |
| 45 | |
| 46 | const selectedActivity = fullActivities.find( |
| 47 | activity => activity.activityid === selectedActivityId |
| 48 | ); |
| 49 | |
| 50 | return ( |
| 51 | <div className="min-h-screen bg-gray-100 p-6"> |
| 52 | <div className="flex justify-between items-center mb-6"> |
| 53 | <h1 className="text-3xl font-bold">Pt站</h1> |
| 54 | <Link to="/upload" className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"> |
| 55 | 上传种子 |
| 56 | </Link> |
| 57 | </div> |
| 58 | |
| 59 | {/* 种子列表区域 */} |
| 60 | <div className="bg-white p-4 rounded shadow mb-8"> |
| 61 | <h2 className="text-xl font-semibold mb-4">种子列表</h2> |
| 62 | <TorrentList /> |
| 63 | </div> |
| 64 | |
| 65 | {/* 活动区域 */} |
| 66 | <div className="bg-white p-4 rounded shadow mb-8"> |
| 67 | <h2 className="text-xl font-semibold mb-4">活动预览</h2> |
| 68 | {!selectedActivity ? ( |
| 69 | <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4"> |
| 70 | {activityPreviews.map(activity => ( |
| 71 | <div key={activity.activityid} className="border p-3 rounded shadow"> |
| 72 | <h3 className="text-lg font-medium mb-2">{activity.title}</h3> |
| 73 | <img |
| 74 | src={activity.photo} |
| 75 | alt={activity.title} |
| 76 | className="w-full h-40 object-cover mb-2 rounded" |
| 77 | /> |
| 78 | <button |
| 79 | className="bg-blue-500 text-white px-3 py-1 rounded hover:bg-blue-600" |
| 80 | onClick={() => setSelectedActivityId(activity.activityid)} |
| 81 | > |
| 82 | 查看详情 |
| 83 | </button> |
| 84 | </div> |
| 85 | ))} |
| 86 | </div> |
| 87 | ) : ( |
| 88 | <div className="p-4 border rounded shadow"> |
| 89 | <button |
| 90 | className="mb-4 text-blue-600 underline" |
| 91 | onClick={() => setSelectedActivityId(null)} |
| 92 | > |
| 93 | ← 返回列表 |
| 94 | </button> |
| 95 | <h3 className="text-2xl font-bold mb-2">{selectedActivity.title}</h3> |
| 96 | <img |
| 97 | src={selectedActivity.photo} |
| 98 | alt={selectedActivity.title} |
| 99 | className="w-full h-60 object-cover rounded mb-4" |
| 100 | /> |
| 101 | <p className="mb-2"><strong>内容:</strong>{selectedActivity.content}</p> |
| 102 | <p className="mb-2"><strong>时间:</strong>{selectedActivity.time}</p> |
| 103 | <p className="mb-2"><strong>奖励:</strong>{selectedActivity.award}</p> |
| 104 | </div> |
| 105 | )} |
| 106 | </div> |
| 107 | |
| 108 | {/* 主内容区 */} |
| 109 | <div className="grid grid-cols-1 xl:grid-cols-3 gap-8"> |
| 110 | {/* 帖子区域 */} |
| 111 | <div className="bg-white p-4 rounded shadow xl:col-span-1"> |
| 112 | <h2 className="text-xl font-semibold mb-4">最新帖子</h2> |
| 113 | <Post /> |
| 114 | </div> |
| 115 | |
| 116 | {/* 好友管理区 */} |
| 117 | <div className="bg-white p-4 rounded shadow xl:col-span-1"> |
| 118 | <h2 className="text-xl font-semibold mb-4">好友管理</h2> |
| 119 | <FriendManager |
| 120 | currentUser={currentUser} |
| 121 | onSelectRelation={setSelectedRelation} |
| 122 | /> |
| 123 | </div> |
| 124 | |
| 125 | {/* 聊天窗口 */} |
| 126 | {selectedRelation && ( |
| 127 | <div className="bg-white p-4 rounded shadow xl:col-span-1"> |
| 128 | <h2 className="text-xl font-semibold mb-4">聊天窗口</h2> |
| 129 | <ChatBox |
| 130 | senderId={currentUser.id} |
| 131 | receiverId={selectedRelation.friendId} |
| 132 | /> |
| 133 | </div> |
| 134 | )} |
| 135 | </div> |
| 136 | |
| 137 | {/* 求助帖区域 */} |
| 138 | <div className="bg-white p-4 mt-8 rounded shadow"> |
| 139 | <h2 className="text-xl font-semibold mb-4">求助帖管理</h2> |
| 140 | <RequestBoard currentUserId={currentUser.id} /> |
| 141 | </div> |
| 142 | </div> |
| 143 | ); |
| 144 | }; |
| 145 | |
| 146 | export default Home; |