File size: 5,079 Bytes
8c762ac
b4bf04d
4bae792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c762ac
4bae792
 
 
 
fe7c2ba
4bae792
 
 
 
 
 
 
8c762ac
4bae792
 
 
 
8c762ac
4bae792
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91939c2
 
 
 
 
 
57f5158
 
91939c2
 
 
 
b4bf04d
880ab03
91939c2
880ab03
4bae792
 
 
57f5158
880ab03
 
 
 
4bae792
 
 
57f5158
880ab03
 
 
 
 
 
4bae792
 
 
 
 
 
 
 
 
57f5158
4bae792
57f5158
 
 
 
4bae792
57f5158
4bae792
 
91939c2
4bae792
 
 
 
 
 
 
 
 
 
 
 
 
91939c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4bf04d
 
 
 
 
 
 
 
 
 
 
 
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
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 (
    <div className="relative flex-shrink-0">
      <div
        className="w-9 h-9 rounded-lg flex items-center justify-center text-sm font-semibold overflow-hidden"
        style={{
          background: userColor,
          color: textColor,
        }}
      >
        {avatarEmoji ? (
          <span className="text-base">{avatarEmoji}</span>
        ) : imageUrl && !imgError ? (
          <img
            src={imageUrl}
            alt={name}
            className="w-full h-full object-cover"
            onError={() => setImgError(true)}
          />
        ) : (
          getInitials(name)
        )}
      </div>
      {!isBot && (
        <div
          className="absolute -bottom-0.5 -right-0.5 w-2.5 h-2.5 rounded-full border-2"
          style={{
            borderColor: isDark ? "var(--bg-surface-secondary)" : "#fff",
            background: isOnline ? "var(--online)" : "var(--offline)",
          }}
        />
      )}
    </div>
  );
}

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 (
    <div
      className="px-4 py-3 border-b flex-shrink-0 flex items-center gap-3"
      style={{
        borderColor: "var(--border-primary)",
        background: "var(--bg-surface-secondary)",
      }}
    >
      {(isBotRoom || (isDM && dmUser)) && (
        <UserAvatar
          name={isBotRoom ? "Trợ  AI" : dmUser.name}
          avatarUrl={isBotRoom ? "🤖" : dmUser.avatar}
          color={isBotRoom ? null : dmUser?.color}
          isOnline={isBotRoom ? true : dmUser?.isOnline}
          isDark={isDark}
          isBot={isBotRoom}
        />
      )}
      <div className="min-w-0 flex-1">
        <div
          className="text-[15px] font-semibold flex items-center gap-1.5 truncate"
          style={{ color: "var(--text-primary)" }}
        >
          {headerTitle}
        </div>
        <div
          className="text-xs mt-0.5"
          style={{ color: "var(--text-secondary)" }}
        >
          {headerSubtitle}
        </div>
      </div>

      <div className="flex items-center gap-0.5">
        {/* Toggle RoomList */}
        <button
          onClick={onToggleRoomList}
          className="p-1.5 rounded hover:opacity-70 transition-opacity cursor-pointer"
          style={{
            color: roomListCollapsed ? "var(--primary)" : "var(--text-muted)",
          }}
          title={roomListCollapsed ? "Hiện danh sách room" : "Ẩn danh sách room"}
        >
          <FiSidebar size={18} />
        </button>

        {/* Toggle MemberList */}
        <button
          onClick={onToggleMemberList}
          className="p-1.5 rounded hover:opacity-70 transition-opacity cursor-pointer"
          style={{
            color: memberListCollapsed ? "var(--primary)" : "var(--text-muted)",
          }}
          title={memberListCollapsed ? "Hiện danh sách thành viên" : "Ẩn danh sách thành viên"}
        >
          <FiSidebar size={18} style={{ transform: "scaleX(-1)" }} />
        </button>

        {/* Room Settings — chỉ hiển thị khi ở room (không phải DM) */}
        {!isDM && (
          <button
            onClick={onOpenRoomSettings}
            className="p-1.5 rounded hover:opacity-70 transition-opacity cursor-pointer"
            style={{ color: "var(--text-muted)" }}
            title="Thiết lập room"
          >
            <FiSettings size={18} />
          </button>
        )}
      </div>
    </div>
  );
}

export default ChatHeader;