ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 1 | import React, { useState, useEffect } from 'react'; |
| 2 | import { |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 3 | Box, Container, Typography, Paper, |
| 4 | List, ListItem, ListItemText, Chip, Stack, CircularProgress, Alert |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 5 | } from '@mui/material'; |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 6 | import { Link } from 'react-router-dom'; // 假设你已经配置了 React Router |
| 7 | |
| 8 | // import '../styles/base/base.css'; // ✅ 引入统一样式 - 我们将尝试用 sx prop 替代 |
| 9 | |
| 10 | // 模拟从API获取数据 |
| 11 | const fetchTorrents = () => { |
| 12 | return new Promise((resolve) => { |
| 13 | setTimeout(() => { |
| 14 | resolve([ |
| 15 | { |
| 16 | id: 1, |
| 17 | title: 'Ubuntu ISO 镜像', |
| 18 | tags: ['Linux', '操作系统', 'ISO'], |
| 19 | size: '2.1 GB', |
| 20 | uploader: 'admin', |
| 21 | description: '最新的 Ubuntu Desktop 长期支持版。适用于开发者和普通用户。' |
| 22 | }, |
| 23 | { |
| 24 | id: 2, |
| 25 | title: '开源电影素材包 - Blender "Sintel"', |
| 26 | tags: ['视频', '素材', '开源', 'Blender'], |
| 27 | size: '800 MB', |
| 28 | uploader: 'user001', |
| 29 | description: '来自 Blender Foundation 的开源电影 "Sintel" 的原始素材,可用于学习和创作。' |
| 30 | }, |
| 31 | { |
| 32 | id: 3, |
| 33 | title: 'React 学习资料大全', |
| 34 | tags: ['React', 'JavaScript', '前端开发', '教程'], |
| 35 | size: '150 MB', |
| 36 | uploader: 'dev_guru', |
| 37 | description: '包含 React 官方文档、优秀教程链接和项目示例。' |
| 38 | } |
| 39 | ]); |
| 40 | }, 1500); // 模拟网络延迟 |
| 41 | }); |
| 42 | }; |
| 43 | |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 44 | |
| 45 | function Home() { |
| 46 | const [torrents, setTorrents] = useState([]); |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 47 | const [loading, setLoading] = useState(true); |
| 48 | const [error, setError] = useState(null); |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 49 | |
| 50 | useEffect(() => { |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 51 | setLoading(true); |
| 52 | fetchTorrents() |
| 53 | .then(data => { |
| 54 | setTorrents(data); |
| 55 | setLoading(false); |
| 56 | }) |
| 57 | .catch(err => { |
| 58 | console.error("Failed to fetch torrents:", err); |
| 59 | setError("加载资源失败,请稍后再试。"); |
| 60 | setLoading(false); |
| 61 | }); |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 62 | }, []); |
| 63 | |
| 64 | return ( |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 65 | <Box sx={{ minHeight: '100vh', py: 4, background: 'linear-gradient(135deg, #2c3e50, #4ca1af)', color: 'white' }}> |
| 66 | <Container maxWidth="md" sx={{ position: 'relative', zIndex: 10 }}> {/* Changed to md for wider content */} |
| 67 | <Typography variant="h4" sx={{ textAlign: 'center', mb: 3, fontWeight: 'bold' }}> |
| 68 | 🌐 首页 · Mini-Tracker |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 69 | </Typography> |
| 70 | |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 71 | <Paper sx={{ position: 'relative', zIndex: 20, padding: '2rem', backgroundColor: 'rgba(30, 30, 30, 0.9)', borderRadius: '12px' }}> |
| 72 | <Typography variant="h6" gutterBottom sx={{ mb: 2, color: '#eee' }}> |
| 73 | 最新种子资源 |
| 74 | </Typography> |
| 75 | |
| 76 | {loading && ( |
| 77 | <Box sx={{ display: 'flex', justifyContent: 'center', my: 3 }}> |
| 78 | <CircularProgress color="inherit" /> |
| 79 | </Box> |
| 80 | )} |
| 81 | |
| 82 | {error && ( |
| 83 | <Alert severity="error" sx={{ my: 2 }}>{error}</Alert> |
| 84 | )} |
| 85 | |
| 86 | {!loading && !error && ( |
| 87 | <List sx={{ '& .MuiListItem-root:hover': { backgroundColor: 'rgba(255, 255, 255, 0.05)' }}}> |
| 88 | {torrents.map(torrent => ( |
| 89 | <ListItem |
| 90 | key={torrent.id} |
| 91 | component={Link} // react-router-dom Link |
| 92 | to={`/detail/${torrent.id}`} // 假设的详情页路由 |
| 93 | sx={{ |
| 94 | color: 'white', |
| 95 | textDecoration: 'none', |
| 96 | mb: 1.5, // 增加列表项间距 |
| 97 | p: 1.5, // 增加列表项内边距 |
| 98 | borderBottom: '1px solid rgba(255, 255, 255, 0.1)', |
| 99 | '&:last-child': { |
| 100 | borderBottom: 'none' // 移除最后一个元素的边框 |
| 101 | }, |
| 102 | display: 'flex', // 确保内部元素正确对齐 |
| 103 | flexDirection: { xs: 'column', sm: 'row' }, // 响应式布局 |
| 104 | alignItems: { xs: 'flex-start', sm: 'center' } |
| 105 | }} |
| 106 | divider={false} // 使用自定义边框代替 |
| 107 | > |
| 108 | <ListItemText |
| 109 | primary={torrent.title} |
| 110 | secondary={`大小: ${torrent.size} · 上传者: ${torrent.uploader}`} |
| 111 | primaryTypographyProps={{ variant: 'h6', component: 'div', sx: { mb: 0.5, color: '#f5f5f5', fontWeight: 500 } }} |
| 112 | secondaryTypographyProps={{ sx: { color: '#bbb', fontSize: '0.85rem' } }} |
| 113 | sx={{ flexGrow: 1, mb: { xs: 1, sm: 0 }, mr: { sm: 2 } }} // 响应式边距 |
| 114 | /> |
| 115 | <Stack direction="row" spacing={1} sx={{ flexWrap: 'wrap', gap: 0.5 }}> {/* 允许标签换行 */} |
| 116 | {torrent.tags.map(tag => ( |
| 117 | <Chip |
| 118 | key={tag} |
| 119 | label={tag} |
| 120 | size="small" |
| 121 | sx={{ |
| 122 | backgroundColor: 'rgba(255, 255, 255, 0.2)', // upload-chip 样式 |
| 123 | color: 'white', |
| 124 | cursor: 'pointer', |
| 125 | '&:hover': { |
| 126 | backgroundColor: 'rgba(255, 255, 255, 0.3)', |
| 127 | } |
| 128 | }} |
| 129 | /> |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 130 | ))} |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 131 | </Stack> |
| 132 | </ListItem> |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 133 | ))} |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 134 | </List> |
| 135 | )} |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 136 | </Paper> |
| 137 | </Container> |
| 138 | |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 139 | {/* 背景泡泡动画复用 - 这些类名 (.bubbles, .bubble) 和 @keyframes rise 应该在全局CSS或者下面的 <style> 标签中定义 */} |
| 140 | <Box className="bubbles" sx={{ |
| 141 | pointerEvents: 'none', |
| 142 | position: 'fixed', |
| 143 | top: 0, |
| 144 | left: 0, |
| 145 | width: '100%', |
| 146 | height: '100%', |
| 147 | overflow: 'hidden', |
| 148 | zIndex: 1, |
| 149 | }}> |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 150 | {[...Array(40)].map((_, i) => ( |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 151 | <Box |
| 152 | key={i} |
| 153 | className="bubble" |
| 154 | sx={{ |
| 155 | position: 'absolute', |
| 156 | bottom: '-150px', // 确保从屏幕外开始 |
| 157 | background: `hsla(${Math.random() * 360}, 70%, 80%, 0.15)`, // 增加颜色多样性 |
| 158 | borderRadius: '50%', |
| 159 | animation: 'rise 20s infinite ease-in', |
| 160 | width: `${Math.random() * 25 + 10}px`, // 调整大小范围 |
| 161 | height: `${Math.random() * 25 + 10}px`, |
| 162 | left: `${Math.random() * 100}%`, |
| 163 | animationDuration: `${15 + Math.random() * 20}s`, // 调整动画时长 |
| 164 | animationDelay: `${Math.random() * 10}s`, // 调整动画延迟 |
| 165 | opacity: 0, // 初始透明 |
| 166 | }} |
| 167 | /> |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 168 | ))} |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 169 | </Box> |
| 170 | {/* 定义动画和其他全局可能需要的样式 */} |
| 171 | <style> |
| 172 | {` |
| 173 | body { /* 基本重置 */ |
| 174 | margin: 0; |
| 175 | font-family: 'Roboto', sans-serif; /* 确保字体一致 */ |
| 176 | } |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 177 | |
ZBD | ff4d40a | 2025-05-27 17:05:20 +0800 | [diff] [blame^] | 178 | @keyframes rise { |
| 179 | 0% { |
| 180 | transform: translateY(0) scale(0.8); |
| 181 | opacity: 0; /* 从透明开始 */ |
| 182 | } |
| 183 | 10% { |
| 184 | opacity: 1; /* 渐显 */ |
| 185 | } |
| 186 | 90% { |
| 187 | opacity: 1; /* 保持可见 */ |
| 188 | } |
| 189 | 100% { |
| 190 | transform: translateY(-130vh) scale(0.3); /* 飘得更高更小 */ |
| 191 | opacity: 0; /* 渐隐 */ |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* 如果 .bubbles 和 .bubble 样式没有在 sx 中完全覆盖,可以在这里补充 */ |
| 196 | /* .bubbles { ... } */ |
| 197 | /* .bubble { ... } */ |
| 198 | `} |
| 199 | </style> |
ZBD | 7e88c22 | 2025-05-07 21:07:12 +0800 | [diff] [blame] | 200 | </Box> |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | export default Home; |
| 205 | |