-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathtabBarController.ts
179 lines (162 loc) · 6.63 KB
/
tabBarController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import fs from '../../../shared/fs/fs'
import {
DetailedListActionClickMessage,
DetailedListFilterChangeMessage,
DetailedListItemSelectMessage,
} from '../../view/connector/connector'
import * as vscode from 'vscode'
import { Messenger } from './messenger/messenger'
import { Database } from '../../../shared/db/chatDb/chatDb'
import { TabBarButtonClick, SaveChatMessage } from './model'
import { Conversation, Tab } from '../../../shared/db/chatDb/util'
import { DetailedListItemGroup, MynahIconsType } from '@aws/mynah-ui'
export class TabBarController {
private readonly messenger: Messenger
private chatHistoryDb = Database.getInstance()
private loadedChats: boolean = false
private searchTimeout: NodeJS.Timeout | undefined = undefined
private readonly DebounceTime = 300 // milliseconds
constructor(messenger: Messenger) {
this.messenger = messenger
}
async processActionClickMessage(msg: DetailedListActionClickMessage) {
if (msg.listType === 'history') {
if (msg.action.text === 'Delete') {
this.chatHistoryDb.deleteHistory(msg.action.id)
this.messenger.sendUpdateDetailedListMessage('history', { list: this.generateHistoryList() })
} else if (msg.action.text === 'Export') {
// If conversation is already open, export it
const openTabId = this.chatHistoryDb.getOpenTabId(msg.action.id)
if (openTabId) {
await this.exportChatButtonClicked({ tabID: openTabId, buttonId: 'export_chat' })
} // If conversation is not open, restore it before exporting
else {
const selectedTab = this.chatHistoryDb.getTab(msg.action.id)
this.restoreTab(selectedTab, true)
}
}
}
}
async processFilterChangeMessage(msg: DetailedListFilterChangeMessage) {
if (msg.listType === 'history') {
const searchFilter = msg.filterValues['search']
if (typeof searchFilter !== 'string') {
return
}
// Clear any pending search
if (this.searchTimeout) {
clearTimeout(this.searchTimeout)
}
// Set new timeout for this search
this.searchTimeout = setTimeout(() => {
const searchResults = this.chatHistoryDb.searchMessages(searchFilter)
this.messenger.sendUpdateDetailedListMessage('history', { list: searchResults })
}, this.DebounceTime)
}
}
// If selected is conversation is already open, select that tab. Else, open new tab with conversation.
processItemSelectMessage(msg: DetailedListItemSelectMessage) {
if (msg.listType === 'history') {
const historyID = msg.item.id
if (historyID) {
const openTabID = this.chatHistoryDb.getOpenTabId(historyID)
if (!openTabID) {
const selectedTab = this.chatHistoryDb.getTab(historyID)
this.restoreTab(selectedTab)
} else {
this.messenger.sendSelectTabMessage(openTabID, historyID)
}
this.messenger.sendCloseDetailedListMessage('history')
}
}
}
restoreTab(selectedTab?: Tab | null, exportTab?: boolean) {
if (selectedTab) {
this.messenger.sendRestoreTabMessage(
selectedTab.historyId,
selectedTab.tabType,
selectedTab.conversations.flatMap((conv: Conversation) => conv.messages),
exportTab
)
}
}
loadChats() {
if (this.loadedChats) {
return
}
this.loadedChats = true
const openConversations = this.chatHistoryDb.getOpenTabs()
if (openConversations) {
for (const conversation of openConversations) {
if (conversation.conversations && conversation.conversations.length > 0) {
this.restoreTab(conversation)
}
}
}
}
async historyButtonClicked(message: TabBarButtonClick) {
this.messenger.sendOpenDetailedListMessage(message.tabID, 'history', {
header: { title: 'Chat history' },
filterOptions: [
{
type: 'textinput',
icon: 'search' as MynahIconsType,
id: 'search',
placeholder: 'Search...',
autoFocus: true,
},
],
list: this.generateHistoryList(),
})
}
generateHistoryList(): DetailedListItemGroup[] {
const historyItems = this.chatHistoryDb.getHistory()
return historyItems.length > 0 ? historyItems : [{ children: [{ description: 'No chat history found' }] }]
}
async processSaveChat(message: SaveChatMessage) {
try {
await fs.writeFile(message.uri, message.serializedChat)
} catch (error) {
void vscode.window.showErrorMessage('An error occurred while exporting your chat.')
}
}
async processTabBarButtonClick(message: TabBarButtonClick) {
switch (message.buttonId) {
case 'history_sheet':
await this.historyButtonClicked(message)
break
case 'export_chat':
await this.exportChatButtonClicked(message)
break
}
}
private async exportChatButtonClicked(message: TabBarButtonClick) {
const defaultFileName = `q-dev-chat-${new Date().toISOString().split('T')[0]}.md`
const workspaceFolders = vscode.workspace.workspaceFolders
let defaultUri
if (workspaceFolders && workspaceFolders.length > 0) {
// Use the first workspace folder as root
defaultUri = vscode.Uri.joinPath(workspaceFolders[0].uri, defaultFileName)
} else {
// Fallback if no workspace is open
defaultUri = vscode.Uri.file(defaultFileName)
}
const saveUri = await vscode.window.showSaveDialog({
filters: {
Markdown: ['md'],
HTML: ['html'],
},
defaultUri,
title: 'Export chat',
})
if (saveUri) {
// Determine format from file extension
const format = saveUri.fsPath.endsWith('.md') ? 'markdown' : 'html'
this.messenger.sendSerializeTabMessage(message.tabID, saveUri.fsPath, format)
}
}
}