🐛 fix(chat): reset activeTopicId when switching agent/group (#11523)

When switching agents via dropdown menu, the activeTopicId was not being
reset, causing messages to be saved to the wrong topic bucket. This fix
adds useEffect hooks to AgentIdSync and GroupIdSync components to detect
agent/group changes and synchronously reset activeTopicId.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Arvin Xu
2026-01-15 23:32:39 +08:00
committed by GitHub
parent b9341c3183
commit fde54b064e
2 changed files with 24 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import { useUnmount } from 'ahooks';
import { usePrevious, useUnmount } from 'ahooks';
import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { createStoreUpdater } from 'zustand-utils';
@@ -9,10 +10,20 @@ const AgentIdSync = () => {
const useStoreUpdater = createStoreUpdater(useAgentStore);
const useChatStoreUpdater = createStoreUpdater(useChatStore);
const params = useParams<{ aid?: string }>();
const prevAgentId = usePrevious(params.aid);
useStoreUpdater('activeAgentId', params.aid);
useChatStoreUpdater('activeAgentId', params.aid ?? '');
// Reset activeTopicId when switching to a different agent
// This prevents messages from being saved to the wrong topic bucket
useEffect(() => {
// Only reset topic when switching between agents (not on initial mount)
if (prevAgentId !== undefined && prevAgentId !== params.aid) {
useChatStore.getState().switchTopic(null, { skipRefreshMessage: true });
}
}, [params.aid, prevAgentId]);
// Clear activeAgentId when unmounting (leaving chat page)
useUnmount(() => {
useAgentStore.setState({ activeAgentId: undefined });

View File

@@ -1,4 +1,5 @@
import { useUnmount } from 'ahooks';
import { usePrevious, useUnmount } from 'ahooks';
import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { createStoreUpdater } from 'zustand-utils';
@@ -10,6 +11,7 @@ const GroupIdSync = () => {
const useAgentGroupStoreUpdater = createStoreUpdater(useAgentGroupStore);
const useChatStoreUpdater = createStoreUpdater(useChatStore);
const params = useParams<{ gid?: string }>();
const prevGroupId = usePrevious(params.gid);
const router = useQueryRoute();
// Sync groupId to agentGroupStore and chatStore
@@ -19,6 +21,15 @@ const GroupIdSync = () => {
// Inject router to agentGroupStore for navigation
useAgentGroupStoreUpdater('router', router);
// Reset activeTopicId when switching to a different group
// This prevents messages from being saved to the wrong topic bucket
useEffect(() => {
// Only reset topic when switching between groups (not on initial mount)
if (prevGroupId !== undefined && prevGroupId !== params.gid) {
useChatStore.getState().switchTopic(null, { skipRefreshMessage: true });
}
}, [params.gid, prevGroupId]);
// Clear activeGroupId when unmounting (leaving group page)
useUnmount(() => {
useAgentGroupStore.setState({ activeGroupId: undefined, router: undefined });