@@ -9,6 +9,7 @@ import { cn } from "@/lib/utils";
99import { TypeAnimation } from "react-type-animation" ;
1010import { DropdownMenu , DropdownMenuContent , DropdownMenuTrigger } from "@/components/ui/dropdown-menu" ;
1111import { prepareArg } from "../utils" ;
12+ import { NodeObject } from "react-force-graph-2d" ;
1213
1314type PathData = {
1415 nodes : any [ ]
@@ -84,6 +85,7 @@ interface Props {
8485 isPathResponse : boolean | undefined
8586 setIsPathResponse : ( isPathResponse : boolean | undefined ) => void
8687 setData : Dispatch < SetStateAction < GraphData > >
88+ chartRef : any
8789}
8890
8991const SUGGESTIONS = [
@@ -105,7 +107,7 @@ const RemoveLastPath = (messages: Message[]) => {
105107 return messages
106108}
107109
108- export function Chat ( { repo, path, setPath, graph, selectedPathId, isPathResponse, setIsPathResponse, setData } : Props ) {
110+ export function Chat ( { repo, path, setPath, graph, selectedPathId, isPathResponse, setIsPathResponse, setData, chartRef } : Props ) {
109111
110112 // Holds the messages in the chat
111113 const [ messages , setMessages ] = useState < Message [ ] > ( [ ] ) ;
@@ -131,8 +133,7 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
131133 const p = paths . find ( ( path ) => [ ...path . links , ...path . nodes ] . some ( ( e : any ) => e . id === selectedPathId ) )
132134
133135 if ( ! p ) return
134-
135- handelSetSelectedPath ( p )
136+ handleSetSelectedPath ( p )
136137 } , [ selectedPathId ] )
137138
138139 // Scroll to the bottom of the chat on new message
@@ -153,7 +154,10 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
153154 setPaths ( [ ] )
154155 } , [ isPathResponse ] )
155156
156- const handelSetSelectedPath = ( p : PathData ) => {
157+ const handleSetSelectedPath = ( p : PathData ) => {
158+ const chart = chartRef . current
159+
160+ if ( ! chart ) return
157161 setSelectedPath ( prev => {
158162 if ( prev ) {
159163 if ( isPathResponse && paths . some ( ( path ) => [ ...path . nodes , ...path . links ] . every ( ( e : any ) => [ ...prev . nodes , ...prev . links ] . some ( ( e : any ) => e . id === e . id ) ) ) ) {
@@ -206,6 +210,9 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
206210 } ) ;
207211 }
208212 setData ( { ...graph . Elements } )
213+ setTimeout ( ( ) => {
214+ chart . zoomToFit ( 1000 , 150 , ( n : NodeObject < Node > ) => p . nodes . some ( node => node . id === n . id ) ) ;
215+ } , 0 )
209216 }
210217
211218 // A function that handles the change event of the url input box
@@ -262,6 +269,10 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
262269 }
263270
264271 const handleSubmit = async ( ) => {
272+ const chart = chartRef . current
273+
274+ if ( ! chart ) return
275+
265276 setSelectedPath ( undefined )
266277
267278 if ( ! path ?. start ?. id || ! path . end ?. id ) return
@@ -297,6 +308,9 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
297308 setPath ( undefined )
298309 setIsPathResponse ( true )
299310 setData ( { ...graph . Elements } )
311+ setTimeout ( ( ) => {
312+ chart . zoomToFit ( 1000 , 150 , ( n : NodeObject < Node > ) => formattedPaths . some ( p => p . nodes . some ( node => node . id === n . id ) ) ) ;
313+ } , 0 )
300314 }
301315
302316 const getTip = ( disabled = false ) =>
@@ -425,7 +439,7 @@ export function Chat({ repo, path, setPath, graph, selectedPathId, isPathRespons
425439 setIsPathResponse ( undefined )
426440
427441 }
428- handelSetSelectedPath ( p )
442+ handleSetSelectedPath ( p )
429443 } }
430444 >
431445 < p className = "font-bold" > #{ i + 1 } </ p >
0 commit comments