From 401d245fe43aee951987d1304644fe906da9a3d8 Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Thu, 3 Jul 2025 21:02:12 -0400 Subject: [PATCH] CRITICAL FIX: trash buttons now properly update localStorage - All delete buttons now call loadConversationHistory/saveConversationHistory - Update both localStorage and chat.js module for consistency - User message delete: filters by content match - AI message delete: removes both user and AI messages in pair - Historical message delete: removes by array index - Intro message delete: filters by assistant role and content - No more localStorage sync issues with deleted messages --- src/uncloseai-embed-modal.js | 60 +++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/src/uncloseai-embed-modal.js b/src/uncloseai-embed-modal.js index 343e613..13c41b6 100644 --- a/src/uncloseai-embed-modal.js +++ b/src/uncloseai-embed-modal.js @@ -1354,13 +1354,18 @@ async function openUncloseaiEmbeddedModalNew() { userDeleteBtn.style.background = colors.actionBg; }; userDeleteBtn.onclick = async () => { - // Remove from chat history - const currentHistory = await getChatHistory(); + // Remove from chat history and localStorage + const { loadConversationHistory, saveConversationHistory } = await import("./storage.js"); + const { updateChatHistory } = await import("./chat.js"); + + const currentHistory = await loadConversationHistory(); const updatedHistory = currentHistory.filter( (msg) => !(msg.role === "user" && msg.content === message), ); - await updateChatHistory(updatedHistory); + + // Update both localStorage and chat.js module await saveConversationHistory(updatedHistory); + await updateChatHistory(updatedHistory); userMsg.remove(); }; @@ -1561,7 +1566,23 @@ async function openUncloseaiEmbeddedModalNew() { color: ${colors.primaryText}; transition: all 0.2s; `; - deleteBtn.onclick = () => { + deleteBtn.onclick = async () => { + // Remove both user and AI messages from history and localStorage + const { loadConversationHistory, saveConversationHistory } = await import("./storage.js"); + const { updateChatHistory } = await import("./chat.js"); + + const currentHistory = await loadConversationHistory(); + const updatedHistory = currentHistory.filter( + (msg) => !( + (msg.role === "user" && msg.content === message) || + (msg.role === "assistant" && msg.content === response) + ) + ); + + // Update both localStorage and chat.js module + await saveConversationHistory(updatedHistory); + await updateChatHistory(updatedHistory); + userMsg.remove(); aiMsg.remove(); }; @@ -2152,9 +2173,21 @@ You have complete knowledge of this page content and can reference any details, font-size: 12px; transition: all 0.2s; `; - deleteBtn.onclick = () => { + deleteBtn.onclick = async () => { + // Remove from chat history and localStorage by index + const { loadConversationHistory, saveConversationHistory } = await import("./storage.js"); + const { updateChatHistory } = await import("./chat.js"); + + const currentHistory = await loadConversationHistory(); + if (index >= 0 && index < currentHistory.length) { + currentHistory.splice(index, 1); + + // Update both localStorage and chat.js module + await saveConversationHistory(currentHistory); + await updateChatHistory(currentHistory); + } + aiMsg.remove(); - // TODO: Remove from chat history }; // Copy Raw (Markdown) button @@ -2416,7 +2449,20 @@ You have complete knowledge of this page content and can reference any details, font-size: 12px; transition: all 0.2s; `; - deleteBtn.onclick = () => { + deleteBtn.onclick = async () => { + // Remove intro message from localStorage + const { loadConversationHistory, saveConversationHistory } = await import("./storage.js"); + const { updateChatHistory } = await import("./chat.js"); + + const currentHistory = await loadConversationHistory(); + const updatedHistory = currentHistory.filter( + (msg) => !(msg.role === "assistant" && msg.content === response) + ); + + // Update both localStorage and chat.js module + await saveConversationHistory(updatedHistory); + await updateChatHistory(updatedHistory); + introMsg.remove(); };