MemberPage.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import debug from 'debug';
  2. const rpcLogger = debug('frontend:api:rpc');
  3. import React, { useEffect, useState } from 'react';
  4. import { UserIcon, PencilIcon } from '@heroicons/react/24/outline';
  5. import { useParams, useNavigate } from 'react-router-dom';
  6. import { userClient } from '@/client/api';
  7. import { useAuth, User } from '@/client/home/hooks/AuthProvider';
  8. const MemberPage: React.FC = () => {
  9. const [user, setUser] = useState<User | null>(null);
  10. const [loading, setLoading] = useState(true);
  11. const { id: userId } = useParams<{ id: string }>();
  12. const id = Number(userId);
  13. const navigate = useNavigate();
  14. const { user: currentUser } = useAuth();
  15. // 获取用户资料
  16. const fetchUserProfile = async () => {
  17. if (!id) return;
  18. try {
  19. setLoading(true);
  20. rpcLogger('Fetching user profile for id: %s', id);
  21. const response = await userClient[':id'].$get({
  22. param: { id }
  23. });
  24. if (!response.ok) throw new Error('获取用户资料失败');
  25. const userData = await response.json();
  26. setUser(userData);
  27. } catch (error) {
  28. console.error('Error fetching user profile:', error);
  29. } finally {
  30. setLoading(false);
  31. }
  32. };
  33. useEffect(() => {
  34. fetchUserProfile();
  35. }, [id]);
  36. if (loading) {
  37. return (
  38. <div className="flex justify-center items-center min-h-[50vh]">
  39. <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500"></div>
  40. </div>
  41. );
  42. }
  43. if (!user) {
  44. return (
  45. <div className="text-center py-12">
  46. <h2 className="text-2xl font-bold text-gray-900 mb-4">用户不存在</h2>
  47. <button
  48. onClick={() => navigate('/')}
  49. className="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
  50. >
  51. 返回首页
  52. </button>
  53. </div>
  54. );
  55. }
  56. return (
  57. <div className="min-h-screen bg-gray-50">
  58. <div className="container mx-auto px-4 py-8 max-w-4xl">
  59. {/* 用户资料卡片 */}
  60. <div className="bg-white rounded-lg shadow-sm p-6 mb-8">
  61. <div className="flex flex-col items-center">
  62. <div className="w-24 h-24 rounded-full bg-gray-200 flex items-center justify-center mb-4">
  63. {user.avatar ? (
  64. <img src={user.avatar} alt={user.nickname || user.username} className="h-full w-full object-cover rounded-full" />
  65. ) : (
  66. <UserIcon className="h-12 w-12 text-gray-500" />
  67. )}
  68. </div>
  69. <h1 className="text-2xl font-bold text-gray-900 mb-1">{user.nickname || user.username}</h1>
  70. <div className="flex space-x-8 my-4">
  71. <div className="text-center">
  72. <p className="text-2xl font-semibold text-gray-900">0</p>
  73. <p className="text-sm text-gray-500">内容</p>
  74. </div>
  75. <div className="text-center">
  76. <p className="text-2xl font-semibold text-gray-900">0</p>
  77. <p className="text-sm text-gray-500">关注</p>
  78. </div>
  79. <div className="text-center">
  80. <p className="text-2xl font-semibold text-gray-900">0</p>
  81. <p className="text-sm text-gray-500">粉丝</p>
  82. </div>
  83. </div>
  84. {currentUser && currentUser.id === user.id && (
  85. <button
  86. onClick={() => navigate('/profile/edit')}
  87. className="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 flex items-center"
  88. >
  89. <PencilIcon className="w-4 h-4 mr-2" />
  90. 编辑资料
  91. </button>
  92. )}
  93. {(user as any).bio && (
  94. <p className="mt-4 text-center text-gray-600 max-w-lg">
  95. {(user as any).bio}
  96. </p>
  97. )}
  98. <div className="flex items-center mt-4 space-x-4">
  99. {(user as any).location && (
  100. <div className="flex items-center text-gray-600">
  101. <svg xmlns="http://www.w3.org/2000/svg" className="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
  102. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z" />
  103. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 11a3 3 0 11-6 0 3 3 0 016 0z" />
  104. </svg>
  105. <span className="text-sm">{(user as any).location}</span>
  106. </div>
  107. )}
  108. {(user as any).website && (
  109. <a
  110. href={(user as any).website}
  111. target="_blank"
  112. rel="noopener noreferrer"
  113. className="flex items-center text-blue-600 hover:text-blue-800"
  114. >
  115. <svg xmlns="http://www.w3.org/2000/svg" className="h-4 w-4 mr-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
  116. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6v6m0 0v6m0-6h-6" />
  117. </svg>
  118. <span className="text-sm truncate max-w-[150px]">{(user as any).website}</span>
  119. </a>
  120. )}
  121. </div>
  122. </div>
  123. </div>
  124. {/* 用户内容区域 */}
  125. <div className="bg-white rounded-lg shadow-sm p-6">
  126. <h2 className="text-xl font-semibold mb-6">个人资料</h2>
  127. <div className="space-y-4">
  128. <div className="border-b border-gray-100 pb-4">
  129. <h3 className="text-sm font-medium text-gray-500 mb-1">用户名</h3>
  130. <p className="text-gray-900">{user.username}</p>
  131. </div>
  132. <div className="border-b border-gray-100 pb-4">
  133. <h3 className="text-sm font-medium text-gray-500 mb-1">电子邮箱</h3>
  134. <p className="text-gray-900">{user.email || '未设置'}</p>
  135. </div>
  136. <div className="border-b border-gray-100 pb-4">
  137. <h3 className="text-sm font-medium text-gray-500 mb-1">注册时间</h3>
  138. <p className="text-gray-900">{user.createdAt ? new Date(user.createdAt).toLocaleDateString() : '未知'}</p>
  139. </div>
  140. <div className="border-b border-gray-100 pb-4">
  141. <h3 className="text-sm font-medium text-gray-500 mb-1">最后登录</h3>
  142. <p className="text-gray-900">{user.updatedAt ? new Date(user.updatedAt).toLocaleString() : '从未登录'}</p>
  143. </div>
  144. </div>
  145. <div className="mt-8">
  146. <button
  147. onClick={() => navigate('/profile/security')}
  148. className="w-full py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
  149. >
  150. 安全设置
  151. </button>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. );
  157. };
  158. export default MemberPage;