blob: fbb384d9cfc66deea9c37b993087c0df51838f66 [file] [log] [blame]
ym9236e0ddcf2025-06-09 20:14:11 +08001import React, { useState, useEffect } from 'react';
2import { useNavigate } from 'react-router-dom';
3import { getActivityPreviews, getFullActivities } from '../api/activity';
4import FriendManager from '../components/FriendManager';
5import ChatBox from '../components/ChatBox';
6import Navbar from '../components/Navbar';
7import { TeamOutlined, MessageOutlined } from '@ant-design/icons';
8import { Layout, Row, Col, Typography, Empty, Spin } from 'antd';
9import './Friend.css';
10
11const { Title, Text } = Typography;
12const { Content } = Layout;
13
14const Friend = () => {
15 const navigate = useNavigate();
16
17 // 使用 localStorage 获取当前登录用户
18 const [currentUser, setCurrentUser] = useState(null);
19 const [loading, setLoading] = useState(true);
20
21 useEffect(() => {
22 // 从 localStorage 获取用户信息
23 const userData = localStorage.getItem('user');
24 if (userData) {
25 try {
26 const user = JSON.parse(userData);
27 setCurrentUser(user);
28 } catch (e) {
29 console.error('Failed to parse user data');
30 navigate('/login');
31 } finally {
32 setLoading(false);
33 }
34 } else {
35 // 未登录则重定向到登录页
36 navigate('/login');
37 }
38 }, [navigate]);
39
40 const [selectedRelation, setSelectedRelation] = useState(null);
41 const [activityPreviews, setActivityPreviews] = useState([]);
42 const [fullActivities, setFullActivities] = useState([]);
43 const [selectedActivityId, setSelectedActivityId] = useState(null);
44
45 useEffect(() => {
46 if (currentUser) {
47 getActivityPreviews().then(res => setActivityPreviews(res.data));
48 getFullActivities().then(res => setFullActivities(res.data));
49 }
50 }, [currentUser]);
51
52 if (loading) {
53 return (
54 <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '100vh' }}>
55 <Spin size="large" />
56 </div>
57 );
58 }
59
60 return (
61 <Layout style={{ minHeight: '100vh', background: '#f0f2f5', padding: '24px' }}>
62 <Content>
63 <Navbar />
64 <Row justify="space-between" align="middle" style={{ marginBottom: 24 }} />
65
66 <Row gutter={24} style={{ height: 'calc(100vh - 140px)' }}>
67 {/* 好友管理 - 左侧 */}
68 <Col xs={24} sm={24} md={10} lg={8} xl={6} style={{ background: '#fff', borderRadius: 8, padding: 24, boxShadow: '0 2px 8px rgba(0,0,0,0.1)', overflowY: 'auto' }}>
69 <Title level={4} style={{ color: '#722ed1', marginBottom: 24 }}>
70 <TeamOutlined style={{ marginRight: 8 }} />
71 好友管理
72 </Title>
73 <FriendManager
74 currentUser={currentUser} // 传递真实登录用户
75 onSelectRelation={setSelectedRelation}
76 />
77 </Col>
78
79 {/* 聊天窗口 - 右侧 */}
80 <Col
81 xs={24}
82 sm={24}
83 md={14}
84 lg={16}
85 xl={18}
86 style={{
87 background: '#fff',
88 borderRadius: 8,
89 padding: 24,
90 boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
91 display: 'flex',
92 flexDirection: 'column',
93 }}
94 >
95 <Title level={4} style={{ color: '#eb2f96', marginBottom: 24 }}>
96 <MessageOutlined style={{ marginRight: 8 }} />
97 聊天窗口
98 </Title>
99
100 <div style={{ flex: 1, minHeight: 0 }}>
101 {selectedRelation ? (
102 <div style={{ height: 'calc(100vh - 220px)' }}>
103 <ChatBox
104 senderId={currentUser.userid} // 使用真实用户ID
105 receiverId={selectedRelation.friendId}
106 />
107 </div>
108 ) : (
109 <Empty
110 image={Empty.PRESENTED_IMAGE_SIMPLE}
111 description={<Text type="secondary">请选择一位好友开始聊天</Text>}
112 style={{
113 height: '100%',
114 display: 'flex',
115 justifyContent: 'center',
116 alignItems: 'center',
117 }}
118 />
119 )}
120 </div>
121 </Col>
122 </Row>
123 </Content>
124 </Layout>
125 );
126};
127
128export default Friend;