@@ -92,46 +92,53 @@ const SUGGESTIONS = [
9292
9393const RemoveLastPath = ( messages : Message [ ] ) => {
9494 const index = messages . findIndex ( ( m ) => m . type === MessageTypes . Path )
95-
95+
9696 if ( index !== - 1 ) {
9797 messages = [ ...messages . slice ( 0 , index - 2 ) , ...messages . slice ( index + 1 ) ] ;
9898 messages = RemoveLastPath ( messages )
9999 }
100-
100+
101101 return messages
102102}
103103
104104export function Chat ( { repo, path, setPath, graph, chartRef, selectedPathId, isPath, setIsPath } : Props ) {
105-
105+
106106 // Holds the messages in the chat
107107 const [ messages , setMessages ] = useState < Message [ ] > ( [ ] ) ;
108-
108+
109109 // Holds the messages in the chat
110110 const [ paths , setPaths ] = useState < { nodes : any [ ] , edges : any [ ] } [ ] > ( [ ] ) ;
111-
111+
112112 const [ selectedPath , setSelectedPath ] = useState < { nodes : any [ ] , edges : any [ ] } > ( ) ;
113-
113+
114114 // Holds the user input while typing
115115 const [ query , setQuery ] = useState ( '' ) ;
116-
116+
117117 const [ isPathResponse , setIsPathResponse ] = useState ( false ) ;
118-
118+
119119 const [ tipOpen , setTipOpen ] = useState ( false ) ;
120-
120+
121121 const [ sugOpen , setSugOpen ] = useState ( false ) ;
122-
122+
123123 // A reference to the chat container to allow scrolling to the bottom
124124 const containerRef : React . RefObject < HTMLDivElement > = useRef ( null ) ;
125-
125+
126126 const isSendMessage = messages . some ( m => m . type === MessageTypes . Pending ) || ( messages . some ( m => m . text === "Please select a starting point and the end point. Select or press relevant item on the graph" ) && ! messages . some ( m => m . type === MessageTypes . Path ) )
127-
127+
128128 useEffect ( ( ) => {
129129 const p = paths . find ( ( path ) => [ ...path . edges , ...path . nodes ] . some ( ( e : any ) => e . id === selectedPathId ) )
130-
130+
131131 if ( ! p ) return
132-
132+
133133 handleSetSelectedPath ( p )
134134 } , [ selectedPathId ] )
135+
136+ // Scroll to the bottom of the chat on new message
137+ useEffect ( ( ) => {
138+ setTimeout ( ( ) => {
139+ containerRef . current ?. scrollTo ( 0 , containerRef . current ?. scrollHeight ) ;
140+ } , 300 )
141+ } , [ messages ] ) ;
135142
136143 useEffect ( ( ) => {
137144 handleSubmit ( )
@@ -261,13 +268,6 @@ export function Chat({ repo, path, setPath, graph, chartRef, selectedPathId, isP
261268
262269 }
263270
264- // Scroll to the bottom of the chat on new message
265- useEffect ( ( ) => {
266- setTimeout ( ( ) => {
267- containerRef . current ?. scrollTo ( 0 , containerRef . current ?. scrollHeight ) ;
268- } , 300 )
269- } , [ messages ] ) ;
270-
271271 const handleSubmit = async ( ) => {
272272 setSelectedPath ( undefined )
273273
@@ -329,7 +329,7 @@ export function Chat({ repo, path, setPath, graph, chartRef, selectedPathId, isP
329329 } ) ;
330330 elements . layout ( LAYOUT ) . run ( )
331331 setPaths ( formattedPaths )
332- setMessages ( prev => [ ...( prev . some ( m => m . type === MessageTypes . Path ) ? prev . slice ( 0 , - 2 ) : prev ) , { type : MessageTypes . PathResponse , paths : formattedPaths } ] )
332+ setMessages ( ( prev ) => [ ...RemoveLastPath ( prev ) , { type : MessageTypes . PathResponse , paths : formattedPaths } ] ) ;
333333 setPath ( undefined )
334334 setIsPathResponse ( true )
335335 }
0 commit comments