Skip to content

Commit

Permalink
add button to clear all conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed May 5, 2024
1 parent 77bbb54 commit 5aca8dc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
7 changes: 7 additions & 0 deletions src/extension/conversation-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/webview/conversation-history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const ConversationHistory = ({ onSelect }: ConversationHistoryProps) => {
const {
conversations: savedConversations,
setActiveConversation,
removeConversation
removeConversation,
clearAllConversations
} = useConversationHistory()

const handleSetConversation = (conversation: Conversation) => {
Expand All @@ -33,11 +34,16 @@ export const ConversationHistory = ({ onSelect }: ConversationHistoryProps) => {
removeConversation(conversation)
}

const handleClearAllConversations = () => clearAllConversations()

const conversations = Object.values(savedConversations).reverse()

return (
<div>
<h3>Conversation history</h3>
<VSCodeButton appearance="primary" onClick={handleClearAllConversations}>
Clear conversations
</VSCodeButton>
{conversations.length ? (
conversations.map((conversation) => (
<div
Expand Down
9 changes: 8 additions & 1 deletion src/webview/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ export const useConversationHistory = () => {
} as ClientMessage<Conversation>)
}

const clearAllConversations = () => {
global.vscode.postMessage({
type: CONVERSATION_EVENT_NAME.clearAllConversations
} as ClientMessage<string>)
}

const handler = (event: MessageEvent) => {
const message = event.data
if (message.value?.data) {
Expand All @@ -357,7 +363,8 @@ export const useConversationHistory = () => {
getConversations,
removeConversation,
saveLastConversation,
setActiveConversation
clearAllConversations,
setActiveConversation,
}
}

Expand Down

0 comments on commit 5aca8dc

Please sign in to comment.