Skip to content

Commit cc855cb

Browse files
authored
fix: disable new chat button if request in progress (#58)
1 parent 7a7bc17 commit cc855cb

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/core/ChatWidget.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,8 @@ export class ChatWidgetCore {
629629
);
630630
if (newChatButton) {
631631
newChatButton.addEventListener('click', () => this.startNewChat());
632+
// Set initial disabled state
633+
this.updateNewChatButtonState();
632634
}
633635
}
634636

@@ -902,6 +904,7 @@ export class ChatWidgetCore {
902904
}
903905
this.abortController = new AbortController();
904906
this.historyPanel.updateDisabledState();
907+
this.updateNewChatButtonState();
905908

906909
try {
907910
// Create FormData for the request
@@ -1025,6 +1028,7 @@ export class ChatWidgetCore {
10251028
// Reset controller once streaming is done
10261029
this.abortController = null;
10271030
this.historyPanel.updateDisabledState();
1031+
this.updateNewChatButtonState();
10281032
break;
10291033
}
10301034

@@ -1050,15 +1054,18 @@ export class ChatWidgetCore {
10501054
}
10511055
this.abortController = null;
10521056
this.historyPanel.updateDisabledState();
1057+
this.updateNewChatButtonState();
10531058
}
10541059
} catch (error) {
10551060
console.error('Error sending message to API:', error);
10561061
if (this.abortController?.signal.aborted) {
10571062
this.abortController = null;
10581063
this.historyPanel.updateDisabledState();
1064+
this.updateNewChatButtonState();
10591065
} else {
10601066
this.abortController = null;
10611067
this.historyPanel.updateDisabledState();
1068+
this.updateNewChatButtonState();
10621069
// Add an error message to the chat
10631070
this.addBotMessage(
10641071
'Sorry, there was an error processing your message. Please try again later.'
@@ -1701,4 +1708,13 @@ export class ChatWidgetCore {
17011708
}
17021709
});
17031710
}
1711+
1712+
private updateNewChatButtonState(): void {
1713+
const newChatButton = this.widgetElement?.querySelector<HTMLButtonElement>(
1714+
'.chat-widget-new-chat'
1715+
);
1716+
if (newChatButton) {
1717+
newChatButton.disabled = !!this.abortController;
1718+
}
1719+
}
17041720
}

src/core/styles.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@
184184
margin-right: 6px;
185185
}
186186

187+
.chat-widget-new-chat:disabled {
188+
opacity: 0.5;
189+
cursor: not-allowed;
190+
color: #999;
191+
}
192+
193+
.chat-widget-new-chat:disabled:hover {
194+
background-color: transparent;
195+
}
196+
187197
.chat-widget-close {
188198
background: none;
189199
border: none;

0 commit comments

Comments
 (0)