@@ -164,10 +164,10 @@ const ChatAI = memo(
164164 isMCPActive ,
165165 changeInput ,
166166 websocketSessionId ,
167- showChatHistory ,
167+ showChatHistory
168168 ) ;
169169
170- const { dealMsg, messageTimeoutRef } = useMessageHandler (
170+ const { dealMsg } = useMessageHandler (
171171 curIdRef ,
172172 setCurChatEnd ,
173173 setTimedoutShow ,
@@ -184,7 +184,7 @@ const ChatAI = memo(
184184 } , [ dealMsg , updateDealMsg ] ) ;
185185
186186 const clearChat = useCallback ( ( ) => {
187- console . log ( "clearChat" ) ;
187+ // console.log("clearChat");
188188 setTimedoutShow ( false ) ;
189189 chatClose ( activeChat ) ;
190190 setActiveChat ( undefined ) ;
@@ -193,15 +193,12 @@ const ChatAI = memo(
193193 } , [
194194 activeChat ,
195195 chatClose ,
196- clearChatPage ,
197- setCurChatEnd ,
198- setTimedoutShow ,
199196 ] ) ;
200197
201198 const init = useCallback (
202199 async ( value : string ) => {
203200 try {
204- console . log ( "init" , isLogin , curChatEnd , activeChat ?. _id ) ;
201+ // console.log("init", isLogin, curChatEnd, activeChat?._id);
205202 if ( ! isLogin ) {
206203 addError ( "Please login to continue chatting" ) ;
207204 return ;
@@ -222,7 +219,7 @@ const ChatAI = memo(
222219 [
223220 isLogin ,
224221 curChatEnd ,
225- activeChat ,
222+ activeChat ?. _id ,
226223 createNewChat ,
227224 handleSendMessage ,
228225 websocketSessionId ,
@@ -234,21 +231,6 @@ const ChatAI = memo(
234231 createChatWindow ( createWin ) ;
235232 } , [ createChatWindow , createWin ] ) ;
236233
237- useEffect ( ( ) => {
238- setCurChatEnd ( true ) ;
239- return ( ) => {
240- if ( messageTimeoutRef . current ) {
241- clearTimeout ( messageTimeoutRef . current ) ;
242- }
243- Promise . resolve ( ) . then ( ( ) => {
244- chatClose ( activeChat ) ;
245- setActiveChat ( undefined ) ;
246- setCurChatEnd ( true ) ;
247- disconnectWS ( ) ;
248- } ) ;
249- } ;
250- } , [ chatClose , setCurChatEnd ] ) ;
251-
252234 const onSelectChat = useCallback (
253235 async ( chat : Chat ) => {
254236 setTimedoutShow ( false ) ;
@@ -261,7 +243,6 @@ const ChatAI = memo(
261243 }
262244 } ,
263245 [
264- clearAllChunkData ,
265246 cancelChat ,
266247 activeChat ,
267248 chatClose ,
@@ -274,19 +255,21 @@ const ChatAI = memo(
274255 ( chatId : string ) => {
275256 handleDelete ( chatId ) ;
276257
277- setChats ( ( prev ) => prev . filter ( ( chat ) => chat . _id !== chatId ) ) ;
258+ setChats ( ( prev ) => {
259+ const updatedChats = prev . filter ( ( chat ) => chat . _id !== chatId ) ;
278260
279- if ( activeChat ?. _id === chatId ) {
280- const remainingChats = chats . filter ( ( chat ) => chat . _id !== chatId ) ;
281-
282- if ( remainingChats . length > 0 ) {
283- setActiveChat ( remainingChats [ 0 ] ) ;
284- } else {
285- init ( "" ) ;
261+ if ( activeChat ?. _id === chatId ) {
262+ if ( updatedChats . length > 0 ) {
263+ setActiveChat ( updatedChats [ 0 ] ) ;
264+ } else {
265+ init ( "" ) ;
266+ }
286267 }
287- }
268+
269+ return updatedChats ;
270+ } ) ;
288271 } ,
289- [ activeChat , chats , init , setActiveChat ]
272+ [ activeChat ?. _id , handleDelete , init ]
290273 ) ;
291274
292275 const handleOutsideClick = useCallback ( ( e : MouseEvent ) => {
@@ -317,38 +300,33 @@ const ChatAI = memo(
317300 ! isSidebarOpenChat && getChatHistory ( ) ;
318301 } , [ isSidebarOpenChat , setIsSidebarOpen , getChatHistory ] ) ;
319302
320- const renameChat = ( chatId : string , title : string ) => {
321- setChats ( ( prev ) => {
322- const updatedChats = prev . map ( ( item ) => {
323- if ( item . _id !== chatId ) return item ;
303+ const renameChat = useCallback (
304+ ( chatId : string , title : string ) => {
305+ setChats ( ( prev ) => {
306+ const chatIndex = prev . findIndex ( ( chat ) => chat . _id === chatId ) ;
307+ if ( chatIndex === - 1 ) return prev ;
324308
325- return { ...item , _source : { ...item . _source , title } } ;
326- } ) ;
309+ const modifiedChat = {
310+ ...prev [ chatIndex ] ,
311+ _source : { ...prev [ chatIndex ] . _source , title } ,
312+ } ;
327313
328- const modifiedChat = updatedChats . find ( ( item ) => {
329- return item . _id === chatId ;
314+ const result = [ ...prev ] ;
315+ result . splice ( chatIndex , 1 ) ;
316+ return [ modifiedChat , ...result ] ;
330317 } ) ;
331318
332- if ( ! modifiedChat ) {
333- return updatedChats ;
319+ if ( activeChat ?. _id === chatId ) {
320+ setActiveChat ( ( prev ) => {
321+ if ( ! prev ) return prev ;
322+ return { ...prev , _source : { ...prev . _source , title } } ;
323+ } ) ;
334324 }
335325
336- return [
337- modifiedChat ,
338- ...updatedChats . filter ( ( item ) => item . _id !== chatId ) ,
339- ] ;
340- } ) ;
341-
342- if ( activeChat ?. _id === chatId ) {
343- setActiveChat ( ( prev ) => {
344- if ( ! prev ) return prev ;
345-
346- return { ...prev , _source : { ...prev . _source , title } } ;
347- } ) ;
348- }
349-
350- handleRename ( chatId , title ) ;
351- } ;
326+ handleRename ( chatId , title ) ;
327+ } ,
328+ [ activeChat ?. _id , handleRename ]
329+ ) ;
352330
353331 return (
354332 < div
0 commit comments