11import React , { useEffect , useRef , useState } from 'react' ;
22import { Button , Widget , Typography , Avatar , TextInput , IconButton , Modal , useCopyToClipboard } from '@neo4j-ndl/react' ;
3- import { InformationCircleIconOutline , XMarkIconOutline , ClipboardDocumentIconOutline , SpeakerWaveIconOutline , SpeakerXMarkIconOutline } from '@neo4j-ndl/react/icons' ;
3+ import {
4+ InformationCircleIconOutline ,
5+ XMarkIconOutline ,
6+ ClipboardDocumentIconOutline ,
7+ SpeakerWaveIconOutline ,
8+ SpeakerXMarkIconOutline ,
9+ } from '@neo4j-ndl/react/icons' ;
410import ChatBotAvatar from '../assets/images/chatbot-ai.png' ;
511import { ChatbotProps , Source , UserCredentials } from '../types' ;
612import { useCredentials } from '../context/UserCredentials' ;
@@ -33,9 +39,7 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
3339 const [ value , copy ] = useCopyToClipboard ( ) ;
3440 const { speak, cancel, supported } = useSpeechSynthesis ( {
3541 onEnd : ( ) => {
36- setListMessages ( ( msgs ) =>
37- msgs . map ( ( msg ) => ( { ...msg , speaking : false } ) )
38- ) ;
42+ setListMessages ( ( msgs ) => msgs . map ( ( msg ) => ( { ...msg , speaking : false } ) ) ) ;
3943 } ,
4044 } ) ;
4145
@@ -58,7 +62,7 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
5862 total_tokens ?: number ;
5963 response_time ?: number ;
6064 speaking ?: boolean ;
61- copying ?: boolean
65+ copying ?: boolean ;
6266 } ,
6367 index = 0
6468 ) => {
@@ -201,24 +205,16 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
201205
202206 const handleCancel = ( id : number ) => {
203207 cancel ( ) ;
204- setListMessages ( ( msgs ) =>
205- msgs . map ( ( msg ) =>
206- ( msg . id
207- === id ? { ...msg , speaking : false } : msg )
208- )
209- ) ;
210- }
208+ setListMessages ( ( msgs ) => msgs . map ( ( msg ) => ( msg . id === id ? { ...msg , speaking : false } : msg ) ) ) ;
209+ } ;
211210
212211 const handleSpeak = ( chatMessage : any , id : number ) => {
213212 speak ( { text : chatMessage } ) ;
214213 setListMessages ( ( msgs ) => {
215- const messageWithSpeaking = msgs . find ( msg => msg . speaking ) ;
216- return msgs . map ( ( msg ) =>
217- ( msg . id
218- === id && ! messageWithSpeaking ? { ...msg , speaking : true } : msg )
219- ) ;
214+ const messageWithSpeaking = msgs . find ( ( msg ) => msg . speaking ) ;
215+ return msgs . map ( ( msg ) => ( msg . id === id && ! messageWithSpeaking ? { ...msg , speaking : true } : msg ) ) ;
220216 } ) ;
221- }
217+ } ;
222218
223219 return (
224220 < div className = 'n-bg-palette-neutral-bg-weak flex flex-col justify-between min-h-full max-h-full overflow-hidden' >
@@ -261,14 +257,16 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
261257 < Widget
262258 header = ''
263259 isElevated = { true }
264- className = { `p-4 self-start ${ isFullScreen ? 'max-w-[55%]' : '' } ${ chat . user === 'chatbot' ? 'n-bg-palette-neutral-bg-strong' : 'n-bg-palette-primary-bg-weak'
265- } `}
260+ className = { `p-4 self-start ${ isFullScreen ? 'max-w-[55%]' : '' } ${
261+ chat . user === 'chatbot' ? 'n-bg-palette-neutral-bg-strong' : 'n-bg-palette-primary-bg-weak'
262+ } `}
266263 >
267264 < div
268- className = { `${ listMessages [ index ] . isLoading && index === listMessages . length - 1 && chat . user == 'chatbot'
269- ? 'loader'
270- : ''
271- } `}
265+ className = { `${
266+ listMessages [ index ] . isLoading && index === listMessages . length - 1 && chat . user == 'chatbot'
267+ ? 'loader'
268+ : ''
269+ } `}
272270 >
273271 < ReactMarkdown > { chat . message } </ ReactMarkdown >
274272 </ div >
@@ -308,19 +306,23 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
308306 < ClipboardDocumentIconOutline className = 'w-4 h-4 inline-block' />
309307 </ IconButtonWithToolTip >
310308 { copyMessageId === chat . id && (
311- < > < span className = 'pt-4 text-xs' > Copied!</ span >
312- < span style = { { display :'none' } } > { value } </ span > </ >
309+ < >
310+ < span className = 'pt-4 text-xs' > Copied!</ span >
311+ < span style = { { display : 'none' } } > { value } </ span >
312+ </ >
313313 ) }
314- { supported && chat . speaking ? < IconButtonWithToolTip
315- placement = 'top'
316- label = 'text to speak'
317- clean
318- onClick = { ( ) => handleCancel ( chat . id ) }
319- text = { chat . speaking ? tooltips . stopSpeaking : tooltips . textTospeech }
320- disabled = { chat . isTyping || chat . isLoading }
321- >
322- < SpeakerXMarkIconOutline className = "w-4 h-4 inline-block" />
323- </ IconButtonWithToolTip > :
314+ { supported && chat . speaking ? (
315+ < IconButtonWithToolTip
316+ placement = 'top'
317+ label = 'text to speak'
318+ clean
319+ onClick = { ( ) => handleCancel ( chat . id ) }
320+ text = { chat . speaking ? tooltips . stopSpeaking : tooltips . textTospeech }
321+ disabled = { chat . isTyping || chat . isLoading }
322+ >
323+ < SpeakerXMarkIconOutline className = 'w-4 h-4 inline-block' />
324+ </ IconButtonWithToolTip >
325+ ) : (
324326 < IconButtonWithToolTip
325327 placement = 'top'
326328 clean
@@ -329,18 +331,18 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
329331 disabled = { chat . isTyping || chat . isLoading }
330332 label = 'speech'
331333 >
332- < SpeakerWaveIconOutline className = " w-4 h-4 inline-block" />
334+ < SpeakerWaveIconOutline className = ' w-4 h-4 inline-block' />
333335 </ IconButtonWithToolTip >
334- }
336+ ) }
335337 </ div >
336338 ) }
337339 </ div >
338340 </ Widget >
339341 </ div >
340342 ) ) }
341343 </ div >
342- </ Widget >
343- </ div >
344+ </ Widget >
345+ </ div >
344346 < div className = 'n-bg-palette-neutral-bg-weak flex gap-2.5 bottom-0 p-2.5 w-full' >
345347 < form onSubmit = { handleSubmit } className = 'flex gap-2.5 w-full' >
346348 < TextInput
@@ -383,7 +385,7 @@ const Chatbot: React.FC<ChatbotProps> = (props) => {
383385 total_tokens = { tokensUsed }
384386 />
385387 </ Modal >
386- </ div >
388+ </ div >
387389 ) ;
388390} ;
389391
0 commit comments