From 66dd6920284cd26bbba8aa8350b24d25e311ff2d Mon Sep 17 00:00:00 2001
From: Marco Beretta <81851188+berry-13@users.noreply.github.com>
Date: Thu, 1 Oct 2026 18:39:47 +0200
Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=9B=EF=B8=8F=20refactor:=20Pass=20the?=
=?UTF-8?q?=20New-Chat=20Panel=20Preference=20Into=20the=20Sidebar=20(#166?=
=?UTF-8?q?05)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* refactor(client): Pass the new-chat panel preference into the sidebar
Both new-chat controls, the desktop rail's and the mobile drawer's, read
newChatSwitchToHistory straight from the store. It is an app-global
preference the sidebar only consumes, so the host (Root) reads it and
passes it down, and the sidebar no longer reaches into the store for it.
* test(e2e): Cover the new-chat panel preference on desktop and mobile
---
.../UnifiedSidebar/ExpandedPanel.tsx | 9 +-
.../src/components/UnifiedSidebar/Sidebar.tsx | 3 +
.../UnifiedSidebar/UnifiedSidebar.tsx | 12 +-
.../__tests__/ExpandedPanel.spec.tsx | 18 +--
.../__tests__/drawerPaint.spec.tsx | 2 +-
.../UnifiedSidebar/mobile/Header.tsx | 4 +-
.../UnifiedSidebar/mobile/NewChat.tsx | 11 +-
.../mobile/__tests__/Header.spec.tsx | 116 ++++++++++++++----
.../mobile/__tests__/NewChat.spec.tsx | 32 ++++-
client/src/routes/Root.tsx | 6 +-
.../new-chat-panel-preference.spec.ts | 100 +++++++++++++++
11 files changed, 264 insertions(+), 49 deletions(-)
create mode 100644 e2e/specs/mock/scenarios/new-chat-panel-preference.spec.ts
diff --git a/client/src/components/UnifiedSidebar/ExpandedPanel.tsx b/client/src/components/UnifiedSidebar/ExpandedPanel.tsx
index 471bf2f987..82c85291db 100644
--- a/client/src/components/UnifiedSidebar/ExpandedPanel.tsx
+++ b/client/src/components/UnifiedSidebar/ExpandedPanel.tsx
@@ -1,5 +1,4 @@
import { memo, useCallback, lazy, Suspense } from 'react';
-import { useRecoilValue } from 'recoil';
import { SquarePen } from 'lucide-react';
import { useLocation } from 'react-router-dom';
import { Skeleton, Sidebar, Button, TooltipAnchor } from '@librechat/client';
@@ -11,17 +10,17 @@ import { CLOSE_SIDEBAR_ID } from '~/components/Chat/Menus/OpenSidebar';
import useNewChat from '~/hooks/Chat/useNewChat';
import { useLocalize } from '~/hooks';
import { cn } from '~/utils';
-import store from '~/store';
const AccountSettings = lazy(() => import('~/components/Nav/AccountSettings'));
const NewChatButton = memo(function NewChatButton({
setActive,
+ switchToHistory,
}: {
setActive: (id: string) => void;
+ switchToHistory: boolean;
}) {
const localize = useLocalize();
- const switchToHistory = useRecoilValue(store.newChatSwitchToHistory);
const tooltipDescription = useShortcutHint('newChat', localize('com_ui_new_chat'));
const ariaKey = useShortcutAriaKey('newChat');
@@ -129,6 +128,7 @@ function ExpandedPanel({
onExpand,
onNavigate,
onLeaveInsights,
+ switchToHistory,
}: {
links: NavLink[];
expanded?: boolean;
@@ -136,6 +136,7 @@ function ExpandedPanel({
onExpand?: () => void;
onNavigate?: () => void;
onLeaveInsights?: () => void;
+ switchToHistory: boolean;
}) {
const localize = useLocalize();
const location = useLocation();
@@ -169,7 +170,7 @@ function ExpandedPanel({
}
/>
-
+
diff --git a/client/src/components/UnifiedSidebar/Sidebar.tsx b/client/src/components/UnifiedSidebar/Sidebar.tsx
index 319b6651ad..ef048b6c60 100644
--- a/client/src/components/UnifiedSidebar/Sidebar.tsx
+++ b/client/src/components/UnifiedSidebar/Sidebar.tsx
@@ -13,6 +13,7 @@ function Sidebar({
onCollapse,
onExpand,
onLeaveInsights,
+ switchToHistory,
onResizeStart,
onResizeKeyboard,
}: {
@@ -24,6 +25,7 @@ function Sidebar({
onCollapse: () => void;
onExpand: () => void;
onLeaveInsights: () => void;
+ switchToHistory: boolean;
onResizeStart: (e: React.MouseEvent) => void;
onResizeKeyboard: (direction: 'shrink' | 'grow') => void;
}) {
@@ -36,6 +38,7 @@ function Sidebar({
onCollapse={onCollapse}
onExpand={onExpand}
onLeaveInsights={onLeaveInsights}
+ switchToHistory={switchToHistory}
/>
@@ -276,6 +285,7 @@ function UnifiedSidebar({ isSliding = false }: { isSliding?: boolean }) {
onCollapse={handleCollapse}
onExpand={handlePanelExpand}
onLeaveInsights={handleLeaveInsights}
+ switchToHistory={switchToHistory}
onResizeStart={handleResizeStart}
onResizeKeyboard={handleResizeKeyboard}
/>
diff --git a/client/src/components/UnifiedSidebar/__tests__/ExpandedPanel.spec.tsx b/client/src/components/UnifiedSidebar/__tests__/ExpandedPanel.spec.tsx
index feab690f2a..668cb688b3 100644
--- a/client/src/components/UnifiedSidebar/__tests__/ExpandedPanel.spec.tsx
+++ b/client/src/components/UnifiedSidebar/__tests__/ExpandedPanel.spec.tsx
@@ -5,7 +5,6 @@ import { MemoryRouter } from 'react-router-dom';
import { MessagesSquare, NotebookPen } from 'lucide-react';
import { render, fireEvent, screen } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
-import type { MutableSnapshot } from 'recoil';
import type { NavLink } from '~/common';
import { ActivePanelProvider, DEFAULT_PANEL } from '~/Providers';
@@ -15,10 +14,6 @@ const mockClearMessagesCache = jest.fn();
jest.mock('~/store', () => {
const { atom } = jest.requireActual('recoil');
let counter = 0;
- const switchAtom = atom({
- key: 'mock-newChatSwitchToHistory',
- default: true,
- });
const customShortcutsAtom = atom({
key: 'mock-customShortcuts',
default: {},
@@ -34,7 +29,6 @@ jest.mock('~/store', () => {
atom({ key: `mock-conversationByIndex-${counter++}`, default: null }),
conversationIdByIndex: () =>
atom({ key: `mock-conversationIdByIndex-${counter++}`, default: null }),
- newChatSwitchToHistory: switchAtom,
customShortcuts: customShortcutsAtom,
shortcutsEnabled: shortcutsEnabledAtom,
},
@@ -87,7 +81,6 @@ jest.mock('~/components/Nav/AccountSettings', () => ({
}));
import ExpandedPanel from '../ExpandedPanel';
-import store from '~/store';
const createLinks = (): NavLink[] => [
{
@@ -111,7 +104,7 @@ function renderPanel({
onExpand = jest.fn(),
onNavigate,
initialPanel = DEFAULT_PANEL,
- initializeState,
+ switchToHistory = true,
}: {
expanded?: boolean;
links?: NavLink[];
@@ -119,7 +112,7 @@ function renderPanel({
onExpand?: jest.Mock;
onNavigate?: jest.Mock;
initialPanel?: string;
- initializeState?: (snapshot: MutableSnapshot) => void;
+ switchToHistory?: boolean;
} = {}) {
if (initialPanel !== DEFAULT_PANEL) {
localStorage.setItem('side:active-panel', initialPanel);
@@ -128,7 +121,7 @@ function renderPanel({
const result = render(
-
+
@@ -219,9 +213,7 @@ describe('ExpandedPanel', () => {
renderPanel({
expanded: true,
initialPanel: 'prompts',
- initializeState: ({ set }: MutableSnapshot) => {
- set(store.newChatSwitchToHistory, false);
- },
+ switchToHistory: false,
});
const newChatLink = screen.getByTestId('new-chat-button');
diff --git a/client/src/components/UnifiedSidebar/__tests__/drawerPaint.spec.tsx b/client/src/components/UnifiedSidebar/__tests__/drawerPaint.spec.tsx
index e939e2d452..bae4c5548d 100644
--- a/client/src/components/UnifiedSidebar/__tests__/drawerPaint.spec.tsx
+++ b/client/src/components/UnifiedSidebar/__tests__/drawerPaint.spec.tsx
@@ -71,7 +71,7 @@ const drawerVisibility = (expanded: boolean, isSliding: boolean): string => {
mockSidebarState.expanded = expanded;
const { unmount } = render(
-
+
,
);
const drawer = document.getElementById(MOBILE_DRAWER_ID);
diff --git a/client/src/components/UnifiedSidebar/mobile/Header.tsx b/client/src/components/UnifiedSidebar/mobile/Header.tsx
index fd03602658..8def10b126 100644
--- a/client/src/components/UnifiedSidebar/mobile/Header.tsx
+++ b/client/src/components/UnifiedSidebar/mobile/Header.tsx
@@ -22,6 +22,7 @@ function Header({
expanded,
onClose,
onNewChat,
+ switchToHistory,
onLeaveInsights,
routeActiveId,
}: {
@@ -29,6 +30,7 @@ function Header({
expanded: boolean;
onClose: () => void;
onNewChat: (afterSlide?: () => void) => void;
+ switchToHistory: boolean;
onLeaveInsights?: () => void;
routeActiveId?: string;
}) {
@@ -77,7 +79,7 @@ function Header({
onNavigate={onClose}
routeActiveId={routeActiveId}
/>
-
+
}>
diff --git a/client/src/components/UnifiedSidebar/mobile/NewChat.tsx b/client/src/components/UnifiedSidebar/mobile/NewChat.tsx
index 7c40dfc945..9e17a22bfe 100644
--- a/client/src/components/UnifiedSidebar/mobile/NewChat.tsx
+++ b/client/src/components/UnifiedSidebar/mobile/NewChat.tsx
@@ -1,12 +1,10 @@
import { memo, useCallback } from 'react';
-import { useRecoilValue } from 'recoil';
import { SquarePen } from 'lucide-react';
import { Button, TooltipAnchor } from '@librechat/client';
import { useShortcutAriaKey } from '~/hooks/useKeyboardShortcuts';
import { useActivePanel, DEFAULT_PANEL } from '~/Providers';
import useNewChat from '~/hooks/Chat/useNewChat';
import { useLocalize } from '~/hooks';
-import store from '~/store';
/**
* New chat, in the drawer's header strip beside the panel switcher.
@@ -16,9 +14,14 @@ import store from '~/store';
* header is where the drawer keeps the controls that mean the same thing
* whichever panel is showing.
*/
-function NewChat({ onNewChat }: { onNewChat: (afterSlide?: () => void) => void }) {
+function NewChat({
+ onNewChat,
+ switchToHistory,
+}: {
+ onNewChat: (afterSlide?: () => void) => void;
+ switchToHistory: boolean;
+}) {
const localize = useLocalize();
- const switchToHistory = useRecoilValue(store.newChatSwitchToHistory);
const { setActive } = useActivePanel();
const { startNewChat } = useNewChat();
diff --git a/client/src/components/UnifiedSidebar/mobile/__tests__/Header.spec.tsx b/client/src/components/UnifiedSidebar/mobile/__tests__/Header.spec.tsx
index 4f4f961ee5..8cf5160ca0 100644
--- a/client/src/components/UnifiedSidebar/mobile/__tests__/Header.spec.tsx
+++ b/client/src/components/UnifiedSidebar/mobile/__tests__/Header.spec.tsx
@@ -52,9 +52,18 @@ const links = [] as NavLink[];
describe('mobile drawer header', () => {
it('claims the close identity while the drawer is open', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
const close = screen.getByTestId('close-sidebar-button');
expect(close).toHaveAttribute('id', 'close-sidebar-button');
@@ -67,9 +76,18 @@ describe('mobile drawer header', () => {
* would find one sitting off-viewport and act on it.
*/
it('gives up that identity once closed', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
expect(screen.queryByTestId('close-sidebar-button')).not.toBeInTheDocument();
expect(document.getElementById('close-sidebar-button')).toBeNull();
@@ -77,9 +95,18 @@ describe('mobile drawer header', () => {
/** The only close control while open, so its binding must be discoverable here. */
it('advertises the toggle shortcut on the close control', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
expect(screen.getByTestId('close-sidebar-button')).toHaveAttribute(
'aria-keyshortcuts',
@@ -88,9 +115,18 @@ describe('mobile drawer header', () => {
});
it('keeps the closed drawer out of the tab order', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
expect(screen.getByLabelText('com_nav_close_sidebar')).toHaveAttribute('tabindex', '-1');
});
@@ -103,22 +139,45 @@ describe('mobile drawer header', () => {
*/
it('moves focus to the toggle when the drawer opens', () => {
const { rerender } = render(
- ,
+ ,
{
wrapper: MemoryRouter,
},
);
expect(document.activeElement).toBe(document.body);
- rerender();
+ rerender(
+ ,
+ );
expect(document.activeElement).toBe(screen.getByTestId('close-sidebar-button'));
});
it('never steals focus while closed', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
expect(document.activeElement).toBe(document.body);
});
@@ -130,7 +189,13 @@ describe('mobile drawer header', () => {
*/
it('leads the row with the shared header-action toggle', () => {
const { container } = render(
- ,
+ ,
{
wrapper: MemoryRouter,
},
@@ -146,9 +211,18 @@ describe('mobile drawer header', () => {
* switcher because it means the same thing whichever panel is showing, which
* is exactly why it no longer repeats under each panel's contents. */
it('carries new chat in the strip, and not the marketplace', () => {
- render(, {
- wrapper: MemoryRouter,
- });
+ render(
+ ,
+ {
+ wrapper: MemoryRouter,
+ },
+ );
expect(screen.getByTestId('nav-new-chat-fab')).toBeInTheDocument();
expect(screen.queryByTestId('nav-agents-marketplace-button')).not.toBeInTheDocument();
diff --git a/client/src/components/UnifiedSidebar/mobile/__tests__/NewChat.spec.tsx b/client/src/components/UnifiedSidebar/mobile/__tests__/NewChat.spec.tsx
index 4a6cbd5e08..f9bd8b9f66 100644
--- a/client/src/components/UnifiedSidebar/mobile/__tests__/NewChat.spec.tsx
+++ b/client/src/components/UnifiedSidebar/mobile/__tests__/NewChat.spec.tsx
@@ -24,8 +24,9 @@ jest.mock('@librechat/client', () => ({
useToastContext: () => ({ showToast: jest.fn() }),
}));
+const mockSetActive = jest.fn();
jest.mock('~/Providers', () => ({
- useActivePanel: () => ({ active: 'conversations', setActive: jest.fn() }),
+ useActivePanel: () => ({ active: 'conversations', setActive: mockSetActive }),
resolveActivePanel: () => 'conversations',
DEFAULT_PANEL: 'conversations',
}));
@@ -73,7 +74,7 @@ describe('mobile header new chat', () => {
render(
-
+
,
);
@@ -91,7 +92,7 @@ describe('mobile header new chat', () => {
render(
-
+
,
);
@@ -100,4 +101,29 @@ describe('mobile header new chat', () => {
expect(onNewChat).not.toHaveBeenCalled();
expect(mockNewConversation).not.toHaveBeenCalled();
});
+ /** The host supplies the "new chat returns to the chat list" preference;
+ * the switch-back rides the same afterSlide as the reset. */
+ it.each([
+ [true, 1],
+ [false, 0],
+ ])('with switchToHistory=%s returns to the chat list %i time(s)', (switchToHistory, calls) => {
+ let afterSlide: (() => void) | undefined;
+ const onNewChat = jest.fn((callback?: () => void) => {
+ afterSlide = callback;
+ });
+ render(
+
+
+
+
+ ,
+ );
+ fireEvent.click(screen.getByTestId('nav-new-chat-fab'));
+ act(() => afterSlide?.());
+
+ expect(mockSetActive).toHaveBeenCalledTimes(calls);
+ if (calls) {
+ expect(mockSetActive).toHaveBeenCalledWith('conversations');
+ }
+ });
});
diff --git a/client/src/routes/Root.tsx b/client/src/routes/Root.tsx
index 6cbe66937f..338339fee7 100644
--- a/client/src/routes/Root.tsx
+++ b/client/src/routes/Root.tsx
@@ -87,6 +87,7 @@ function RootLayout() {
/** Off by default, matching the drawer that covers the screen and closes by
* swipe. Opting in narrows it and gives the strip a dismiss target. */
const drawerStrip = useRecoilValue(store.mobileDrawerStrip);
+ const newChatSwitchToHistory = useRecoilValue(store.newChatSwitchToHistory);
const paneRef = useRef(null);
/** Keyed off the committed state rather than the scrim's own click, because
* the header button, Escape, conversation selection and the bottom bar all
@@ -182,7 +183,10 @@ function RootLayout() {
{/* The drawer stops being painted once it is closed and
settled, so it needs the same travel window the scrim and
the pane's `inert` read. */}
-
+
{
+ localStorage.setItem('newChatSwitchToHistory', JSON.stringify(value));
+ localStorage.setItem('unifiedSidebarExpanded', JSON.stringify(true));
+ localStorage.removeItem('side:active-panel');
+ }, switchToHistory);
+ await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
+ await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
+ timeout: 30000,
+ });
+}
+
+async function desktopNewChat(page: Page, switchToHistory: boolean) {
+ await load(page, switchToHistory);
+ const chats = page.getByTestId('nav-panel-conversations');
+ const other = page.getByTestId('nav-panel-bookmarks');
+ await other.click();
+ await expect(other).toHaveAttribute('aria-pressed', 'true');
+
+ await page.getByTestId('new-chat-button').click();
+ await expect(page).toHaveURL(/\/c\/new$/);
+ return { chats, other };
+}
+
+async function mobileNewChat(page: Page, switchToHistory: boolean) {
+ await load(page, switchToHistory);
+ const drawer = page.locator('#mobile-drawer');
+ const openDrawer = async () => {
+ if (!(await drawer.getByTestId('close-sidebar-button').isVisible())) {
+ await page.getByTestId('header-open-sidebar-button').click();
+ }
+ await expect(drawer.getByTestId('close-sidebar-button')).toBeVisible();
+ await expect(drawer).not.toHaveAttribute('inert');
+ };
+
+ await openDrawer();
+ const switcher = drawer.getByTestId('panel-switcher-button');
+ await switcher.click();
+ await page.getByRole('menuitemcheckbox', { name: OTHER_PANEL }).click();
+ await expect(switcher).toContainText(OTHER_PANEL);
+
+ await drawer.getByRole('link', { name: 'New chat' }).click();
+ await expect(drawer).toHaveAttribute('inert', '');
+ await expect(page).toHaveURL(/\/c\/new$/);
+
+ await openDrawer();
+ return switcher;
+}
+
+test.describe('new chat panel preference, desktop', () => {
+ test.use({ viewport: { width: 1280, height: 800 } });
+
+ test('new chat from another panel returns the desktop sidebar to the chat list @scenario:desktop-new-chat-returns-to-chat-list', async ({
+ page,
+ }) => {
+ const { chats, other } = await desktopNewChat(page, true);
+ await expect(chats).toHaveAttribute('aria-pressed', 'true');
+ await expect(other).toHaveAttribute('aria-pressed', 'false');
+ });
+
+ test('with the preference off the desktop sidebar keeps its panel @scenario:desktop-new-chat-keeps-panel-when-off', async ({
+ page,
+ }) => {
+ const { chats, other } = await desktopNewChat(page, false);
+ await expect(other).toHaveAttribute('aria-pressed', 'true');
+ await expect(chats).toHaveAttribute('aria-pressed', 'false');
+ });
+});
+
+test.describe('new chat panel preference, mobile drawer', () => {
+ /** Loaded at phone width rather than resized into it (berry-13/LibreChat#205). */
+ test.use({ viewport: { width: 390, height: 844 } });
+
+ test('new chat from another panel returns the drawer to the chat list @scenario:mobile-new-chat-returns-to-chat-list', async ({
+ page,
+ }) => {
+ const switcher = await mobileNewChat(page, true);
+ await expect(switcher).toContainText('Chat History');
+ });
+
+ test('with the preference off the drawer keeps its panel @scenario:mobile-new-chat-keeps-panel-when-off', async ({
+ page,
+ }) => {
+ const switcher = await mobileNewChat(page, false);
+ await expect(switcher).toContainText(OTHER_PANEL);
+ });
+});