mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 22:14:27 +00:00
📲 fix: Close the Mobile Drawer When Selecting an Agent (#17696)
* 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.
This commit is contained in:
parent
12b86050c7
commit
76f9246497
6 changed files with 122 additions and 4 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -173,6 +173,8 @@ export type AgentPanelContextType = {
|
|||
setActivePanel: React.Dispatch<React.SetStateAction<Panel>>;
|
||||
setCurrentAgentId: React.Dispatch<React.SetStateAction<string | undefined>>;
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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(<AgentPanel />, { 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');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
44
client/src/hooks/Nav/__tests__/useRevealChat.spec.ts
Normal file
44
client/src/hooks/Nav/__tests__/useRevealChat.spec.ts
Normal file
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
23
client/src/hooks/Nav/useRevealChat.ts
Normal file
23
client/src/hooks/Nav/useRevealChat.ts
Normal file
|
|
@ -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],
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue