@@ -116,8 +116,6 @@ And some sample html:
116116
117117const userMessages : any [ ] = [ ] ;
118118
119- let isResponseSent : boolean ;
120-
121119const _messageAuthorTemplate = ( { message } : ChatMessageRenderContext ) => {
122120 return message . sender !== 'user'
123121 ? html `
@@ -134,7 +132,7 @@ const _messageAuthorTemplate = ({ message }: ChatMessageRenderContext) => {
134132 : nothing ;
135133} ;
136134const _messageActionsTemplate = ( { message } : ChatMessageRenderContext ) => {
137- return message . sender !== 'user' && message . text . trim ( ) && isResponseSent
135+ return message . sender !== 'user' && message . text . trim ( )
138136 ? html `
139137 < div >
140138 < igc-icon-button
@@ -185,7 +183,7 @@ function handleCustomSendClick(chat: IgcChatComponent) {
185183 chat . draftMessage = { text : '' , attachments : [ ] } ;
186184}
187185
188- function handleMessageSend ( event : CustomEvent < IgcChatMessage > ) : void {
186+ async function handleMessageSend ( event : CustomEvent < IgcChatMessage > ) {
189187 const chat = event . target as IgcChatComponent ;
190188 const message = event . detail ;
191189
@@ -205,29 +203,31 @@ function handleMessageSend(event: CustomEvent<IgcChatMessage>): void {
205203 ]
206204 : [ ] ;
207205
208- isResponseSent = false ;
209- setTimeout ( async ( ) => {
210- chat . messages = [
211- ...chat . messages ,
212- { id : crypto . randomUUID ( ) , text : '' , sender : 'bot' , attachments } ,
213- ] ;
214-
215- await showResponse ( chat , generateAIResponse ( message . text ) . split ( ' ' ) ) ;
216-
217- messages = chat . messages ;
218- isResponseSent = true ;
219- chat . options = { ...chat . options , suggestions : [ ] , isTyping : false } ;
220- // TODO: add attachments (if any) to the response message
221- } , 1000 ) ;
222- }
206+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
207+
208+ chat . options = { ...chat . options , isTyping : false } ;
209+
210+ const responseText = generateAIResponse ( message . text ) . split ( ' ' ) ;
223211
224- async function showResponse ( chat : IgcChatComponent , responseParts : string [ ] ) {
225- const lastMessage = chat . messages [ chat . messages . length - 1 ] ;
212+ const aiResponse = {
213+ id : Date . now ( ) . toString ( ) ,
214+ sender : 'bot' ,
215+ text : '' ,
216+ attachments,
217+ } ;
218+
219+ chat . messages = [ ...chat . messages , aiResponse ] ;
226220
227- for ( const part of responseParts ) {
228- await new Promise ( ( resolve ) => requestAnimationFrame ( resolve ) ) ;
229- lastMessage . text = `${ lastMessage . text } ${ part } ` ;
230- chat . messages = [ ...chat . messages ] ;
221+ for ( const part of responseText ) {
222+ await new Promise ( ( resolve ) => setTimeout ( resolve , 30 ) ) ;
223+ const updated = [ ...chat . messages ] ;
224+ const currentMessage = updated [ updated . length - 1 ] ;
225+ const updatedMessage = {
226+ ...aiResponse ,
227+ text : `${ currentMessage . text } ${ part } ` ,
228+ } ;
229+ updated [ updated . length - 1 ] = updatedMessage ;
230+ chat . messages = updated ;
231231 }
232232}
233233
@@ -441,15 +441,6 @@ export const Basic: Story = {
441441 render : ( ) => {
442442 messages = initialMessages ;
443443 return html `
444- < style >
445- igc-chat ::part (typing-dot ) {
446- background : var (--igc-background , # f00 );
447- border : solid 1px var (--igc-background , # f00 );
448- border-radius : 8px ;
449- color : var (--igc-color , # 932424 );
450- width : 12px ;
451- }
452- </ style >
453444 < igc-chat
454445 style ="--igc-chat-height: calc(100vh - 32px); "
455446 .messages =${ messages }
0 commit comments