File size: 5,014 Bytes
5ded781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";

// First, let's add the Marked library for markdown parsing
const markedScript = document.createElement("script");
markedScript.src =
  "https://cdnjs.cloudflare.com/ajax/libs/marked/4.2.12/marked.min.js";
document.head.appendChild(markedScript);

// Initialize toggle button functionality immediately
const chatToggleBtn = document.getElementById("chatToggleBtn");
const chatBox = document.querySelector(".chat-box");
const notificationBadge = document.querySelector(".notification-badge");

chatToggleBtn.addEventListener("click", function () {
  chatBox.classList.toggle("active");
  if (notificationBadge) {
    notificationBadge.remove();
  }
});

// Wait for marked to load before initializing chat
markedScript.onload = function () {
  initializeChat();
};

function initializeChat() {
  var chatInput = document.querySelector("#chat_input");
  var typing = document.querySelector("#typing");
  var send = document.querySelector("#send");
  var chatMessages = document.querySelector("#chat_messages");
  var chatBoxBody = document.querySelector("#chat_box_body");
  var chatForm = document.querySelector("#chat-form");

  // Profile configuration
  const profile = {
    my: {
      name: "You",
      pic: "https://imgproxy.attic.sh/unsafe/rs:fit:540:540:1:1/t:1:FF00FF:false:false/aHR0cHM6Ly9hdHRp/Yy5zaC9sYmxjODRn/OTcxeXV5bnQwcXMz/NjNmbGNrZTQ5.webp",
    },
    other: {
      name: "Assistant",
      pic: "/static/favicon.PNG",
    },
  };

  // Configure marked options
  marked.setOptions({
    breaks: true,
    gfm: true,
    smartLists: true,
    smartypants: true,
    xhtml: true,
  });

  function sanitizeHTML(str) {
    const div = document.createElement("div");
    div.textContent = str;
    return div.innerHTML;
  }

  function renderMarkdown(text) {
    try {
      const rawHtml = marked.parse(text);
      const tempDiv = document.createElement("div");
      tempDiv.innerHTML = rawHtml;

      tempDiv.querySelectorAll("pre code").forEach((block) => {
        block.textContent = block.innerHTML;
      });

      return tempDiv.innerHTML;
    } catch (e) {
      console.error("Markdown parsing error:", e);
      return sanitizeHTML(text);
    }
  }

  function renderProfile(p) {
    return `
                    <div class="profile ${p}-profile hide">
                        <img src="${profile[p].pic}" alt="${profile[p].name}" width="30" height="30" />
                        &nbsp;<span>${profile[p].name}</span>
                    </div>
                `;
  }

  function renderMessage(p, m) {
    const messageContent = p === "other" ? renderMarkdown(m) : sanitizeHTML(m);
    return `<div class="message ${p}-message hide">${messageContent}</div>`;
  }

  function appendMessage(p, message) {
    const messageHtml = renderProfile(p) + renderMessage(p, message);
    chatMessages.insertAdjacentHTML("beforeend", messageHtml);

    // Reveal new elements with animation
    const newElements = document.querySelectorAll(
      ".profile.hide, .message.hide"
    );
    newElements.forEach((elm) => {
      if (elm.classList.contains("profile")) {
        elm.style.height = "auto";
      }
      elm.classList.remove("hide");
    });

    // Scroll to bottom
    chatBoxBody.scrollTop = chatBoxBody.scrollHeight;

    // Initialize syntax highlighting if available
    if (window.hljs) {
      document.querySelectorAll("pre code").forEach((block) => {
        hljs.highlightBlock(block);
      });
    }
  }

  // Auto-resize textarea
  chatInput.addEventListener("input", function () {
    this.style.height = "0";
    this.style.height = this.scrollHeight + 1 + "px";
  });

  // Handle enter key
  chatInput.addEventListener("keydown", function (evt) {
    if (evt.keyCode == 13 && !evt.shiftKey) {
      handleSubmit();
      evt.preventDefault();
    }
  });

  // Handle form submission
  if (chatForm) {
    chatForm.addEventListener("submit", function (evt) {
      evt.preventDefault();
      handleSubmit();
    });
  }

  async function handleSubmit() {
    const message = chatInput.value.trim();
    if (!message) return;

    // Show user message
    appendMessage("my", message);

    // Clear input
    chatInput.value = "";
    chatInput.style.height = "40px";

    // Show typing indicator
    typing.classList.add("active");

    try {
      // Send message to backend
      const formData = new FormData();
      formData.append("text", message);

      const response = await fetch("/get", {
        method: "POST",
        body: formData,
      });

      const data = await response.text();

      // Hide typing indicator
      typing.classList.remove("active");

      // Show bot response
      appendMessage("other", data);
    } catch (error) {
      console.error("Error:", error);
      typing.classList.remove("active");
      appendMessage(
        "other",
        "Sorry, I encountered an error. Please try again."
      );
    }
  }

  // Initialize scroll position
  chatBoxBody.scrollTop = chatBoxBody.scrollHeight;
}