From 76f9246497ba55e02bbf82d973dba8c047fa332a Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:29:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=B2=20fix:=20Close=20the=20Mobile=20Dr?= =?UTF-8?q?awer=20When=20Selecting=20an=20Agent=20(#17696)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Close the Mobile Drawer When Selecting an Agent Select Agent starts a chat from the agent builder, which on the narrow layout sits in the drawer. Like picking a conversation, it now closes the drawer through the shared slide-first toggle and starts the chat after the slide; the desktop rail is unchanged. * refactor: Reveal the Agent Chat Through the Agent Panel Host The agent form no longer reads or writes the sidebar itself. AgentPanelProvider, the host of the agent panels, supplies revealChat from a small useRevealChat hook: in drawer mode it closes the drawer through the slide-first toggle and starts the chat on the slide, and on the wide layout it starts it at once. The provider reads only the drawer viewport, not the sidebar's normalizing state. --- client/src/Providers/AgentPanelContext.tsx | 3 ++ client/src/common/types.ts | 2 + .../SidePanel/Agents/AgentPanel.test.tsx | 47 ++++++++++++++++++- .../SidePanel/Agents/AgentPanel.tsx | 7 ++- .../hooks/Nav/__tests__/useRevealChat.spec.ts | 44 +++++++++++++++++ client/src/hooks/Nav/useRevealChat.ts | 23 +++++++++ 6 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 client/src/hooks/Nav/__tests__/useRevealChat.spec.ts create mode 100644 client/src/hooks/Nav/useRevealChat.ts diff --git a/client/src/Providers/AgentPanelContext.tsx b/client/src/Providers/AgentPanelContext.tsx index e6b6a2a35a..b5840c4585 100644 --- a/client/src/Providers/AgentPanelContext.tsx +++ b/client/src/Providers/AgentPanelContext.tsx @@ -19,6 +19,7 @@ import { } from '~/hooks'; import { isMCPServerReadyForAgent } from '~/components/MCP/mcpServerUtils'; import { useMCPRefresh } from '~/hooks/MCP/useMCPRefresh'; +import useRevealChat from '~/hooks/Nav/useRevealChat'; import { Panel } from '~/common'; import store from '~/store'; @@ -42,6 +43,7 @@ export function AgentPanelProvider({ }) { const localize = useLocalize(); const location = useLocation(); + const revealChat = useRevealChat(); /** The panel stays mounted while the sidebar is hidden (collapsed, mobile * drawer, or the insights route collapsing it), so only a visible form * releases the MCP catalogs ahead of the background warmup schedule */ @@ -187,6 +189,7 @@ export function AgentPanelProvider({ }, [mcpData, localize, mcpServerNames, connectionStatus, availableMCPServersMap]); const value: AgentPanelContextType = { + revealChat, mcp, mcps, action, diff --git a/client/src/common/types.ts b/client/src/common/types.ts index 22e2a940a6..26c4287aa4 100644 --- a/client/src/common/types.ts +++ b/client/src/common/types.ts @@ -173,6 +173,8 @@ export type AgentPanelContextType = { setActivePanel: React.Dispatch>; setCurrentAgentId: React.Dispatch>; agent_id?: string; + /** Runs `start` once the chat it opens is in view: the host closes the mobile drawer first. */ + revealChat: (start: () => void) => void; startupConfig?: t.TStartupConfig | null; agentsConfig?: t.TAgentsEndpoint | null; endpointsConfig?: t.TEndpointsConfig | null; diff --git a/client/src/components/SidePanel/Agents/AgentPanel.test.tsx b/client/src/components/SidePanel/Agents/AgentPanel.test.tsx index caab6e9194..c6bbdd8718 100644 --- a/client/src/components/SidePanel/Agents/AgentPanel.test.tsx +++ b/client/src/components/SidePanel/Agents/AgentPanel.test.tsx @@ -2,8 +2,8 @@ * @jest-environment jsdom */ import * as React from 'react'; -import { render, waitFor, fireEvent, act } from '@testing-library/react'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { act, render, screen, waitFor, fireEvent } from '@testing-library/react'; import type { UseFormReturn } from 'react-hook-form'; import type { Agent } from 'librechat-data-provider'; import type { AgentForm, AgentModelPanelProps } from '~/common'; @@ -33,6 +33,7 @@ let mockAgentPanelContext = { endpointsConfig: {}, setCurrentAgentId: jest.fn(), agent_id: 'agent-123' as string | undefined, + revealChat: undefined as ((start: () => void) => void) | undefined, }; // Mock notification severity enum before other imports @@ -116,8 +117,11 @@ jest.mock('~/utils', () => ({ })), })); +const mockSelectAgent = jest.fn(); +const mockRevealChat = jest.fn(); + jest.mock('~/hooks', () => ({ - useSelectAgent: () => ({ onSelect: jest.fn() }), + useSelectAgent: () => ({ onSelect: mockSelectAgent }), useLocalize: () => (key: string) => key, useAuthContext: () => ({ user: { id: 'user-123', role: mockUserRole } }), })); @@ -340,6 +344,7 @@ describe('AgentPanel - Update Agent Toast Messages', () => { endpointsConfig: { openai: {} }, setCurrentAgentId: jest.fn(), agent_id: 'agent-123', + revealChat: jest.fn(), }; localStorage.clear(); }); @@ -1321,3 +1326,41 @@ describe('AgentPanel - Update Agent Toast Messages', () => { }); }); }); + +describe('AgentPanel - Select Agent', () => { + beforeEach(() => { + jest.clearAllMocks(); + mockShowToast = jest.fn(); + mockFormDefaults = {}; + mockHasPermission = true; + mockUserRole = 'USER'; + mockModelsQuery = { + data: { openai: ['gpt-4'] }, + isFetchedAfterMount: true, + isSuccess: true, + }; + mockAgentPanelContext = { + activePanel: 'builder', + agentsConfig: { allowedProviders: [] }, + setActivePanel: jest.fn(), + endpointsConfig: { openai: {} }, + setCurrentAgentId: jest.fn(), + agent_id: 'agent-123', + revealChat: mockRevealChat, + }; + const { mockUseGetAgentByIdQuery } = setupMocks(); + mockAgentQuery(mockUseGetAgentByIdQuery, {}); + }); + + it('reveals the chat through the host before starting it with the agent', () => { + render(, { wrapper: createWrapper() }); + fireEvent.click(screen.getByRole('button', { name: 'com_ui_select_agent' })); + + expect(mockRevealChat).toHaveBeenCalledTimes(1); + expect(mockSelectAgent).not.toHaveBeenCalled(); + + const start = mockRevealChat.mock.calls[0][0] as () => void; + start(); + expect(mockSelectAgent).toHaveBeenCalledWith('agent-123'); + }); +}); diff --git a/client/src/components/SidePanel/Agents/AgentPanel.tsx b/client/src/components/SidePanel/Agents/AgentPanel.tsx index d844f7e1c9..bbc94c3eab 100644 --- a/client/src/components/SidePanel/Agents/AgentPanel.tsx +++ b/client/src/components/SidePanel/Agents/AgentPanel.tsx @@ -468,6 +468,7 @@ export default function AgentPanel() { const { showToast } = useToastContext(); const { activePanel, + revealChat, agentsConfig, startupConfig, setActivePanel, @@ -960,11 +961,13 @@ export default function AgentPanel() { ], ); + /** Selecting an agent starts a chat with it, so the host first reveals the chat (on the + * narrow layout, by closing the drawer this builder sits in). */ const handleSelectAgent = useCallback(() => { if (agent_id) { - onSelectAgent(agent_id); + revealChat(() => onSelectAgent(agent_id)); } - }, [agent_id, onSelectAgent]); + }, [agent_id, onSelectAgent, revealChat]); const canEditAgent = useMemo(() => { if (!agentQuery.data?.id) { diff --git a/client/src/hooks/Nav/__tests__/useRevealChat.spec.ts b/client/src/hooks/Nav/__tests__/useRevealChat.spec.ts new file mode 100644 index 0000000000..2decc53e0d --- /dev/null +++ b/client/src/hooks/Nav/__tests__/useRevealChat.spec.ts @@ -0,0 +1,44 @@ +import { renderHook } from '@testing-library/react'; +import useRevealChat from '../useRevealChat'; + +let mockIsDrawer = false; +const mockSetSidebarOpen = jest.fn(); + +jest.mock('../useDrawerViewport', () => ({ + __esModule: true, + default: () => mockIsDrawer, +})); + +jest.mock('../useSidebarToggle', () => ({ + __esModule: true, + default: () => ({ setSidebarOpen: mockSetSidebarOpen, toggleSidebar: jest.fn() }), +})); + +describe('useRevealChat', () => { + beforeEach(() => { + mockSetSidebarOpen.mockClear(); + }); + + it('closes the drawer first and starts the chat on the slide in drawer mode', () => { + mockIsDrawer = true; + const start = jest.fn(); + const { result } = renderHook(() => useRevealChat()); + + result.current(start); + + expect(mockSetSidebarOpen).toHaveBeenCalledTimes(1); + expect(mockSetSidebarOpen).toHaveBeenCalledWith(false, start); + expect(start).not.toHaveBeenCalled(); + }); + + it('starts the chat at once and leaves the sidebar alone on the wide layout', () => { + mockIsDrawer = false; + const start = jest.fn(); + const { result } = renderHook(() => useRevealChat()); + + result.current(start); + + expect(start).toHaveBeenCalledTimes(1); + expect(mockSetSidebarOpen).not.toHaveBeenCalled(); + }); +}); diff --git a/client/src/hooks/Nav/useRevealChat.ts b/client/src/hooks/Nav/useRevealChat.ts new file mode 100644 index 0000000000..25f0613596 --- /dev/null +++ b/client/src/hooks/Nav/useRevealChat.ts @@ -0,0 +1,23 @@ +import { useCallback } from 'react'; +import useDrawerViewport from './useDrawerViewport'; +import useSidebarToggle from './useSidebarToggle'; + +/** + * Runs `start` once the chat it opens can be seen. In drawer mode the drawer closes first through + * the slide-first toggle and `start` rides the slide, as picking a conversation does; on the wide + * layout nothing covers the chat, so it runs at once. + */ +export default function useRevealChat(): (start: () => void) => void { + const isDrawer = useDrawerViewport(); + const { setSidebarOpen } = useSidebarToggle(); + return useCallback( + (start: () => void) => { + if (isDrawer) { + setSidebarOpen(false, start); + return; + } + start(); + }, + [isDrawer, setSidebarOpen], + ); +}