-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (136 loc) · 5.11 KB
/
script.js
File metadata and controls
164 lines (136 loc) · 5.11 KB
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
const typingForm = document.querySelector(".prompt_form");
const suggestions = document.querySelectorAll("#suggestion-list .card");
const chatList = document.querySelector(".chat-list");
const deleteBtn = document.querySelector("#del");
let userMessage = null;
const API_KEY = your-api-key;
const API_URL = `https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent?key=${API_KEY}`;
const loadLocalStorage = () => {
const savedChats = localStorage.getItem("savedChats");
chatList.innerHTML = savedChats || "";
document.body.classList.toggle("hide-header", savedChats);
};
// create a new Element and return it
const createMessageElemet = (content, ...classes) => {
const div = document.createElement("div");
div.classList.add("message", ...classes);
div.innerHTML = content;
return div;
};
// show typing effect one by one
// showTypingEffect = (text, textElement) => {
// const words = text.splits(" ");
// let currentWordIndex = 0;
// const typingInterval = setInterval(() => {
// // append each word to the text element with a space
// textElement.innerText +=
// (currentWordIndex === 0 ? "" : " ") + words[currentWordIndex++];
// }, 7);
// // if all words are displayed
// if (currentWordIndex === words.length) {
// clearInterval(typingInterval);
// }
// };
// fetch response from API based on user message
const generate_API_Response = async (incomingMessageDiv) => {
const textElement = incomingMessageDiv.querySelector(".text");
// send a POST request to the API with the user's message
try {
const response = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [
{
role: "user",
parts: [{ text: userMessage }],
},
],
}),
});
const data = await response.json();
const apiResponse = data?.candidates[0].content.parts[0].text.replace(
/\*\*(.*?)\*\*/g,
"$1"
);
textElement.innerText = apiResponse;
incomingMessageDiv.classList.remove("loading");
localStorage.setItem("savedChats", chatList.innerHTML);
chatList.scrollTo(0, chatList.scrollHeight);
// showTypingEffect(apiResponse, textElement.innerText);
} catch (error) {
console.log(error);
} finally {
incomingMessageDiv.classList.remove("loading");
}
};
// show a loading animation while waiting for the API response
const showLoadingAnimation = () => {
const html = `
<div class="message-content">
<img src="image/ai.png" alt="IG image" class="avatar">
<p class="text"></p>
<div class="loading-indicator">
<div class="loading-bar"></div>
<div class="loading-bar"></div>
<div class="loading-bar"></div>
</div>
</div>
<span onclick="copyMessage(this)" class="material-symbols-rounded icon">
content_copy
</span>`;
const incomingMessageDiv = createMessageElemet(html, "incoming", "loading");
chatList.appendChild(incomingMessageDiv);
generate_API_Response(incomingMessageDiv);
};
const copyMessage = () => {
const messageText = copyIcon.parentElement.querySelector(".text").innerText;
navigator.clipboard.writeText(copyText);
copyIcon.innerText = "done"; // show tick icon
setTimeout(() => (copyIcon.innerText = "content_copy"), 1000); // revert icon after 1 second
};
// handle sending outgoing chat messages
const handleOutgoingChat = () => {
userMessage = typingForm.querySelector(".typing-input").value.trim() || userMessage;
if (!userMessage) return;
const html = `<div class="message-content">
<img src="image/user.png" alt="User Image" class="avatar">
<p class="text"></p>
</div>`;
const outgoingMessageDiv = createMessageElemet(html, "outgoing");
outgoingMessageDiv.querySelector(".text").innerText = userMessage;
chatList.appendChild(outgoingMessageDiv);
typingForm.reset(); // clear input
chatList.scrollTo(0, chatList.scrollHeight);
document.body.classList.add("hide-header");
setTimeout(showLoadingAnimation, 500); // show loading animation after a delay
};
// set userMessage and handle outgoing chat when a suggestion is clicked
suggestions.forEach((suggestion) => {
suggestion.addEventListener("click", () => {
userMessage = suggestion.querySelector(".card-text").innerText;
handleOutgoingChat();
});
});
deleteBtn.addEventListener("click", () => {
if (confirm("Are you sure you want to delete all messages ?")) {
localStorage.removeItem("savedChats");
loadLocalstorageData();
}
});
// prevent default form submission and handle outgoing chat
typingForm.addEventListener("submit", (e) => {
e.preventDefault();
handleOutgoingChat();
});
// Function to handle scrolling to the bottom
const scrollToBottom = () => {
chatList.scrollTop = chatList.scrollHeight;
};
// Ensure scrolling to the bottom after page load
window.onload = function () {
setTimeout(() => {
scrollToBottom();
}, 100); // Small delay to ensure content is rendered
};
loadLocalStorage();