@@ -15,6 +15,8 @@ import { useEventBus } from '@/hooks/eventBus'
1515import type { VSCodeEvent } from '@bus/callbacks'
1616import { URI } from 'vscode-uri'
1717import type { Model } from '@/api/client'
18+ import { useRpc } from '@/utils/rpc'
19+ import { isErr } from '@bus/result'
1820
1921export function LineagePage ( ) {
2022 const { emit } = useEventBus ( )
@@ -66,49 +68,96 @@ export function LineagePage() {
6668}
6769
6870function Lineage ( ) {
69- const [ selectedModelSet , setSelectedModelSet ] = useState < string | undefined > (
71+ const [ selectedModel , setSelectedModel ] = useState < string | undefined > (
7072 undefined ,
7173 )
7274 const { on } = useEventBus ( )
7375 const queryClient = useQueryClient ( )
7476
7577 const { data : models , isLoading : isLoadingModels } = useApiModels ( )
78+ const rpc = useRpc ( )
79+
7680 React . useEffect ( ( ) => {
77- if ( selectedModelSet === undefined && models && Array . isArray ( models ) ) {
78- setSelectedModelSet ( models [ 0 ] . name )
81+ const fetchFirstModelIfNotSet = async (
82+ models : Model [ ] ,
83+ ) : Promise < string | undefined > => {
84+ console . log ( 'fetchFirstModelIfNotSet' , models )
85+ if ( ! Array . isArray ( models ) ) {
86+ return undefined
87+ }
88+ const activeFile = await rpc ( 'get_active_file' , { } )
89+ console . log ( 'activeFileResponse' , activeFile )
90+ // @ts -ignore
91+ if ( ! activeFile . fileUri ) {
92+ return models [ 0 ] . name
93+ }
94+ // @ts -ignore
95+ const fileUri : string = activeFile . fileUri
96+ const filePath = URI . parse ( fileUri ) . fsPath
97+ const model = models . find ( ( m : Model ) => m . full_path === filePath )
98+ if ( model ) {
99+ return model . name
100+ }
101+ return undefined
102+ }
103+ if ( selectedModel === undefined && Array . isArray ( models ) ) {
104+ fetchFirstModelIfNotSet ( models ) . then ( modelName => {
105+ if ( modelName && selectedModel === undefined ) {
106+ setSelectedModel ( modelName )
107+ }
108+ } )
109+ }
110+ } , [ models , selectedModel ] )
111+
112+ const modelsRecord =
113+ Array . isArray ( models ) &&
114+ models . reduce (
115+ ( acc , model ) => {
116+ acc [ model . name ] = model
117+ return acc
118+ } ,
119+ { } as Record < string , Model > ,
120+ )
121+
122+ React . useEffect ( ( ) => {
123+ const handleChangeFocusedFile = ( fileUri : { fileUri : string } ) => {
124+ const full_path = URI . parse ( fileUri . fileUri ) . fsPath
125+ const model = Object . values ( modelsRecord ) . find (
126+ m => m . full_path === full_path ,
127+ )
128+ if ( model ) {
129+ setSelectedModel ( model . name )
130+ }
79131 }
80- } , [ models , selectedModelSet ] )
132+
133+ const handleSavedFile = ( ) => {
134+ queryClient . invalidateQueries ( )
135+ }
136+
137+ const offChangeFocusedFile = on (
138+ 'changeFocusedFile' ,
139+ handleChangeFocusedFile ,
140+ )
141+ const offSavedFile = on ( 'savedFile' , handleSavedFile )
142+
143+ // If your event bus returns an "off" function, call it on cleanup
144+ return ( ) => {
145+ if ( offChangeFocusedFile ) offChangeFocusedFile ( )
146+ if ( offSavedFile ) offSavedFile ( )
147+ }
148+ } , [ on , queryClient , modelsRecord ] )
81149
82150 if (
83151 isLoadingModels ||
84152 models === undefined ||
85- selectedModelSet === undefined
153+ modelsRecord === false ||
154+ selectedModel === undefined
86155 ) {
87156 return < div > Loading models...</ div >
88157 }
89158 if ( ! Array . isArray ( models ) ) {
90159 return < div > Error: Models data is not in the expected format</ div >
91160 }
92- const modelsRecord = models . reduce (
93- ( acc , model ) => {
94- acc [ model . name ] = model
95- return acc
96- } ,
97- { } as Record < string , Model > ,
98- )
99- const selectedModel = selectedModelSet
100- on ( 'changeFocusedFile' , fileUri => {
101- const full_path = URI . parse ( fileUri . fileUri ) . fsPath
102- const model = Object . values ( modelsRecord ) . find (
103- m => m . full_path === full_path ,
104- )
105- if ( model ) {
106- setSelectedModelSet ( model . name )
107- }
108- } )
109- on ( 'savedFile' , ( ) => {
110- queryClient . invalidateQueries ( )
111- } )
112161
113162 return (
114163 < LineageComponentFromWeb
0 commit comments