1- import { useEffect , useMemo , useState } from 'react' ;
1+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
22import { CallbackGeneratedChunk , useAppContext } from '../utils/app.context' ;
33import ChatMessage from './ChatMessage' ;
44import { CanvasType , Message , PendingMessage } from '../utils/types' ;
@@ -103,9 +103,7 @@ export default function ChatScreen() {
103103
104104 const textarea : ChatTextareaApi = useChatTextarea ( prefilledMsg . content ( ) ) ;
105105
106- const { extraContext, clearExtraContext } = useVSCodeContext ( textarea ) ;
107- // TODO: improve this when we have "upload file" feature
108- const currExtra : Message [ 'extra' ] = extraContext ? [ extraContext ] : undefined ;
106+
109107
110108 // keep track of leaf node for rendering
111109 const [ currNodeId , setCurrNodeId ] = useState < number > ( - 1 ) ;
@@ -132,7 +130,7 @@ export default function ChatScreen() {
132130 scrollToBottom ( true ) ;
133131 } ;
134132
135- const sendNewMessage = async ( ) => {
133+ const sendNewMessage = useCallback ( async ( ) => {
136134 const lastInpMsg = textarea . value ( ) ;
137135 if ( lastInpMsg . trim ( ) . length === 0 || isGenerating ( currConvId ?? '' ) )
138136 return ;
@@ -141,6 +139,8 @@ export default function ChatScreen() {
141139 setCurrNodeId ( - 1 ) ;
142140 // get the last message node
143141 const lastMsgNodeId = messages . at ( - 1 ) ?. msg . id ?? null ;
142+ // TODO: improve this when we have "upload file" feature
143+ const currExtra = extraContextRef . current ? [ extraContextRef . current ] : undefined ;
144144 if (
145145 ! ( await sendMessage (
146146 currConvId ,
@@ -155,7 +155,14 @@ export default function ChatScreen() {
155155 }
156156 // OK
157157 clearExtraContext ( ) ;
158- } ;
158+ } , [ textarea , currConvId , isGenerating , messages , sendMessage , onChunk ] ) ;
159+
160+ const { extraContext, clearExtraContext } = useVSCodeContext ( textarea , sendNewMessage ) ;
161+ const extraContextRef = useRef ( extraContext ) ;
162+
163+ useEffect ( ( ) => {
164+ extraContextRef . current = extraContext ;
165+ } , [ extraContext ] ) ;
159166
160167 const handleEditMessage = async ( msg : Message , content : string ) => {
161168 if ( ! viewingChat ) return ;
0 commit comments