import { useState } from "react";
import { FiSidebar, FiSettings } from "react-icons/fi";
import { getUserColor } from "../../utils/userColor";
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 = isBot ? "var(--tertiary-active)" : getUserColor(name, color);
const textColor = isBot
? "var(--tertiary)"
: isDark
? "var(--bg-surface)"
: "#fff";
const avatarEmoji = isEmoji(avatarUrl) ? avatarUrl : null;
const imageUrl = avatarUrl && !avatarEmoji ? avatarUrl : null;
const [imgError, setImgError] = useState(false);
return (
{avatarEmoji ? (
{avatarEmoji}
) : imageUrl && !imgError ? (

setImgError(true)}
/>
) : (
getInitials(name)
)}
{!isBot && (
)}
);
}
function ChatHeader({
isDark,
activeRoom,
isBotRoom,
isDM,
dmUser,
roomName,
roomDescription,
onToggleRoomList,
onToggleMemberList,
roomListCollapsed,
memberListCollapsed,
onOpenRoomSettings,
spaceWelcome,
}) {
const hasNoSelection = (isDM && !dmUser) || spaceWelcome;
const headerTitle = hasNoSelection
? "Tin nhắn"
: isBotRoom
? "Trợ lý AI"
: isDM && dmUser
? dmUser.name
: roomName || (activeRoom ? `# ${activeRoom}` : "");
const headerSubtitle = hasNoSelection
? "Chọn một cuộc trò chuyện để bắt đầu"
: isBotRoom
? "Hỏi đáp với trợ lý AI"
: isDM && dmUser
? dmUser.isOnline
? "Đang hoạt động"
: "Ngoại tuyến"
: roomDescription || "";
return (
{(isBotRoom || (isDM && dmUser)) && (
)}
{headerTitle}
{headerSubtitle}
{/* Toggle RoomList */}
{/* Toggle MemberList */}
{/* Room Settings — chỉ hiển thị khi ở room (không phải DM) */}
{!isDM && (
)}
);
}
export default ChatHeader;