Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbkz authored May 31, 2024
1 parent 20b2d3b commit 1e16b19
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
}

conversationHistory.push({ role: 'assistant', content: botMessage });
localStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
saveConversationHistory();
removeElement(typingIndicator);
addMessage(botMessage, false);
} catch (error) {
Expand All @@ -262,7 +262,7 @@
if (userMessage === '') return;

conversationHistory.push({ role: 'user', content: userMessage });
localStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
saveConversationHistory();

addMessage(userMessage, true);
userInput.value = '';
Expand All @@ -273,15 +273,33 @@
processBotResponse(typingIndicator);
}

sendButton.addEventListener('click', handleUserInput);
function debounce(fn, delay) {
let timeoutId;
return function (...args) {
if (timeoutId) clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
}

const debouncedHandleUserInput = debounce(handleUserInput, 300);

function saveConversationHistory() {
try {
localStorage.setItem('conversationHistory', JSON.stringify(conversationHistory));
} catch (error) {
console.error('Erro ao salvar histórico de conversa:', error);
}
}

sendButton.addEventListener('click', debouncedHandleUserInput);

userInput.addEventListener('input', () => {
sendButton.disabled = userInput.value.trim() === '';
});

userInput.addEventListener('keypress', (event) => {
if (event.key === 'Enter' && !sendButton.disabled) {
handleUserInput();
debouncedHandleUserInput();
}
});

Expand All @@ -294,10 +312,14 @@
});

function loadConversationHistory() {
const storedHistory = localStorage.getItem('conversationHistory');
if (storedHistory) {
conversationHistory = JSON.parse(storedHistory);
conversationHistory.forEach(msg => addMessage(msg.content, msg.role === 'user'));
try {
const storedHistory = localStorage.getItem('conversationHistory');
if (storedHistory) {
conversationHistory = JSON.parse(storedHistory);
conversationHistory.forEach(msg => addMessage(msg.content, msg.role === 'user'));
}
} catch (error) {
console.error('Erro ao carregar histórico de conversa:', error);
}
}

Expand Down

0 comments on commit 1e16b19

Please sign in to comment.