Skip to content

Commit 64f39e7

Browse files
authored
πŸ› fix(copilot): history popover not refreshing when agentId changes (#11731)
* πŸ› fix(copilot): sync chatStore activeAgentId when switching agent When user switches agent in Copilot toolbar, also update useChatStore's activeAgentId to keep both stores in sync. This ensures topic selectors and other chatStore-dependent features work correctly. * πŸ’„ style(copilot): show loading state for history button when switching agent - Show loading/disabled state while topics are being fetched - Only hide the button when confirmed there are no topics - Improves UX by avoiding sudden button disappearance during agent switch
1 parent 5de4742 commit 64f39e7

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

β€Žsrc/features/PageEditor/Copilot/Toolbar.tsxβ€Ž

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ActionIcon, Block, Flexbox, Popover } from '@lobehub/ui';
22
import { createStaticStyles, cx } from 'antd-style';
33
import { ChevronsUpDownIcon, Clock3Icon, PanelRightCloseIcon, PlusIcon } from 'lucide-react';
4-
import { Suspense, memo, useMemo, useState } from 'react';
4+
import { Suspense, memo, useCallback, useMemo, useState } from 'react';
55
import { useTranslation } from 'react-i18next';
66

77
import AgentAvatar from '@/app/[variants]/(main)/home/_layout/Body/Agent/List/AgentItem/Avatar';
@@ -164,6 +164,15 @@ const CopilotToolbar = memo<CopilotToolbarProps>(({ agentId, isHovered }) => {
164164
const setActiveAgentId = useAgentStore((s) => s.setActiveAgentId);
165165
const [topicPopoverOpen, setTopicPopoverOpen] = useState(false);
166166

167+
const handleAgentChange = useCallback(
168+
(id: string) => {
169+
setActiveAgentId(id);
170+
// Sync chatStore's activeAgentId to ensure topic selectors work correctly
171+
useChatStore.setState({ activeAgentId: id });
172+
},
173+
[setActiveAgentId],
174+
);
175+
167176
// Fetch topics for the agent builder
168177
useChatStore((s) => s.useFetchTopics)(true, { agentId });
169178

@@ -175,13 +184,15 @@ const CopilotToolbar = memo<CopilotToolbarProps>(({ agentId, isHovered }) => {
175184

176185
const [toggleRightPanel] = useGlobalStore((s) => [s.toggleRightPanel]);
177186

178-
const hideHistory = !topics || topics.length === 0;
187+
// topics === undefined means still loading, topics.length === 0 means confirmed empty
188+
const isLoadingTopics = topics === undefined;
189+
const hideHistory = !isLoadingTopics && topics.length === 0;
179190

180191
return (
181192
<NavHeader
182193
left={
183194
<Flexbox align="center" gap={8} horizontal>
184-
<AgentSelector agentId={agentId} onAgentChange={setActiveAgentId} />
195+
<AgentSelector agentId={agentId} onAgentChange={handleAgentChange} />
185196
</Flexbox>
186197
}
187198
right={
@@ -218,7 +229,7 @@ const CopilotToolbar = memo<CopilotToolbarProps>(({ agentId, isHovered }) => {
218229
</Flexbox>
219230
}
220231
onOpenChange={setTopicPopoverOpen}
221-
open={topicPopoverOpen}
232+
open={isLoadingTopics ? false : topicPopoverOpen}
222233
placement="bottomRight"
223234
styles={{
224235
content: {
@@ -228,7 +239,12 @@ const CopilotToolbar = memo<CopilotToolbarProps>(({ agentId, isHovered }) => {
228239
}}
229240
trigger="click"
230241
>
231-
<ActionIcon icon={Clock3Icon} size={DESKTOP_HEADER_ICON_SIZE} />
242+
<ActionIcon
243+
disabled={isLoadingTopics}
244+
icon={Clock3Icon}
245+
loading={isLoadingTopics}
246+
size={DESKTOP_HEADER_ICON_SIZE}
247+
/>
232248
</Popover>
233249
)}
234250
</div>

0 commit comments

Comments
Β (0)