File size: 15,149 Bytes
fbd260e
44009db
e065eb6
a28332d
fbd260e
1539090
79858c0
787bc91
44009db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539090
 
 
fbd260e
e065eb6
44009db
 
 
fbd260e
a28332d
1539090
44009db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a28332d
1539090
 
 
 
fbd260e
1539090
a28332d
 
 
 
 
 
 
 
4e6c4ab
a28332d
4e6c4ab
 
 
 
a28332d
4e6c4ab
 
 
 
 
a28332d
 
 
 
1539090
787bc91
fbd260e
1539090
fbd260e
1539090
4e6c4ab
1539090
 
 
44009db
1539090
 
4e6c4ab
 
 
a28332d
4e6c4ab
 
 
 
 
 
 
 
 
 
a28332d
 
4e6c4ab
a28332d
4e6c4ab
 
 
 
 
 
 
 
 
 
 
1539090
fbd260e
4e6c4ab
a28332d
44009db
4e6c4ab
 
 
1539090
 
 
fbd260e
a28332d
 
fbd260e
787bc91
fbd260e
 
 
 
44009db
fbd260e
 
 
a28332d
fbd260e
 
 
1539090
 
e065eb6
 
 
 
 
 
 
44009db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539090
44009db
1539090
44009db
 
 
 
 
 
 
 
 
 
 
a28332d
 
44009db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539090
 
 
fbd260e
 
a28332d
44009db
a28332d
44009db
 
 
 
 
 
 
 
a28332d
44009db
 
a28332d
 
44009db
 
a28332d
 
44009db
 
a28332d
 
44009db
 
a28332d
fbd260e
 
 
a28332d
 
e065eb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44009db
e065eb6
 
 
 
 
 
 
 
 
fbd260e
e065eb6
 
a28332d
1539090
 
a28332d
 
7d8a459
a28332d
fbd260e
 
44009db
7d8a459
 
a28332d
 
4e6c4ab
 
 
7d8a459
a28332d
fbd260e
 
a28332d
4e6c4ab
fbd260e
 
a28332d
fbd260e
 
44009db
1539090
44009db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539090
 
 
 
 
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
import { useState, useEffect, useRef } from "react";
import { FaUser, FaRobot, FaPaperPlane, FaCircle, FaPlus, FaPizzaSlice, FaInfoCircle, FaTimes, FaFilePdf, FaFileWord, FaDatabase, FaGlobe, FaSun, FaMoon } from "react-icons/fa";
import { MdOutlineContentCopy, MdCheck } from "react-icons/md";
import ReactMarkdown from "react-markdown";
import "./ChatBot.css";

const BASE_URL = "https://tanmaivan-hutmind.hf.space";

const TRANSLATIONS = {
  vi: {
    newChat: "Trò chuyện mới",
    welcomeTitle: "Tôi có thể giúp gì cho bạn?",
    welcomeSubtitle: "Hãy hỏi tôi về chính sách JRG, thông tin hệ thống Pizza Hut VN hoặc về Data Analytics Team.",
    knowledgeBaseBtn: "Dữ liệu thông tin",
    suggestion1: "Giới thiệu về team data analytics, ban quản lý là ai?",
    suggestion1Chip: "Giới thiệu Data Analytics",
    suggestion2: "Kể cho tôi nghe lịch sử hình thành của Pizza Hut!",
    suggestion2Chip: "Lịch sử Pizza Hut",
    suggestion3: "Data Analytics Team có ai làm việc ở nước ngoài không?",
    suggestion3Chip: "Data Analytics nước ngoài",
    suggestion4: "Tập đoàn JRG đang quản lý thương hiệu Pizza Hut ở những nước nào?",
    suggestion4Chip: "Quy mô JRG toàn cầu",
    inputPlaceholder: "Gửi tin nhắn cho HutMind...",
    footerDisclaimer: "HutMind có thể mắc lỗi. Vui lòng kiểm tra lại các thông tin quan trọng.",
    modalTitle: "Dữ liệu tham khảo",
    modalDescription: "Dưới đây là các tài liệu và nguồn dữ liệu hiện đang được HutMind sử dụng để trả lời câu hỏi của bạn:",
    errorConnection: "Đã xảy ra lỗi khi kết nối với chatbot. Vui lòng thử lại sau.",
    errorInit: "Lỗi khi khởi tạo lại chatbot",
    copyTooltip: "Sao chép",
    themeLight: "Chế độ sáng",
    themeDark: "Chế độ tối",
    sources: [
      { name: "Thông tin về Data Analytics Team (JRGVN)", type: "database" },
      { name: "Hướng dẫn Chính sách Bảo mật Thông tin (2024)", type: "pdf" },
      { name: "Quy định Nghỉ phép và Nghỉ bệnh (PSL)", type: "pdf" },
      { name: "Chính sách Công tác và Tiếp khách nước ngoài (2025)", type: "pdf" },
      { name: "Chính sách Bảo mật Dữ liệu PHV (2023)", type: "word" },
      { name: "Chỉ thị Ứng phó Sự cố CNTT (DRP)", type: "pdf" },
      { name: "Danh mục từ viết tắt và Thuật ngữ chuyên môn", type: "word" },
      { name: "Quy trình và Chính sách Phần mềm", type: "pdf" },
    ]
  },
  en: {
    newChat: "New Chat",
    welcomeTitle: "How can I help you today?",
    welcomeSubtitle: "Ask me about JRG policies, Pizza Hut VN systems, or the Data Analytics Team.",
    knowledgeBaseBtn: "Knowledge Base",
    suggestion1: "Intro to Data Analytics Team, who's the management?",
    suggestion1Chip: "Data Analytics Intro",
    suggestion2: "Tell me about the history of Pizza Hut!",
    suggestion2Chip: "Pizza Hut History",
    suggestion3: "Does anyone in the Data Analytics Team work overseas?",
    suggestion3Chip: "Overseas Data Analytics Team",
    suggestion4: "Which countries does JRG manage the Pizza Hut brand in?",
    suggestion4Chip: "JRG Global Scale",
    inputPlaceholder: "Message HutMind...",
    footerDisclaimer: "HutMind can make mistakes. Check important info.",
    modalTitle: "Reference Data",
    modalDescription: "Below are the documents and data sources currently being used by HutMind to answer your questions:",
    errorConnection: "An error occurred connecting to the chatbot. Please try again later.",
    errorInit: "Error re-initializing chatbot",
    copyTooltip: "Copy",
    themeLight: "Light mode",
    themeDark: "Dark mode",
    sources: [
      { name: "Data Analytics Team Info (JRGVN)", type: "database" },
      { name: "Info Security Policy Guidelines (2024)", type: "pdf" },
      { name: "Leave & Sick Leave Regulations (PSL)", type: "pdf" },
      { name: "Overseas Travel & Entertainment Policy (2025)", type: "pdf" },
      { name: "PHV Data Privacy Policy (2023)", type: "word" },
      { name: "IT Disaster Recovery Directive (DRP)", type: "pdf" },
      { name: "Abbreviations & Technical Terms", type: "word" },
      { name: "Software Policy & Procedure", type: "pdf" },
    ]
  }
};

const ChatBot = () => {
  const [messages, setMessages] = useState([]);
  const [userInput, setUserInput] = useState("");
  const [isLoading, setIsLoading] = useState(false);
  const [copiedIndex, setCopiedIndex] = useState(null);
  const [showKnowledgeModal, setShowKnowledgeModal] = useState(false);
  const [language, setLanguage] = useState(() => localStorage.getItem("hutmind-lang") || "vi");
  const [theme, setTheme] = useState(() => localStorage.getItem("hutmind-theme") || "light");
  const chatWindowRef = useRef(null);
  const textareaRef = useRef(null);

  const t = (key) => TRANSLATIONS[language][key];

  // Persist settings
  useEffect(() => {
    localStorage.setItem("hutmind-lang", language);
  }, [language]);

  useEffect(() => {
    localStorage.setItem("hutmind-theme", theme);
    if (theme === 'dark') {
      document.body.classList.add('dark');
    } else {
      document.body.classList.remove('dark');
    }
  }, [theme]);

  // Auto scroll
  useEffect(() => {
    if (chatWindowRef.current) {
      chatWindowRef.current.scrollTop = chatWindowRef.current.scrollHeight;
    }
  }, [messages]);

  // Adjust textarea height automatically
  useEffect(() => {
    if (textareaRef.current) {
      textareaRef.current.style.height = 'auto';
      textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 200)}px`;
    }
  }, [userInput]);

  const handleSend = async () => {
    if (userInput.trim() === "" || isLoading) return; 

    setMessages((prevMessages) => [
      ...prevMessages,
      { sender: "user", text: userInput },
      { sender: "bot", text: "..." }, 
    ]);

    setUserInput("");
    setIsLoading(true);

    if (textareaRef.current) {
      textareaRef.current.style.height = 'auto';
    }

    try {
      const response = await fetch(`${BASE_URL}/process_query_stream/`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ query: userInput }),
      });

      if (!response.ok) {
        throw new Error(t("errorConnection"));
      }

      const reader = response.body.getReader();
      const decoder = new TextDecoder();
      let botResponse = "";
      let isFirstChunk = true; 

      while (true) {
        const { done, value } = await reader.read();
        if (done) break;

        const chunk = decoder.decode(value);
        botResponse += chunk;

        if (isFirstChunk) {
          setMessages((prevMessages) => [
            ...prevMessages.filter((msg) => msg.text !== "..."), 
            { sender: "bot", text: botResponse }, 
          ]);
          isFirstChunk = false; 
        } else {
          setMessages((prevMessages) => {
            const newMessages = [...prevMessages];
            const lastMessageIndex = newMessages.length - 1;
            if (newMessages[lastMessageIndex].sender === "bot") {
              newMessages[lastMessageIndex].text = botResponse;
            }
            return newMessages;
          });
        }
      }
    } catch (error) {
      console.error("Lỗi:", error);
      setMessages((prevMessages) => [
        ...prevMessages.filter((msg) => msg.text !== "..."),
        { sender: "bot", text: t("errorConnection") }, 
      ]);
    } finally {
      setIsLoading(false);
    }
  };

  const handleNewChat = async () => {
    setMessages([]); 
    setIsLoading(false); // Reset loading state
    try {
      const response = await fetch(`${BASE_URL}/newchat/`, {
        method: "GET",
      });

      if (!response.ok) {
        throw new Error(t("errorInit"));
      }

      const data = await response.json();
      console.log(data.message); 
    } catch (error) {
      console.error("Lỗi:", error);
    }
  };

  const copyToClipboard = (text, index) => {
    navigator.clipboard.writeText(text).then(() => {
      setCopiedIndex(index);
      setTimeout(() => setCopiedIndex(null), 2000);
    });
  };

  const getSourceIcon = (type) => {
    switch (type) {
      case "pdf": return <FaFilePdf className="source-icon pdf" />;
      case "word": return <FaFileWord className="source-icon word" />;
      case "database": return <FaDatabase className="source-icon db" />;
      default: return <FaInfoCircle className="source-icon" />;
    }
  };

  const toggleLanguage = () => {
    setLanguage(prev => prev === "vi" ? "en" : "vi");
  };

  const toggleTheme = () => {
    setTheme(prev => prev === "light" ? "dark" : "light");
  };

  return (
    <div className={`chatbot-container ${theme}`}>
      <div className="chat-header">
        <div className="header-left">
          <div className="header-logo" onClick={handleNewChat} title="HutMind">
            <img 
              src={theme === 'light' ? "/logo_light.png" : "/logo_dark.png"} 
              alt="Logo" 
              width={50} 
              height={50} 
            />
            <div className="header-title">
              <h1>HutMind</h1>
            </div>
          </div>
        </div>

        <div className="header-right">
          <button className="theme-switcher" onClick={toggleTheme} title={theme === 'light' ? t("themeDark") : t("themeLight")}>
            {theme === 'light' ? <FaMoon size={16} /> : <FaSun size={16} />}
          </button>
          
          <button className="lang-switcher" onClick={toggleLanguage} title="Switch Language">
            <FaGlobe size={14} />
            <span>{language.toUpperCase()}</span>
          </button>

          <button className="new-chat-btn" onClick={handleNewChat} title={t("newChat")}>
            <FaPlus size={16} />
            <span>{t("newChat")}</span>
          </button>
        </div>
      </div>

      <div className="chat-window" ref={chatWindowRef}>
        {messages.length === 0 && (
          <div className="welcome-container">
            <div className="welcome-logo">
              <img src="/mascot.png" alt="Mascot" width={200} height={250} />
            </div>
            <h2>{t("welcomeTitle")}</h2>
            <p className="welcome-subtitle">{t("welcomeSubtitle")}</p>
            
            <button className="knowledge-base-btn" onClick={() => setShowKnowledgeModal(true)}>
              <FaInfoCircle size={14} />
              <span>{t("knowledgeBaseBtn")}</span>
            </button>

            <div className="suggestion-chips">
              <button onClick={() => setUserInput(t("suggestion1"))}>
                {t("suggestion1Chip")}
              </button>
              
              <button onClick={() => setUserInput(t("suggestion2"))}>
                {t("suggestion2Chip")}
              </button>
              
              <button onClick={() => setUserInput(t("suggestion3"))}>
                {t("suggestion3Chip")}
              </button>
              
              <button onClick={() => setUserInput(t("suggestion4"))}>
                {t("suggestion4Chip")}
              </button>
            </div>
          </div>
        )}

        <div className="messages-list">
          {messages.map((message, index) => {
            const lastBotIndex = [...messages].reverse().findIndex(m => m.sender === 'bot');
            const actualLastBotIndex = lastBotIndex !== -1 ? messages.length - 1 - lastBotIndex : -1;
            const isLatestBotMessage = index === actualLastBotIndex;

            return (
              <div
                key={index}
                className={`message-row ${message.sender}`}
              >
                <div className="message-content">
                  {message.sender === "bot" && message.text === "..." ? (
                    <div className="typing-indicator">
                      <FaCircle className="dot dot1" />
                      <FaCircle className="dot dot2" />
                      <FaCircle className="dot dot3" />
                    </div>
                  ) : message.sender === "bot" ? (
                    <div className={`bot-message-wrapper ${isLatestBotMessage ? 'latest' : ''}`}>
                      <div className="markdown-body">
                        <ReactMarkdown>{message.text}</ReactMarkdown>
                      </div>
                      {message.text !== "..." && (
                        <button 
                          className="copy-btn" 
                          onClick={() => copyToClipboard(message.text, index)}
                          title={t("copyTooltip")}
                        >
                          {copiedIndex === index ? <MdCheck size={18} /> : <MdOutlineContentCopy size={18} />}
                        </button>
                      )}
                    </div>
                  ) : (
                    <div className="user-text">{message.text}</div>
                  )}
                </div>
              </div>
            );
          })}
        </div>
      </div>

      <div className="input-area">
        <div className="input-container">
          <textarea
            ref={textareaRef}
            value={userInput}
            onChange={(e) => setUserInput(e.target.value)}
            placeholder={t("inputPlaceholder")}
            rows={1}
            onKeyDown={(e) => {
              if (e.key === "Enter" && !e.shiftKey) {
                e.preventDefault();
                handleSend();
              }
            }}
            className="chat-input"
          />
          <button
            onClick={handleSend}
            disabled={userInput.trim() === "" || isLoading}
            className={`send-button ${userInput.trim() === "" || isLoading ? "disabled" : ""}`}
            title="Gửi"
          >
            <FaPaperPlane size={16} />
          </button>
        </div>
        <div className="footer-text">{t("footerDisclaimer")}</div>
      </div>

      {showKnowledgeModal && (
        <div className="modal-overlay" onClick={() => setShowKnowledgeModal(false)}>
          <div className="modal-content" onClick={(e) => e.stopPropagation()}>
            <div className="modal-header">
              <h3>{t("modalTitle")}</h3>
              <button className="close-modal" onClick={() => setShowKnowledgeModal(false)}>
                <FaTimes />
              </button>
            </div>
            <div className="modal-body">
              <p className="modal-description">{t("modalDescription")}</p>
              <div className="sources-list">
                {t("sources").map((source, idx) => (
                  <div key={idx} className="source-item">
                    {getSourceIcon(source.type)}
                    <span className="source-name">{source.name}</span>
                  </div>
                ))}
              </div>
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

export default ChatBot;