From aac58956b97754b27a9c85b90850602eb273495d Mon Sep 17 00:00:00 2001 From: nickchecan Date: Sat, 11 Jan 2025 21:51:59 -0600 Subject: [PATCH] feat: add loading indicator to ai request --- .../resources/views/scripts.js | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/com.developer.nefarious.zjoule.plugin/resources/views/scripts.js b/com.developer.nefarious.zjoule.plugin/resources/views/scripts.js index 73f1e21..b119339 100644 --- a/com.developer.nefarious.zjoule.plugin/resources/views/scripts.js +++ b/com.developer.nefarious.zjoule.plugin/resources/views/scripts.js @@ -1,9 +1,37 @@ +const loadingIconSvg = ` + + + + + + + + + + + + + + + + + + + + + + + + +` + function sendMessage() { const userInput = document.getElementById('userInput').value; if (userInput.trim() === "") return; try { addUserMessage(userInput); getAIResponse(userInput); // Java callback + startLoading(); } catch (e) { } updatePlaceholder(); @@ -33,6 +61,7 @@ function clearMessageAndScrollDown() { } function receiveMessage(output) { + finishLoading(); addBotMessage(output); updatePlaceholder(); clearMessageAndScrollDown(); @@ -106,4 +135,21 @@ function showPlaceholder() { function hidePlaceholder() { const placeholder = document.getElementById("placeholder"); placeholder.style.display = "none"; +} + +function startLoading() { + const chatBox = document.getElementById('chatBox'); + const message = document.createElement('div'); + message.className = 'message bot-message'; + message.innerHTML = loadingIconSvg; + chatBox.appendChild(message); +} + +function finishLoading() { + const chatBox = document.getElementById('chatBox'); + const botMessages = document.getElementsByClassName('message bot-message'); + if (botMessages.length > 0) { + const loadingIndicator = botMessages[botMessages.length - 1]; + chatBox.removeChild(loadingIndicator); + } } \ No newline at end of file