Skip to content

Commit 87178a5

Browse files
authored
fix: open initial tab using mynahUI defaults instead of waiting for ChatOptions (#1322)
* fix: open initial tab using mynahUI defaults instead of waiting for ChatOptions * fix: push banner message to initial tab after chat options are received
1 parent 25445e1 commit 87178a5

File tree

4 files changed

+38
-26
lines changed

4 files changed

+38
-26
lines changed

chat-client/src/client/chat.test.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,25 @@ describe('Chat', () => {
6969
})
7070

7171
it('publishes ready event when initialized', () => {
72-
assert.calledWithExactly(clientApi.postMessage.firstCall, { command: READY_NOTIFICATION_METHOD })
73-
})
74-
75-
it('creates initial tab when chat options are provided', () => {
76-
const bannerText = 'This is a test banner message'
77-
const eventParams = {
78-
command: CHAT_OPTIONS,
79-
params: {
80-
chatNotifications: {
81-
bannerText: bannerText,
82-
},
83-
},
84-
}
85-
const sendToPromptEvent = createInboundEvent(eventParams)
86-
window.dispatchEvent(sendToPromptEvent)
87-
88-
assert.calledWithExactly(clientApi.postMessage.firstCall, { command: READY_NOTIFICATION_METHOD })
72+
assert.callCount(clientApi.postMessage, 4)
8973

90-
assert.calledWithExactly(clientApi.postMessage.secondCall, {
74+
assert.calledWithExactly(clientApi.postMessage.firstCall, {
9175
command: TELEMETRY,
92-
params: { name: ENTER_FOCUS },
76+
params: { name: 'enterFocus' },
9377
})
78+
assert.calledWithExactly(clientApi.postMessage.secondCall, { command: READY_NOTIFICATION_METHOD })
9479

9580
assert.calledWithExactly(clientApi.postMessage.thirdCall, {
9681
command: TAB_ADD_NOTIFICATION_METHOD,
97-
params: { tabId: sinon.match.string },
82+
params: { tabId: initialTabId },
9883
})
9984

10085
assert.calledWithExactly(clientApi.postMessage.lastCall, {
10186
command: TELEMETRY,
10287
params: {
10388
triggerType: 'click',
10489
name: TAB_ADD_TELEMETRY_EVENT,
105-
tabId: sinon.match.string,
90+
tabId: initialTabId,
10691
},
10792
})
10893
})

chat-client/src/client/chat.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
CONTEXT_COMMAND_NOTIFICATION_METHOD,
4242
CONVERSATION_CLICK_REQUEST_METHOD,
4343
CREATE_PROMPT_NOTIFICATION_METHOD,
44+
ChatMessage,
4445
ChatOptionsUpdateParams,
4546
ChatParams,
4647
ChatUpdateParams,
@@ -216,12 +217,26 @@ export const createChat = (
216217
tabFactory.enableExport()
217218
}
218219

219-
const initialTabId = mynahApi.createTabId()
220-
if (initialTabId) mynahUi.selectTab(initialTabId)
221-
222220
const allExistingTabs: MynahUITabStoreModel = mynahUi.getAllTabs()
223221
const highlightCommand = featureConfig.get('highlightCommand')
224222

223+
if (tabFactory.initialTabId && allExistingTabs[tabFactory.initialTabId] && params?.chatNotifications) {
224+
// Edge case: push banner message to initial tab when ChatOptions are received
225+
// Because initial tab is added to MynahUi store at initialisation,
226+
// that tab does not have banner message, which arrives in ChatOptions above.
227+
const store = mynahUi.getTabData(tabFactory.initialTabId)?.getStore() || {}
228+
const chatItems = store.chatItems || []
229+
const updatedInitialItems = tabFactory.getChatItems(false, false, chatItems as ChatMessage[])
230+
231+
// First clear the tab, so that messages are not appended https://github.com/aws/mynah-ui/blob/38608dff905b3790d85c73e2911ec7071c8a8cdf/docs/USAGE.md#using-updatestore-function
232+
mynahUi.updateStore(tabFactory.initialTabId, {
233+
chatItems: [],
234+
})
235+
mynahUi.updateStore(tabFactory.initialTabId, {
236+
chatItems: updatedInitialItems,
237+
})
238+
}
239+
225240
for (const tabId in allExistingTabs) {
226241
mynahUi.updateStore(tabId, {
227242
...tabFactory.getDefaultTabData(),

chat-client/src/client/mynahUi.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ export const createMynahUi = (
233233
},
234234
onReady: () => {
235235
messager.onUiReady()
236+
messager.onTabAdd(tabFactory.initialTabId)
236237
},
237238
onFileClick: (tabId, filePath, deleted, messageId, eventId, fileDetails) => {
238239
messager.onFileClick({ tabId, filePath, messageId, fullPath: fileDetails?.data?.['fullPath'] })
@@ -483,7 +484,15 @@ export const createMynahUi = (
483484
}
484485

485486
const mynahUiProps: MynahUIProps = {
486-
tabs: {},
487+
tabs: {
488+
[tabFactory.initialTabId]: {
489+
isSelected: true,
490+
store: {
491+
...tabFactory.createTab(true),
492+
chatItems: tabFactory.getChatItems(true, true),
493+
},
494+
},
495+
},
487496
defaults: {
488497
store: tabFactory.createTab(false),
489498
},

chat-client/src/client/tabs/tabFactory.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class TabFactory {
1919
private history: boolean = false
2020
private export: boolean = false
2121
private agenticMode: boolean = false
22+
initialTabId: string
2223

2324
public static generateUniqueId() {
2425
// from https://github.com/aws/mynah-ui/blob/a3799f47ca4b7c02850264e328539a40709a6858/src/helper/guid.ts#L6
@@ -31,7 +32,9 @@ export class TabFactory {
3132
private defaultTabData: DefaultTabData,
3233
private quickActionCommands?: QuickActionCommandGroup[],
3334
private bannerMessage?: ChatMessage
34-
) {}
35+
) {
36+
this.initialTabId = TabFactory.generateUniqueId()
37+
}
3538

3639
public createTab(disclaimerCardActive: boolean): MynahUIDataModel {
3740
const tabData: MynahUIDataModel = {

0 commit comments

Comments
 (0)