From 5aca8dcdf6276c7f129c61835da5b02bb2893b71 Mon Sep 17 00:00:00 2001 From: rjmacarthy Date: Sun, 5 May 2024 23:29:05 +0100 Subject: [PATCH] add button to clear all conversations --- src/common/constants.ts | 3 ++- src/extension/conversation-history.ts | 7 +++++++ src/webview/conversation-history.tsx | 8 +++++++- src/webview/hooks.ts | 9 ++++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/common/constants.ts b/src/common/constants.ts index 1737fba..07115cf 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -88,7 +88,8 @@ export const CONVERSATION_EVENT_NAME = { removeConversation: 'twinny.remove-conversation', saveConversation: 'twinny.save-conversation', saveLastConversation: 'twinny.save-last-conversation', - setActiveConversation: 'twinny.set-active-conversation' + setActiveConversation: 'twinny.set-active-conversation', + clearAllConversations: 'twinny.clear-all-conversations' } export const PROVIDER_EVENT_NAME = { diff --git a/src/extension/conversation-history.ts b/src/extension/conversation-history.ts index 8ab9f6b..ee59777 100644 --- a/src/extension/conversation-history.ts +++ b/src/extension/conversation-history.ts @@ -58,6 +58,8 @@ export class ConversationHistory { case CONVERSATION_EVENT_NAME.saveConversation: if (!message.data) return return this.saveConversation(message.data) + case CONVERSATION_EVENT_NAME.clearAllConversations: + return this.clearAllConversations() default: // do nothing } @@ -207,6 +209,11 @@ export class ConversationHistory { this.getAllConversations() } + clearAllConversations() { + this._context.globalState.update(CONVERSATION_STORAGE_KEY, {}) + this.setActiveConversation(undefined) + } + async saveConversation(conversation: Conversation) { const activeConversation = this.getActiveConversation() if (activeConversation) diff --git a/src/webview/conversation-history.tsx b/src/webview/conversation-history.tsx index 87c236c..b90f45c 100644 --- a/src/webview/conversation-history.tsx +++ b/src/webview/conversation-history.tsx @@ -14,7 +14,8 @@ export const ConversationHistory = ({ onSelect }: ConversationHistoryProps) => { const { conversations: savedConversations, setActiveConversation, - removeConversation + removeConversation, + clearAllConversations } = useConversationHistory() const handleSetConversation = (conversation: Conversation) => { @@ -33,11 +34,16 @@ export const ConversationHistory = ({ onSelect }: ConversationHistoryProps) => { removeConversation(conversation) } + const handleClearAllConversations = () => clearAllConversations() + const conversations = Object.values(savedConversations).reverse() return (

Conversation history

+ + Clear conversations + {conversations.length ? ( conversations.map((conversation) => (
{ } as ClientMessage) } + const clearAllConversations = () => { + global.vscode.postMessage({ + type: CONVERSATION_EVENT_NAME.clearAllConversations + } as ClientMessage) + } + const handler = (event: MessageEvent) => { const message = event.data if (message.value?.data) { @@ -357,7 +363,8 @@ export const useConversationHistory = () => { getConversations, removeConversation, saveLastConversation, - setActiveConversation + clearAllConversations, + setActiveConversation, } }