Spaces:
Sleeping
Sleeping
File size: 13,432 Bytes
57f5158 91939c2 57f5158 880ab03 4bae792 880ab03 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 880ab03 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 57f5158 91939c2 4bae792 aa61a49 4bae792 57f5158 91939c2 4bae792 880ab03 57f5158 880ab03 57f5158 4bae792 91939c2 57f5158 4bae792 91939c2 4bae792 91939c2 880ab03 91939c2 4bae792 91939c2 4bae792 57f5158 91939c2 57f5158 91939c2 57f5158 880ab03 57f5158 91939c2 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | import { useState, useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { FiSearch, FiPlus, FiSliders } from "react-icons/fi";
import { createRoom } from "../../store/slices/spaceSlice";
import { setActiveRoom } from "../../store/slices/appSlice";
import { getSpaceIconComponent } from "../../constants/spaceIcons";
function RoomItem({
room,
isActive,
onClick,
lastMessage,
lastMessageTime,
unreadCount,
}) {
const [isHovered, setIsHovered] = useState(false);
return (
<div
className="flex items-center px-3 py-2.5 rounded-md cursor-pointer transition-colors gap-2.5 mb-0.5"
style={{
background: isActive
? "var(--primary-active)"
: isHovered
? "var(--hover-primary)"
: "transparent",
borderRadius: "8px",
}}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
onClick={onClick}
>
{/* Room info */}
<div className="flex-1 min-w-0 overflow-hidden">
<div className="flex items-center justify-between min-w-0">
<div className="flex items-center gap-1.5 min-w-0">
<div
className="text-sm font-semibold truncate"
style={{ color: "var(--text-primary)" }}
>
{room.name}
</div>
{room.is_private && (
<span
className="text-[10px] px-1 py-0.5 rounded flex-shrink-0"
style={{
background: "var(--primary-active)",
color: "var(--primary)",
}}
>
Private
</span>
)}
</div>
{/* Last message time */}
{lastMessageTime && (
<span
className="text-[10px] flex-shrink-0 ml-1"
style={{ color: "var(--text-muted)" }}
>
{lastMessageTime}
</span>
)}
</div>
{/* Last message */}
<div
className="text-xs mt-0.5 truncate"
style={{
color:
unreadCount > 0 ? "var(--text-primary)" : "var(--text-secondary)",
fontWeight: unreadCount > 0 ? 500 : 400,
}}
>
{lastMessage || "Bắt đầu trò chuyện"}
</div>
</div>
{/* Unread badge */}
{unreadCount > 0 && (
<span
className="px-1.5 py-0.5 rounded-full text-[10px] font-bold text-white flex-shrink-0"
style={{ background: "#ef4444", lineHeight: 1 }}
>
{unreadCount > 99 ? "99+" : unreadCount}
</span>
)}
</div>
);
}
function LoadingDots() {
return (
<div className="flex items-center justify-center gap-1 py-4">
<span
className="w-2 h-2 rounded-full animate-bounce"
style={{ background: "var(--text-muted)", animationDelay: "0ms" }}
/>
<span
className="w-2 h-2 rounded-full animate-bounce"
style={{ background: "var(--text-muted)", animationDelay: "150ms" }}
/>
<span
className="w-2 h-2 rounded-full animate-bounce"
style={{ background: "var(--text-muted)", animationDelay: "300ms" }}
/>
</div>
);
}
function EmptyState({ isDark, onCreateRoomClick }) {
return (
<div className="flex flex-col items-center justify-center py-10 px-4 text-center">
<div
className="w-14 h-14 rounded-lg flex items-center justify-center mb-3"
style={{
background: isDark ? "rgba(255,255,255,0.05)" : "rgba(0,0,0,0.03)",
}}
>
<FiPlus size={24} style={{ color: "var(--text-muted)" }} />
</div>
<div
className="text-sm font-medium mb-1"
style={{ color: "var(--text-primary)" }}
>
Chưa có room nào
</div>
<div className="text-xs mb-4" style={{ color: "var(--text-muted)" }}>
Tạo room đầu tiên để bắt đầu thảo luận
</div>
<button
onClick={onCreateRoomClick}
className="px-4 py-2 rounded-md text-xs font-medium transition-colors cursor-pointer"
style={{
background: "var(--primary)",
color: "#fff",
}}
>
Tạo room mới
</button>
</div>
);
}
function CreateRoomModal({ isOpen, onClose, onCreate, isDark }) {
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [isPrivate, setIsPrivate] = useState(false);
if (!isOpen) return null;
const handleSubmit = (e) => {
e.preventDefault();
if (name.trim()) {
onCreate({
name: name.trim(),
description: description.trim(),
isPrivate,
});
setName("");
setDescription("");
setIsPrivate(false);
onClose();
}
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black/50">
<div
className="w-full max-w-md rounded-lg p-6 shadow-xl"
style={{
background: "var(--bg-surface-secondary)",
border: "1px solid var(--border-primary)",
}}
>
<h2
className="text-lg font-semibold mb-4"
style={{ color: "var(--text-primary)" }}
>
Tạo Room mới
</h2>
<form onSubmit={handleSubmit} className="space-y-4">
<div>
<label
className="block text-sm font-medium mb-1"
style={{ color: "var(--text-secondary)" }}
>
Tên room
</label>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="VD: Thảo luận, Tài liệu..."
className="w-full px-3 py-2 rounded-md text-sm border outline-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
autoFocus
/>
</div>
<div>
<label
className="block text-sm font-medium mb-1"
style={{ color: "var(--text-secondary)" }}
>
Mô tả (tùy chọn)
</label>
<input
type="text"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Mô tả ngắn về room..."
className="w-full px-3 py-2 rounded-md text-sm border outline-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
/>
</div>
<div className="flex items-center gap-2">
<input
type="checkbox"
id="isPrivate"
checked={isPrivate}
onChange={(e) => setIsPrivate(e.target.checked)}
className="w-4 h-4 cursor-pointer"
style={{ accentColor: "var(--primary)" }}
/>
<label
htmlFor="isPrivate"
className="text-sm cursor-pointer"
style={{ color: "var(--text-secondary)" }}
>
Room riêng tư
</label>
</div>
<div className="flex justify-end gap-3 pt-2">
<button
type="button"
onClick={onClose}
className="px-4 py-2 rounded-md text-sm font-medium"
style={{ color: "var(--text-secondary)" }}
>
Hủy
</button>
<button
type="submit"
disabled={!name.trim()}
className="px-4 py-2 rounded-md text-sm font-medium disabled:opacity-50"
style={{
background: "var(--primary)",
color: isDark ? "var(--bg-surface)" : "#fff",
}}
>
Tạo
</button>
</div>
</form>
</div>
</div>
);
}
function SpaceRoomList({
activeSpace,
activeRoom,
setActiveRoom,
searchQuery,
setSearchQuery,
onCreateRoomClick,
}) {
const { isDark } = useSelector((state) => state.theme);
const dispatch = useDispatch();
const [isSearching, setIsSearching] = useState(false);
const {
spaces,
roomsMap,
roomsLoading,
fetchedRooms,
roomUnreadCounts,
} = useSelector((state) => state.space);
const currentUser = useSelector((state) => state.auth.user);
const spaceRooms = roomsMap[activeSpace] || [];
const isFetched = fetchedRooms[activeSpace];
// Helper to format last message from API
const getRoomLastMessage = (room) => {
const lastMsg = room.last_message;
if (!lastMsg) return null;
const isOwn =
lastMsg.sender_id &&
currentUser?.id &&
String(lastMsg.sender_id) === String(currentUser.id);
const senderName = isOwn
? "Bạn"
: lastMsg.sender_display_name || lastMsg.username || "Unknown";
return {
content: lastMsg.content,
senderName,
};
};
// Rooms are already fetched globally in App.jsx
// This component only reads from Redux store, no need to fetch here
const filteredRooms = spaceRooms.filter((room) =>
room.name.toLowerCase().includes(searchQuery.toLowerCase()),
);
const handleSearchChange = (e) => {
setSearchQuery(e.target.value);
if (e.target.value.trim()) {
setIsSearching(true);
setTimeout(() => setIsSearching(false), 300);
} else {
setIsSearching(false);
}
};
const handleCreateRoom = (roomData) => {
if (activeSpace) {
dispatch(createRoom({ spaceId: activeSpace, data: roomData }));
}
};
const currentSpace = spaces.find((s) => s.id === activeSpace);
return (
<div
className="w-60 min-w-60 flex flex-col h-screen border-r"
style={{
background: "var(--bg-surface-secondary)",
borderColor: "var(--border-primary)",
}}
>
{/* Header */}
<div
className="p-4 border-b"
style={{ borderColor: "var(--border-primary)" }}
>
<div className="flex items-center justify-between mb-3">
<div className="flex items-center gap-2 min-w-0">
<div
className="text-base font-semibold truncate"
style={{ color: "var(--text-primary)" }}
>
{currentSpace?.name || "Space"}
</div>
</div>
<button
onClick={() => console.log("Space settings clicked")}
className="p-1.5 rounded hover:opacity-70 transition-opacity cursor-pointer"
style={{ color: "var(--text-muted)" }}
title="Cài đặt space"
>
<FiSliders size={14} />
</button>
</div>
<div className="relative">
<FiSearch
size={16}
className="absolute left-3 top-1/2 -translate-y-1/2"
style={{ color: "var(--text-muted)" }}
/>
<input
type="text"
className="w-full pl-9 pr-3 py-2 border rounded-md text-sm outline-none transition-colors"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
placeholder="Tìm kiếm room..."
value={searchQuery}
onChange={handleSearchChange}
/>
</div>
</div>
{/* Room list */}
<div className="flex-1 overflow-y-auto p-2">
{/* Header with create button — always visible */}
<div className="flex items-center justify-between px-3 py-2">
<div
className="text-xs font-medium uppercase tracking-wider"
style={{ color: "var(--text-muted)" }}
>
Rooms
</div>
<button
onClick={(e) => {
e.stopPropagation();
if (onCreateRoomClick) onCreateRoomClick();
}}
className="p-1 rounded hover:opacity-70 transition-opacity cursor-pointer"
style={{ color: "var(--text-muted)" }}
title="Tạo room mới"
>
<FiPlus size={14} />
</button>
</div>
{(roomsLoading || isSearching) && <LoadingDots />}
{!roomsLoading && !isSearching && filteredRooms.length === 0 && (
<EmptyState isDark={isDark} onCreateRoomClick={onCreateRoomClick} />
)}
{!isSearching && filteredRooms.length > 0 && (
<div>
{filteredRooms.map((room) => {
const lastMsg = getRoomLastMessage(room);
return (
<RoomItem
key={room.id}
room={room}
isActive={activeRoom === room.id}
onClick={() => setActiveRoom(room.id)}
lastMessage={
lastMsg ? `${lastMsg.senderName}: ${lastMsg.content}` : null
}
unreadCount={roomUnreadCounts[room.id] || 0}
/>
);
})}
</div>
)}
</div>
</div>
);
}
export default SpaceRoomList;
|