blob: 4afe125d3f631a0554261479539a93042f71adc3 [file] [log] [blame]
TRM-coding29174c22025-06-18 23:56:51 +08001import React, { useState, useEffect } from 'react';
2import { getFavorites } from '../api/api_ljc';
3import { FaHeart } from 'react-icons/fa';
4
5const FavoritePosts = ({ userId }) => {
6 const [favorites, setFavorites] = useState([]);
7 const [loading, setLoading] = useState(true);
8
9 useEffect(() => {
10 const fetchFavorites = async () => {
11 try {
12 setLoading(true);
13 const response = await getFavorites(userId);
14 setFavorites(response.data);
15 } catch (error) {
16 console.error('Failed to fetch favorites:', error);
17 } finally {
18 setLoading(false);
19 }
20 };
21
22 if (userId) {
23 fetchFavorites();
24 }
25 }, [userId]);
26
27 if (loading) {
28 return (
29 <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
30 {[1, 2, 3, 4, 5, 6].map(item => (
31 <div key={item} className="bg-gray-100 rounded-xl aspect-square animate-pulse"></div>
32 ))}
33 </div>
34 );
35 }
36
37 if (favorites.length === 0) {
38 return (
39 <div className="text-center py-16">
40 <div className="inline-flex items-center justify-center w-16 h-16 rounded-full bg-red-100 text-red-500 mb-4">
41 <FaHeart className="text-2xl" />
42 </div>
43 <h3 className="text-lg font-medium text-gray-900">暂无收藏内容</h3>
44 <p className="mt-1 text-gray-500">你还没有收藏任何笔记</p>
45 </div>
46 );
47 }
48
49 // 模拟瀑布流布局数据
50 const waterfallData = favorites.map(post => ({
51 ...post,
52 height: Math.floor(Math.random() * 100) + 200 // 随机高度
53 }));
54
55 return (
56 <div className="grid grid-cols-2 md:grid-cols-3 gap-4">
57 {waterfallData.map(post => (
58 <div
59 key={post.id}
60 className="bg-white rounded-xl overflow-hidden shadow-sm hover:shadow-md transition-shadow"
61 >
62 <div
63 className="relative bg-gray-200"
64 style={{ height: `${post.height}px` }}
65 >
66 {/* 占位图片 */}
67 <div className="absolute inset-0 bg-gradient-to-br from-pink-100 to-orange-100"></div>
68
69 {/* 类型标签 */}
70 <div className="absolute top-2 right-2 bg-black bg-opacity-50 text-white text-xs px-2 py-1 rounded-full">
71 {post.type === 'image' ? '图文' :
72 post.type === 'video' ? '视频' : '文档'}
73 </div>
74
75 {/* 收藏标记 */}
76 <div className="absolute bottom-2 right-2 bg-red-500 rounded-full p-1">
77 <FaHeart className="text-white text-xs" />
78 </div>
79 </div>
80
81 <div className="p-3">
82 <h3 className="font-medium line-clamp-2">{post.title}</h3>
83 <div className="flex items-center mt-2 text-xs text-gray-500">
84 <span>❤️ 2.5k</span>
85 <span className="mx-2">•</span>
86 <span> 156</span>
87 </div>
88 </div>
89 </div>
90 ))}
91 </div>
92 );
93};
94
95export default FavoritePosts;