fix(chat): 修复 ChatPanel 无限重渲染导致的 Maximum update depth exceeded

- useTaskStore 选择器内调用 getCurrentTask() 每次返回新对象引用,
  改为分别选取 currentTaskId + tasks 后用 useMemo 派生
- chatHistory[taskId] || [] 在选择器内每次创建新空数组引用,
  改为选择器返回原始值,外部用 ?? [] 兜底

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
huangjianwu
2026-03-23 14:40:47 +08:00
parent efadbc267d
commit 3cd4c749c1

View File

@@ -49,11 +49,16 @@ export default function ChatPanel({ taskId }: ChatPanelProps) {
const [indexing, setIndexing] = useState(false)
const [indexed, setIndexed] = useState<boolean | null>(null)
const messages = useChatStore(state => state.chatHistory[taskId] || [])
const messages = useChatStore(state => state.chatHistory[taskId]) ?? []
const addMessage = useChatStore(state => state.addMessage)
const clearChat = useChatStore(state => state.clearChat)
const currentTask = useTaskStore(state => state.getCurrentTask())
const currentTaskId = useTaskStore(state => state.currentTaskId)
const tasks = useTaskStore(state => state.tasks)
const currentTask = useMemo(
() => tasks.find(t => t.id === currentTaskId) ?? null,
[tasks, currentTaskId],
)
// 检查索引状态
useEffect(() => {