1- import React , { useState , useMemo , useRef , forwardRef , useImperativeHandle } from 'react'
1+ import React , { useState , useMemo , useRef , forwardRef , useImperativeHandle , useCallback } from 'react'
22import { usePagesStore } from '../../../stores/pagesStore'
33import { useTabsStore } from '../../../stores/tabsStore'
44import { useUIStore } from '../../../stores/uiStore'
@@ -63,36 +63,36 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
6363 } , [ chat ?. messages ] )
6464
6565 // 处理分支切换(所有消息都使用兄弟分支切换)
66- const handleSwitchBranch = ( messageId : string , branchIndex : number ) => {
66+ const handleSwitchBranch = useCallback ( ( messageId : string , branchIndex : number ) => {
6767 const newPath = messageTree . switchToSiblingBranch ( messageId , branchIndex )
6868 updateCurrentPath ( chatId , newPath )
69- }
69+ } , [ messageTree , updateCurrentPath , chatId ] )
7070
71- const handleToggleMessageCollapse = ( messageId : string ) => {
71+ const handleToggleMessageCollapse = useCallback ( ( messageId : string ) => {
7272 toggleMessageCollapse ( chatId , messageId )
73- }
73+ } , [ toggleMessageCollapse , chatId ] )
7474
75- const handleCollapseAll = ( ) => {
75+ const handleCollapseAll = useCallback ( ( ) => {
7676 collapseAllMessages (
7777 chatId ,
7878 chat . messages . map ( ( msg ) => msg . id )
7979 )
80- }
80+ } , [ collapseAllMessages , chatId , chat ?. messages ] )
8181
82- const handleExpandAll = ( ) => {
82+ const handleExpandAll = useCallback ( ( ) => {
8383 expandAllMessages ( chatId )
84- }
84+ } , [ expandAllMessages , chatId ] )
8585
86- const handleOpenSettings = ( ) => {
86+ const handleOpenSettings = useCallback ( ( ) => {
8787 const settingsPageId = createAndOpenSettingsPage ( 'llm' ) // 从聊天窗口点击设置通常是想配置模型
8888 setActiveTab ( settingsPageId )
89- }
89+ } , [ createAndOpenSettingsPage , setActiveTab ] )
9090
91- const handleToggleMessageTree = ( ) => {
91+ const handleToggleMessageTree = useCallback ( ( ) => {
9292 setMessageTreeCollapsed ( ! messageTreeCollapsed )
93- }
93+ } , [ messageTreeCollapsed ] )
9494
95- const handleMessageTreeNodeSelect = ( messageId : string ) => {
95+ const handleMessageTreeNodeSelect = useCallback ( ( messageId : string ) => {
9696 // 构建到选中消息的路径
9797 const path : string [ ] = [ ]
9898 let currentMsg = chat ?. messages . find ( ( msg ) => msg . id === messageId )
@@ -108,19 +108,19 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
108108
109109 // 更新当前路径
110110 updateCurrentPath ( chatId , path )
111- }
111+ } , [ chat ?. messages , updateCurrentPath , chatId ] )
112112
113- const handleMessageTreePathChange = ( path : string [ ] ) => {
113+ const handleMessageTreePathChange = useCallback ( ( path : string [ ] ) => {
114114 // 更新当前路径
115115 updateCurrentPath ( chatId , path )
116- }
116+ } , [ updateCurrentPath , chatId ] )
117117
118- const handleMessageTreeWidthChange = ( width : number ) => {
118+ const handleMessageTreeWidthChange = useCallback ( ( width : number ) => {
119119 setMessageTreeWidth ( width )
120120 localStorage . setItem ( 'messageTreeWidth' , width . toString ( ) )
121- }
121+ } , [ ] )
122122
123- const handleAutoQuestionChange = ( enabled : boolean , mode : 'ai' | 'preset' , listId ?: string ) => {
123+ const handleAutoQuestionChange = useCallback ( ( enabled : boolean , mode : 'ai' | 'preset' , listId ?: string ) => {
124124 console . log ( 'ChatWindow handleAutoQuestionChange:' , {
125125 enabled,
126126 mode,
@@ -140,7 +140,7 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
140140 setAutoQuestionListId ( defaultListId )
141141 console . log ( 'ChatWindow 自动设置 defaultListId:' , defaultListId )
142142 }
143- }
143+ } , [ settings . promptLists , settings . defaultPromptListId ] )
144144
145145 if ( ! chat ) {
146146 return < div className = "chat-window-error" > 聊天不存在</ div >
@@ -238,7 +238,7 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
238238 ref = { chatInputRef }
239239 value = { inputValue }
240240 onChange = { setInputValue }
241- onSend = { async ( ) => {
241+ onSend = { useCallback ( async ( ) => {
242242 if ( inputValue . trim ( ) ) {
243243 setInputValue ( '' ) // 立即清空输入框
244244 await onSendMessage ( inputValue )
@@ -247,7 +247,7 @@ const ChatWindow = forwardRef<ChatWindowRef, ChatWindowProps>(({ chatId }, ref)
247247 chatInputRef . current ?. focus ( )
248248 } , 0 )
249249 }
250- } }
250+ } , [ inputValue , onSendMessage ] ) }
251251 onStop = { onStopGeneration }
252252 disabled = { isLoading }
253253 loading = { isLoading }
0 commit comments