Spaces:
Sleeping
Sleeping
File size: 11,693 Bytes
4bae792 57f5158 4bae792 8c762ac 4bae792 8c762ac 4bae792 fe7c2ba 4bae792 8c762ac 4bae792 8c762ac 4bae792 4c47e22 57f5158 4bae792 57f5158 4bae792 fe7c2ba 8c762ac 4bae792 8c762ac fe7c2ba 8c762ac 4bae792 57f5158 4bae792 57f5158 4bae792 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | import { useState, useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import SharedFile from "./SharedFile";
import { getUserColor } from "../../utils/userColor";
import { dmService } from "../../services/dm.service";
import { updateUserProfile } from "../../store/slices/dmSlice";
import {
FiMail,
FiFileText,
FiInbox,
FiSearch,
FiMousePointer,
FiMessageCircle,
FiZap,
FiUsers,
} from "react-icons/fi";
function getInitials(name) {
if (!name) return "?";
return name
.split(" ")
.map((n) => n[0])
.join("")
.slice(0, 2)
.toUpperCase();
}
function isEmoji(str) {
if (!str) return false;
return /\p{Emoji}/u.test(str) && str.length <= 2;
}
function UserAvatar({ name, avatarUrl, isOnline, isDark, isBot, color }) {
const userColor = getUserColor(name, color);
const textColor = isDark ? "var(--bg-surface)" : "#fff";
const avatarEmoji = isEmoji(avatarUrl) ? avatarUrl : null;
const imageUrl = avatarUrl && !avatarEmoji ? avatarUrl : null;
const [imgError, setImgError] = useState(false);
return (
<div className="relative shrink-0">
<div
className="w-16 h-16 rounded-lg flex items-center justify-center text-2xl font-semibold overflow-hidden"
style={{
background: userColor,
color: textColor,
}}
>
{avatarEmoji ? (
<span className="text-3xl">{avatarEmoji}</span>
) : imageUrl && !imgError ? (
<img
src={imageUrl}
alt={name}
className="w-full h-full object-cover"
onError={() => setImgError(true)}
/>
) : (
getInitials(name)
)}
</div>
</div>
);
}
function DMProfile({ isDark, dmUser }) {
const dispatch = useDispatch();
const onlineUsers = useSelector((state) => state.dm.onlineUsers);
const [profileColor, setProfileColor] = useState(null);
const isOnlineRealtime = dmUser?.id && onlineUsers.includes(String(dmUser.id));
console.log("[DMProfile] dmUser.id:", dmUser?.id, "onlineUsers:", onlineUsers, "isOnlineRealtime:", isOnlineRealtime, "dmUser.isOnline:", dmUser?.isOnline);
const dmUserOnline = isOnlineRealtime || dmUser?.isOnline || false;
useEffect(() => {
if (!dmUser) {
setProfileColor(null);
return;
}
if (dmUser.color) {
setProfileColor(dmUser.color);
return;
}
let mounted = true;
dmService
.getUserProfile(dmUser.id)
.then(({ data }) => {
const color = data?.user?.color || data?.color || null;
const displayName =
data?.user?.display_name || data?.display_name || null;
const avatarUrl = data?.user?.avatar_url || data?.avatar_url || null;
if (mounted && color) {
setProfileColor(color);
}
// Update Redux store with user profile info
if (color || displayName || avatarUrl) {
dispatch(
updateUserProfile({
userId: dmUser.id,
updates: {
...(color && { color }),
...(displayName && { display_name: displayName }),
...(avatarUrl && { avatar_url: avatarUrl }),
},
}),
);
}
})
.catch(() => {});
return () => {
mounted = false;
};
}, [dmUser]);
const effectiveColor = dmUser?.color || profileColor || null;
if (!dmUser) {
return (
<div
className="w-60 min-w-60 flex flex-col h-screen overflow-y-auto border-l"
style={{
background: "var(--bg-surface-secondary)",
borderColor: "var(--border-primary)",
}}
>
<div className="p-4 pt-5">
<div
className="text-sm font-semibold uppercase tracking-wider mb-8"
style={{ color: "var(--text-primary)" }}
>
Hướng dẫn
</div>
<div className="flex flex-col gap-4">
<div className="flex gap-3 items-center">
<div
className="w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background: isDark
? "rgba(255,255,255,0.06)"
: "rgba(0,0,0,0.04)",
color: "var(--primary)",
}}
>
<FiSearch size={15} />
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Tìm kiếm bạn bè
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-muted)" }}
>
Nhập tên hoặc email để tìm
</div>
</div>
</div>
<div className="flex gap-3 items-center">
<div
className="w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background: isDark
? "rgba(255,255,255,0.06)"
: "rgba(0,0,0,0.04)",
color: "var(--primary)",
}}
>
<FiMousePointer size={15} />
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Chọn để nhắn tin
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-muted)" }}
>
Click vào tên để bắt đầu trò chuyện
</div>
</div>
</div>
<div className="flex gap-3 items-center">
<div
className="w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background: isDark
? "rgba(255,255,255,0.06)"
: "rgba(0,0,0,0.04)",
color: "var(--primary)",
}}
>
<FiUsers size={15} />
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Tham gia space học tập
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-muted)" }}
>
Vào các nhóm môn học để trao đổi
</div>
</div>
</div>
<div className="flex gap-3 items-center">
<div
className="w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background: isDark
? "rgba(255,255,255,0.06)"
: "rgba(0,0,0,0.04)",
color: "var(--primary)",
}}
>
<FiZap size={15} />
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Hỏi StudyBot
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-muted)" }}
>
Trợ lý AI sẵn sàng giải đáp 24/7
</div>
</div>
</div>
<div className="flex gap-3 items-center">
<div
className="w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background: isDark
? "rgba(255,255,255,0.06)"
: "rgba(0,0,0,0.04)",
color: "var(--primary)",
}}
>
<FiMessageCircle size={15} />
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Kết nối & học tập
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-muted)" }}
>
Trao đổi bài tập và tài liệu
</div>
</div>
</div>
</div>
</div>
</div>
);
}
return (
<div
className="w-60 min-w-60 flex flex-col h-screen overflow-y-auto border-l"
style={{
background: "var(--bg-surface-secondary)",
borderColor: "var(--border-primary)",
}}
>
{/* Avatar + Name */}
<div className="p-4 text-center">
<div className="flex justify-center">
<UserAvatar
name={dmUser.name}
avatarUrl={dmUser.avatar}
color={effectiveColor}
isOnline={dmUserOnline}
isDark={isDark}
isBot={dmUser.isBot}
/>
</div>
<div
className="text-base font-semibold mt-3"
style={{ color: "var(--text-primary)" }}
>
{dmUser.name}
</div>
<div
className="text-xs mt-1"
style={{ color: "var(--text-secondary)" }}
>
{dmUserOnline ? "Đang hoạt động" : "Ngoại tuyến"}
</div>
</div>
{/* Info */}
<div
className="mx-4 py-3 border-t"
style={{ borderColor: "var(--border-primary)" }}
>
{dmUser.email && (
<div className="flex items-center gap-2 py-1.5">
<FiMail size={14} style={{ color: "var(--text-muted)" }} />
<span
className="text-sm truncate"
style={{ color: "var(--text-secondary)" }}
>
{dmUser.email}
</span>
</div>
)}
{dmUser.bio && (
<div className="flex items-start gap-2 py-1.5">
<FiFileText size={14} style={{ color: "var(--text-muted)" }} />
<span
className="text-sm"
style={{ color: "var(--text-secondary)" }}
>
{dmUser.bio}
</span>
</div>
)}
</div>
{/* Shared Files */}
<div
className="mx-4 mt-2 py-3 border-t"
style={{ borderColor: "var(--border-primary)" }}
>
<div
className="text-xs font-medium mb-2"
style={{ color: "var(--text-muted)" }}
>
Tệp chia sẻ
</div>
{dmUser.sharedFiles && dmUser.sharedFiles.length > 0 ? (
dmUser.sharedFiles.map((file, idx) => (
<SharedFile
key={idx}
isDark={isDark}
fileName={file.name}
time={file.time}
type={file.type}
/>
))
) : (
<div className="flex flex-col items-center justify-center py-6 text-center">
<FiInbox size={24} style={{ color: "var(--text-muted)" }} />
<div
className="text-xs mt-2"
style={{ color: "var(--text-muted)" }}
>
Chưa có tệp nào
</div>
</div>
)}
</div>
</div>
);
}
export default DMProfile;
|